boxes will display a grid of cells (the corners indicated by +. The edges bordering the cells may be open (left blank) or closed (using | or - characters as appropriate). If all four edges of a cell are closed, then the cell may store a non-space character. For example:

+ + + +
|
+ +-+-+
|A|B|
+ +-+-+
|
+-+ + +

Beginning with an empty grid, players will take turns choosing an open edge to close. If the new edge means that a cell is now completely surrounded (completed), two things will happen: First, the players symbol is placed in the cell. Second, the player has another turn immediately

The game ends when there are no more open edges left in the grid or if EOF is detected when user input is required. The winners are the players with the most cells with their symbol in them.

The first player will use A as their symbol, second will use B and so on using successive ASCII values. Players will indicate their choice of edges using the coordinate of the upper/leftmost corner in the edge followed by h (horizontal edge) or v (vertical edge). For example, the edge in this grid:

+ + + +
|
+ + + +
+ + + +
+ + + +

would be described by: 0 2 v (an edge starting at row 0, column 2 and going downwards). Regarding extra turns:

1. A player will keep getting extra turns as long as their each extra turn completes a cell.

2. It is possible that a single edge will complete two cells, in this case the player only gets one extra turn for both cells.

3. A player does not get an extra turn if the game is finished.

Interaction

Before each players turn, the current grid will be displayed. Each time the grid is printed, it will be followed by a newline. The user will be prompted with their symbol followed by >, then a space. For example:

B>

If the input describes a valid open edge, then the grid will be updated and the next turn begins (or the game ends). If the input is not valid, then the prompt will be reprinted for the same player to enter a different move. The program should keep prompting until a valid move is entered (or EOF).

If a valid move was entered, but the game is over, the (updated) grid will be printed one more time. Note that leading whitespace is not permitted in valid moves.

Example game

Note: extra gaps have been introduced between turns for clarity. In your implementation, there will be no blank lines between entering a valid move and the updated grid.

+ + +
+ + +
+ + +
A> 0 0 h

+-+ +
+ + +
+ + +
B> 0 0 v

+-+ +
|
+ + +
+ + +
A> 0 2 v

+-+ +
| |
+ + +
+ + +
B> 1 2 h
B> 1 2 v

+-+ +
| |
+ + +
|
+ + +
A> 1 1 h

+-+ +
| |
+ +-+
|
+ + +
B> 0 1 h

+-+-+
| |
+ +-+
|
+ + +
A> 0 1 v

+-+-+
| |A|
+ +-+
|
+ + +
A> 1 0 h

+-+-+
|A|A|
+-+-+
|
+ + +
A> 2 0 h

+-+-+
|A|A|
+-+-+
|
+-+ +
B> 1 0 v

+-+-+
|A|A|
+-+-+
| |
+-+ +
A> 2 1 h

+-+-+
|A|A|
+-+-+
| |
+-+-+
B> 1 1 v

+-+-+
|A|A|
+-+-+
|B|B|
+-+-+
Winner(s): A, B

Invocation

When run with no arguments, boxes should print usage instructions to stderr:

Usage: boxes height width playercount [filename]

and exit (see the error table).

height and width (measured in cells) must be integers greater than 1 and less than 1, 000. playercount must be an integer greater than 1 and less than 101. If filename is present, it will be interpreted as a path to a readable file containing a grid state to attempt to load as the start of the game. If filename is not present, then the game should begin with Player As turn on an empty grid.

Input file format

The first line contains only a single integer between 1 and number of players inclusive. This indicates which player is next to play.

The next set of lines describe the state of the edges in the grid (0 for an open edge and 1 for a closed edge). The first line describes the first row of horizontal edges (left to right). The second line describes the first row of vertical edges (left to right). The third line describes the second row of horizontal edges and so on. Note that there will be more vertical edges in a row than horizontal edges.

The next set of lines describe which symbols are in the cells. Each row describes one row of cells (left to right). Each cell is described by a single integer indicating the player who completed the cell (0 indicates a cell which hasnt been completed yet). The integers in each row are separated by commas.

For example, a two player game on the board shown one Page 1 would look like:

1
000
0100
011
0111
011
1000
100
0,0,0
0,1,2
0,0,0

Faulty input files

Things to watch out for:

  • Files with less lines than the grid size requires.
  • Files with short lines (less content than expected).
  • Files with lines which are longer than expected.
  • Files with characters in the wrong places.
  • Numbers outside the number of players.

Saving Games

There is another type of valid move. A player can enter, w a space and then the path to the file to save the current game state in. The file should be output in exactly the same format described above for input files. After the save operation has been completed (or failed), your program should print the appropriate message (see table below) and then prompt for another move by the same player.

Errors and messages

When one of the conditions in the following table happens, the program should print the error message and exit with the specified status. All error messages in this program should be sent to standard error and followed by a newline. Error conditions should be tested in the order given in the table.

Condition Exit Status Message
Program started with incorrect number
of arguments
1 Usage: boxes height width playercount
[filename]
Invalid grid dimensions 2 Invalid grid dimensions
Invalid player count 3 Invalid player count
Cannot open grid file 4 Invalid grid file
Error reading grid. Eg: bad chars in
input, not enough lines, short lines
5 Error reading grid contents
End of file while waiting for user input 6 End of user input

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 Action Message
Error opening file for saving grid Prompt again Can not open file for write
Save of grid successful Prompt again Save complete
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.