diff --git a/week1.java b/week1.java index 90e59d4..f3a2717 100644 --- a/week1.java +++ b/week1.java @@ -1,4 +1,5 @@ import java.util.Calendar; +import java.util.Random; import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; @@ -7,7 +8,7 @@ class runLabs { new Lab4(); new Lab5(); new Lab6(); - new Lab7(); + // new Lab7(); } } @@ -369,7 +370,7 @@ class Lab6 extends Lab { * @param padChar Character to pad with * @return The padded result */ - public String stringLPad(String toPad, int paddedLength, char padChar) { + public static String stringLPad(String toPad, int paddedLength, char padChar) { // early out - return unmodified if (toPad.length() >= paddedLength) { return toPad; @@ -387,11 +388,78 @@ class Lab6 extends Lab { } class six9 extends Exercise { + private Random random = new Random(); // initialise once + // see https://stackoverflow.com/a/35277291, to pick a random member of an enum, first convert it to an array + private Throw[] throwsArray = Throw.values(); // can't call it 'throws' for obvious reasons + + final Throw randomThrow() { + return throwsArray[random.nextInt(throwsArray.length)]; + } + public six9() { super(9); - System.out.println(String.format("TODO: ro sham bo once we have stdin working")); + System.out.println(String.format("TODO: Take input when we have stdin working")); + Throw player1 = Throw.ROCK; // "Good ol' rock, nothing beats that" + Throw player2 = randomThrow(); + playRockPaperScissors(player1, player2); + player2 = randomThrow(); + playRockPaperScissors(player1, player2); + player2 = randomThrow(); + playRockPaperScissors(player1, player2); + } + } + + public void playRockPaperScissors (Throw player1, Throw player2) { + // slight semantic overload with throwing an error but I think that's the term + // for plays in RPS + String result; + // early out - TOASK why this doesn't work when it works in a REPL!! + if (player1 == player2) { + result = "a draw"; + } else { + switch (player1) { + case ROCK: + if (player2 == Throw.SCISSORS) { result = "R>S Player 1 wins"; } + else { result = "R