Question
Today, were going to build a tic-tac-toe game. We will be using classes and arrays and we will be building many methods to help us
Today, were going to build a tic-tac-toe game. We will be using classes and arrays and we will be building many methods to help us meet the goals of Tic Tac Toe.
Using Java as programming language.
We will discuss how to attack the problem and then complete some of the methods together during lab time.
Overview
Two players will play our game of tic-tac-toe, player 1 and player 2.
The board is represented by an array of 9 integer elements: elements at indexes 0, 1, and 2 represent the top row; elements at indexes 3, 4 and 5 represent the middle row; elements at indexes 6, 7 and 8 represent the bottom row. Here is a representation of the index values for each element:
0 | 1 | 2 |
3 | 4 | 5 |
6 | 7 | 8 |
For the elements in the array:
- 0 indicates the space is available
- 1 indicates the space is occupied by player 1
- 2 indicates the space is occupied by player 2
For simplicity, you may display your tic-tac-toe board as 1s and 2s instead of Xs and Os.
In the main method of the client class, your program will simulate a tic-tac-toe game by doing the following:
Instantiate a TicTacToe object
In a loop, prompt for plays (as ints), from the user. At each iteration of the loop, you will need to call methods of the TicTacToe class to update the TicTacToe object. You will need to keep track of whose turn it is (player 1 or player 2), enforce the rules of the game, check if either player has won the game, check if there is a tie (the board is completely filled in). Your loop should exit when it has been determined there is a tie or when a player wins. Your program should print the result of the board after each play so the next player can determine their move. Your program should also print out the winner (or indicate there was a tie).
Create a new project (NetBeans users, remember to make certain the Create Main Class check box is NOT checked).
TicTacToe Object
Create a new Java Class for you project called TicTacToe. [Remember to make this a regular Java Class not a Java Main Class!!!]
The TicTacToe Object should have:
An attribute called board
should be an integer array of length 9. Remember to make it private!
A default constructor
This constructor should loop through the board elements and assign them all to 0.
A printBoard() method
This method should return void and print a copy of the current board. It takes no parameters. Ideally, youd want the board to print out looking like a tic-tac-toe board. You should have some indication of which squares are Player 1, which are Player 2 and which are empty. You may simply use 0, 1 and 2 to indicate this, if you wish.
An isLegal () method
This method returns a boolean indicating whether the desired move is legal.
The parameter for this method is a square (int).
The method should return false when the move is not legal. Moves are only legal when they are between 0 and 8 AND no one has previously claimed the square.
A move() method
This method returns void.
This method takes two parameters both integers the player number and the position on the board the player is choosing.
This method should check to make sure the move is legal. If it is not legal, the method should do nothing.
If the move is legal, this method should update the board element indicating the position with the player.
A boardFull() method
This method returns a boolean indicating whether the board is completely full. No parameters.
A isWin() method
This method should return a boolean. It takes a single parameter an integer representing a player (1 or 2). It should return true if it is determined that the player has won the game. Otherwise, it should return false.
Note: It may make your logic easier if you break isWin() into several methods. Consider methods such as horizontalWin (int player), which returns true if the player has won horizontally.
An isTie() method
This method should return a boolean indicating whether the game ended in a tie. A tie occurs when neither player has won and the board is full. You will want to call some of your other methods to determine if there is a tie.
For this class, you do not need to create a getter/setter for your attribute. Nor do you need to create .equals() or .toString() methods. However, if you find these methods useful for testing, go ahead and create them.
Client/Driver/Main Class:
Create a Driver/Main/Client class. Once you have the client class correctly added to your project, complete the below tasks.
Your Client class should have:
- A Scanner to read user input from the console.
- a new instance of TicTacToe.
- A loop which continues until the game is won or has resulted in a tie.
Your loop will need to use your instance of TicTacToe to:
- Show the current board to the user.
- Ask the user where they would like to play.
- Checks that the move is valid. If not, print an error to the user. You may choose to tell the user they have lost their turn!
- Make the move for the current player
- Check if the player has won
- Check if the game is a tie
- Switch players
- You should also print the final board.
Once you are done: Save your code, export your project to a zip file (double-check your .java files are in the zip file!), and submit your code to this assignment.
POINT BREAKDOWN:
Correct and Complete TicTacToe Object (40 points)
5 points for build attribute
5 points for Default Constructor
5 points for printBoard() method
5 points for isLegal() method
5 points for move() method
5 points for boardFull() method
5 points for isWin() method
5 points for isTie() method
Correct and Complete Client/Main/Driver (40 points)
5 points for correctly creating the TicTacToe object
5 points for ending your loop when appropriate
5 points for prompting correct user for next move and making the move
5 points for correctly reporting the game is a tie / reporting a winner
5 points for providing error on invalid moves
5 points for switching between player 1 and player 2 / between player 2 and player 1
10 points for all other main/driver/client logic
Correct and Complete JavaDoc: (20 points)
Extra-Credit Opportunities:
10 points - If a user chooses an invalid move, continue to prompt them until a valid move is entered.
20 points Create an .equals() method on your TicTacToe object. In your driver, show that your .equals() method works. Note: In order for two TicTacToe objects to be .equal, they must contain the same values for all of the array elements for board.
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