CONNECT FOUR
TOPIC 03v2 – CONNECT FOUR AI

 

FAQ

 

 

Q1 - What happens if your AI chooses a column that is already full?

As of version 2.0, you lose the game. 

Before this version, choosing a full column simply led to your AI losing its turn.  But we encountered infinite loops when two AIs facing off both continuously chose a full column.  This actually halted a tournament one year midway through the period.  Ugh…


 

Q2 - Can we submit multiple files for our AI program?

 

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.




Q3 – How do I check what is in the slot at row 0 and column 0?

Your AIAgent gets a copy of the game object when playMove is called.  There are a few ways of finding out what is at location r=0, c=0:

 

Option 1

Use
g.getSlotValue(0, 0) to get the value of the slot.  This will return either RED, BLU or the String “ o “ which means empty.

Option 2

Use g.getSlotOwner(0, 0) to get the owner of the slot.  If the slot is RED, the owner is player 1.  If BLU, the owner is 2.  If the slot is empty, the owner is 0.

Option 3

Use the following statements:
     Slot[][] gboard = g.getBoard();

     String value = gboard[0][0].value;

     int owner = gboard[0][0].owner();

 

 

Q4 – How do I know what player number I am currently playing as?

 

Your player number (1 or 2) is passed to you as a parameter to the playMove method.

 

Q5 – Is there an easy way to know my opponent’s player number?

It is the opposite of your AI’s player number.  So, if your AI is player 1, then the opponent is player 2 (and vice versa).

Code like this would help you:

int oppPlayerNumber;
if (playerNumber == 1)
     oppPlayerNumber = 2;
else
     oppPlayerNumber = 1;

 

//Now, you can use oppPlayerNumber wherever you need it.

 

 

Q6 – How do I run code once before my AI starts a game?

If you want to run code once when the AIAgent is created, then you could place that code in the AIAgent’s constructor.  However, for a series or tournament, code in a constructor would only be ran once as opposed to every time before a game.

 

To run a segment of code once at the start of each game, then you could simply check the turnCount.  The variable turnCount variable keeps track of how many moves have already occurred in the game. 

 

Because of the two random moves, player 1’s first move occurs when two moves have already be done, or when turnCount is 2.  Similarly, when player 2’s first move occurs, three moves have already been done, so turnCount is 3.

 

The following code would work:

if (g.getTurnCount() <= 3)   //if this is my AI’s first turn

{

   //code to run once

}




Q7 – How can I have my AI run specific code on its first turn, other specific code on its second turn, and so on…

Consider the following table:

 

Move

Value of turnCount

Description

1

0

Random move

2

1

Random move

3

2

Player 1’s first move

4

3

Player 2’s first move

5

4

Player 1’s second move

6

5

Player 2’s second move

7

6

Player 1’s third move

8

7

Player 2’s third move

and so on…

 

 

 

So you could do something like this:

 

 

if (g.getTurnCount() == 2 || g.getTurnCount() == 3)        //1st turn

{

   //code to run on first turn

}

else if (g.getTurnCount() == 4 || g.getTurnCount() == 5)   //2nd turn

{

   //code to run on second turn

}

else if (g.getTurnCount() == 5 || g.getTurnCount() == 6)   //3rd turn

{

   //code to run on third turn

}

//and so on…