The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
In this lab you will modify the TicTacToe program developed in the last lab to exhibit more interesting behavior.
Â
Step 1.Modify the existing program so that when the program starts, it asks whether it (the machine player) will go first and acts accordingly. Note that the first player is always named X. The start of a session should be as follows, where the last 'yes' is typed by the user. Any response that isn't 'yes' should be taken as 'no'. If 'yes' then the program will play X.
Â
$ java TicTacToe
May I go first (yes/no): yes
Â
Â
Â
Step 2.Modify the program as necessary to ensure that the machine player computes its next move with the following priorities.
Â
1. Win immediately if such a move is possible, otherwise
2. Block an immediate win by the other player if they have one, otherwise
3. Make a clever move (as defined in the last lab) if such a move is possible, otherwise
4. Block a clever move (as defined in the last lab) by the other player if they have one, else
5. Make a random move.
Â
//zhenghao yu yuxx0522@umn.eduimport java.io.*;class TicTacToe{public static void main(String args[])throws IOException{InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);String ttt[][] = new String[3][3];for(int i=0;i<3;i++)for(int j=0;j<3;j++)ttt[i][j] = " ";int c = 1, e, p = 1, m = 0, f;System.out.println("Enter the position of the '0' or 'X' as shown inthe next figure.");for(int i = 0; i < 3; i++){System.out.print("|");for(int j = 0; j < 3; j++)System.out.print((c++) + "|");System.out.println();}System.out.println("Enter the positions.");do{f = 0;do{if(f != 0)System.out.println("Invalid position");System.out.println("Player " + p + ":");e = Integer.parseInt(br.readLine());f++;}while(e < 1 || e > 9 || ttt[(e - 1) / 3][(e - 1) % 3] != "");if(p == 1){ttt[(e - 1) / 3][(e - 1) % 3] = "0";p = 2;}else{ttt[(e - 1) / 3][(e - 1) % 3] = "X";p = 1;}for(int i = 0; i < 3; i++){System.out.print("|");for(int j = 0; j < 3; j++)System.out.print(ttt[i][j] + "|");System.out.println();}for(int i = 0; i < 3; i++){c = 1;for(int j = 0; j < 2; j++)if(ttt[i][j].equals(ttt[i][j + 1]) && !(ttt[i][j].equals(" ")))c++;if(c == 3)break;c = 1;for(int j = 0; j < 2; j++)