For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays Lesson. We will add a couple of methods to
For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays Lesson.
We will add a couple of methods to the TicTacToe class.
To track whose turn it is, we will use a counter turn. This is already declared as a private instance variable.
Create a getTurn method that returns the value of turn.
Other methods to implement:
-
printBoard()- This method should print the TicTacToe array onto the console. The board should include numbers that can help the user figure out which row and which column they are viewing at any given time. Sample output for this would be:
0 1 2 0 - - - 1 - - - 2 - - -
- pickLocation(int row, int col)- This method returns a boolean value that determines if the spot a user picks to put their piece is valid. A valid space is one where the row and column are within the size of the board, and there are no X or O values currently present.
- takeTurn(int row, int col)- This method adds an X or O to the array at position row,col depending on whose turn it is. If its an even turn, X should be added to the array, if its odd, O should be added. It also adds one to the value of turn.
-
checkWin()- This method returns a boolean that determines if a user has won the game. This method uses three methods to make that check:
- checkCol- This checks if a player has three X or O values in a single column, and returns true if thats the case.
- checkRow - This checks if a player has three X or O values in a single row.
- checkDiag - This checks if a player has three X or O values diagonally.
checkWin() only returns true if one of these three checks is true.
The code below is the code that I need to complete
public class TicTacToe { //copy over your constructor from the Tic Tac Toe Board activity in the previous lesson! private int turn; //this method returns the current turn public int getTurn() { } /*This method prints out the board array on to the console */ public void printBoard() { } //This method returns true if space row, col is a valid space public boolean pickLocation(int row, int col) { } //This method places an X or O at location row,col based on the int turn public void takeTurn(int row, int col) { } //This method returns a boolean that returns true if a row has three X or O's in a row public boolean checkRow() { } //This method returns a boolean that returns true if a col has three X or O's public boolean checkCol() { } //This method returns a boolean that returns true if either diagonal has three X or O's public boolean checkDiag() { } //This method returns a boolean that checks if someone has won the game public boolean checkWin() { }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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