CS 313 - Homework 4 - Connect 4 player
Due: Wednesday 10/13 at 10amThis 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 formatThe 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:
Then the input to your program would look like this:| | | O | | X | | O O X | | X X O X X | | O X O O X | +---------------+ 1 2 3 4 5 6 7
The 8th line in the input indicates that it is player 2's turn ('O').OXOXO XX OO OXO XXX 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.
ScoringIn 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 frameworkTo get started, download and unzip hw4.zip. You will get a hw4/ foder with these contents. See the file READ_ME.txt 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.
- If you use my C code, note that columns and rows are numbered from 0, while in the printed representation they are numbered from 1, which is also what my python scripts expect. So when writing your program you have to mentally subtract 1 everywhere. This also means you'll have to add 1 to the column number before outputting the move you want to make.
- It's easy to have bugs accessing invalid array indices, which might not result in any crashes, just unpredictable behavior. To guard against this, define functions setBoard(r, c, val) and val = getBoard(r, c) and use these functions rather than accessing the array directly. In those functions, check that r and c are within bounds and print an error otherwise.
- If you use randomization, it can be useful to turn off the "seeding" of the random number generator, to get the same sequence of random numbers each time you run your program. To do that in the C version, add srand(0); AFTER the call to read_board();.
Online submissionUpload your file connect4.p or connect4.c via the HW 4 submission page by 10am 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). If you worked in a team, only one of the team members should submit; your code should have both names at the top, and you should also list your partner's name on the submit form.