CS 313 - Homework 4 - Connect 4 player

Due: Wednesday 3/15 at 11:15am

This homework consists of a single programming problem, either in C or Pascal - your choice. This week, you are encouraged to work in groups of two, but if you prefer you can also work alone. If you need to find a partner, send me an email! Submit your program via the link at the bottom of this page. Only one person per group should submit. Be sure your names appear in a comment at the top of your program.

Your task for this assignment is to implement a player for the game Connect 4 in C or Pascal. Briefly, in a game of Connect 4 players take turns dropping their colored chips into a 7x6 grid, which pile up at the bottom. To win, a player must connect four of their chips in a row (horizontally, vertically, or diagonally).

In coding your player you can implement any strategy you want -- from random moves to a full-fledged time-limited minimax search. I'm not expecting any background in game playing algorithms -- the basic idea is to have fun and get creative writing a non-trivial program in a procedural language. Your grade will be based your effort and creativity, your coding style, and on the performance of your player (yes, we will have a tournament!).

Your program should compute a single move in the game. It should read the current board configuration as input (details below) and produce a move to make as output within 1 second. I am providing a Python script "match.py" to run your programs, which prints the board configuration, lets players take turns, and detects wins, ties, illegal moves, and time limit violations. This script allows (1) to let a human play against your player, and (2) pitch two computer players against each other. Once all programs have been submitted we will run a tournament to determine the best player and an overall ranking.

Input format

The current board configuration is provided via stdin as a string of 7 lines representing the column contents, followed by a line indicating whose turn it is. Player 1 is 'X' and player 2 is 'O'. For instance, given the following board configuration, where it is player 2's turn:
|               |
| O             |
| X             |
| O       O   X |
| X X O   X   X |
| O X O   O   X |
  1 2 3 4 5 6 7
Then the input to your program would look like this:


The 8th line in the input indicates that it is player 2's turn ('O').

The output of a good player program in this case should be '7' since otherwise player 1 could win in the next move by getting four X's in a row in column 7. Your program can print anything it wants (e.g. the current board configuration and debugging output), as long as it prints the move to make (an integer between 1 and 7) on a single line as the very last line of its output to stdout. It must produce all its output within 1 second.


In a match between two programs, if the game ends in a regular win (4 in a row), then the winner receives 4 points, and the loser 1 point. If it's a tie, both programs receive 2 points. If one program makes an illegal move (illegal output, or the column is full already), or exceeds the time limit, then the failing program receives 0 points, and the other program receives 1 point.

Code framework

Copy my directory ~schar/cs313/hw4 to get started. To run this on your own computer, download hw4.zip. See the READ_ME.txt file for more information. Run ./match.py to play games between computer players and/or humans, and ./tournament.py to run a tournament between 3 or more computer players. You may use the provided sample players a a starting point for your own implementation.

Online submission

Upload your file connect4.p or connect4.c via the HW 4 submission page by the beginning of class on Wed. It doesn't matter if your file has a different name, but be sure to use the upload file with the correct extension (one of .c or .p).