Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA question, Connect Four PROBLEM PROMPT: Board Games, Connect Four For this problem, you are given an interface Game that describes a 2-player grid-based board

JAVA question, Connect Four

PROBLEM PROMPT: Board Games, Connect Four

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.

ConnectFourGame

ConnectFourGame has a 6 x 7 board on which players can place either a red or a yellow piece (represented by 'r' and 'y', respectively). However, in Connect Four, unlike tic-tac-toe, each column is a "chute" into which a player drops a piece to make a move. The pieces fill up the column one by one (up to 6 pieces in one column). If you are unfamiliar with the game, please see https://en.wikipedia.org/wiki/Connect_Four and/or look it up on youtube.

A "move" in ConnectFourGame is a string that contains the column number (0-6), and the character to mark the space with (either 'r' or 'y'). For example, placing a red piece down the last column would be done with the command: "6r".

A "move" in ConnectFourGame is valid if:

The column number represents a valid column on the board that is not already full

The mark is 'r' if it is r's turn, or 'y' if it is y's turn.

Whoever makes the first move is player 1. For instance, if 'r' makes the first move, then 'r' is player 1 (and 'y' is player 2), and vice-versa.

A player wins by getting 4 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

Describe the various types of research designs.

Answered: 1 week ago

Question

Discuss the effectiveness of a national infrastructure for HRD

Answered: 1 week ago