Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 a
random 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);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote