How to make the computer generate a random number for a move for \'O\' in tic ta
ID: 3539598 • Letter: H
Question
How to make the computer generate a random number for a move for 'O' in tic tac toe java game
Fix the program that I wrote please
import java.lang.Math;
import javax.swing.JOptionPane;
class random
{
private static int random;
public static void main(String args[])
{
char[][] board=new char[3][3];
for(int i=0;i<9;i++)
board[i/3][i%3]=' ';
char c='X';
int y;
for(int i=0;i<9;i++)
{
y =Integer.parseInt(JOptionPane.showInputDialog("Please pick a square (1-9)"));
y--;
while(board[y/3][y%3]!=' ') {
int random = (int)(Math.random()* 9 ) + 1;
}
while(board[y/3][y%3]!=' ')
{
y=Integer.parseInt(JOptionPane.showInputDialog("Square already filled. Please pick a different square (1-9)"));
y--;
}
board[y/3][y%3]=c;
// 4 times random
if(c=='X') {
board[random][random] = c;
c = 'O';
} else {
c='X';
}
//printing board
System.out.println("BOARD:");
System.out.println("-------");
for(int j=0;j<3;j++)
{
System.out.println("|"+board[j][0]+"|"+board[j][1]+"|"+board[j][2]+"|");
System.out.println("-------");
}
System.out.println();
}
}
}
Explanation / Answer
//The program is working perfectly as expected. I have added comments wherever I made changes and wherever code requires attention.
import java.lang.Math;
import javax.swing.JOptionPane;
class random
{
private static int random;
public static void main(String args[])
{
char[][] board=new char[3][3];
for(int i=0;i<9;i++){
board[i/3][i%3]=' ';
}
char c='X';
int y;
int random = (int)(Math.random()* 9 );
int numEmptySquares = 9;//Needed to keep a count of number of empty squares left.
//Continue the procedure until all squares are filled
while(numEmptySquares > 0)
{
y =Integer.parseInt(JOptionPane.showInputDialog("Please pick a square (1-9)"));
y--;
//First ask the user to input a square position. Continue to ask until an empty square is entered
while(board[y/3][y%3]!=' ') {
System.out.println("Random!!!!");
y=Integer.parseInt(JOptionPane.showInputDialog("Square already filled. Please pick a different square (1-9)"));
y--;
}
//mark the position entered by user as 'X' or 'O'
board[y/3][y%3]=c;
//Decrease the left number of empty squares
numEmptySquares--;
//Now, randomly pick a square. Keep generating random number until an empty square is not picked.
//Don't generate random number if no empty square is left. It means game is over
while(board[random/3][random%3]!=' ' && numEmptySquares > 0) {
random = (int)(Math.random()* 9 );
}
//Mark the randomly picked empty square as opposite of user's choice 'X' or 'O'
//Don't mark the board if no empty square is left
if(c=='X' && numEmptySquares > 0) {
board[random/3][random%3] = 'O';
numEmptySquares--;
} else if(c=='O' && numEmptySquares > 0) {
board[random/3][random%3] = 'X';
}
//printing board
System.out.println("BOARD:");
System.out.println("-------");
for(int j=0;j<3;j++)
{
System.out.println("|"+board[j][0]+"|"+board[j][1]+"|"+board[j][2]+"|");
System.out.println("-------");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.