Question
Learning Outcomes Construct classes that represent real world concepts. Write methods to perform operations on objects. Add constructors to a class. Use static methods to
Learning Outcomes
- 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.
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.
- Accessors for both private instance variables (mutators are not necessary).
- A void method called addWin 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:
- X has won 1 game. O has won 2 games.
- 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 character ' ' (a blank space)
- A method called tryMove that takes a TTTPlayer and a string representing a move and does the following:
- 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:
- T - top row
- C - center row
- B - bottom row
- whose second character represents the column:
- L - left column
- C - center column
- R - right column
- example, a move to mark the upper left square would be TL and a move to mark the center square would be CC.
- 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:
-
- if the move is not valid or the square is taken, it returns false.
- A method called winByPlayer that takes a TTTPlayer as an argument and does the following:
- 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.
- Otherwise it returns false.
- A method called isFull that takes no arguments does the following:
- If any of the spaces are empty, it returns false.
- 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 YourlastnameProgram2 class has the following members:
- A main method that does the following:
- Initialize the two players to use the marks X and O.
- Call the playGame static method to play a game.
- Call the displayScore static method to display the current scores.
- Use the again method to prompt the user if they want to play another game.
- If the user answers Y or y go back to step 2.
- Otherwise exit the program.
- A static method called playGame that takes two TTTPlayer objects as arguments and does the following:
- Create a new TTTBoard object to use as the board.
- Set the current player to player 1.
- Display the board.
- Call the makeMove static method to prompt for and process the current players move.
- Display the board.
- Call the checkGameOver static method to determine if the game is over and display the results if it is.
- Switch the current player to the other player.
- If the game is not over go back to step 4.
- A static method called makeMove that takes a TTTBoard and a TTTPlayer as arguments and does the following:
- Prompt for and read the next move from the player.
- Attempt to make the move on the board.
- If the move is not valid, go back to step 1.
- A static method called checkGameOver that takes a TTTBoard and a TTTPlayer as arguments and does the following:
- If the player has won the game, display a message to the user, increment the players wins, and return true.
- 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.
- If the player has not won the game and the board is not full, return false.
- A static method called displayScore that takes two TTTPlayers and displays their scores.
- A static method called again that takes no arguments and does the following:
- Asks the user if they want to play another game.
- If the users response is not Y, y, N, or n, go back to step one.
- 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
Notes and Comments
Upload your Java source files to the dropbox named Programming Assignment 2. The name of the source file for the main class must be your last name followed by Program2 with the extension .java.
Make sure to include comments with your name, a description of what the program does, the course (CSCI 2010), and the assignment name (Programming Assignment 2) at the beginning of all of your source files. Also make sure to add comments at the beginning of each of the methods you write describing what they do.
Make sure you only hand in the source files for your assignment, not the class files, or any other files that NetBeans creates.
Your programs must compile without errors in order to be graded. Once your program compiles make sure to test it using multiple test cases to see if it meets the requirements of the assignment.
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