Question: JAVA question PROBLEM PROMPT: Board Games For this problem, you are given an interface Game that describes a 2-player grid-based board game: Game.java. Game declares
JAVA question
PROBLEM PROMPT: Board Games
For this problem, you are given an interface Game that describes a 2-player grid-based board game: Game.java. Game declares the following methods:
- boolean isValidMove(String move) // returns true if the move represented by the String is valid
- void executeMove(String move) // executes the move represented by the String
- boolean gameOver() // returns true if the game is over
- String displayBoard() // returns a String representing the current state of the board
- int determineWinner() // if there is a winner, returns the player number of the winner (either 1 or 2); if no winner, returns 0
Note that with these five methods alone, this interface is flexible enough to handle games such as tic-tac-toe, Connect Four, chess, checkers, Othello, the game of Nim, etc. Your job is to provide implementations of the Game interface for two of these games: tic-tac-toe and Connect Four. Specifically, you must implement two classes that implement the Game interface: TicTacToeGame and ConnectFourGame.
TicTacToeGame
- TicTacToeGame has a 3 x 3 board on which players can place either 'x' or 'o' in any blank spot
- A "move" in TicTacToeGame is a string that contains the row number (0-2), column number (0-2), and the character to mark the space with (either 'x' or 'o'). For example, placing an 'x' in the top middle square (space 0, 1) would be done with the command: "01x".
- A "move" in TicTacToeGame is valid if:
- The row and column number represent a valid space on the board that has not yet been taken
- The mark is 'x' if it is x's turn, or 'o' if it is y'o turn.
- Whoever makes the first move is player 1. For instance, if 'x' makes the first move, then 'x' is player 1 (and 'o' is player 2), and vice-versa.
- A player wins by getting 3 in a row, either horizontally, vertically, or diagonally
- A game is over if there are no more moves, or a player has won.
Game Driver and Game engine are provided,
package labs.lab5;
import java.util.Scanner;
public class GameDriver { public static void main(String[] args) { System.out.println("Which game do you want to play? Enter 1 for Tic-Tac-Toe, 2 for Connect Four: "); Scanner scanner = new Scanner(System.in); int option = scanner.nextInt(); GameEngine gamePlayer; if (option == 1) { gamePlayer = new GameEngine(new TicTacToeGame()); } else { gamePlayer = new GameEngine(new ConnectFourGame()); } gamePlayer.play(); } }
package labs.lab5;
import java.util.Scanner;
/** * General player for two-player game. */ public class GameEngine { private Game game; private int playerTurn;
/** * Create a game player for the provided game. * * @param game game model */ public GameEngine(Game game) { this.game = game; this.playerTurn = 1; }
/** * Play the game. */ public void play() { Scanner scanner = new Scanner(System.in);
System.out.println(game.displayBoard());
while (!game.gameOver()) { String move = getMove(scanner); game.executeMove(move); switchPlayer(); System.out.println(game.displayBoard()); }
printWinner(); }
private String getMove(Scanner scanner) { boolean valid; String move; do { System.out.print(" Enter move Player " + playerTurn + ": "); move = scanner.next(); valid = game.isValidMove(move); if (!valid) { System.out.println("Invalid move!"); } } while (!valid);
return move; }
private void switchPlayer() { if (playerTurn == 1) { playerTurn = 2; } else { playerTurn = 1; } }
private void printWinner() { int winner = game.determineWinner(); if (winner > 0) { System.out.println(" Player " + winner + " wins!"); } else { System.out.println(" Nobody wins."); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
