Question
(JAVA) Implement 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
(JAVA) Implement 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. Put everything a package called connect
Starter Code:
*************************************************************************************************** CFPlayer.java
package connect;
public interface CFPlayer {
int nextMove(CFGame g);
//return value of getName cannot be "draw"
String getName();
}
***************************************************************************************************
CFGame.java
package connect;
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
for (int j=0; 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
for (int j=0; j
ret_arr[i][j] = state[i][j];
return ret_arr;
}
public boolean isRedTurn() {
return isRedTurn;
}
public boolean play(int column) {
return false;
}
public boolean isGameOver() {
return false;
}
public int winner() {
return 0;
}
}
***************************************************************************************************
Test.java
import java.util.Scanner;
import connect.CFPlayer;
import connect.RandomAI;
import connect.YourNameAI;
import connect.ConsoleCF;
import connect.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
ConsoleCF game = new ConsoleCF(ai1, ai2);
game.playOut();
if(game.getWinner() == ai1.getName())
winCount++;
}
System.out.println(((double) winCount));
} else {
ConsoleCF game = new ConsoleCF(new YourNameAI());
game.playOut();
System.out.println(game.getWinner() + " has won.");
}
}
}
***************************************************************************************************
Remark. Code must work with Test.java. Remark. When writing a class, its usually a bad idea to place the main function in the same class youre trying to debug. There are several reasons for this, but one reason is that doing so will circumvent encapsulation.
********************************************************************************************* 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.
************************************************************************************************ Remark. 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.
************************************************************************************************ Write the class RandomAI that implements CFPlayer.
The implementation of the method
int nextMove(CFGame g) returns, but does not itself play, a random legal column.
************************************************************************************************* The implementation of the method
String getName() returns "Random Player".
************************************************************************************************* Write a class named YourNameAI that implements CFPlayer, where you should replace YourName with your actual name.
The implementation of the method
int nextMove(CFGame g) returns, but does not itself play, a legal column that your AI wants to play. Your AI must be good enough to beat RandomAI 80% of the time. You do not need a sophisticated algorithm for this.
************************************************************************************************** The implementation of the method
String getName() returns "Your Names AI".
************************************************************************************************** Hint. Start by writing a very simple strategy and make it work. If you start by writing a complex strategy, you may have to spend an inordinate amount of time debugging your complex logic. Hint. The following is a good starting point. Checks if there is a winning move, and if so play it. Then check if the opponent would have a winning move, and if so block it. Otherwise play a random move by, say, calling nextMove from a RandomAI Object. Is this approach good enough? If so youre done, and if not improve upon it. Clarification. The nextMove methods of CFPlayers dont play the moves. They just return what move they think" you should play. The idea is to keep CFGame and CFPlayer minimal and to defer the logic of how to play the game to ConsoleCF and GUICF.
***************************************************************************************************
Write the class ConsoleCF that inherits CFGame. ConsoleCF is a command-line implementation of the Connect Four game. ConsoleCF should have the following contructors, methods, and inner class.
The constructor
public ConsoleCF(CFPlayer ai) sets up a human vs. AI game, where the red player (the player who goes first) is randomly decided.
*************************************************************************************************** The constructor
public ConsoleCF(CFPlayer ai1, CFPlayer ai2) sets up a AI vs. AI game, where the red player (the player who goes first) is randomly decided.
*************************************************************************************************** The method
public void playOut() plays the game until the game is over.
************************************************************************************* The method
public String getWinner() return either "draw", "Human Player", or the AIs name given by CFPlayers getName method.
*************************************************************************************************** The private inner class
private class HumanPlayer implements CFPlayer. HumanPlayers nextMove implementation should print the state of the board to the command line and ask the user for the next move. If the provided move is invalid, say so and ask again for a valid move. HumanPlayers getName implementation should return "Human Player".
*************************************************************************************************** Clarification. With this code, you should be able to run public static void main (String [] args ) { CFPlayer ai1 = new JohnAI (); CFPlayer ai2 = new RandomAI (); int n = 10000 ; int winCount = 0; for ( int i=0; i *************************************************************************************************** Write the class GUICF that inherits CFGame. ConsoleCF is a graphical implementation of the Connect Four game. GUICF should have the following fields, contructors, methods, and inner class. The private field private GameBoard this_board ; represents the graphics for the 6 7 board, but not the buttons. *************************************************************************************************** The constructor public GUICF ( CFPlayer ai) sets up and starts a human vs. AI game, where the red player (the player who goes first) is randomly decided. ************************************************************************************* The constructor public GUICF ( CFPlayer ai1 , CFPlayer ai2 ) sets up and starts a AI vs. AI game, where the red player (the player who goes first) is randomly decided. A human vs. AI game and an AI vs. AI game creates slightly different GUIs. A human vs. AI game will have a graphical interface like Figure 1. At each turn, the AI makes the move automatically and the human players move is specified through clicking a button. An AI vs. AI game will have a graphical interface like Figure 2. When you click the Play" the next AI makes the next move. *************************************************************************************************** The private method playGUI private boolean playGUI ( int c) plays the column c. So the internal game logic inherited from CFGame and the displayed board represented in this_board must be updated. If c is a column that can be played, play the column and return true. Otherwise return false and do not update the state of the game. *************************************************************************************************** Write the private class private class GameBoard extends javax . swing . JPanel { private GameBoard () { // initialize empty board ... } private void paint ( int x, int y, int color ) { // paints specified coordinate red or black ... } } that represents the game board. In real life, the game pieces are circular, but in GameBoard they can be square. These square pieces can be done with 6*7=42 JLabels and a GridLayout. (You may want to use setOpaque(...) with the JLabels.) If you want circular pieces, you will have to manually draw them using the Graphics2D object. The GameBoard does not include the buttons. *************************************************************************************************** Remark. Problems 4 and 5 do not specify what happens at the end of the game. Your game should display who the winner is or that the game ended in a draw in some reasonable way. Hint. To get a downward pointing arrow shown in Figure 1, create a JButton with text "\u2193". The following link exaplains why this works. http://www.fileformat.info/info/unicode/char/2193/index.htm *************************************************************************************************** Figure 1: Human vs. AI GUI sketch (one with down arrows) Figure 2: AI vs. AI GUI sketch (one with play)
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