Lab assignment: C++.

The topic of the lab today is C++.

Preliminary Information:

To work with C++ in the lab you will need to do the following steps:

1) Write the program. This can be done with a text editor, for instance the same editor you use for the quizes. For this lab the skeleton of the program will be provided to you.
2) Save the file containing the program as a .cpp file. You can save it on your desktop or in your home folder.
3) Compile the program: Start the terminal. Go to the folder where the program was saved by using cd. Suppose the file is called lab10.cpp. You can compile it by typing: g++ lab10.cpp -o lab10.exe and pressing ENTER. This command translates the program into a binary executable file named lab10.exe in the same folder.
4) Execute the program: After step 3, if you do not get any error messages, a file named lab10.exe should have been created in the same folder. To execute the file type ./lab10.exe and press ENTER.

LAB 10. COIN JUMP

You will need to write a program for playing a simple game with 4 coins. The game starts as in configuration 1 in the figure below. The heads are represented with an O and the tails are represented with an X.

The purpose of the game is to arrive at configuration N, shown at the bottom, in which the coins are swapped, heads on the left, and tails on the right. The heads can move only from right to left and the tails can move only from left to right. The coins can move only in the empty position, only if they are adjacent to the empty position, as in configuration 2, or if the empty position is at most two spaces ahead, as in configurations 3 and 5, by jumping over another coin.

You must write a C++ program that declares an array of chars and asks to the user which coin the user wants to move. The program must display the array after every move. The program must also update the array to simulate the move. A run of the program is shown below:

For every move the program prints the array of characters and prints the indices of each element on top of every element.
The player types on the keyboard the position of the coin to be moved and the destination. The player types -1 to exit.

The three stages are described below:

Stage 1:
The .cpp file below contains code to get you started:

lab10.cpp

The file contains variable declarations and the main. Some functions are called inside the main. You MUST CREATE AND WRITE THE FUNCTIONS, so that the code works as in the figure above. Notice the function calls inside the main.

1) First the board must be displayed with displayBoard. Notice that the method receives the array of chars as a parameter in input.
2) Then a method is called that is responsible for prompting the message about entering input and then reading the user input. The name of the function is promptAndGetInput. Notice how it receives two parameters (that must be passed by reference) in input, and then returns an integer value in case the user wants to exit.
3) The last function, makeMove, updates the board. Notice how it receives in input the two variables containing the user input and the array of chars.

This page of examples contains simple examples on how to pass arrays and reference paramters as arguments inside C++ functions.

Stage 2:
Implement error checking. The following errors might occur:

1) The user must only input values between -1 (for exiting) and 4 included. If the user types values outside the range, the user must be prompted again to type in the correct values.
2) If the origin and destination typed by the user are more then 2 positions from each other. E.g. the user is tryin to move from position 0 to position 4.
3)If the directions of movement are not correct, e.g. the user is trying to move a O on the right or trying to move an X on the left.
4)If the destination of the move is not the empty space, that is the user is trying to move a coin in a position already filled by another coin.

If any of these errors occur, prompt the user for input again.

Stage 3:
Add code to detect when game is done, and exit the loop. That is the program should detect automatically when the game ends, not wait for the user input -1

The turnin command for this assignment is turnin -c cs102 -p lab10 lab10.cpp
If you write code on other files (not necessary) turnin the other files as well.

SOLUTION:

lab10.cpp