Question

In: Computer Science

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

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

  • slide a marble 1 space (into the empty position), or
    For example, to slide the white (W) marble at index 2: W W W . B B BW 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 BW W B W . B B

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.

  • Your program should get 2 integers from the player which represent 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 the operation. The direction of this move depends on the color of the marble.
  • 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!" to warn 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 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!

Solutions

Expert Solution

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


Related Solutions

How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
WHY DO I NEED TO STUDY POLYNOMIALS? HAVE I EVER GOING TO USE IT? JOBS THAT...
WHY DO I NEED TO STUDY POLYNOMIALS? HAVE I EVER GOING TO USE IT? JOBS THAT USE POLYNOMIALS Visit the following site, read and give an example of your understanding use of polynomials in your job or any example you have. https://careertrend.com/list-6330381-jobs-use-polynomials.htm (Links to an external site.)Links to an external site.l
Please do the math by hand, do not use a program, I need to see the...
Please do the math by hand, do not use a program, I need to see the procedure, the answer itself is less important. Comparison of peak expiratory flow rate (PEFR) before and after a walk on a cold winter's day for a random sample of 9 asthmatics. Use the following data to determine if the patients conditioned changed after a walk. Present your results and make some interpretations. Subject Before After 1 312 300 2 242 201 3 340 232...
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT