Connect Four is an old children's game consisting of a vertical plastic grid into which the players alternately drop colored checkers. A player wins when he or she manages to line up four checkers either vertically, horizontally, or diagonally. Here is a fairly sophisticated online version of the game that you can play against to get a sense of the game.
You are to finish the implementation of a computer version of the game by first writing the game rules and testing them with several "dumb" players, then writing a "smart" player that is capable of regularly beating the dumb players. Your player does not need to be as strong as the one in the example (although you can try it for extra credit). You also do not need to worry about updating the graphics to make the game look better.
Currently, the game consists of two players that each move interactively. However,
since the board does not correctly verify a legal move or when the game is over, nothing
really happens. Thus, your first task is to complete the following
methods
to make a playable game.
public boolean isLegalMove (int column)
true
if it is possible to drop a piece in the given column.
This is only possible if the column is not full (i.e., it has at least one
empty space).
public boolean boardIsFull ()
true
if there are no more possible moves on the board. This is only possible
if all the columns on the board are full.
public void makeMove (Color color, int column)
Note, your methods should be written so that they work for any size board, any number of players, and any number of pieces in a row required. Although we may not test it, it should be possible to play the game with five players, needing five in a row, on a 100x100 sized board.
Next, create your own computer player by creating a class, StudentPlayer
.
You should write the makeMove
method such that it
returns the column you think is the best move. At the very least, your code
should win if possible (i.e., if you currently have three pieces in a row and
the fourth space is free, you should take it), not lose when possible (i.e.,
if your opponent has three pieces in a row and the fourth space is free, you
should block it) and otherwise move randomly. At the very least, this makes
your piece better than any of the given players and difficult for a novice
player to defeat.
Any intelligence you add beyond that will be extra credit. Intelligence in this case requires you to learn more about the game and how to build and block threats (i.e., three in a rows). There are several algorithms for doing this, including one that is perfect (i.e., cannot lose if it is the first player). Essentially, the basic idea is to determine how many possible threats each space on the board contributes to and rank your move accordingly. For example, it should be clear that moving in the middle is a better initial strategy than moving in a corner since more threats are possible from the middle (i.e., at least both left and right horizontal directions).
The following resources may help you to come up with some more intelligent strategies for playing the game:
When you are finished, you should submit your project for grading and update your course web portfolio.