Wednesday, June 16, 2004

Exercise[31]

Play Rock-Paper-Scissors

You are player 1. I am player 2.

A Rock-Paper-Scissors Tournament

The code displayed when you press the first four buttons below implements a rock-paper-scissors tournament. When viewing the JavaScript version of the code, a fifth button labeled Try to win by: followed by a value (3 by default) is visible.

Assignment

Question and Coding (100 points + possible bonus points)

  1. Describe the range of values that Math.random may return. (10 points)
  2. Modify Utility.randomInt (under To Do) so that it returns a random integer between the values of min and max inclusive. (20 points)
  3. Modify KingOfTheHill (under To Do) so that an instance of KingOfTheHill:
    • always wins at least one out of three matches in the Rock-Paper-Scissors tournament described above. (30 points)
    • always wins at least two out of three matches in the Rock-Paper-Scissors tournament. (15 points)
    • always wins at least two out of three matches in the fewest number of rounds. (pride)
  4. Submit your answer and your code for Utility.randomInt and KingOfTheHill by end of day, Tuesday, 3 January 2017 (extended due to snow days):
    • Email your answer and code to jspurgeon@vcstudent.org with the subject "Exercise[31]". (10 points)
    • Work submitted after the due date will be penalized 5 points per day late, up to 20 points.

How to Play in Java

@ www.compilejava.net ...

class PlayRPS {
  public static void main(String[] args) {
    RPSMatch match = new RPSMatch(null, null);
    match.setChoiceOfPlayer1(args[0]);
    match.setIndexOfChoiceOfPlayer2(Utility.randomInt(0, 2));
    System.out.println(match.getReplay());
  }
}

How We Play (Above)

function PlayRPS(choice) {
  var match = new RPSMatch(null, null);
  match.setChoiceOfPlayer1(choice);
  match.setIndexOfChoiceOfPlayer2(Utility.randomInt(0, 2));
  var results = match.getReplay();
  console.log(results);
  if (document.getElementById('RPSAlertFlag').selectedIndex === 0) {
    alert(results);
  }
}