Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java programming question. Making a tik tak toc program. Before anything, apologize for the long post. Tried to divide it into few questions but everything

Java programming question.

Making a tik tak toc program.

Before anything, apologize for the long post.

Tried to divide it into few questions but everything is kind of connected

so one does not really make sense without the other.

TicTacToe.java, Location.java, Board.java is the part that I need help with.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

There are some provided codes that could be useful for creating the program.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

Pasted code for Board.java

public class Board {

public GameState getGameState() { GameState current = GameState.ONGOING; current = checkHorizontalWin(); if (current != GameState.ONGOING) { return current; } current = checkVerticalWin(); if (current != GameState.ONGOING) { return current; } current = checkDiagonalWin(); if (current != GameState.ONGOING) { return current; } if (checkTie()) { return GameState.TIE; } return GameState.ONGOING; }

private GameState checkHorizontalWin() { for (int row = 0; row

private GameState checkVerticalWin() { for (int i = 0; i

private GameState checkDiagonalWin() { char boardMiddle = board[1][1]; boolean diagnolOne = board[0][0] == boardMiddle && boardMiddle == board[2][2]; boolean diagnolTwo = board[0][2] == boardMiddle && boardMiddle == board[2][0]; if (diagnolOne || diagnolTwo) { if (boardMiddle == 'X') { return GameState.PLAYER1_WIN; } else if (boardMiddle == 'O') { return GameState.PLAYER2_WIN; } } return GameState.ONGOING; }

private boolean checkTie() { for (int i = 0; i

public String toString() { String boardString = ""; for (int i = 0; i

Pasted code for TicTacToe.java

import java.util.Scanner;

public class TicTacToe {

private static int getNumberPlayers(Scanner sc) { boolean repeatPrompt = true; int numPlayers = 0; while (repeatPrompt) { System.out.print("How many players (1 or 2)? "); String input = sc.next(); try { numPlayers = Integer.parseInt(input); if (numPlayers == 1 || numPlayers == 2) { repeatPrompt = false; } else { System.out.println("Enter 1 or 2 players."); } } catch (NumberFormatException e) { System.out.println("Please only enter a number."); } } return numPlayers; }

private static Location getInput(String player, Scanner sc) { boolean repeatPrompt = true; int row = -1; int col = -1; while (repeatPrompt) { System.out.print("Enter desired square for " + player + ": "); String input = sc.next(); input = input.trim(); String[] splitInput = input.split(","); try { row = Integer.parseInt(splitInput[0]); col = Integer.parseInt(splitInput[1]); repeatPrompt = false; } catch (NumberFormatException | IndexOutOfBoundsException e) { System.out.println("Please follow the format 'row,col'; for ex '1,2'"); } } Location loc = new Location(row, col); return loc; }

}

Rules 1. The game is played on a 3x3 grid. 2. Player 1 is the letter X, and Player 2 is the letter 0. Players take turns putting their marks in empty squares. 3. The first player to get 3 marks in a row (vertically across any column, horizontally along any row, or diagonally across any diagonal) is the winner. 4. When all 9 squares are full, the game is over. If no player has won, the game ends in a tie. TicTacToe.java We will run this Java file to play our game of Tic-Tac-Toe (i.e. this is our driver class). Implement the following methods. Make sure these methods are public! a main method. - Initialize a Scanner here - Call a method to prompt the user for the number of players * If there is one player, it will be a player vs computer game * If there are two players, it will be a player vs player game * Remember that Player 1 is always the letter X, and Player 2 is always the letter 0. - Begin the game. Hint: Other methods in the Tic Tac Toe class will be very useful here! Remember that we want to reuse code whenever possible. - Continue the game until someone has won or there is a tie. Once the game is over, the program will finish. It will not prompt the user if they would like to play again. a method which simulates a one-player game with the computer. Hint: Make sure you take advantage of methods in the Board and TicTacToe classes. They are handy and make this method much easier! - Create a Tic-Tac-Toe board. - While the game isn't over, the player and the computer will take turns placing their letter (X or 0) on empty squares. Be sure to prompt the player for where they want to go. - The player will always be Player 1. The computer will always be Player 2. - On Player 1's turn, the program will print out the board. It will prompt the player for input and update the board with the player's move. - On the computer's turn, the computer will randomly pick an empty square on the board and place its letter there. - When the game is over, let the user know who won (or if it was a tie) a method which simulates a two-player game. Hint: Make sure you take advantage of methods in the Board and Tic Tac Toe classes. They are handy and make this method much easier! - Create a Tic-Tac-Toe board. While the game isn't over, prompt players for where they want to go. Players will take turns placing their letter (X or O) on empty squares For each player's turn, the program will print out the board. Then it will prompt whoever's turn it is for input, and update the board with that player's move. - When the game is over, let the user know who won (or if it was a tie) We have provided the following methods for TicTacToe.java. a getNumberPlayers method Given a Scanner, this method prompts the user and returns the number of players. import java.util.Scanner; public class TicTacToe { private static int getNumberPlayers (Scanner sc) { boolean repeatPrompt = true; int numPlayers = 0; while (repeatPrompt) { System.out.print("How many players (1 or 2)? "); String input = sc.next(); try { numPlayers = Integer.parseInt(input); if (numPlayers == 1 numPlayers == 2) { repeatPrompt = false; } else { System.out.println("Enter 1 or 2 players."); } catch (NumberFormatException e) { System.out.println("Please only enter a number."); return numPlayers; a getInput method - Given a Scanner, this method reads user input from the command line and returns a Location object. private static Location getInput(String player, Scanner sc) { boolean repeatPrompt = true; int row = -1; int col = -1; while (repeatPrompt) { System.out.print("Enter desired square for " + player + ": "); String input = sc.next(); input = input.trim(); String[] splitInput = input.split(","); try { row = Integer.parseInt(splitInput[0]); col = Integer.parseInt(splitInput[1]); repeatPrompt = false; } catch (NumberFormatException IndexOutOfBoundsException e) { System.out.println("Please follow the format 'row, col'; for ex '1,2"); sy Location loc = new Location(row, col); return loc; } GameState.java Provided Java file. It is an enum representing the game's state - a win for Player 1, a win for Player 2, a tie, or an ongoing game. Do not modify this file. 1 2 3 public enum GameState { PLAYER1 WIN, PLAYER2_WIN, ONGOING, TIE; - Location.java This Java file represents a given location on the board. It has two instance variables: row and column. For example, a Location object where the row is 2 and the column is 2 would represent the square at the bottom right of a Tic-Tac-Toe board. Because they are private, we cannot access them from outside the Location class. Therefore, implement the following getter methods in Location.java. Make these methods public. a getter for row, properly named (ie, getRow) a getter for column, properly named (ie, getColumn) public class Location { private int row; private int column; Soco van WN public Location(int row, int column) { this.row = row; this.column = column; Il getter for row in this place Il getter for column in this place 12 13 } Board.java This Java file represents our Tic-Tac-Toe board. Create a 2D character array called board to hold the current state of the Tic-Tac-Toe board. This board should be 3x3 and be an instance variable. When the game begins, the board should only hold empty spots. An empty spot is represented with a space character (' '). As players place their tokens, it should begin to fill with Xs and Os. Player 1 is X and Player 2 is 0. You will implement the following public methods for Board. a method that takes in a location and returns a boolean. True if the location is valid (ie, on the board) and the location is an empty spot. False if the location is invalid or non-empty. An example of an invalid location would be the spot 40,2 because there is no row 40 in our 3x3 grid. a method that takes in a location and a letter, and returns a boolean This method places the letter (which should be an X or 0), in the specified location. This should only happen if the location is valid. - Return true if placing the letter is successful, false if not We have provided the following public methods for Board. the getGameState method - Returns a GameState representing the current state of the game Either there was a winner, a tie, or it's still ongoing 1 public class Board { public GameState getGameState() { GameState current = GameState.ONGOING; current = checkHorizontalwin ; if (current != GameState.ONGOING) { return current; current = checkVerticalWin(); if (current != GameState.ONGOING) { return current; No BBURHEBEO von Awn current = checkDiagonalWin(); if (current != GameState.ONGOING) { return current; if (checkTie()) { return GameState.TIE; return GameState.ONGOING; private GameState checkHorizontalwin() { for (int row = 0; row

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions