Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java Write a class to represent a connect 4 game, using a 2-dimensional array (I would recommend an array of strings or chars, with

Using Java

Write a class to represent a connect 4 game, using a 2-dimensional array (I would recommend an array of strings or chars, with each cell initially set to " ") Two players, player 1 and player 2, will be playing. Both players are people sitting at the same computer console. Use a string or char of X and O, or * and O, or something similar to represent each player's token. The board is represented by a 2-dimensional array, 6 rows of 7 columns, of strings or chars. The value of a " " represents that this space is available; the value X indicates the space is occupied by player 1, the value O indicates the space is occupied by player 2. (Replace X and O with whichever token symbols you choose to use). Hint: Using " " makes your game board print on the screen easily when the player's token is dropped in that spot.

You will need two objects for the game:

A Player object that has the player's name, and their token symbol

A Connect4 object that has the 2-dimensional game board, and several methods:

Play - this method will play the game between the two people represented by two instances of the Player object. The game is played by two people sitting at the computer console, there is no "AI" in this. The Play method will call several other methods outlined below to verify the move is legal, update the board, and check for a winner.

The game board is 6x7, so there are a total of 42 moves possible - loop and prompt for plays from each player in turn. In the play method on your Connect4 object, you will need to loop, and prompt for plays(A6x7 board= 42possible plays),asking which column to"drop"their token into. Make sure the column is a valid column,and that the column isn't full. At each iteration of the loop,you will need to call methods of the Connect4class to update the Connect4object. You need to keep track of who is playing(player1or player2),enforce the rules,check if either player has won the game.

If a player wins,exit the loop and return the winner to the client. You can also present the results of the game. If the game ends in a tie,you should output that result.

In your Connect4 class, you will need to code the following methods:

A default constructor that takes two players who will play the game, and instantiating the 2-dimensional array representing the board

A play method, that will start/play the game

A method that allows a player to make a move; it takes two arguments: the player and the column on the board

A method checking if a play is legal

A method checking if a player has won the game; you can break up that method into several methods if you like (for instance, check if a player has won with vertical 4 in a row, horizontal 4 in a row, / diagonal 4 in a row, or \ diagonal 4 in a row).

Write a client (driver) class, where the main method is located, to test all the methods in your class and enable the user to play. In the main method of your client class, your program will simulate a connect-4 game from the command line, doing the following:

Create two Player objects

Create a Connect4 object, and instantiate it

Call the Connect4 object's play method, and report the result of the game. Prompt the users to play again or exit the game.

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

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago

Question

Write short notes on departmentation.

Answered: 1 week ago

Question

What techniques are used to capture the readers attention?

Answered: 1 week ago

Question

What reader benefits are included?

Answered: 1 week ago