/* SwitchSecond.cpp - Coins swapping program. The playing board is 5 squares. Think of one coin each on the two left-most squares, with "heads" shown on those coins. The middle square is blank, and the two right-most squares each have a coin, with "tails" shown. The object is to swap the coins. For the sake of the program, we will represent "heads" as 'X', and "tails" as 'O'. The starting board configuration is then: XX OO and the final board configuration is: OO XX An X can only move to the right, and an O can only move to the left. On each move you can move any piece into an adjacent square, or you can jump over an opposite piece into a blank square. Thus an X can jump an O, but an X cannot jump another X (and similarly for O jumping X). */ #include using namespace std; // display identifying information void displayIDinformation() { cout<< "Name: Dale Reed\n" << "Date... etc. \n"; } // display instructions void displayInstructions() { cout<< "Welcome to the coin swap program. 'X' represents heads, 'O'\n" << "represents tails. To make a move, simply enter the number of \n" << "the square of the piece you wish to move or -1 to exit.\n" << "\n"; } // display board void displayBoard(char p1, char p2, char p3, char p4, char p5) { cout<< "1 2 3 4 5\n" << p1 << " " << p2 << " " << p3 << " " << p4 << " " << p5 << "\n"; } // Prompt for and get the position of the piece to move void promptAndGetPieceToMove(int &pieceToMove) { cout<< "Enter the square of the piece to move: "; cin>> pieceToMove; // See if -1 was entered to exit the program. if (pieceToMove == -1) { cout<< "Thanks for playing, goodbye. \n\n"; exit( 0); } cout<< "You selected to move from square " << pieceToMove << "\n\n"; } // Find what direction we're moving void findDirectionWereMoving( int pieceToMove, int &direction, char p1, char p2, char p3, char p4, char p5) { // assume it is not a blank, i.e. perfect input switch ( pieceToMove) { case 1: p1=='X' ? direction=1 : direction=-1; break; case 2: p2=='X' ? direction=1 : direction=-1; break; case 3: p3=='X' ? direction=1 : direction=-1; break; case 4: p4=='X' ? direction=1 : direction=-1; break; case 5: p5=='X' ? direction=1 : direction=-1; break; default: cout<< "*** Error 1\n\n"; break; } } // Using direction, find neighbor and destination position void findNeighborAndDestination(int pieceToMove, int direction, char &neighbor, int &destination, char &jumpDestination, char p1, char p2, char p3, char p4, char p5) { switch ( pieceToMove + direction) { case 1: neighbor = p1; destination = 1; break; case 2: neighbor = p2; destination = 2; break; case 3: neighbor = p3; destination = 3; break; case 4: neighbor = p4; destination = 4; break; case 5: neighbor = p5; destination = 5; break; default: cout<< "*** Error 2\n\n"; break; } if ( neighbor != ' ') { // find destination when jumping switch ( pieceToMove + (direction*2)) { case 1: jumpDestination = p1; destination = 1; break; case 2: jumpDestination = p2; destination = 2; break; case 3: jumpDestination = p3; destination = 3; break; case 4: jumpDestination = p4; destination = 4; break; case 5: jumpDestination = p5; destination = 5; break; default: cout<< "*** Error 3\n\n"; break; } } } // Make the move void movePieceIntoDestinationAndClearSource( int destination, int direction, int pieceToMove, char &p1, char &p2, char &p3, char &p4, char &p5) { char pieceBeingMoved; // piece to move character // set the type of piece to be moved if ( direction == 1) { pieceBeingMoved = 'X'; } else { pieceBeingMoved = 'O'; } switch ( destination) { case 1: p1 = pieceBeingMoved; break; case 2: p2 = pieceBeingMoved; break; case 3: p3 = pieceBeingMoved; break; case 4: p4 = pieceBeingMoved; break; case 5: p5 = pieceBeingMoved; break; default: cout<< "*** Error 4\n\n"; break; } // clear out the pieceToMove switch ( pieceToMove) { case 1: p1 = ' '; break; case 2: p2 = ' '; break; case 3: p3 = ' '; break; case 4: p4 = ' '; break; case 5: p5 = ' '; break; default: cout<< "*** Error 5\n\n"; break; } } // Move the piece void moveThePiece(int pieceToMove, char &p1, char &p2, char &p3, char &p4, char &p5) { char neighbor; // character stored in neighbor, in direction to move char jumpDestination; // character stored in destination if a jump is made int destination; // destination square number for move int direction = -1; // Find what direction we're moving findDirectionWereMoving( pieceToMove, direction, p1,p2,p3,p4,p5); // Using direction, find neighbor and destination position findNeighborAndDestination( pieceToMove, direction, neighbor, destination, jumpDestination, p1,p2,p3,p4,p5); // Make the move movePieceIntoDestinationAndClearSource( destination, direction, pieceToMove, p1,p2,p3,p4,p5); } // See if we're done yet void seeIfWereDoneYet(bool ¬Done, char p1, char p2, char p3, char p4, char p5) { if ( (p1=='O') && (p2=='O') && (p3 == ' ') && (p4=='X') && (p5 == 'X')) { // We ARE done notDone = false; cout<< "Congratulations, you did it!\n\n"; } } int main() { // declare variables char p1 = 'X'; // The 5 'p' variables are the 5 pieces on the game board char p2 = 'X'; char p3 = ' '; char p4 = 'O'; char p5 = 'O'; bool notDone = true; // flag telling whether or not the game is done int pieceToMove = -1; // location of piece to be moved // display identifying information displayIDinformation(); // display instructions displayInstructions(); while ( notDone) { // display board displayBoard( p1,p2,p3,p4,p5); // Prompt for and get the position of the piece to move promptAndGetPieceToMove( pieceToMove); // Move the piece, assuming it is a valid move moveThePiece( pieceToMove, p1,p2,p3,p4,p5); // See if we're done yet seeIfWereDoneYet( notDone, p1,p2,p3,p4,p5); } return 0; }