Question
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
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).
Using Java 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.
Using accessor and mutators for the rows and columns
// Some notes i have jotted down for format
Board Player1 Player2 Rules Chips
Connect 4 Board int WIDTH; 7 - can be constant int HEIGHT; 6 - can be constant chips [height] [width] board
Player int numChips; 25 Chips [numChips] COLOR playerColor String name
Constructor to create the board Public board ()
Accessors * private getRow (int rowNum) - return: chip[width] * private getColumn (int colNum) - return: chip [height] * private getDiagonal (int row, int col) - return: chip [2] [6] (length of array will be no greater than the height, may want to create constant about height of the diagonal array * public getDiagonal (int row, int col)
Will need to return 2 diagonals to check for winning Array will be created by the Ask for start position by subtracting a column and subtracting a row until you hit [0] [0] or [6] [7] (end of board). Array will check if filled, this will determine the winner
Mutators public setCell (int col, Chip chip) - return: void Chip color could be a class (separate file in the source) public enum COLOR: final public chip (enum Color)
public checkForWinner () - return: color -will also need to test for a tie
Connect 4 Engine Connect 4 Board board Player player1 Player player2
connect4Engine (Player p1, Player p2) play ( ) - return: void play will be a loop until someone wins or a tie
public Draw ( ) - return: void will draw the board
Play -> Draw -> make p1 move -> draw the board -> check for win/tie -> if winner terminate, if not -> make p2 move -> draw the board ->
Should have connect 4 driver to ask for their names, create 2 players with appropriate color. Creates a game engine.
Might want private method to check rows private checkRows() - return: COLOR private checkCols() - return: COLOR private checkDiag () - return: COLOR
Might want to check diagonals to determine
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