main that enables us to play Spin Out interactively or check
a solution.
First, create a class called SpinOut in the cs427_527 namespace. The
header file must be named "spinout.h" and the implementation file
must be named "spinout.cpp"; those must be the only two files
necessary to use the SpinOut class and
those files must compile cleanly with the -Wall -pedantic -std=c++11 compiler options.
The SpinOutclass must have the following members that behave
as follows.
SpinOut() that initializes the puzzle so all disks are vertical.
SpinOut(const std::string& s) that initializes the puzzle according to
the given string. The string will consist of 7 characters, each of which is '/' or '-'
respectively to indicate a disk that is intially vertical or horizontal respectively.
bool isLegalMove(int i) const which takes an integer between
0 and 6 inclusive and determines whether it is possible to rotate that disk in the current
object to its other
orientation. Disks are numbered from left to right starting with zero.
void makeMove(int i) which rotates the indicated disk in the
current object into
its other orientation, provided that such a move is legal.
int totalMoves() const which counts the number of legal moves
performed on the current object since it was created.
bool isSolved() const which determines if the current object
is in its solved configuration (all disks rotated horizontally).
std::string toString() const which returns a string representation
of the current object in the same format as that passed to the second version of the
constructor.
static const int SIZE that is initialized to 7.
new/delete
or malloc/free). 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.
Second, create a program with an executable named SpinOut that allows users to
play interactively or check a solution depending
on the command-line arguments.
To play interactvely, the first command-line
argument will be -i and the second,
if present, will be a 7-character string of
- and / giving the starting configuration in the form required
by the Spinout class constructor;
if the second argument is not present then the
game should start with all disks vertical.
In interactive mode, the program should
read from standard input integers representing the disk to rotate. There will be one
integer per line of input, possibly with leading and trailing whitespace. 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. The program should continue to read input
until the puzzle is solved or there are no more integers available as 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 line that displays the
solved configuration 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 first command-line argument may be a 7-character string
giving the starting configuration in the format required by the constructor. If
the first argument is not a starting configuration then the default configuration
/////// should be used. The following zero or more
arguments will be the indices of disks in the range 0 to 6 inclusive
indicating the sequence of disks to rotate, in the order they should
be rotated. The program should print a single
line to standard output, where the line is of the form
SpinOut: illegal move n in position m for config if one of the moves in the sequence is
illegal where m is position within the sequence of moves of the first illegal
move (the first move is position 1), n is the index of the disk that could
not be moved, and config is the configuration of the puzzle before
the attempted illegal move in the format returned by toString;
SOLVED if the sequence of moves ends with the puzzle solved; or,
otherwise, the final state of the puzzle in the format returned by toString.
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 any input, whether in interactive mode or on the command-line, is not as specified.
We reserve the right to deduct points from submissions for violations of the following guidelines.
#included regardless
of what other files have been #included -- if a declaration is required
by your header files, then #include the appropriate header file
in your header file (or write a forward declaration in your header) rather than relying
on your #includers to have already #included those
header files before they #include yours.
cin
and cout with the I/O operators << and >>
rather than printf and scanf).
const should be declared
as const.
-Wall
and -pedantic options.
SpinOut program in various
ways.
[jrg94@frog SpinOut]$ ./SpinOut -i /////// 6 //////- 5 illegal move 4 ////-/- [jrg94@frog SpinOut]$ ./SpinOut -i ------/ ------/ 6 ------- 1 moves [jrg94@frog SpinOut]$ ./SpinOut ------/ 6 SOLVED [jrg94@frog SpinOut]$ ./SpinOut 6 4 5 SpinOut: illegal move 5 in position 3 for ////-/- [jrg94@frog SpinOut]$ ./SpinOut 6 4 6 5 6 ////---