Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA: Modify the Tic-Tac-Toe coded game below to simulate playing a game. The basic flow for populating the board is: For Player 1, pick

IN JAVA: Modify the Tic-Tac-Toe coded game below to simulate playing a game. The basic flow for populating the board is:

For Player 1, pick a random space to move, if empty, place 1 in that space.

Now its Player 2 turn.

For Player 2, pick a random space to move, if empty place 2 in that space.

Now its Player 1 turn again.

Keep moving until the board is full or a player wins.

Then play 1000 iterations of the game, within 3 modes. After the thousand games in each mode, display the number of times X won, O won and ties. The three modes are: 1) X and O move randomly as described above. If a random square chosen has been played, have the player choose again. 2) X always moves to the middle square in the first move. 3) O always moves to the middle square in the first move, if X has not moved there.

Note: X always moves first

Your program should be structured like the following:

For mode from 1 to 3 {

For each game 1 to 1000 {

Play game considering the current mode

Add result to totals

}

Print totals (#Xwins, #Owins, #Ties)

}

X should win around 550 times in mode 1, 750 in mode 2 and 450 in mode. These are very approximate, as slight variations in your code can modify these slightly from other students.

------------------------------------------------------------

HERE IS THE TIC TAC TOE CODED GAME (Copy and Paste):

import java.util.Random;

public class TwoDim {

public static void main(String[] args) {

int[][] board = new int[3][3];

Random r = new Random(12);

for (int row = 0; row < board.length; row++) {

for (int col = 0; col < board[row].length; col++) {

board[row][col]= r.nextInt(3);

}

}

PrintBoard(board);

boolean xwinner = CheckWin(1,board);

boolean owinner = CheckWin(2,board);

System.out.println(xwinner + "-" + owinner);

}

public static boolean CheckWin(int player ,int[][] inboard){

int PCNT;

for (int row = 0; row < inboard.length; row++) {

PCNT = 0;

for (int col = 0; col < inboard[row].length; col++) {

if (inboard[row][col]== player) { PCNT++;}

}

if (PCNT==3) {return true;}

}

return false;

}

public static void PrintBoard(int[][] inboard){

String letter="";

for (int row = 0; row < inboard.length; row++) {

for (int col = 0; col < inboard[row].length; col++) {

switch(inboard[row][col]) {

case 0: letter= " "; break;

case 1: letter= "X"; break;

case 2: letter= "O"; break;

}

System.out.print(letter + " ");

}

System.out.println("");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions