-


> > > > Programming Assignments > Assignment 2 - Spin Out

Objectives

Introduction

SpinOut is a version of the Chinese Ring Puzzle. SpinOut has 7 disks that can be rotated between a vertical or horizontal orientation. Only one disk can be rotated at once and a disk can only be rotated if the one immediately to its right is vertical and all the ones to the right of that are horizontal. The puzzle typically starts with all disks in the vertical orientation and the puzzle is solved when all disks are in the horizontal orientation.

Assignment

Create a class and a 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.

You must use a plain, constant-size array (so no 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.

Example

The following is a transcript of running the 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
////---

Valid HTML 4.01 Transitional