Question: Lang: JAVA Write a class Player which has instance variables playerName, a String, the name of the player symbol, a String, the symbol the player
Lang: JAVA
Write a class Player which has instance variables
- playerName, a String, the name of the player
- symbol, a String, the symbol the player will use on a tic-tac-toe grid. (e.g. "X", "O")
- board, a TicTac (we will be creating this class in part B)
- wins, an int to keep track of how many times a player has won
In the mutator for symbol, check that the String given has length exactly 1 and is not " " (the String consisting of a single space).
In the toString, use the player's name and put the symbol in parens, something like
Constance (X)
Test out your class in a harness.
Part B
Add a class TicTac which has instance variables
- grid, a 2D array of Strings
- player1 and player2, both Players
- movesLeft, an int, which keeps track of how many moves are left in the game.
In the mutators for the players, only accept the new value if the Player passed in is not null, and their symbol is different from the other player (if there is no other player, we will accept any symbol). Make sure to set the players' boards to this TicTac as well.
Add a method resetGrid which sets up grid up as a 3x3 array and fills all elements with " " (the string consisting of a single space). Also set movesLeft to 9.
In the default constructor, use resetGrid to set up the grid, and create player1 and player2 with symbols "X" and "O"
For testing purposes, write public accessor and mutator for grid so that we can pass 2D string arrays in and out to set grid.
[EC+10]: In the mutator, only accept arrays that are 3x3 and contain only " ", and the symbols of the two Players.
Add a method makemove( which takes as arguments two ints and a Player, and returns a boolean. If the ints are within the bounds of grid, check that element of grid. If it is currently " " put the Player's symbol at that location in grid, decrease movesLeft, and return true. Otherwise make no changes and return false.
Add a toString that returns a string for the grid in a nice format. e.g.
1 2 3 ------------- 1 | X | | O | ------------- 2 | | X | O | ------------- 3 | X | | | -------------
In Harness, in the main, test that your TicTac methods so far work.
This class does not need to work for boards of sizes other than 3x3.
Part C
In TicTac, add a method ticTacWin() which returns a Player. It returns player1 if player1's symbol has a tic-tac-toe win in any row, column, or diagonal of grid, player2 if player2's symbol has a win, and null if neither has a win.
In the case that both have a win, you can return either (i.e., whichever you find first). This should never come up in a real game anyway.
There are many ways to do this, but a method that checks each row and column individually, rather than using loops, will not receive much credit (hint: a single rather than nested loop may work for both rows and columns). However, checking diagonals individually is reasonable.
In Harness, in main, create at least three 3 x 3 arrays of Strings (you may do this however you please), and fill them with " ", "X", and "O" so that one has a diagonal win for X, one has a vertical win for O, and one has at least three X's and three O's, but neither wins. Create a TicTac object, set its grid to each of your examples, and check for each that ticTacWin() correctly reports the winner.
Part D
In Player, add a method userMove which interacts with the user. It shows (by printing) the player the current state of the board (remember, Player has an instance var for the board) and then prompts (see input incantations) the player by name for a row and a column number for the player's next move.
The player will enter a number from 1 to 3 for each; but remember that the board uses numbers from 0 to 2, so you will need to translate. Try to have the board use makemove to set that position to the player's symbol. As long as makemove returns false (could not make that move) prompt the user again.
In TicTac, add a method playGame. In playGame reset the grid, then alternate letting each player make a move, until ticTacWin returns a winner (not null) or there are no movesLeft. If a player wins, the game should stop and not give the other player one last turn. Print a congratulatory message to the winner if there is one, and show how many times they have won. If the game ends in a tie, don't give any more turns, but announce that a tie happened.
Part E
Add a new class TicTacInterface with a main. In the main, set up a TicTac.
Prompt the first player for their name, set up a Player with the name, and have this Player join the TicTac as player1. Then do the same for a second player for player2.
Once the players are set up, in a loop call playGame for the ticTac, then ask whether they want to quit or play again.
[EC +10]
To TicTac, add an array of Strings winMessages and in the constructor fill it with messages of congratulation for winners, e.g. "Good Job" "Yay You!" "Your Tic Tac Toe Skillz are Beyond 9000", "I CANNOT WITH ALL THE WINNING OF YOU!" etc. Include at least 10 such messages. When a player wins, randomly choose a message from the list to print.
[EC+5]
When a player wins, as a reward offer them the opportunity to change their symbol. If they want to change, get the new symbol from them, but don't allow it to be set to the other player's current symbol (keep asking until you get one you can use). No need to care about the case where they set it back to the same thing they had before.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
