CONNECT FOUR
TOPIC 03v2 – CONNECT FOUR AI

 

GUIDE TO CREATING StackerAI01

 

 

DESCRIPTION

 

In this guide, you will create an AIAgent that focuses on dropping chips on a single column.  This requires that the AI remembers the targeted column from one move to another.  We do this by creating an instance variable and constructor for the AI.

 

STEP 1

 

Let’s start with all of the small details filled in for us.  Copy and paste the following code into Eclipse.  Take a minute to make sure you are comfortable with everything here.

 

public class StackerAI01 implements AIAgent

{

    

     @Override

     public int playMove(Game g, int playerNumber)

     {

           //incomplete

     }

 

     @Override

     public String getName()

     {

           return "StackerAI01";

     }

 

     @Override

     public String getWinQuote(double winPercentage)

     {

           if (Math.random() < 0.50)

                return "Stackin' the odds in my favour";

           else

                return "You just suffered from a Stack Overflow error!";

     }

 

     @Override

     public String getAuthor()

     {

           return "Patrick Campeau";

     }

 

     @Override

     public void justWon()

     {

     }

 

     @Override

     public void justLost()

     {

     }

 

     @Override

     public void justTied()

     {

     }

}

 


STEP 2

 

We want to choose a target column and repeatedly drop a chip into that column.  This requires that we remember the column number from one move to another – so we cannot have column be a local variable inside playMove as such a variable would be erased at the end of the method. 

 

The solution is to use an instance variable.  We will call it targetColumn.

 

The best place to initialize the instance variable is in a constructor for the AI as this code is only executed once.  Inside the constructor, we simply give a random number between 0 and 6 to column.

 

public class StackerAI01 implements AIAgent

{

     private int targetColumn;

    

     //constructor

     public StackerAI01()

     {

           targetColumn = (int)(Math.random() * 7);  //0 to 6

     }

    

     @Override

     public int playMove(Game g, int playerNumber)

     {

           //incomplete

     }

 

     @Override

     public String getName()

     {

           return "StackerAI01";

     }

 

     @Override

     public String getWinQuote(double winPercentage)

     {

           if (Math.random() < 0.50)

                return "Stackin' the odds in my favour";

           else

                return "You just suffered from a Stack Overflow error!";

     }

 

     @Override

     public String getAuthor()

     {

           return "Patrick Campeau";

     }

 

     @Override

     public void justWon()

     {

     }

 

     @Override

     public void justLost()

     {

     }

 

     @Override

     public void justTied()

     {

     }

}

 

 

STEP 3

There is one simply step left.  We need to implement the playMove method.  Of course, we simply want the AI to return the targetColumn.

 

Note: If the column is full, this will be an illegal move and this AI will lose.  So that is a weakness that we could improve on.

 

public class StackerAI01 implements AIAgent

{

     private int targetColumn;

    

     //constructor

     public StackerAI01()

     {

           targetColumn = (int)(Math.random() * 7);  //0 to 6

     }

    

     @Override

     public int playMove(Game g, int playerNumber)

     {

           return targetColumn;

     }

 

     @Override

     public String getName()

     {

           return "StackerAI01";

     }

 

     @Override

     public String getWinQuote(double winPercentage)

     {

           if (Math.random() < 0.50)

                return "Stackin' the odds in my favour";

           else

                return "You just suffered from a Stack Overflow error!";

     }

 

     @Override

     public String getAuthor()

     {

           return "Patrick Campeau";

     }

 

     @Override

     public void justWon()

     {

     }

 

     @Override

     public void justLost()

     {

     }

 

     @Override

     public void justTied()

     {

     }

}

 

 

STEP 4

 

Take a minute to understand what is going on.

 

When the StackerAI is first created, it’s constructor initializes the targetColumn variable to a random value from 0 to 6.

 

Now, whenever playMove is called, the AI simply returns that target column stacking chips up hoping for a victory.

 

STEP 5

 

Test this new AI versus other AIs.  Make sure to watch a few games to fully understand what is going on before jumping to using series or tournaments for testing.

 

IMPROVEMENTS

 

Column full weakness – This AI does not check if a column is full before returning the targetColumn.  The AI could check if the column was full and reset the targetColumn to a new one that wasn’t full.

 

Series/Tournament repetition – The AI object is created only once.  So in its current state, a series of 1000 games would all have the same targetColumn.  Maybe that is fine but perhaps one should reset the targetColumn at the start of each game.  We can check if the game is starting by using the Game’s method getTurnCount().