Professor Simonson       How Computers Work

CS 195

Programming Project - Connect Four

Board

This project is due on the day of the final exam.  It is worth 125 points.  You will be graded on the graphics and quality of your program’s performance including its resistance to crashing in the middle of play.  You will also be graded on the style of your programming including use of procedures, parameters, recursion and appropriate data structures.  Finally, you will be graded on your documentation including how easy it is for me to understand your code.

You will write a program to allow a person to play the game Connect Four.  You should allow them to choose before each game whether they want to play the computer or another person, and then whether they wish to go first or not.  After the game, they should be allowed the opportunity to play again.  After they no longer wish to play, a tally of how many games the player wins out of the total number of games played, should be printed.  For presentation, you should ask the person their name and refer to them by name in all subsequent messages.  You should also explain the rules of the game with appropriate diagrams and detail, before play begins.  During play, only legal moves should be allowed, and the end of the game should be detected.

You can check out an online version of this game to get some idea for a graphical user interface (GUI).

The game has seven columns in which a black or a red checker can be placed.  A checker placed in a given column falls to the bottom of that column and lands on top of the previously played checker in that column.  Your program should not refresh the whole picture of the board after each move, but instead should add the new checkers as each is played.  A player should indicate the column he is playing, by clicking on or in that column.  When four in a row of a given color is achieved either vertically, or horizontally or diagonally, the game is over.   Note that the maximum number of checkers allowed in any column is six.

Hints on Structuring Your Program

In any GUI program there is usually an underlying data structure that keeps track of the information that is displayed.  It is important to realize that just the picture that the  computer draws on the screen is not the same as this data structure.  For example, for this program a data structure of 13 lists is helpful:  one for each column (6 all together), and one for each row  (7 all together).  Each list contains a sequence of letters:  B(black), R(red) and E(empty).  At the start, all thirteen lists contain nothing but E’s.   As the game progresses, these lists must be changed and updated to reflect the current state of the board, and you will need to write procedure to do these updates.  These lists are accessed in order to:

Let's examine the procedure that checks if someone won.  This procedure must check to see whether there are four consecutive pieces of a particular color in any row, column or diagonal.  To check the rows and columns, the 13 lists can be consulted one at a time.  You should write a procedure that takes a list and a color, and reports whether there are four consecutive occurrences of that color in the list.  I will help you write this if you can't figure it out.  There are exactly 24 different diagonals of length four on the board.  Each of these can be checked by extracting the four appropriate colors from the lists. Note:  it is possible for there to be no legal moves left, and then a ties has occurred.  Make sure to check for this possibility.

I will help you with any details as the questions come up.
  A preliminary outline of your program similar to the Nim outline I gave you for assignment 5 should be designed before you even touch the computer.  This will ensure that your program is organized and that you will not have to redesign it in the middle.   You should show this to me in class as soon as  it is complete, and I will help you make any necessary modifications.

Extra Credit

The minimum requirements for this assignment, is to have the computer play random legal moves.  You can earn up to 40 points extra credit by adding extra features.  One convenient feature is an "undo"  option that lets a person take back a mov ein case of a typo.  Another much fancier feature is programming the computer to make smart moves.  Strategies for doing this can be discussed with me.  Other possible extra features include various graphics features like highlighting on a mouseover.  Feel free to add your own features for extra credit, but first get the main game working and then discuss your ideas with me first before implementing them.