CS 239 Lab:Review of Iteration,
Selection, and Random Number Generation
Objectives:
|
- Review iteration
problems
- Review selection
problems
- Review the use of
random number generators, specifically the generator in the Math class.
|
Background:
|
The lab will be a game of PseudoWar.
There will be two players. In each round, the players will each generate an
integer in the range of 1 - 52. The player with the higher number wins that
round and receives a point. If the numbers are the same, no one receives a
point and result is indicated as a tie.
Play continues for a user specified number of rounds. The winner is
the person with the most rounds one at the end of the game.
|
New Terms:
|
There are no new terms for this lab.
|
Materials:
|
You must write this application entirely from scratch.
|
Acknowledgment
|
Lab by Elizabeth Adams
|
1 General Instructions:
Set up your programming environment for this lab. Create a
folder named CS239-Lab2a on your flash drive, in your Novell account, or in
C:\temp
2 Specific Output:
- You must start the game
by announcing the start of the program with a welcome message.
- You must prompt for the
names of the two players.
- You must echo the names
by asking them how many rounds.
- You must echo the number of
rounds before play begins with an appropriate message.
- The game output be should look like the following table:
Round # Player1name Player2name Winner
---------------------------------------------------------
1 2 7 Player2name
2 25 22 Player1name
...
7 16 16 Tie
The game has ended.
7 rounds were played
1 round was a tie.
Player1name won 3 rounds.
Player2name won 3 rounds.
This program has ended normally.
3 Other requirements:
Your application must have 2 files, one containing
a main method which will control the game and a second which will hold the PseudoWar game.
To gain practice using the Math.random
method, you MUST use that method in the program.
Your PseudoWar game file
will keep track of each of the players and their accumulated points and carry
out the actions using those attributes.
- A constructor to set the
names of the players, the number of rounds, and their initial scores.
Alternately, you could have a default constructor which would call an intro
method which would obtain the name of each player and the number of rounds
to play. This method may print prompt lines.
- A startGame
method which prints the first heading lines (See item 5 above) for the
output.
- A playGame
method which will run each round of the game and print the appropriate
output.
- A generateNumber
method that will generate a single number in the 1 - 52 range.
- A determineWinner
method that receives the two numbers representing the roll of each of the
players and returns a 0, 1 or 2 depending on which number is higher. (1
the first number is higher, 2 the second is higher, 0 for a tie). Note,
determine winner prints tie in
the case of a tie.
- A runRound
method that uses the generateNumber
method and determineWinner method to
set the new scores of the players and returns a String with the
appropriate round String.
- A getFinalResult
method that returns a String with the final output including the final
game results.
5 Steps:
- Before you do any coding you must create
tests. Since you are using a Random
number generator in this program you will not be able "control"
your test cases, but you should test your program thoroughly by running it
enough times to see that you have that all possible situations are
covered. Print out and turn in your test
document by the end of the lab period.
- The second thing you
must do is to build your stubs for your program. In other words, based on
the requirements above, create your method signatures, documentation, and
appropriate return values where required. You MUST also
include within each method inline comments that show how you will carry
out that method (remember to document groups of steps, not each individual
line). Think carefully about what attributes you will need in your PseudoWar file.
- When your stubs are
created and documentation done, submit your stubs. NOTE: These must compile or no credit
will be issued. This MUST be done by midnight tonight, Wednesday,
September 27th .
- Next,
implement your design. The implemented design must be submitted by midnight
Friday, September 29th.
6. Grading
- You can earn 10 points
for your test cases
- You can earn 30 points
for working stubs that are properly documented.
- You can earn 60 points
for a correctly working program meeting all specifications.
updated 8/26/08 (esa)