C Assignment 1
C Assignment 1
Introduction
Your task is to write an C99 program (called nogo) which allows the user to play a game of atari-go(described
later). This will require I/O from both the user and from files. Your assignment submission must comply with
the C style guide (v2.0.2) available on the course website.
The Game
nogo will display a grid of cells like this:
/----\
|....|
|....|
|....|
|....|
\----/
The dots indicate empty cells (the other marks are just borders). The two s in the game will take turns putting
stones into empty cells. The first will indicate their stones with O and the second will use X. The cells are
numbered from the top left corner as 0,0 (row, then column).
Connected sets of stones of the same type form strings. To be connected, stones need to be horizontally
or vertically adjacent. In this example:
/----\
|OOO.|
|...O|
|.X..|
|X.X.|
\----/
The first player has 2 strings while the second player has 3.
Strings may have liberties (spaces which they could grow into). A liberty is an empty cell horizontally or
vertically adjacent to a string. In this example:
/----\
|X.OX|
|..OO|
|..X.|
|....|
\----/
The X at 0,3 has no liberties; the X at 2,2 has 3 liberties and the string of Os has 3.
A string is considered captured when it has no liberties. So in the example above, the string at 0,3 would
be captured. The first player to have one of their strings captured loses. In cases where both s would have a
string captured for example:
/-----\
|OX OX|
|OOXXX|
|O....|
|.....|
\-----/
the player placing the stone checks their opponents strings first. So, if O placed at 0,2 then X would lose.
Interaction
At the beginning of the game and after each players turn, the grid will be displayed. There are two types of in
the game, h (where moves will be read from stdin) and c (where moves will be generated by your program).
For c-s, the move the program generates will be printed like this (substitute X for O as needed):
Player O: 1 0
then go to the next players turn. For h-s, the user will be prompted:
Player X>
and wait for the user to enter row column (single space separated with no leading nor trailing spaces). If the
users input is not valid (eg not two numbers, not within the bounds of the grid, not empty), then the prompt
is reprinted for them to try again.
When a winner is determined, the following is displayed:
Player ? wins
(substitute O/X for ? as appropriate).
Invocation
When run with an incorrect number of arguments, nogo should print usage instructions to stderr:
Usage: nogo p1type p2type [height width | filename]
and exit (see the error table).
p1type and p2type must be either c or h. height and width (measured in cells) must be integers between
4 and 1, 000 inclusive. If height and width are omitted, then a filename can be given instead, in that case, the
program should try to load a saved game from the named file.
Computer moves
The computer s will generate their moves using the following algorithm (note that the should continue generating
moves until an empty space is found):
Ir is the initial row (1 for O and 2 for X).
Ic is the initial column (4 for O and 10 for X).
F is a multiplication factor (29 for O and 17 for X).
Gw is the width of the grid.
Gh is the height of the grid.
M is a counter of moves generated (starting at zero).
Initial setup:
r = Ir , c = Ic
B = Ir Gw + Ic
To get a move, return (r mod Gh ) and (c mod Gw ).
To generate the next move.
if M is a multiple of 5:
N = (B + M/5 F ) mod 1000003
r = N/Gw (integer division)
3
c = N mod Gw
if M mod 5 is:
1. r+ = 1, c+ = 1
2. r+ = 2, c+ = 1
3. r+ = 1, c+ = 0
4. r+ = 0, c+ = 1
So for a 7 7 grid the move sequence generated for Player O would start with:
(1, 4), (2, 5), (4, 6), (5, 6), (5, 0)
(5, 5), (6, 6), (1, 0), (2, 0), (2, 1)
(2, 6), (3, 0), (5, 1), . . .
Saving games
To save a game, instead of entering a row and column, enter w followed immediately by the path of the file to
save to. That is, no space between w and the start of the path.
Exit Status
1
2
3
4
5
Message
Usage: nogo p1type p2type [height
width | filename]
Invalid type
Invalid board dimension
Unable to open file
Incorrect file contents
For a normal exit, the status will be 0 and no special message is printed.
There are a number of conditions which should cause messages to be displayed but which should not immediately terminate the program. These messages should also go to standard error.
Condition
Error opening file for saving grid
Action
Prompt again
Message
Unable to save game
Compilation
Your code must compile with command:
make
When you compile, you must use at least the following flags: -Wall -pedantic -std=gnu99.
You must not use flags or pragmas to try to disable or hide warnings.
Submission
Submission must be made electronically by committing using subversion.
Player X: 3 3
/----\
|....|
|O...|
|.OX.|
|...X|
\----/
Player O: 0 2
/----\
|..O.|
|O...|
|.OX.|
|...X|
\----/
Player X: 2 0
/----\
|..O.|
|O...|
|XOX.|
|...X|
\----/
Player
/----\
|..O.|
|O.O.|
|XOX.|
|...X|
\----/
Player
/----\
|..OX|
|O.O.|
|XOX.|
|...X|
\----/
Player
/----\
|..OX|
|O.OO|
|XOX.|
|...X|
\----/
Player
O: 1 2
X: 0 3
O: 1 3
O wins