Question
Learning Outcomes Using Java (Net Beans) construct classes that represent real world concepts. Write methods to perform operations on objects. Add constructors to a class.
Learning Outcomes
Using Java (Net Beans) construct classes that represent real world concepts.
Write methods to perform operations on objects.
Add constructors to a class.
Use static methods to break up the tasks performed in the main method.
Required Reading
Savitch - Chapters 5-6
Instructions
For this assignment you will be implementing a two-player tic-tac-toe game. Tic-tac-toe is not all that interesting of a game to those over seven years old, but it is an excellent example of how even a simple game can be very complicated to implement. Luckily we can reduce the complexity of this problem by using classes and methods to make our code more modular.
You will be implementing three classes for this program:
The TTTPlayer class keeps track of a players mark (X or O) and how many games the player has won.
The TTTBoard class keeps track of the state of the tic-tac-toe board.
The YourlastnameProgram2 main class implements the text-based interface to the game.
The TTTPlayer class has the following members:
Private instance variables for the players mark and the number of games the player won.
A default constructor that initializes the mark to 'X' and the number of wins to zero.
A one-argument constructor that initializes the mark to the argument provided and the number of wins to zero.
Accessory for both private instance variables (mutators are not necessary).
A void method called adds Win that has no parameters and increments the number of wins.
A void method called display that has no parameters and displays the player information in the following format:
Player X has won 1 game. Player O has won 2 games.
Note that the singular game is used if the number of games won is 1 and the plural is used for any other value.
The TTTBoard class has the following members:
A private instance variable for the mark (or lack thereof) on each square of the board.
A default constructor that sets all of the private data members to the blank character ' '.
A method called try Move that takes a TTTPlayer and a string representing a move and does the following:
1. If the move is a valid move and the corresponding square is empty, it sets the appropriate private instance variable to the players mark and returns true. A valid move is a two-character string whose first character represents the row:
o T - top row
o C - center row
o B - bottom row
and whose second character represents the column:
o L - left column
o C - center column
o R - right column
For example, a move to mark the upper left square would be TL and a move to mark the center square would be CC.
1. if the move is not valid or the square is taken, it returns false.
A method called win By Player that takes a TTTPlayer as an argument and does the following:
1. If the players mark is in all of the squares of a row, all of the squares of a column or all of the squares of a diagonal, it returns true.
2. Otherwise it returns false.
A method called is Full that takes no arguments does the following:
1. If any of the spaces are empty, it returns false.
2. Otherwise it returns true
A void method called display that displays the current state of the board. An empty board would look like this:
L C R T | | ---+---+--- C | | ---+---+--- B | |
The YourlastnameProgram1 class has the following members:
A main method that does the following:
1. Initialize the two players to use the marks X and O.
2. Call the play Game static method to play a game.
3. Call the display Score static method to display the current scores.
4. Use the again method to prompt the user if they want to play another game.
5. If the user answers Y or y go back to step 2.
6. Otherwise exit the program.
A static method called play Game that takes two TTTPlayer objects as arguments and does the following:
1. Create a new TTTBoard object to use as the board.
2. Set the current player to player 1.
3. Display the board.
4. Call the make Move static method to prompt for and process the current players move.
5. Display the board.
6. Call the checkGameOver static method to determine if the game is over and display the results if it is.
7. Switch the current player to the other player.
8. If the game is not over go back to step 4.
A static method called make Move that takes a TTTBoard and a TTTPlayer as arguments and does the following:
1. Prompt for and read the next move from the player.
2. Attempt to make the move on the board.
3. If the move is not valid, go back to step 1.
A static method called check Game Over that takes a TTTBoard and a TTTPlayer as arguments and does the following:
1. If the player has won the game, display a message to the user, increment the players wins, and return true.
2. If the player has not won the game and the board is full, display a message indicating that the game is a draw and return true.
3. If the player has not won the game and the board is not full, return false.
A static method called display Score that takes two TTTPlayer and displays their scores.
A static method called again that takes no arguments and does the following:
1. Asks the user if they want to play another game.
2. If the users response is not Y, y, N, or n, go back to step one.
3. Return the users response as a String.
Example Input and Output
L C R T | | ---+---+--- C | | ---+---+--- B | | Enter move for X: move Enter move for X: CC L C R T | | ---+---+--- C | X | ---+---+--- B | | Enter move for O: TL L C R T O | | ---+---+--- C | X | ---+---+--- B | |
Several moves later:
Enter move for X: TR L C R T O | O | X ---+---+--- C | X | ---+---+--- B X | | X wins! Scoreboard: Player X has won 1 game. Player O has won 0 games. Would you like to play again? (Y/N) Y L C R T | | ---+---+--- C | | ---+---+--- B | | Enter move for X: CC L C R T | | ---+---+--- C | X | ---+---+--- B | |
Several moves later:
Enter move for X: BR L C R T O | X | O ---+---+--- C X | X | O ---+---+--- B X | O | X It's a draw! Scoreboard: Player X has won 1 game. Player O has won 0 games. Would you like to play again? (Y/N) N
Step 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