Lab 10 In-Class: More Work with Classes
The goal of today's lab is to write a version of Rock, Paper, Scissors
that uses classes. In the process, you will gain more experience in
writing classes and in working with the interaction of different classes.
We will model the game using two classes -- one to represent a play
(the player's choice of rock, paper, or scissors) and one to represent
a player. (NOTE: The game itself could be
modeled using a class though we won't do that.)
The main program will be written to let the user play over and over again
and keep the score (number of times each player wins).
The Play Class
The Pre-Lab described part of the class Play that will model a player's
play in the Rock, Paper, Scissors game. The complete class will have the
following components: (NOTE: This is an overview of the class. Read it
to see what the class will contain but DON'T WRITE CODE YET!!!)
- an instance variable of type String (it will have one of
three value: "R", "P", or "S") that stores the current value of the Play object
- a random number generator (this needs to be a static instance
variable in the class -- private static Random generator = new Random())
- a constructor with no parameters that generates a random choice
(generate a random number between 0 and 2 (inclusive) and then assign "R" to
0, "P" to 1, and "S" to 2)
- a constructor one with one parameter -- that parameter is
a value of the user's play (type String). This constructor should
make sure the play is stored as an uppercase R, P, or S by converting the
parameter to uppercase. If the string is not a valid play (an X for example)
the constructor should use a default value of R and also print a warning
message to indicate the play was not valid.
- a method that returns the value of the play (the "R", "P", or "S")
- a toString method that returns the string "Rock", "Paper", or "Scissors"
corresponding to the current value of the Play object
- a method boolean equals(Play play2) that returns true if the
current play (this Play) is the same as play2 and false
otherwise.
- a method boolean beats(Play play2) that returns true if the current
play (this Play) beats play2 and false otherwise. (Use the same
logic you used in Lab 6 when you wrote a version of Rock, Paper, Scissors.)
This method should also print the messages
("Rock crushes Scissors", "Paper covers Rock", and "Scissors cut Paper")
as it does its comparison of plays. NOTE: do not print a message
here about who won and don't print a message in the case of ties.
Implement and test this class as follows:
- First define the class including NO MORE than the following methods (you
may start with fewer): the two constructors, the method that returns the
play (as an R, P, or S), and the toString method.
- The file TestPlay.java contains part of
a program to test the class. Fill in the instantiations and the method
calls as instructed in the comments. Compile and run the program to
make sure everything works so far. (NOTE: You only need to compile the
TestPlay.java file. The compiler will recognize it uses Play.java and
will automatically compile it.)
- Now add the equals method to the Play class and add a statement
to TestPlay.java to test the method (an if that prints out whether or
not the two Play objects play1 and play2 are equal). Run the program to
make sure equals is correct.
- Finally add the beats method to the Play class. Note that
this method should also print a message (see description above) in addition
to returning a boolean. It would be helpful in writing this method
to declare some local variables -- one for the boolean you will
return and one for the value of play2 (otherwise you will need to
call a method several times to get the value of play2 -- call it once
and store the value in a local variable then use that variable everywhere
you need to compare)
- Add two if statements to the test program - one to see if player1 beats
player2 and another to see if player2 beats player1.
- Print the final version of TestPlay.java.
The Player Class
The player in a game can also be viewed as an object. (A game has two
players who each choose plays.)
For our program, a player will
be described by the following instance variables:
- a name
- a type (a person or a computer: we'll use an instance
variable of type char -- 'p' for person and 'c' for computer)
- a number of wins.
The class must provide the following methods:
- a constructor that takes two parameters -- the name (String) and type
of the player object (char). The constructor should initialize the number of
wins to 0.
- a method that returns the name
- a toString method that returns the string formed by the
name, the type, and the number of wins (for example, if the player
is named Hal and is a computer and
currently has 5 wins the method would return "Hal, a computer, has 5 wins")
- a method void updateWins() that increments the number of wins
- a method Play choosePlay() that gets and returns the player's
play. If the player is a computer, the method should get a random
play (remember -- there is a constructor for the Play class that
will initialize a Play object with a random value); if the player is
a person the method should prompt the user for his/her choice and
read it in then use it to initialize a Play object.
Implement the Player class and fill in code in the file
TestPlayer.java to test it. Note that
the purpose of TestPlayer.java is just to call each of the
methods to make sure they work -- it doesn't actually play the
game. Print the final version of TestPlayer.java.
Rock, Paper, Scissors Program
Now we're ready to put all of this together in a program that plays
the game. The file
RPS.java contains a start for the program.
Fill it in (you can use much of your code from the TestPlayer.java
program) and test it as follows:
- Get the info about the two players and instantiate the player
objects.
- Use a do...while loop to play the game as many times
as the user wishes.
- Control the loop by asking if the player wants
to play again (user should enter a Y or N). The game should continue as
long as the user enters NEITHER an N or n (in other words, the
default is to continue if the user hits the wrong key -- stop only on
an N or n).
- Inside the loop a single game is played as follows:
- each player chooses their play (invoke the appropriate methods!)
- the plays are printed (along with the name of the
user -- for example, Hal chose Rock),
- the winner is determined (use if...else's with the equals and
beats methods)-- a message is printed about who won and the
winner's score is updated. Also, keep count of the number of times the
players tie.
- print the updated scores
- determine if the user wants to play again.
- Test the program thoroughly. Run it with two people playing,
with a person and a computer, and with two computers. Make sure it
works in all cases.
- Print your final version of RPS.java, Play.java, and Player.java.
Hand In:
Hard copies of RPS.java, Play.java, Player.java, TestPlay.java, and
TestPlayer.java.
Email a tar file of your work to your instructor at cs.roanoke.edu.