Objectives
* Obtain greater facility with functions and modular programming;
* Learn to pass arguments by reference to functions;
* Continue exploring the applications of branching and looping
structures;
* Generating random numbers within specified ranges;
Specifications
There is an ancient world-famous childrens game called Stomachache
that is played as follows: Two players start with three rows of candy
piecesthe top row contains three to five jelly beans, the middle row
contains five to seven sweet tarts, and the bottom row contains seven
to nine Hersheys kisses. The two players take turns choosing candy
to eat according to the following rules:
1. Pick from one and only one row during each turn.
2. Take any number of the available pieces of candy (greater than
0) from that row.
The game is over when there is no more candy left. The person who
takes the last piece of candy is the loser and gets a stomachache.
Write a program that will allow two players A and B to play this game
on a computer. Your program must include the following:
* A main program that initializes everything and monitors the
players' turns.
* A function to show whose turn it is and display the current
configuration of the rows of candy (pass by value the player and
number of pieces of candy in each row). This function will call the
next three functions, each of which prints one row of candy pieces:
o A function to print the current row of jelly beans,
o A function to print the current row of sweet tarts,
o A function to print the current row of Hersheys
kisses.
* A function to ask the given player for a row of candy and a
number of candy pieces to choose from that row. This function should
be made robust in that it disallows an invalid choice of row (a row
with no candy left). It should also disallow an incorrect choice of
candy pieces from the selected row (disallow numbers less than or
equal to zero or numbers higher than the current amount of candy left
in the row). This function updates the number of candy pieces in the
rows after each valid selection. (Probably there should be some
passing by reference so that the number of candy pieces in each row
can be updated.)
* A function that generates a random integer in a specified (upper
bound/lower bound) range.
* Any other functions that you desire to accomplish the given
tasks.
Here are some implementation notes:
* You may not use arrays in this program, even if you have already
studied them.
* You may not use global variables in any program you write for
this course (including this one).
* You may find if-else-if a convenient method for testing things,
or you may want to use the switch statement. Switch could be very
beneficial in this program.
A sample run is provided. Your program should include at least the
features shown in this run.
Welcome to the game of
stomachache. This is a two
person game (Players A and B). It starts
with three rows of candy containing 3-5, 5-7,
and 7-9 candy pieces respectively. When it's your move ,
you must select a row containing one or more
pieces of candy, and then select at least one piece of candy
from only that row. You may select all if you wish.
The player taking the last piece of candy loses.
It is Player A's move
The current configuration is:
Row 1: JB JB JB JB
Row 2: ST ST ST ST ST
Row 3: HK HK HK HK HK HK HK HK
Player A, from which row do you wish to draw? 2
How many from row 2? 2
It is Player B's move
The current configuration is:
Row 1: JB JB JB JB
Row 2: ST ST ST
Row 3: HK HK HK HK HK HK HK HK
Player B, from which row do you wish to draw? 4
Row must be 1, 2, or 3
Player B, from which row do you wish to draw? 3
How many from row 3? 8
It is Player A's move
The current configuration is:
Row 1: JB JB JB JB
Row 2: ST ST ST
Row 3:
Player A, from which row do you wish to draw? 3
No candy in row 3.
Player A, from which row do you wish to draw? 1
How many from row 1? 2
It is Player B's move
The current configuration is:
Row 1: JB JB
Row 2: ST ST ST
Row 3:
Player B, from which row do you wish to draw? 1
How many from row 1? 2
It is Player A's move
The current configuration is:
Row 1:
Row 2: ST ST ST
Row 3:
Player A, from which row do you wish to draw? 2
How many from row 2? 2
It is Player B's move
The current configuration is:
Row 1:
Row 2: ST
Row 3:
Player B, from which row do you wish to draw? 1
No candy in row 1.
Player B, from which row do you wish to draw? 3
No candy in row 3.
Player B, from which row do you wish to draw? 2
How many from row 2? 0
Incorrect number. Try again! 1
Player B, from which row do you wish to draw? 2
How many from row 2? 1
Player B selected the last piece of candy and gets a stomachache!
HINT: Before you start trying to program this, outline its actions,
inputs, and outputs. As you do so, make special note of how each
function is to be designed and implemented. As functions are designed,
test them with drivers and/or stubs. Failure to take this approach
could greatly complicate your process and end up wasting unnecessary
time. This program is more involved than the first three, because of
the need to break it into a greater number of pieces that all work
together; however, no individual piece is very complicated. |