Question
Dictionary implementation of TicTacToe game. Please write the rest of the needed code for the project. Thank you! import java.util.Arrays; import java.util.Scanner; public class GameBoard
Dictionary implementation of TicTacToe game.
Please write the rest of the needed code for the project. Thank you!
import java.util.Arrays;
import java.util.Scanner;
public class GameBoard {
private char[][] gameBoard;
private boolean gameOngoing = true;
/**
* This is the constructor for the GameBoard class
*/
public GameBoard() {
gameBoard = new char[3][3];
for (int row = 0; row
Arrays.fill(gameBoard[row], ' ');
}
} // end of constructor
/**
* This method will display the gameBaord to the screen
*/
public void displayBoard() {
for (int row = 0; row
for (int col = 0; col
System.out.print("\t" + gameBoard[row][col] + "\t");
if (col == 0 || col == 1) {
System.out.print("|");
}
if ((row == 0 && col == 2) || (row == 1 && col == 2)) {
System.out.print(" ------------------------------------------------- ");
}
}
}
System.out.println();
System.out.println();
System.out.println();
} // end displayBoard method
/**
* This method will validate if a players move is allowed and return true if the
* move was completed
*/
public boolean makeMove(char player, int row, int col) {
if (row >= 0 && row = 0 && col
if (gameBoard[row][col] != ' ') {
return false;
} else {
gameBoard[row][col] = player;
return true;
}
} else {
return false;
}
} // end of makeMove method
/**
* Will return true if the game is still active
*/
public boolean gameActive() {
return gameOngoing;
} // end of gameActive method
/**
* This method will ask the user to pick a row and column, validate the inputs
* and call the method makeMove()
*/
public void askPlayer(char player) {
Scanner sc = new Scanner(System.in);
int row, col;
do {
System.out.printf("Player %s please enter a row (1-3): ", player);
row = sc.nextInt();
System.out.printf("Player %s Please enter a column (1-3): ", player);
col = sc.nextInt();
} while (notValid(row, col));
makeMove(player, row - 1, col - 1);
} // end of askPlayer method
/**
* This method will validate if the row and column are between 1-3 and if the
* position is currently empty
*/
public boolean notValid(int row, int col) {
if (row > 3 || row 3 || col
return true;
} else {
return false;
}
} // end notValid method
/**
* This method will check if a position is empty
*
* @return true if the position is empty, false otherwise
*/
public boolean isEmpty(int row, int col) {
if (gameBoard[row - 1][col - 1] == ' ') {
return true;
} else {
System.out.println("That postion is taken.");
return false;
}
}// end isEmpty method
/**
* This method will check to see if there are 3 X's or O's in a row
*
* @return true if there is a winner, false otherwise
*/
public boolean checkForWinner() {
for (int row = 0; row
// checking horizontal
if (gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2]
&& gameBoard[row][0] != ' ') {
System.out.println("The winner is " + gameBoard[row][0]);
gameOngoing = false;
}
}
for (int col = 0; col
// checking vertical
if (gameBoard[0][col] == gameBoard[1][col] && gameBoard[1][col] == gameBoard[2][col]
&& gameBoard[0][col] != ' ') {
System.out.println("The winner is " + gameBoard[0][col]);
gameOngoing = false;
}
}
if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2] && gameBoard[0][0] != ' ') {
System.out.println("The winner is " + gameBoard[0][0]);
gameOngoing = false;
}
if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2] && gameBoard[0][2] != ' ') {
System.out.println("The winner is " + gameBoard[2][0]);
gameOngoing = false;
}
return false;
} // end checkForWinner method
// Getters for gameBoard and gameOngoing
public char[][] getGameBoard() {
return gameBoard;
}
public boolean isGameOngoing() {
return gameOngoing;
}
} // end class
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
GameBoard myGame = new GameBoard();
myGame.displayBoard();
// myGame.makeMove('O',0,0);
// myGame.makeMove('X', 1, 1);
int counter = 1;
while (myGame.gameActive() && counter
if (counter % 2 == 0) {
myGame.askPlayer('O');
} else {
myGame.askPlayer('X');
}
counter++;
System.out.println();
myGame.displayBoard();
myGame.checkForWinner();
if(counter == 10) {
System.out.println("Stale mate! ");
}
}
} // end main
}// end class
Outcomes: * Use an implementation of Dictionary to solve a problenm Implement a class so that it can work with a data structure Naming requirements (not following any of these may result in a score of 0): You should have a class named TicTacToe in the default package. This class is where the logic of the game will be. This is the class that will contain a Dictionary for storing Tic Tac Toe boards You should have a class named TicTacToe!nteraction in the default package. This is where the user interaction for the game should be. This is where the mainQ method should be Preliminaries: Review the dictionary lab Overall goal: See programming project 6 in chapter 19 (page 573). Consider all the possible boards in a game of Tic Tac Toe. For each board position, there is a "best move" (or multiple moves that are tied for best). Write a program that plays a game of Tic Tac Toe in which you use a dictionary to calculate and store all possible board positions, and the best move associated with that position. Specifics . In our game, X will always make the first move . Each board position should be stored as an array (one- or two-dimensional) of 9 values, representing who is occupying each of the 9 squares. Note that a square can be occupied by X or by O, or it can be empty. The board position should be the key for the dictionary. The type of array you choose to use is up to you. Choose something that is easy to work with. All you are trying to do is indicate whether each of the 9 squares is X, 0, or empty The value associated with each board should be the best move that can be made in that position Again, the data type you choose to use is up to you. The move could be represented by a row and column, or by a number in the range 0 to 8. Note that you should not have to indicate WHO moves (it should be obvious from the board). It should only have to indicate where to move. If there is more than one best move, it is up to you which of those best moves you choose to returrn Thus, the key-value pair in your dictionary will be a board position (key) and a move (value) . Although there are 39 board configurations (in which each square is X, O, or empty), many of those configurations are not valid in the game of Tic Tac Toe. For example, you cannot have a board in which X appears 7 times and O appears only 1 time. Similarly, you cannot have a board where both players have 3 in a row. You also cannot have a board where O appears 3 times and X appears 2 times (because X goes first). You only need to store valid board positions in your dictionary. It is OK if you want to store ALL positions in the dictionary, but then you should have a plan for what the "best move" value will be for each of those positions, even in the board is not valid Outcomes: * Use an implementation of Dictionary to solve a problenm Implement a class so that it can work with a data structure Naming requirements (not following any of these may result in a score of 0): You should have a class named TicTacToe in the default package. This class is where the logic of the game will be. This is the class that will contain a Dictionary for storing Tic Tac Toe boards You should have a class named TicTacToe!nteraction in the default package. This is where the user interaction for the game should be. This is where the mainQ method should be Preliminaries: Review the dictionary lab Overall goal: See programming project 6 in chapter 19 (page 573). Consider all the possible boards in a game of Tic Tac Toe. For each board position, there is a "best move" (or multiple moves that are tied for best). Write a program that plays a game of Tic Tac Toe in which you use a dictionary to calculate and store all possible board positions, and the best move associated with that position. Specifics . In our game, X will always make the first move . Each board position should be stored as an array (one- or two-dimensional) of 9 values, representing who is occupying each of the 9 squares. Note that a square can be occupied by X or by O, or it can be empty. The board position should be the key for the dictionary. The type of array you choose to use is up to you. Choose something that is easy to work with. All you are trying to do is indicate whether each of the 9 squares is X, 0, or empty The value associated with each board should be the best move that can be made in that position Again, the data type you choose to use is up to you. The move could be represented by a row and column, or by a number in the range 0 to 8. Note that you should not have to indicate WHO moves (it should be obvious from the board). It should only have to indicate where to move. If there is more than one best move, it is up to you which of those best moves you choose to returrn Thus, the key-value pair in your dictionary will be a board position (key) and a move (value) . Although there are 39 board configurations (in which each square is X, O, or empty), many of those configurations are not valid in the game of Tic Tac Toe. For example, you cannot have a board in which X appears 7 times and O appears only 1 time. Similarly, you cannot have a board where both players have 3 in a row. You also cannot have a board where O appears 3 times and X appears 2 times (because X goes first). You only need to store valid board positions in your dictionary. It is OK if you want to store ALL positions in the dictionary, but then you should have a plan for what the "best move" value will be for each of those positions, even in the board is not validStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started