Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.ArrayList;import java.util.Scanner;public class TicTacToe { // Static variables for the TicTacToe class, effectively configuration options // Use these instead of hard-coding ' ', 'X',
import java.util.ArrayList;import java.util.Scanner;public class TicTacToe { // Static variables for the TicTacToe class, effectively configuration options // Use these instead of hard-coding ' ', 'X', and 'O' as symbols for the game // In other words, changing one of these variables should change the program // to use that new symbol instead without breaking anything // Do NOT add additional static variables to the class! static char emptySpaceSymbol = ' '; static char playerOneSymbol = 'X'; static char playerTwoSymbol = 'O'; public static void main(String[] args) { // TODO // This is where the main menu system of the program will be written. // Hint: Since many of the game runner methods take in an array of player names, // you'll probably need to collect names here. } // Given a state, return a String which is the textual representation of the tic-tac-toe board at that state. private static String displayGameFromState(char[][] state) { // TODO // Hint: Make use of the newline character to get everything into one String // It would be best practice to have this with a loop, but since we hardcode the game to only use 3x3 boards // it's fine to have this without one. return null; } // Returns the state of a game that has just started. // This method is implemented for you. You can use it as an example of how to utilize the static class variables. // As you can see, you can use it just like any other variable, since it is instantiated and given a value already. private static char[][] getInitialGameState() { return new char[][]{{emptySpaceSymbol, emptySpaceSymbol, emptySpaceSymbol}, {emptySpaceSymbol, emptySpaceSymbol, emptySpaceSymbol}, {emptySpaceSymbol, emptySpaceSymbol, emptySpaceSymbol}}; } // Given the player names, run the two-player game. // Return an ArrayList of game states of each turn -- in other words, the gameHistory private static ArrayList runTwoPlayerGame(String[] playerNames) { // TODO return null; } // Given the player names (where player two is "Computer"), // Run the one-player game. // Return an ArrayList of game states of each turn -- in other words, the gameHistory private static ArrayList runOnePlayerGame(String[] playerNames) { // TODO return null; } // Repeatedly prompts player for move in current state, returning new state after their valid move is made private static char[][] runPlayerMove(String playerName, char playerSymbol, char[][] currentState) { Scanner sc = new Scanner(System.in); // TODO return null; } // Repeatedly prompts player for move. Returns [row, column] of their desired move such that row & column are on // the 3x3 board, but does not check for availability of that space. private static int[] getInBoundsPlayerMove(String playerName) { Scanner sc = new Scanner(System.in); // TODO return null; } // Given a [row, col] move, return true if a space is unclaimed. // Doesn't need to check whether move is within bounds of the board. private static boolean checkValidMove(int[] move, char[][] state) { // TODO return false; } // Given a [row, col] move, the symbol to add, and a game state, // Return a NEW array (do NOT modify the argument currentState) with the new game state private static char[][] makeMove(int[] move, char symbol, char[][] currentState) { // TODO: // Hint: Make use of Arrays.copyOf() somehow to copy a 1D array easily // You may need to use it multiple times for a 1D array return null; } // Given a state, return true if some player has won in that state private static boolean checkWin(char[][] state) { // TODO // Hint: no need to check if player one has won and if player two has won in separate steps, // you can just check if the same symbol occurs three times in any row, col, or diagonal (except empty space symbol) // But either implementation is valid: do whatever makes most sense to you. // Horizontals // Verticals // Diagonals return false; } // Given a state, simply checks whether all spaces are occupied. Does not care or check if a player has won. private static boolean checkDraw(char[][] state) { // TODO return false; } // Given a game state, return a new game state with move from the AI // It follows the algorithm in the PDF to ensure a tie (or win if possible) private static char[][] getCPUMove(char[][] gameState) { // TODO // Hint: you can call makeMove() and not end up returning the result, in order to "test" a move // and see what would happen. This is one reason why makeMove() does not modify the state argument // Determine all available spaces // If there is a winning move available, make that move // If not, check if opponent has a winning move, and if so, make a move there // If not, move on center space if possible // If not, move on corner spaces if possible // Otherwise, move in any available spot return null; } // Given a game state, return an ArrayList of [row, column] positions that are unclaimed on the board private static ArrayList getValidMoves(char[][] gameState) { // TODO return null; } // Given player names and the game history, display the past game as in the PDF sample code output private static void runGameHistory(String[] playerNames, ArrayList gameHistory) { // TODO // We have the names of the players in the format [playerOneName, playerTwoName] // Player one always gets 'X' while player two always gets 'O' // However, we do not know yet which player went first, but we'll need to know... // Hint for the above: which symbol appears after one turn is taken? // Hint: iterate over gameHistory using a loop }
Program Description Your little brother achieved some amazing results thanks to your program. So you decide to reward him by making a game for him. In particular, he loves to play E tac-toe with you so you decide to build that. X Furthermore, you want him to be able to compete against the oomputer as well as you. Hence, your game is going to have two modes: (i) single player mode (against the computer) and (ii) two player mode. In two player mode, your program should rst ask the players their names and toss a coin to decide who goes rst. Then the program should ask the users to take turns playing the game. Ensure that each move is legitimate and print the game result when someone wins or when the game ends in a draw. In one player mode, your program should rst ask for the player's name. You will be playing with the computer. Then it should toss a coin to decide who gets to go rst. Player one and player two refer to the order in which the players enter their names. Player one gets the 'X' character while player two gets the '0' character. Player one does NOT refer to the player who takes the rst turn, since it's based on a coin-ip. When playing one-player mode, the AI will always be player two. Lastly, the menu should repeat unless the user selects the quit option. Menu contains four options that user can select: l. 1. Single player -> user will play with computer ll. 2. Two player -> user will play with another human player lll. D. Display last match -> displays the last played match IV. 0. Quit -> user wish to quit the game, when selected you should display proper message. Refer to sample output for expected functionality. To get you started. this week we provide starter code in the form of an IntelliJ project folder. which has all the headers of the methods you'd must implement to create the program as well as more specic hints for doing so. You are required to use this starter code and you may modify it by adding additional methods . but do not change the existing methods provided. You can unzip the starter code le to get a project folder. Then. open this in InteIIiJ and work with it as you would any other project. Make extensive use of methods to organize your code and break the problem down into manageable pieces. The starter code contains all the method headers you would need for this the solution we wrote has all these methods and no more than these methods. Start with the two-player game since that'il be most similar to what you're familiar with and makes testing easier. After you have that, the only challenge with the one-player game will be implementing the Al algorithms. as the rest of the game logic will carry over and can be reused. You must use an ArrayList to store the game history, and you must use a 20 char array to store a game state. The starter code has three static variables in the TicTacToe class. which are used to centrally store what symbols in the game represent the empty space, the player one symbol, and the player two symbol. Throughout your code. you must refer to these static variables rather than hard coding the game to rely on ' '. 'X', and '0'. You may not add additional static variables to the class. These are effectively used as conguration options for the game. Certain methods in the starter code are marked as private. Since we're only using one class. there is no functional difference. Later in the course you'll see why some have their access from other classes restricted in this way. All methods are static. You'll also see why when we cover what it means for methods and variables to be static in detail. Your program should conform to these edge-case expectations regarding user input: - In every menu, check whether the users choice of menu option is valid and inform them if their choice isn't valid. - When inputting positions on the board, you do not need to handle non-numerical or non-integer inputs. Assume they will only input integers. However. you should check that the integers they inputted are valid. This means their input should both be within the 3x3 grid and should be an unoCCupied space. Inform the user if their input was invalid and which of these two rules it broke. Deliverables & Reminders The usual deliverables and reminders apply. Compress and submit your project folder to Canvas before the deadline (or after with penalty, per the syllabus). The more serious warnings are listed below, but of course past assignments have more detailed instructions. Ensure your submission contains the necessary le(s) and is the correct version of your code that you want graded. This is your warning. You will not be allowed to resubmit this assignment after the due date if you submit the wrong tile. Remember that this is graded on accuracy, and this is a pair project. Plagiarism or cheating will be dealt with seriously. Rubric A rubric is also available on the assignment page on Canvas. but here is an outline of the point breakdown for different categories. . (10pts) Code cleanliness, conventions. and comments c (1011 Opts) for >95% correctness and all required comments c (7.5!10pts) for repeated minor mistakes or one egregious mistake, or for missing either overall or inline comments c (5110pts) for code that is sloppy and hard to understand or is missing all comments c ( 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