Question: (JAVA) Youll implement the the game Connect Four. https://en.wikipedia.org/wiki/Connect_Four The board size is 7 columns by 6 rows. Use red and black for the two

(JAVA) Youll implement the the game Connect Four. https://en.wikipedia.org/wiki/Connect_Four

The board size is 7 columns by 6 rows. Use red and black for the two players and have red always go first. Use the starter code CFPlayer.java, CFGame.java, and Test.java. Your code must work with Test.java.

starter code CFGame.java:

public class CFGame {

//state[i][j]= 0 means the i,j slot is empty

//state[i][j]= 1 means the i,j slot has red

//state[i][j]=-1 means the i,j slot has black

private final int[][] state;

private boolean isRedTurn;

{

state = new int[7][6];

for (int i=0; i<7; i++)

for (int j=0; j<6; j++)

state[i][j] = 0;

isRedTurn = true; //red goes first

}

public int[][] getState() {

int[][] ret_arr = new int[7][6];

for (int i=0; i<7; i++)

for (int j=0; j<6; j++)

ret_arr[i][j] = state[i][j];

return ret_arr;

}

public boolean isRedTurn() {

return isRedTurn;

}

public boolean play(int column) {

if (state[i][j] < -1 || state[i][j] > 7)

{

return false;

}

}

public boolean isGameOver() {

return false;

}

public int winner() {

return 0;

}

}

Finish writing the class CFGame.

The method:

public boolean play (int c )

plays the column c. If column c cannot be played because it is full or because it is an invalid column, return false. Otherwise, return true. The columns are counted with 1-based indexing, i.e., the columns range from 1 to 7.

The method:

public boolean isGameOver ()

returns true if the game is over because there is a winner or because there are no more possible moves and returns false otherwise.

The method:

public int winner ()

returns 1 if red is the winner, -1 if black is the winner, and 0 if the game is a draw. This method should be called when isGameOver returns true.

CFGame provides the bare-bone game logic but doesnt play the game itself. By having ConsoleCF and GUICF inherit CFGame you avoid duplicating the code that implements the basic Connect Four game logic, while allowing them to play the Connect Four game in a different manner.

==================================

Test.java:

import java.util.Scanner; import hw4.CFPlayer; import hw4.RandomAI; import hw4.YourNameAI; import hw4.ConsoleCF; import hw4.GUICF; public class Test { public static void main(String[] args) { Scanner reader = new Scanner (System.in); int gameMode = reader.nextInt(); if (gameMode==1) { new GUICF(new YourNameAI()); } else if (gameMode==2) { CFPlayer ai1 = new YourNameAI(); CFPlayer ai2 = new RandomAI(); int n = 10000; int winCount = 0; for (int i =0; i < n ; i++) { ConsoleCF game = new ConsoleCF(ai1, ai2); game.playOut(); if(game.getWinner() == ai1.getName()) winCount++; } System.out.println(((double) winCount)/n); } else { ConsoleCF game = new ConsoleCF(new YourNameAI()); game.playOut(); System.out.println(game.getWinner() + " has won."); } } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!