CONNECT FOUR
TOPIC 03v2 – CONNECT FOUR AI

 

LESSON WORK

 

 

TASK

 

  • You will create an AI agent that will play Connect Four (Version 2.X).  

  • You will be given the implementation of the Connect Four game.  You will also be given three basic examples of an AI agent:
    • RandomAI01 – An AI agent that randomly selects a column to play in.
    • CampeauAI01 – An AI agent that will choose a column based on a score that is attributed to each spot on the board.
    • CampeauAI02 – An AI agent that checks if one of the columns leads to a win and chooses that column.  Otherwise, it chooses a random column.

 

  • All student AI agents will face off against each other to see which works best.  The student AIs will have to play as both Player 1 and Player 2.  The AI with the most points will be declared champion.  The point system used will be:

              Win: 1 point
              Tie: 0 points
              Loss: 0 points

 

  • Mr. Campeau will provide an AI agent as well to participate in the competition.  A random AI will also participate.

 

THE GAME

The game board consists of six rows and seven columns of slots.  There are two players in the game.  Turn by turn, a player drops a chip into one of the columns and it falls to the bottommost empty slot. 


 

The player that first makes a straight line of four consecutive chips with their colour wins the game.  The winning line could be vertical, horizontal or diagonal.  If there is not winning line and the board is full, then the game is a tie.

TWIST TO THE GAME

 

For the tournament, the first move for each player will be done randomly.  This is done to ensure that we don’t have the exact same game being repeated over and over between 2 AI agents.

IMPLEMENTATION

 

  • Each possible placement on the board is a slot.  The board consists of 6 rows and 7 columns of slots for a total of 42 slots.

  • The bottom row is row 0 and the top row is row 5.  The leftmost column is column 0 and the rightmost column is column 6.  See the diagram below.

 


Note: This diagram was updated for Version 2.X of the software.

  • Each slot is open or contains a chip owned by player 1 or player 2.


    ORANGE NEEDS TO BE UPDATED

  • While you can study the implementation of the of the Connect Four game inside Game.java, you should probably focus only on the public functions (and not the private ones).

 

  • You are also provided with a RandomAI class that works as a Random AI agent.  It simply returns a number between 0 and 6 representing a column number.  Initially, this RandomAI will be a good tool to understand how the game works and to test your early AI against.

 

 

KEY FILES

 

You will need the following files:

 

Game.java – The actual game with the main function.

Slot.java – A class to create objects that represent the slots in the game.
RandomAI.java – An basic AI agent that randomly selects a column value.

OTHER FILES

 

CampeauAI01.java – A simple AI agent that associates a value to each spot on the board.  It then considers the 7 spots that it can select and chooses the one with the best value.

 

CampeauAI02.java – A simple AI agent that checks if it can win this move.  If it can, it does that selection.  Otherwise, it uses the basic RandomAI strategy of returning a random column number.

 

TournamentAI.java – A file that allows you to setup a tournament between different AI agents.  You do not need to worry about this file while creating your AI.

 

ZIP FILE

 

Click here for a zip file that contains all of the required files to test your AI.  You will have to drag each one individually into your IDE.  Run Game.java.

 

TOURNAMENT ZIP FILE


Click here for a zip file that contains all of the files to run your AI and run an entire tournament.  Run TournamentAI.java.  You are suggested not to use these files initially.

 

WHERE TO START (WE WILL DO THIS TOGETHER)

 

You first need to know how to play the game.

You then need to know how to run the software.  Setting player1 to “human” and player2 to “randomAI” is a good start.  Play a few games.  Try the other settings.

 

You then need to create your own class called YourNameAI.java.  You should start by copying RandomAI.java into it.  Then you can start making your own code.

 

Note: The RandomAI provided with these files has a fatal flaw.  It returns a random column number.  If that column happens to be full, that AI loses its turn.  Therefore, you would be better off improving that RandomAI so that it doesn’t return a column number that is full (very easy to do).  Doing so would give you a better testing opponent for your own AI.

 

API

 

Click here.

 

TIPS

 

Be organized!  Your program will potentially grow considerably.

 

Use functions when possible so that you can easily reused them in later AI programs. 

Once you have a functioning AI program, save it somewhere.  Either duplicate the class or dump the code in another file.  If you just alter it, you have a difficult time coming back to it later.

 

Start small.  Create a few simple AI programs and test them.  It is a lot of fun watching your “babies” (I mean AI programs) play the game.

Avoid making AI programs that are only for player 1.  Try to make your programs work for both player 1 and player 2.

 

Q & A

 

Q:
Are you allowed to intentionally choose a column that is full in order to lose your turn? 

A:
No. 

Reasoning:

Imagine the scenario where there are two spots left one above the other and if RED gets to go last, it will win the game on the final move.  However, it is actually RED’s turn now so RED won’t get that spot.  Unless red skips a turn.  This would be a brilliant move, but if BLU also realizes this, we can end up in an infinite loop situation with both players continuously skipping their turn.

Note: A possible solution to this is if the game is declared a tie after a certain number of illegal moves.  This could be implemented into a future version of the code.

 

Q:
Can we submit multiple files for our AI program?

 

A:

Yes, you can submit a few files.  Since most students will have only one file, you can just reserve a filename (that we can share with others in class). 

 

Of course, if this proves to be too difficult to manage, we will come up with another system.