Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Connect Four Java Game: Connect Four is a two-player game in which the players f

ID: 3705200 • Letter: C

Question

Connect Four Java Game:

Connect Four is a two-player game in which the players first choose a color, and then take turns dropping colored discs from the top into a seven-column, six-row vertically suspended grid. The pieces fall straight down, occupying the next available space within the column. The objective is to be the first one to form a horizontal, vertical or diagonal line of four of one’s own discs.

Write a simple console based program to simulate the Connect four Game where the user enters his/her choice and takes turns to play with the computer.

Explanation / Answer

Requirement1: Game Board:

public class Connect4 {

public enum Color{

RED('R'), GREEN('G'), EMPTY('');

private final char value;

Color(char value)

{

this.value= value;

}

public String toString(){

return String.valueOf(value);

}

}

public static final int COLUMNS=7;

public static final int ROWS=6;

private Color[][] board= new Color[COLUMNS][ROWS];

public Connect4()

{

for(Color[] column : board){

Arrays.fill(column,Color.EMPTY);

}

Requirement2: Introducing DISCS

public void putDisc(int column)

{

if(column> 0 && column<= COLUMNS){

int numOfDiscs = getNumberOfDiscsInColumn(column-1);

if(numOfDiscs < ROWS) {

board[column-1][numOfDiscs] = Color.RED;

}

}

}

private int getNumberOfDiscsInColumn(int column) {

if(column >=0 && column < COLUMNS) {

int row;

for(row=0; row< ROWS; row++) {

if(Color.EMPTY == board[column][row]) {

return row;

}

}

return row;

}

return -1;

}

REQUIREMENT 3 - Player Shifts

private Color currentPlayer = Color.RED;

private void switchPlayer() {

if(Color.RED == currentPlayer) {

currentPlayer= Color.GREEN;

}

else {

currentPlayer= Color.RED;

}

}

public void putDisc(int column) {

if(column > 0 && COLUMN<= COLUMNS){

int numOfDiscs= getNumberOfDiscsInColumn (column-1);

if(numOfDiscs < ROWS) {

boardp[column-1][numOfDiscs]= currentPlayer;

switchPlayer();

}

}

}

REQUIREMENT 4 - GAME'S OUTPUT

private static final String DELIMITER ='|';

private void switchPlayer() {

if(Color.RED== currentPlayer) {

currentPlayer= COlor.GREEN;

}

else {

currentPlayer=Color.RED;

}

System.out.println("Current Turn:" +currentPlayer);

}

public void printBoard() {

for(int row=ROWS-1; row>=0; --row) {

StringJointer stringJoiner= new StringJoiner (DELIMITER, DELIMITER, DELIMITER);

for(int col=0; col< COLUMNS; ++col) {

stringJoiner.add(board[col][row].toString());

}

System.out.println(stringJoiner.toString());

}

}

public void putDisc(int column) {

if(column> 0 && column <=COLUMNS) {

int numOfDiscs= getNumberOfDiscsInColumn(column-1);

if(numOfDiscs < ROWS) {

board[column-1][numOfDiscs]=currentPlayer;

printBoard();

switchPlayer();

} else {

System.out.println(numOfDiscs);

System.out.println("There's no room" + "for a new disc in this column");

printBoard();

}

} else {

System.out.println("Column out of Bounds");

printBoard();

}

}

WIN CONDITION:

public boolean isFinished() {

int numOfDiscs=0;

for(int col=0;col < COLUMNS; ++col) {

numOfDiscs += getNumberOfDiscsInColumn(col);

}

if(numOfDiscs >= COLUMNS * ROWS) {

System.out.println("It's a Draw");

return true;

}

return false;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote