Artificial Intelligence Homework 3
Due date: 2/22 by 11:59pm


In this assignment, you will use the Minimax algorithm to find the optimal strategy for the game of Connect Four. This is a programming assignment that should be done individually in Java. This assignment is more substantial than the previous one so please do not wait until the last few days to start.

Starting Files: Click here to download a directory with the following Java files:

  • ConnectFour class - This is the main Java class that runs the game. It is this class that you should compile and run. It takes one command line argument: the number of ply the computer player should look ahead (default is 1-ply).

  • ComputerConnectFourPlayer class - This is the class you will implement. It contains one method called getNextPlay() that takes in the board game and returns an integer representing the column where the computer should play. Initially, this method returns the leftmost open column (resulting in a very dumb computer player).

  • HumanConnectFourPlayer class - This gets a move from the human.

  • The rest of the files deal with the GUI/graphics.
Although all of the code you will write is inside of ComputerConnectFourPlayer, it might be helpful to skim through the ConnectFour class so you have a sense of how the game is setup and to cannabilize some of the more useful logic.

The getNextPlay() method inside of the ComputerConnectFourPlayer class should return an action for the computer to take. This action should be determined using the Minimax algorithm. Since it is not computationally feasible to run the unmodified Minimax algorithm, you should instead implement the heuristic Minimax algorithm (Section 5.4). This replaces the goal test with a cutoff test and replaces the utility function with an evaluation function.

Cutoff test: The constructor of the ComputerConnectFourPlayer will be passed a depth. Your cutoff test should return true for any node at this depth. For example, suppose we're at the very beginning of the game and the depth is 1. Then the game tree would look like:

If the depth was 2, the game tree would include the next level which are all the possible moves that MIN could make in response. Your cutoff method should also return true for actual terminal states that occur before the cutoff depth.

Evaluation Function: The evaluation function should take in the board and look at all horizontal, vertical, and diagonal spans of 4 contiguous slots. (A standard game board has 24 horizontal spans, 21 vertical ones, and 24 diagonal ones). For each span of 4 contiguous spots,
  • If it has 4 tokens of the AI's color and none of the opponent's, it is worth an infinite amount of points
  • If it has 3 tokens of the AI's color and none of the opponent's, it is worth 100 points.
  • If it has 2 tokens of the AI's color and none of the opponent's, it is worth 10 points.
  • If it has 1 token of the AI's color and none of the opponent's, it is worth 1 points.
  • If it has no tokens, or it has a mix of the AI's and the opponent's tokens, it is worth 0 points.
  • If it has some of the opponent's tokens but none of the AI's, then it is worth the same point total just negated. For example, 3 of the opponent's tokens but none of the AI's should be worth -100 points.
The evaluation function should iterate over all such spans and, for each span, accumulate the values given above. The total sum is then returned.

Additional Information:

  • Wikipedia has a description of Connect Four if you've never played or it's been awhile.

  • The game board is represented as a 2-dimensional array of bytes. A byte is an integer type that has a very limited range (from about -127 to 127). The number 0 is used to denote an empty slot in the board. The numbers 1 and -1 are used to represent the tokens for the two players.

  • This code can get messy fast so I recommend testing each method as you write it. In particular, if you write any smaller helper methods, test them inside a main method. That way you'll only have to worry about the correctness of the actual minimax methods themselves and you'll have confidence that all of your helper methods are correct.

  • You do not need to implement Alpha-Beta pruning

  • Watch out for overflow or underflow if you use Integer.MAX_VALUE and Integer.MIN_VALUE


Running Your Code
I recommend adding a main() method to your ComputerConnectFourPlayer to test your implementation. To play the actual game, compile and run the ConnectFour class. Again, this takes a single command line argument which is the number of ply to look ahead (default of 1).

Grading and Submission
Your assignment will be graded based upon correctness. Most likely, I will either play against your AI player using a pre-determined set of moves or I'll compare the actions taken by your AI player against the actions taken by mine. Your code will also be based on efficiency (i.e. the time/space complexity of your implementation) as well as its adherence to the Java style guide.

To submit, rename your directory hw3_FirstName_LastName. Then zip your directory and upload it to Moodle. Please remember to rename your directory before you zip it.


Last modified: Tue Jul 15 13:34:17 PDT 2014