The getMove method in TicTacToePlayer.java just places an ‘X’ or ‘O’ in a random
ID: 3619708 • Letter: T
Question
The getMove method in TicTacToePlayer.java just places an ‘X’ or ‘O’ in arandom square. That square may even already be occupied. Change this method to intelligently play tic-tac-toe (for whatever piece it has been passed – X or O).
import java.util.Random;
public class TicTacToe {
public void getMove(char[][] theBoard, char myPiece) {
// this method should update the game board to place 'myPiece' (X or O)
// in a space on the board.
// As-is, this method just places an X or O randomly, without even considering
// if the square is already taken.
Random myRand = new Random();
int myRow = myRand.nextInt(3);
int myCol = myRand.nextInt(3);
theBoard[myRow][myCol] = myPiece;
}
}
Explanation / Answer
please rate - thanks public static void getMove(char[][] theBoard, char myPiece) {// this method should update the game board to place 'myPiece' (X or O)
// in a space on the board.
// As-is, this method just places an X or O randomly, without even considering
// if the square is already taken.
Random myRand = new Random();
int myRow,myCol;
do
{
myRow = myRand.nextInt(3);
myCol = myRand.nextInt(3);
}while(theBoard[myRow][myCol]!= ' ');
theBoard[myRow][myCol] = myPiece;
System.out.println(myPiece+"'s move is "+myRow+","+myCol);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.