-


> > > > Programming Assignments > Assignment 3 - Conway's Soldiers

Objectives

Introduction

Conway's Soldiers is a jumping puzzle played on a checkerboard in which the goal is to move any piece into the top row by a sequence of legal moves. The only type of legal move is to jump a piece horizontally or vertically over an adjacent piece into an empty space immediately beyond; the jumped piece is removed from the board.

Assignment

Create a class and a main that enables us to play Conway's Soldiers interactively or check a solution.

First, create a class called Conway in the cs427_527 namespace. The header file must be named "conway.h" and the implementation file must be named "conway.cpp"; those must be the only two files necessary to use the Conway class and those files must compile cleanly with the -Wall -pedantic -std=c++11 compiler options. The Conway class must have the following members that behave as follows.

In addition, there must be an operator ostream& operator<<(ostream&, const Conway&) that outputs the given board to the given stream in the format read by the version of the constructor that reads from an input stream (without the first line giving the size of the board).

You may add other members as necessary.

You must use dynamically allocated array for the board -- no STL containers or smart pointers. If any of the preconditions for functions and methods are violated then your code must behave gracefully -- it may not crash or go into an infinite loop (throwing exceptions is graceful only when the exceptions are caught in a catch block that then exits the program gracefully).

Second, create a program with an executable named Conway that allows users to play interactively or check a solution as determined by the command-line arguments. To play interactvely, the first command-line argument will be -i. The first command-line argument (or second, if -i is given) may be -f, in which case the next argument will be the name of an input file whose contents are in the format required by the appropriate constructor. If the -f argument is not present then the game should start with the four-piece puzzle created by the zero-argument constructor.

In interactive mode, the program should read from standard input integers representing the move to make. There will be four integers per line of input, separated by whitespace and possibly with leading and trailing whitespace. The four integers give the row and column of the position to move a piece from and the position to move it to. If the move is not legal then the message illegal move should be printed to standard output. The resulting state of the puzzle should be printed to standard output at the beginning of the program and after each legal move in the form output by the << operator. The program should continue to read input until the puzzle is solved or the end of input (the user gives up). If a move leaves the puzzle in the solved configuration then then the total number of legal moves made should be printed as the last line of standard output after the solved configuration displayed as the result of the move. The format should be n moves where n is replaced with the number of moves.

In non-interactive mode, the command-line arguments following the -f argument and filename (if present) will be zero or more groups of four integers giving a sequence of moves to make. The program should print a single message to standard output, where the message is

In both modes, there must not be anything written to standard output beyond what is specified above. And unless otherwise specified, your program should behave gracefully if any input, whether in interactive mode or on the command-line, is not as specified.

In both modes, a move is given as input by listing the row to move from, the column to move from, the row to move to, and the column to move to in that order. Each row and column index will be a sequence of decimal digits with no leading sign. Rows and columns are indexed starting with 0 for the top row and 0 for the left column.

We reserve the right to deduct points from submissions for violations of the following guidelines.

Example

The following is a transcript of running the Conway program in various ways.
[jrg94@peacock Conway]$ ./Conway -i
......
......
.XXX..
...X..
......
3 3 1 3
......
...X..
.XX...
......
......
1 3 0 3
illegal move
2 1 2 3
......
...X..
...X..
......
......
[jrg94@peacock Conway]$ ./Conway -i -f conway_2.in
...
.X.
.X.
2 1 0 1
.X.
...
...
1 moves
[jrg94@peacock Conway]$ ./Conway 3 3 1 3  2 1 2 3  2 3 0 3
SOLVED
[jrg94@peacock Conway]$ ./Conway 3 3 1 3  1 3 0 3  2 1 2 3  2 3 2 1
Conway: illegal move 1 3 0 3 in position 2 for
......
...X..
.XX...
......
......
[jrg94@peacock Conway]$ ./Conway -f conway_21.in 5 4 3 4  5 2 5 4
.........
.........
.........
....X....
..XX.XX..
....XXX..
..XXXXX..
..XXXXX..
....X....

Where conway_2.in contains

3 3
...
.X.
.X.
and conway_21.in contains
9 9
.........
.........
.........
.........
..XXXXX..
..XXXXX..
..XXXXX..
..XXXXX..
....X....

Valid HTML 4.01 Transitional