Objectives

This lab is designed to give you practice to develop your skills towards achieving the learning objectives from week 4 (and earlier weeks). In particular, after completing these tasks, you should now have developed skills to demonstrate that you are able to:

  • Write classes that are subclasses of other classes
  • Write code that overrides behaviour of inherited methods.
  • Write code that exploits the benefits of polymorphism
  • Write code that involves dynamic binding
  • Observe/apply principles of good object-oriented design

Background

In this lab you will implement a variation of the game Rock, Paper, Scissors.

In the normal game, two people will count from 1 to 3, and on the saying of 3 will arrange their hand into a shape resembling either a rock (by a closed hand), paper (a flat hand), or scissors (the middle and index fingers protruding from a closed hand).

If person A chooses paper, they will defeat person B only if person B chooses rock the paper will wrap around a rock.

If person A chooses a rock, they will defeat person B only if person B chooses scissors the rock will make scissors blunt.

If person A chooses scissors, they will defeat person B only if person B chooses paper the scissors will cut through the paper.

In this lab, you are to write a separate class to represent each possible choice that a player can make, and a text-based menu to control a sequence of games.

Tasks

Please read through all the tasks before commencing work. You may wish to draw a class diagram to help understand the task, but we will not be marking this diagram.

1. Create a class named HandShape to be the superclass of three different possible hand shapes. It should provide a method named canDefeat which should take as a parameter, a reference to another HandShape object, and return a boolean value. The implementation in this class should just return false It should also provide a method named getPoints which will return how many points a player should score in the case that this hand shape successfully defeats another type of hand shape. The implementation in this class (HandShape) should simply return 0.

2. Create a class named RockShape to be a subclass of the class made in task 1. Override the canDefeat method, so that it return true when the type of object provided as a parameter, happens to be a ScissorShape object. Override the getPoints method so that it returns the value 7.

3. Create a class named ScissorShape to be a subclass of the class made in task 1. Override the canDefeat method, so that it return true when the type of object provided as a parameter, happens to be a PaperShape object. Override the getPoints method so that it returns the value 4.

4. Create a class named PaperShape to be a subclass of the class made in task 1. Override the canDefeat method, so that it return true when the type of object provided as a parameter, happens to be a RockShape object. Override the getPoints method so that it returns the value 2.

5. Create a class named Player which will be a superclass for two types of Player, a HumanPlayer and a ComputerPlayer (these are explained in separate tasks). It should provide a method named chooseHandShape which returns an object reference of the HandShape type. In this class, it should simply always return a PaperShape object. It should provide a method named getPoints to return the total points of the player, which are initially set to 0. It should provide a method named getName to return the name of the player (set at construction time)

6. The Player class should provide another method named playAgainst which should take as a parameter, a reference to another Player object. The method should implement the following algorithm:

a) The object itself should choose a hand shape.

b) Ask the player which was provided in the parameter, to also choose a hand shape.

c) Determine whether the hand shape of the object itself, can defeat the hand shape of the player which was provided in the parameter.

d) If the result is true, then the object itself should increase its points by the number of points that its chosen hand is worth. (If it is false, nobody gets any points).

Include output statements along the way, so that the user running the program understands what is happening but do not reveal either players choice until both players have made their choice.

7. Create a class named HumanPlayer to be a subclass of the Player class. It should only need to override the chooseHandShape method, to use a Scanner object (preferably provided to the constructor so that only one Scanner exists in the program) to ask the user to choose which type of hand shape they want to use on this occasion. Return an instance of the relevant selected kind of hand shape.

8. Create a class named GameMain which will contain the main() method and be responsible for controlling the game.

a. When the program begins, create 2 separate HumanPlayer objects by obtaining relevant information to pass to the constructor.

b. The program should ask how many rounds they wish to play a single round lets each player take a turn of being first to choose their hand shape.

c. There should then be that many rounds played. In each round you should invoke playAgainst on each of the player objects, providing the other player object as the parameter.

d. After all the rounds are completed, report the total number of points for each player.

9. Create a class named ComputerPlayer to be a subclass of the Player class. It should only need to override the chooseHandShape method, and will make a decision randomly, according to the value returned by the nextInt method of the Random class, found in the java.util package. For details on this method, consult the Java 8 SDK API Documentation (at http://docs.oracle.com/javase/8/docs/api/ ). The constructor could still accept a name for this player.

10. Check that the ComputerPlayer class works, by replacing the second HumanPlayer object in the main class with a ComputerPlayer object. Then modify the code of the main program so that when the program begins, it asks the user whether they want to play against the computer, or against another human, and based on the input will decide which type of object to create for player 2.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.