CONNECT
FOUR 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.
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.
STEP
3 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.
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(). |
|||
|