In: Computer Science
hi i need to do a C++ program.
You are going to practice the use of array by implementing the
interface of Shuttle Puzzle.
Here is an example of how you play the Shuttle Puzzle.
Say that you start with a board with 7 holes and there are 3 black
and 3 white marbles on the board in this configuration:
W W W . B B B
The dot (.) represents the empty hole withouth any marble.
The objective of the game is to switch the positions of the black
and white marbles, i.e. the puzzle with former configuration should
end when the board becomes:
B B B . W W W
You have only two types of moves. You can either
You CANNOT jump marbles over more than 1 position, and you
CANNOT backtrack your moves (B can only be moved to left, and W can
only be moved to right).
In this lab, we are implementing the basic version of the game with
1 empty hole only in between the black and white
marbles.
Tasks
You need to use array to implement the interface of shuttle puzzle.
In our test cases, we assume that
You can start from skeleton. The skeleton has already divde the tasks into several functions. Feel free to start from scratch and write your own code as long as it implements the game.
For a puzzle with 2 white marbles and 2 black marbles:
Num of white and black marbles: 2 2
[01234]
WW.BB
Index (-1 to exit): 0
'J' or 'S': J
Error!
Index (-1 to exit): 1
'J' or 'S': S
[01234]
W.WBB
Index (-1 to exit): 3
'J' or 'S': J
[01234]
WBW.B
Index (-1 to exit): 4
'J' or 'S': S
[01234]
WBWB.
Index (-1 to exit): 2
'J' or 'S': J
[01234]
WB.BW
Index (-1 to exit): 0
'J' or 'S': J
[01234]
.BWBW
Index (-1 to exit): 1
'J' or 'S': S
[01234]
B.WBW
Index (-1 to exit): 3
'J' or 'S': J
[01234]
BBW.W
Index (-1 to exit): 2
'J' or 'S': S
[01234]
BB.WW
Congratulations!
For a puzzle with 2 white marbles and 3 black marbles:
>>>2 3
>>>3 S
>>>1 J
>>>0 S
>>>2 J
>>>4 J
>>>5 S
>>>3 J
>>>1 J
>>>2 S
>>>4 J
>>>3 S
<<<Congratulations!
'>>>' represents input and '<<<' represents output. We skip the ragular output in above case.
Your output should be exactly the same as the requirement.
You should NOT modify the output code in the skeleton, and
you MUST clear all redundant ouput before submit.
You should submit merely 1 source code file.
Skeleton
#include <iostream> using namespace std; const int MAX_SIZE = 1000; /* * (Given) * Print the current game board */ void print(const char board[], int valid_length) { cout << " ["; for (int i = 0; i < valid_length; ++i) cout << i; cout << "]" << endl; cout << " "; for (int i = 0; i < valid_length; ++i) cout << board[i]; cout << endl; } /* * Initialize the game board with white (W) marbles on the left and * black (B) marbles on the right, and a gap in between * Returns the length of the puzzle, i.e. num_W + 1 + num_B */ int initialize(char board[], int num_W, int num_B) { // TODO } /* * Jump a marble over 1 and only 1 marble of the opposite color into the empty position. * You CANNOT jump marbles over more than 1 position, and * you CANNOT backtrack your moves (B can only be moved to left, and W can only be moved to right). * * Returns true if the jump is valid * otherwise, returns false */ bool jump(char board[], int length, int index) { // TODO } /* * Slide a marble 1 space (into the empty position) * you CANNOT backtrack your moves (B can only be moved to left, and W can only be moved to right). * * Returns true if the slide is valid * otherwise, returns false */ bool slide(char board[], int length, int index) { // TODO } /* * Returns true if all black marbles are on the left and white marbles are on the right * otherwise, returns false */ bool game_finished(const char board[], int num_W, int num_B) { // TODO } int main() { char board[MAX_SIZE] = {}; int num_W, num_B; // Get the number of white (W) & black (B) marbles cout << "Num of white and black marbles: "; cin >> num_W >> num_B; // Initialize the board int length = initialize(board, num_W, num_B); print(board, length); // Continue while not all marbles are switched while(!game_finished(board, num_W, num_B)) { // Get the index (position) for the move (operation), -1 means give up the game int index; cout << "Index (-1 to exit): "; cin >> index; if(index == -1) { cout << "Exit." << endl; break; } // Get the operation, 'J' for jump or 'S' for slide char op; cout << "'J' or 'S': "; cin >> op; bool res = false; switch (op) { case 'J': res = jump(board, length, index); break; case 'S': res = slide(board, length, index); break; } if(!res) cout << "Error!" << endl; else print(board, length); } if(game_finished(board, num_W, num_B)) { cout << "Congratulations!" << endl; } return 0; }
Test cases for students
We skip the regular output in the following cases.
Input:
2 2 3 S 1 J 0 S 2 J 4 J 3 S 1 J 2 S
Expected ouput:
Congratulations!
Input:
2 2 0 J 1 S 3 J 4 S 3 S 2 J 0 J 1 S 3 J 2 S
Expected ouput:
Error!
Error!
Congratulations!
Input:
2 2 0 J 1 S 3 J 4 S 3 S -1
Expected ouput:
Error!
Error!
Exit.
Input:
3 3 4 S 2 J 1 S 3 J 5 J 6 S 4 J 2 J 0 J 1 S 3 J 5 J 4 S 2 J 3
S
Expected ouput:
Congratulations!
Input:
4 4 5 S 3 J 2 S 4 J 6 J 7 S 5 J 3 J 1 J 0 S 2 J 4 J 6 J 8 J 7 S 5 J
3 J 1 J 2 S 4 J 6 J 5 S 3 J 4 S
Expected ouput:
Congratulations!
Input:
10 11 11 S 9 J 8 S 10 J 12 J 13 S 11 J 9 J 7 J 6 S 8 J 10 J 12 J 14
J 15 S 13 J 11 J 9 J 7 J 5 J 4 S 6 J 8 J 10 J 12 J 14 J 16 J 17 S
15 J 13 J 11 J 9 J 7 J 5 J 3 J 2 S 4 J 6 J 8 J 10 J 12 J 14 J 16 J
18 J 19 S 17 J 15 J 13 J 11 J 9 J 7 J 5 J 3 J 1 J 0 S 2 J 4 J 6 J 8
J 10 J 12 J 14 J 16 J 18 J 20 J 21 S 19 J 17 J 15 J 13 J 11 J 9 J 7
J 5 J 3 J 1 J 2 S 4 J 6 J 8 J 10 J 12 J 14 J 16 J 18 J 20 J 19 S 17
J 15 J 13 J 11 J 9 J 7 J 5 J 3 J 4 S 6 J 8 J 10 J 12 J 14 J 16 J 18
J 17 S 15 J 13 J 11 J 9 J 7 J 5 J 6 S 8 J 10 J 12 J 14 J 16 J 15 S
13 J 11 J 9 J 7 J 8 S 10 J 12 J 14 J 13 S 11 J 9 J 10 S 12 J 11
S
Expected ouput:
Congratulations!
This is the modified code
#include <iostream>
using namespace std;
const int MAX_SIZE = 1000;
/*
* (Given)
* Print the current game board
*/
int currPos;
void print(const char board[], int valid_length)
{
cout << " [";
for (int i = 0; i < valid_length; ++i)
cout << i;
cout << "]" << endl;
cout << " ";
for (int i = 0; i < valid_length; ++i)
cout << board[i];
cout << endl;
}
/*
* Initialize the game board with white (W) marbles on the left
and
* black (B) marbles on the right, and a gap in between
* Returns the length of the puzzle, i.e. num_W + 1 + num_B
*/
int initialize(char board[], int num_W, int num_B)
{
int i,j;
int k=0;
for(i=0;i<num_W;i++)
board[k++]='W';
board[k++]='.';
for(i=0;i<num_B;i++)
board[k++]='B';
currPos=num_W;
}
/*
* Jump a marble over 1 and only 1 marble of the opindexite color
into the empty indexition.
* You CANNOT jump marbles over more than 1 indexition, and
* you CANNOT backtrack your moves (B can only be moved to left, and
W can only be moved to right).
*
* Returns true if the jump is valid
* otherwise, returns false
*/
bool jump(char board[], int length, int index)
{
if(index+2==currPos && board[index]!=board[index+1]
&& board[index]=='W'){
board[currPos]=board[index];
board[index]='.';
currPos=index;
//display(board,total);
return true;
}
else if(index-2==currPos && board[index]!=board[index-1]
&& board[index]=='B'){
board[currPos]=board[index];
board[index]='.';
currPos=index;
//display(board,total);
return true;
}
else{
//cout<<"Error!"<<endl;
return false;
}
}
/*
* Slide a marble 1 space (into the empty indexition)
* you CANNOT backtrack your moves (B can only be moved to left, and
W can only be moved to right).
*
* Returns true if the slide is valid
* otherwise, returns false
*/
bool slide(char board[], int length, int index)
{
if(index+1==currPos && board[index]=='W'){
board[currPos]=board[index];
board[index]='.';
currPos=index;
//display(board,total);
return true;
}
else if(index-1==currPos && board[index]=='B'){
board[currPos]=board[index];
board[index]='.';
currPos=index;
//display(board,total);
return true;
}
else{
//cout<<"Error!"<<endl;
return false;
}
}
/*
* Returns true if all black marbles are on the left and white
marbles are on the right
* otherwise, returns false
*/
bool game_finished(const char board[], int num_W, int num_B)
{
int isComplete=false;
if(currPos==num_B){
isComplete=true;
for(int loop=0;loop<currPos;loop++){
if(board[loop]=='W'){
isComplete=false;
break;
}
}
}
return isComplete;
}
int main()
{
char board[MAX_SIZE] = {};
int num_W, num_B;
// Get the number of white (W) & black (B) marbles
cout << "Num of white and black marbles: ";
cin >> num_W >> num_B;
// Initialize the board
int length = initialize(board, num_W, num_B);
print(board, length);
// Continue while not all marbles are switched
while(!game_finished(board, num_W, num_B))
{
// Get the index (indexition) for the move (operation), -1 means
give up the game
int index;
cout << "Index (-1 to exit): ";
cin >> index;
if(index == -1)
{
cout << "Exit." << endl;
break;
}
// Get the operation, 'J' for jump or 'S' for slide
char op;
cout << "'J' or 'S': ";
cin >> op;
bool res = false;
switch (op)
{
case 'J':
res = jump(board, length, index);
break;
case 'S':
res = slide(board, length, index);
break;
}
if(!res)
cout << "Error!" << endl;
else
print(board, length);
}
if(game_finished(board, num_W, num_B))
{
cout << "Congratulations!" << endl;
}
return 0;
}
//////find screenshot of the working code below