This question should not take you very long. It's a continuation of problem 2 from homework 6. Update your function
void make_game_board() so that it behaves as follows. Submit a single
cpp file for both this question and Question 2, called
- Every other line consists of plusses separated by a single space.
- In between these lines are lines of the same length all of whose characters are spaces. This is important! Make sure you conform to this requirement.
- Assume the max board size is no more than 12.
This question should not take you very long. It's a continuation of problem 2 from homework 6. Write a companion function
void print_game_board() that behaves as follows. Submit a single
cpp file for both this question and Question 1, called
- The function takes two parameters, an array (the game board as in problem 2 from homework 6) and an int (the size of the array).
- The function displays the game board using
cout, but with a little extra information, see sample output below.
- The easiest way to handle the row indices is to use
iomanip. You can assume that the max board size is no more than 12.
The below output assumes you can write a driver program whose
main() function calls both
Enter the game board size (1 to 12): 5
0+ + + + + +
2+ + + + + +
4+ + + + + +
6+ + + + + +
8+ + + + + +
10+ + + + + +
This is Exercise P6.18 from Horstmann. Use vectors, not arrays, for full credit. The instructions are:
Write a program
solitaire.cpp that models the game of Bulgarian Solitaire. The game starts with 45 cards. (They need not be playing cards. Unmarked index cards work just as well.) Randomly divide them into some number of piles of random size. For example, you might start with piles of size 20, 5, 1, 9, and 10. In each round, you take one card from each pile, forming a new pile with these cards. For example, the sample starting configuration would be transformed into piles of size 19, 4, 8, 9, and 5. The solitaire is over (why?) when the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and 9, in some order. (It can be shown that you always end up with such a configuration.) In your program, produce a random starting configuration and print it. Then keep applying the solitaire step and print the result. Stop when the solitaire final configuration is reached.
Make sure that your program produces visible output, showing each "play" of the game. As always, your output should be professional and appropriate. Your solution must use vectors only to receive full credit.
You need a procedure—an algorithm— that handles all cases properly. Pay special attention to piles of size 1 and 0. It would be very good to draw some pictures of some example games to help you understand how these algorithms could be specified. Of course, you are using functions to separate the different parts of your algorithm. Write code in
main to help you test these functions and make sure they are working. Only write the real
main code after you think you are done with the functions.
If you finish, you can make your program cooler. Let the user begin the session by specifying how many games of solitaire to play. Then, for each game, have your program record the number of plays needed to reach the end (use a vector). To end the session, output a summary showing interesting statistics, such as average number of plays, max and min, and whatever else you feel like including. Remember to be careful with division and to use library functions if needed. You can earn up to 10 extra points.