CS 313 - Homework 4 - Connect 4

Due: Wednesday 3/11 in class

This homework consists of a single programming problem, either in C, Pascal, or Python - your choice. This week, you are allowed to work in groups of two, or by yourself. Slightly more work is expected from a group as described below. Hand in one printout of your code per group. You don't need to hand in sample output, but you should again submit your program electronically by typing "submit313" in your hw4 directory (always make a new directory for the current homework). I only need one electronic submission per group. As usual, on the first page of your printout, write your name(s), the names of the students with whom you collaborated, and how much time it took you.
  1. Implement the game "Connect 4" in C, Pascal, or Python. Don't just select the language you have the most experience with (presumably C) but spend some time thinking about which language seems best suited for the task. (Or just pick a new language to get better at it :)

    Your program should let two humans play against each other. A sample executable is in ~schar/cs313/hw4/cfour. The following instructions were taken from the SCS Connect Four website (minus 4 typos :)

    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). At the same time, the opponent is trying to connect four of their own chips. You must balance defense (blocking off the opponent's attempts at connecting four) and offense (connecting four of your own) in order to play successfully.
    My sample program is about as simple as it can get. At the very least, you should try to match what it does (e.g., checking for invalid input, and printing who won (or whether it was a tie), and how many moves it took. There are many ways to make it more interesting: If you are working alone, you must implement at least one of the above extensions, or some other new feature of comparable difficulty. Groups of 2 must implement at least two of these extensions or something comparable. (Adding real graphics would count as at least 2 :) Please include in a comment at the top of your program a brief description of the features/extensions you implemented.

  2. Extra credit: Write a computer player! If enough people do this, we could have a tournament. (Make sure your player can move either first or second.) Online versions that let a human play against the computer can be found here and here.

  3. How much time did it take you to complete this assignment?

Here are the functions for clearing the screen in Pascal, C, and Python:
{ Pascal }
procedure clear; { clear screen and move cursor to top left corner }
begin
    write(chr(27), '[2J', chr(27), '[f');
end;
    
/* C */
void clear() { /* clear screen and move cursor to top left corner */
    printf("%c[2J%c[f", 27, 27);
}

# Python
import sys   # at the beginning of your python file

def clear():
    """clear screen and move cursor to top left corner"""
    sys.stdout.write("%c[2J%c[f" % (27, 27))