Lab 7: Coin Swap
This week we will be practicing Arrays, Functions, and Conditional Branching.
We have given you a part of the code, want your help completing the rest of the game. Take a look at the rules of the coin swap puzzle and the stub of code below. You will help us implement a function for the game.
Consider the coin swap puzzle. In the puzzle four coins are laid out on a board containing 5 spaces as shown below. We are pretending that 'X' represents one side of the coin (e.g. Heads), and 'O' represents the other side (e.g. Tails).
1 2 3 4 5
X X O O
The objective is to move the coins (X and O) from one side of the board to the other – hence Coin Swap.
The rules of the game are simple:
X can only move to the right into an empty square, or X can jump to the right over an O into an empty square.
Conversely O can only move to the left into an empty square, or O can jump to the left over an X into an empty square
For clarification a run of the game is shown below:
Author: Dale Reed
Program: #6, Coin Swap
TA: Claude Shannon, Th 4-5
Nov 17, 2016
Welcome to the coin swap puzzle.
Make moves to solve the puzzle where the objective is to swap the
place of the pieces on either side of the board. X can only move
to the right into an empty square, or can jump to the right over
an O into an empty square. Conversely O can only move to the left
into an empty square, or can jump to the left over an X into an
empty square.
For each move enter the source (1..5) and destination (1..5).
Enter 0 to exit the program.
1 2 3 4 5
X X O O
1. Enter source and destination: 2 3
1 2 3 4 5
X X O O
2. Enter source and destination: 3 4
*** Destination square is not empty. Invalid move, please retry.
3. Enter source and destination: 4 2
1 2 3 4 5
X O X O
4. Enter source and destination: 3 4
1 2 3 4 5
X O X O
5. Enter source and destination: 5 3
1 2 3 4 5
X O O X
6. Enter source and destination: 4 5
1 2 3 4 5
X O O X
7. Enter source and destination: 3 4
*** You cannot move that piece that direction. Invalid move, please retry.
8. Enter source and destination: 1 3
*** Destination square is not empty. Invalid move, please retry.
And here is the code to get you started:
/* coinswap.cpp
Make moves to solve the puzzle where the objective is to swap the place of
the pieces on either side of the board. X can only move to the right into
an empty square, or can jump to the right over an O into an empty square.
Conversely O can only move to the left into an empty square, or can jump
to the left over an X into an empty square.
*/
#include <iostream>
using namespace std;
// Global constants
const int BoardSize=5;
//--------------------------------------------------------------------------------
// Display name and program information
void displayIdentifyingInformation()
{
cout << endl
<< "Author: Dale Reed \n"
<< "Program: #6, Coin Swap \n"
<< "TA: Claude Shannon, Th 4-5 \n"
<< "Nov 17, 2016 \n"
<< "\n"
}//end displayIdentifyingInformation()
//--------------------------------------------------------------------------------
// Display instructions
void displayInstructions()
{
cout << "Welcome to the coin swap puzzle. \n"
<< "Make moves to solve the puzzle where the objective is to swap the \n"
<< "place of the pieces on either side of the board. X can only move \n"
<< "to the right into an empty square, or can jump to the right over \n"
<< "an O into an empty square. Conversely O can only move to the left \n"
<< "into an empty square, or can jump to the left over an X into an \n"
<< "empty square. \n"
<< " \n"
<< "For each move enter the source (1..5) and destination (1..5). \n"
<< "Enter 0 to exit the program. \n";
}//end displayInstructions()
//--------------------------------------------------------------------------------
// See if board pieces have finished being swapped. This is the case when
// the left-most two pieces are both 'O' and the right-most two pieces are 'X'
bool notDone( char board[])
{
return board[0]!='O' || board[1]!='O' || board[3]!='X' || board[4]!='X';
}
//--------------------------------------------------------------------------------
void displayBoard( char board[])
{
cout << endl;
cout << " 1 2 3 4 5\n";
cout << " ";
for( int i=0; i<BoardSize; i++) {
cout << board[ i] << " ";
}
cout << endl;
}
//--------------------------------------------------------------------------------
void promptForAndGetMove( char board[], int &moveNumber, int &source, int &destination)
{
char userInput;
// Infinite loop to handle possible multiple undo of moves
while( true) {
cout << moveNumber << ". Enter source and destination: ";
cin >> userInput;
// See if user input of 0 was given to exit the program
if( userInput == 'x') {
cout << "\n";
cout << "Exiting program...\n";
exit( 0);
}
// Input is likely numeric and is not 'x' or 'u'. Convert to a number.
source = userInput - '0';
// Also get destination portion of user input
cin >> destination;
// Adjust user entry to compensate for 0-based indexing, rather than 1-based
source--;
destination--;
// break out of enclosing loop, as we don't need to undo a move
break;
}
}//end promptForAndGetMove()
//--------------------------------------------------------------------------------
// Implement moveNotValid(...) function here.
// Return true (move is not valid) if any of the following conditions are true:
// 1. X is attempting to move to the left
// 2. O is attempting to move to the right
// 3. The destination location is not empty
// else
// return false (move IS valid)
//
// ...
//--------------------------------------------------------------------------------
int main()
{
char board[ BoardSize + 1] = "XX OO"; // extra character for the NULL
int source, destination;
int moveNumber = 1;
// Display identifying information, the instructions, and the initial board
displayIdentifyingInformation();
displayInstructions();
displayBoard( board);
// Game play loop
while( notDone( board) ) {
promptForAndGetMove( board, moveNumber, source, destination);
if( moveNotValid( board, source, destination)) {
cout << "Invalid move, please retry. \n";
continue;
e}
// Make move. Note that no move validation is being done.
board[ destination] = board[ source];
board[ source] = ' ';
moveNumber++;
displayBoard( board);
}
cout << "Congratulations, you did it! \n"
<< "\n"
<< "Exiting program ...\n";
return 0; // return value to keep C++ happy
}//end main()
You will implement the function moveNotValid to validate the moves for the swap. The function should address the three conditions stated in the comments above:
Return true (move is not valid) if any of the following conditions are true:
1. X is attempting to move to the left
2. O is attempting to move to the right
3. The destination location is not empty
else
return false (move IS valid)
The first two parts of the lab will grade you on the ability to write the function which is able to handle these cases returning true or false.
For the Extra Credit:
Your messages will be a bit more customized:
Instead of displaying a general message like
Invalid move, please retry.
If/when a move attempts to move a piece in an invalid direction
Display message:
*** You cannot move that piece that direction. Invalid move, please retry.
The user attempts to move into a non-blank destination
Display message:
*** Destination square is not empty. Invalid move, please retry.