Professor Simonson       How Computers Work

CSC 195

Programming Project - Connect Four


This project is due on the day of the final exam.  It is worth 100 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, even if it is tie.

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 (graphical user interface) program there is usually an underlying data structure that keeps track of the information that is displayed.  The picture you see on the screen reflects this data structure, but  it is important to realize that the picture the computer draws on the screen is not the same as this data structure.  This idea of keeping an abstract data model of the board is called separating the "model" from the "view". 

One possible data structure for this project is a collection of 13 lists:  7 lists for the columns (each size 6), and 6 lists for the rows  (each size 7).  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. 

Besides helping display a current picture of the board, these lists can be accessed in order to handle many other useful tasks, including:

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 12 different diagonals on the board.  Each of these can be extracted from the columns and checked for four consecutive colors. Note:  it is possible for there to be no legal moves left, and then a tie has occurred.  Make sure to check for this possibility. 

Of course, you could choose to use a simpler data structure (for example, just the column lists).  That would make checking for a win more complicated, but would make updating the lists simpler.  You could also use a more complex data structure (for example, a structure allowing you to extract the color in row i column j).  This would make checking for a win simpler but would make updating the lists more complex.  There is no "right" choice.  It's all up to you.

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 move in 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 mouse_over.  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.

Note:  Although your basic project is a group grade, individuals may do extra credit on their own.  If that is the case, please let me know clearly which person is responsible for which enhancements.