CONNECT FOUR
TOPIC 03v2 – CONNECT FOUR AI

 

GUIDE TO CREATING RecordTrackerAI01

 

 

DESCRIPTION

 

In this guide, you will create an AIAgent that focuses on tracking its win, loss and tie record.  It uses this data to display a message every 100 games but this could easily be used for other reasons.  For example, if an AI were losing most games after 10 games played, it could switch to another strategy all together to see if it has more success.

 

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 RecordTrackerAI01 implements AIAgent  //based on RandomAI01

{

     @Override

     public int playMove(Game g, int playerNumber)

     {

           int column = (int) (Math.random() * 7);

           return column;

     }

 

     @Override

     public String getName()

     {

           return "RecordTrackerAI01";

     }

 

     @Override

     public String getWinQuote(double winPercentage)

     {

           return "Random & Recorded”;

     }

    

     @Override

     public String getAuthor()

     {

           return "Patrick Campeau";

     }

    

     @Override

     public void justWon()

     {

     }

    

     @Override

     public void justLost()

     {

     }

    

     @Override

     public void justTied()

     {

     }

}

 


STEP 2

 

We want to track the wins, losses and ties. 

So, we add instance variables and a constructor to initialize them.  And, we implement the justWon(), justLost() and justTied() methods.

Now, at any point, we can see how wins, losses and ties we have so far.

 

public class RecordTrackerAI01 implements AIAgent  //based on RandomAI01

{

     private int wins;

     private int losses;

     private int ties;

    

     public RecordTrackerAI01()

     {

           wins = 0;

           losses = 0;

           ties = 0;

     }

    

     @Override

     public int playMove(Game g, int playerNumber)

     {

           int column = (int) (Math.random() * 7);

           return column;

     }

 

     @Override

     public String getName()

     {

           return "RecordTrackerAI01";

     }

 

     @Override

     public String getWinQuote(double winPercentage)

     {

           return "Random and Recorded";

     }

    

     @Override

     public String getAuthor()

     {

           return "Patrick Campeau";

     }

    

     @Override

     public void justWon()

     {

           wins++;

     }

    

     @Override

     public void justLost()

     {

           losses++;

     }

    

     @Override

     public void justTied()

     {

           ties++;

     }   

}

 

 

STEP 3 (OPTIONAL)

We can also add a few options to display messages every 100 games that include the AI’s record.  The changes:

  • Added a getRecord() method that returns a String containing the current WLT record.
  • Added a totalGames() method that returns the total games played.
  • Added a displayFunMessages() method that outputs a silly message.  Of course, this could be customized further to be more fun.
  • Updated the getWinQuote method to make use of the getRecord() method.
  • Updated the playMove() method to call displayFunMessages on first turn of every 100th game played.

 

 

public class RecordTrackerAI01 implements AIAgent  //based on RandomAI01

{

     private int wins;

     private int losses;

     private int ties;

    

     public RecordTrackerAI01()

     {

           wins = 0;

           losses = 0;

           ties = 0;

     }

    

     @Override

     public int playMove(Game g, int playerNumber)

     {

           //THIS CODE MAKES USE OF THIS AI’S RECORD TO DISPLAY

           //A MESSAGE EVERY 100 GAMES.  OF COURSE, THE AI’S RECORD

           //COULD BE USED TO DO MORE USEFUL THINGS.

           //NOTE: PROBABLY A BAD IDEA TO SPAM CONSOLE IN A TOURNAMENT

           if (g.getTurnCount() <= 3)

           {

                displayFunMessages();

           }

          

           //BELOW IS THE CODE FOR RANDOMAI01:

           int column = (int) (Math.random() * 7);

           return column;

          

     }

 

     @Override

     public String getName()

     {

           return “RecordTrackerAI01”;

     }

 

     @Override

     public String getWinQuote(double winPercentage)

     {

           return “My record was + getRecord() + “.  Knowledge is power.  Like a little power.”;

     }

    

     @Override

     public String getAuthor()

     {

           return “Patrick Campeau”;

     }

    

     @Override

     public void justWon()

     {

           wins++;

     }

    

     @Override

     public void justLost()

     {

           losses++;

     }

    

     @Override

     public void justTied()

     {

           ties++;

     }

    

     public String getRecord()

     {

           return wins + “W-" + losses + "L-" + ties + "T";

     }

    

     public int totalGames()

     {

           return wins + losses + ties;

     }

    

     public void displayFunMessages()

     {

          if (totalGames() % 100 == 0)  //every 100th game

           {

                System.out.println("That's " + wins + " big wins for the good guy! (" + getRecord() + ")"  );

           }         

     }

    

}