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

Java Programming. Write your own source code with comments. (Game: connect four)

ID: 3787131 • Letter: J

Question

Java Programming. Write your own source code with comments.

(Game: connect four) Connect four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically suspended grid, as shown below. The objective of the game is to connect four same-colored disks in a row, a col- umn, or a diagonal before your opponent can do likewise. The program prompts two players to drop a red or yellow disk alternately. In the preceding figure, the red disk is shown in a dark color and the yellow in a light color. Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:

Drop a red disk at column (0-6) :3 IYI IYI I IIYI RIYIRIR IYI YIRIRIRIYIYIR IYIRIYIYIRIRIRI Drop a yellow disk at column (0-6) 5 IYI IYI IIIYI RIYIRIR YIY IYIRIRIRIYI YIRI IYIRIYIYIRIRIRI The yellow player won.

Explanation / Answer

import java.util.Scanner;

/**
*
* @author paramesh
*/
public class ConnectFour {
char[][] gameBoard;
public ConnectFour() {
gameBoard = new char[7][6];
initialize();
}

// initializing grid
public void initialize() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
this.gameBoard[i][j] = ' ';
}
}
}

//set char into grid
public boolean setChoice(int column, char color) {
if (column < 0 || column > 6) {
System.out.println("Please enter correct value next time...");
} else {
for (int i = 0; i < 6; i++) {
if (this.gameBoard[i][column] == ' ') {
this.gameBoard[i][column] = color;
}
}
}
if(checkWin(color)){
System.out.println(color +" wins");
return true;
}
return false;
}

public boolean checkWin(int color) {
// column check
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 7; i++) {
if (this.gameBoard[i][j] == color && this.gameBoard[i][j + 1] == color && this.gameBoard[i][j + 2] == color && this.gameBoard[i][j + 3] == color) {
return true;
}
}
}
// row check
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 6; j++) {
if (this.gameBoard[i][j] == color && this.gameBoard[i + 1][j] == color && this.gameBoard[i + 2][j] == color && this.gameBoard[i + 3][j] == color) {
return true;
}
}
}
// diagonal check
for (int i = 3; i < 7; i++) {
for (int j = 0; j < 3; j++) {
if (this.gameBoard[i][j] == color && this.gameBoard[i - 1][j + 1] == color && this.gameBoard[i - 2][j + 2] == color && this.gameBoard[i - 3][j + 3] == color) {
return true;
}
}
}
// diagonal check
for (int i = 3; i < 7; i++) {
for (int j = 3; j < 6; j++) {
if (this.gameBoard[i][j] == color && this.gameBoard[i - 1][j - 1] == color && this.gameBoard[i - 2][j - 2] == color && this.gameBoard[i - 3][j - 3] == color) {
return true;
}
}
}
return false;
}
public void printBoard(){
System.out.println(" ");
for(int i=0;i<7;i++){
System.out.print("|");
for(int j=0;j<6;j++){
System.out.print(this.gameBoard[i][j]+"|");
}
System.out.println(" ");
}
}

public static void main(String args[]) {
// creating object
ConnectFour obj = new ConnectFour();
Scanner sc = new Scanner(System.in);
char player = 'R';
while(true){
if(player == 'R'){
System.out.println("Drop a Red disk at column (0-6): ");
int column = sc.nextInt();
if(obj.setChoice(column, player)){
break;
}
obj.printBoard();
player ='Y';
}
else{
System.out.println("Drop a Yellow disk at column (0-6): ");
int column = sc.nextInt();
if(obj.setChoice(column, player)){
break;
}
obj.printBoard();
player ='R';
}
}
}
}

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