hi i need to do a C++ program. You are going to practice the use of...

90.2K

Verified Solution

Question

Programming

hi i need to do a C++ program.

You are going to practice the use of array by implementing theinterface 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 blackand 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 blackand white marbles, i.e. the puzzle with former configuration shouldend when the board becomes:
B B B . W W W
You have only two types of moves. You can either

  • slide a marble 1 space (into the empty position), or
    For example, to slide the white (W) marble at index 2: W WW . B B B → W W . W B B B
  • jump a marble over 1 and only 1 marble of the opposite color(again, into the empty position).
    For example, to jump the black (B) marble at index 4: W W .W B B B → W W B W . B B

You CANNOT jump marbles over more than 1 position, and youCANNOT backtrack your moves (B can only be moved to left, and W canonly be moved to right).
In this lab, we are implementing the basic version of the game with1 empty hole only in between the black and whitemarbles.

Tasks

You need to use array to implement the interface of shuttlepuzzle.

  • Your program should get 2 integers from the player whichrepresent the number of white marbles (num_W) and black marbles(num_B) respectively.
  • The input of every move is an int (position) and a char(operation). You should move the marble at the position with theoperation. The direction of this move depends on the color of themarble.
  • If the input position is -1, your program should print \"Exit.\"and then the program ends and exits.
  • While the puzzle solver (player) playing with your program,your program should identify forbidden move, and print \"Error!\" towarn the puzzle solver.
  • When the puzzle is solved, your program should print\"Congratulations!\" and exit.

In our test cases, we assume that

  • num_W + num_B < 100, num_W > 0 and num_B > 0
  • the position (index) will be always in the range of [0, num_W +num_B]
  • the operation can only be 'J' (jump) or 'S' (slide)

You can start from skeleton. The skeleton has already divde thetasks into several functions. Feel free to start from scratch andwrite 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
<<

'>>>' represents input and '<<<' representsoutput. 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, andyou MUST clear all redundant ouput before submit.
You should submit merely 1 source code file.

Skeleton

#include 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 3S
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 J3 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 14J 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 S15 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 J18 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 8J 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 7J 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 17J 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 18J 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 S13 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 11S
Expected ouput:
Congratulations!

Answer & Explanation Solved by verified expert
4.2 Ratings (807 Votes)
This is the modified codeinclude    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students