Project 5: ConnectFour

In this project you will develop a ConnectFour game using object-oriented concepts. You may work as a team of 2-3 or alone.

Begin by downloading project5.zip and unzip it. You should see the following files:

You should only have to modify connectfour.py and not csplot.py or game.py.

Step 1: Understand ConnectFour

Open up connectfour.py and look at the ConnectFour class. Familiarize yourself with its attributes (fields) and how the methods are implemented. Pay special attention to how you might implement making a move and determining who the winner is.

Run game.py & submit a screenshot for check point 1. You may have to hit Control-C to kill the game if closing the window does not work.

Step 2: makeMove

Implement the makeMove method. Adhere to the method’s comment description when implementing the method.

Suggested Steps:

  1. Change color of one square by setting it equal to 1 or 2 and returning a valid row:

    self.board[0][0] = 1

    return 0

  2. Change the color of the column col.
  3. Loop over the column to find the first free slot and return this row. Don’t forget to use whichplayer to set the color instead of 1!

<img src=”makeMove.JPG” width=400>

Then, test makeMove by running game.py. Take a screenshot & submit a screenshot for check point 2 (make sure to show your code & a board with colored tiles).

Step 3: _isWon

Implement _isWon to determine if a player has won. You may want to try implementing it for the tic tac toe game we looked at in class (tictactoe2.py), and then see how your solution could be adapted to go from looking at 3 spots to 4.

Testing _isWon is difficult with the randomness of the computer player. I recommend commenting out the following line in the play method:

col = self._computerMakeMove()

and uncommenting this one:

col = self._userMakeMove()

This way you can control where all red and black peices go.

Start with vertical, and then do horizontal, followed by diagonal – each one gets a little bit more challenging to get right. Diagonal especially is a bit tricky. You might consider drawing a diagram and writing down all the possible positions you need to check:

<img src=”isWon.JPG” width=400>

Run game.py to test your game. When you’ve tested that both players can win in many configurations, you’re ready to submit. Don’t forget to turn the computer player back on!

Extra Credit: Smarter computer player

For extra credit, change _computerMakeMove to be more intelligent & actively prevent the human player from winning.

Extra Credit: Checkers

Implement support in ConnectFour to play a checkers game. Write your own checkers.py main program to run that instead of ConnectFour.

Submission

Make sure you’ve added your name to the comments at the top of the file, that your program contains comments and follows good programming style.

Once you’re satisfied that your game is working correctly, zip it for submission: