ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 29 May 2017 My Price 9.00

$ java TicTacToe

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++)

Answers

(11)
Status NEW Posted 29 May 2017 12:05 AM My Price 9.00

-----------

Not Rated(0)