|
@@ -1,4 +1,5 @@ |
|
|
import java.util.Calendar; |
|
|
import java.util.Calendar; |
|
|
|
|
|
import java.util.Random; |
|
|
import java.util.Scanner; |
|
|
import java.util.Scanner; |
|
|
import java.util.concurrent.ThreadLocalRandom; |
|
|
import java.util.concurrent.ThreadLocalRandom; |
|
|
|
|
|
|
|
@@ -7,7 +8,7 @@ class runLabs { |
|
|
new Lab4(); |
|
|
new Lab4(); |
|
|
new Lab5(); |
|
|
new Lab5(); |
|
|
new Lab6(); |
|
|
new Lab6(); |
|
|
new Lab7(); |
|
|
|
|
|
|
|
|
// new Lab7(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -369,7 +370,7 @@ class Lab6 extends Lab { |
|
|
* @param padChar Character to pad with |
|
|
* @param padChar Character to pad with |
|
|
* @return The padded result |
|
|
* @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 |
|
|
// early out - return unmodified |
|
|
if (toPad.length() >= paddedLength) { |
|
|
if (toPad.length() >= paddedLength) { |
|
|
return toPad; |
|
|
return toPad; |
|
@@ -387,11 +388,78 @@ class Lab6 extends Lab { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
class six9 extends Exercise { |
|
|
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() { |
|
|
public six9() { |
|
|
super(9); |
|
|
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<P Player 2 wins"; } // must be paper |
|
|
|
|
|
break; |
|
|
|
|
|
case PAPER: |
|
|
|
|
|
if (player2 == Throw.SCISSORS) { result = "P<S Player 2 wins"; } |
|
|
|
|
|
else { result = "P>R Player 1 wins"; } // must be rock |
|
|
|
|
|
break; |
|
|
|
|
|
case SCISSORS: |
|
|
|
|
|
if (player2 == Throw.ROCK) { result = "S<R Player 2 wins"; } |
|
|
|
|
|
else { result = "S>P Player 1 wins"; } // must be paper |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
result = "undefined behaviour"; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
writeRPSPlay(player1, player2, result); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getThrowName(Throw thrown) { |
|
|
|
|
|
// This could probably be done in the enum itself |
|
|
|
|
|
switch (thrown) { |
|
|
|
|
|
case ROCK: return six8.stringLPad("rock", 8, ' '); // pad to 'scissors' length so everything stays lined-up |
|
|
|
|
|
case PAPER: return six8.stringLPad("paper", 8, ' '); |
|
|
|
|
|
case SCISSORS: return "scissors"; |
|
|
|
|
|
default: return "uh?"; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void writeRPSPlay (Throw player1, Throw player2, String result) { |
|
|
|
|
|
System.out.printf("Player 1 throws... %s\t\t", getThrowName(player1)); |
|
|
|
|
|
System.out.printf("Player 2 throws... %s\t\t", getThrowName(player2)); |
|
|
|
|
|
System.out.println(String.format("The result is...\t%s", result)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum Throw { |
|
|
|
|
|
ROCK, |
|
|
|
|
|
PAPER, |
|
|
|
|
|
SCISSORS |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/* TOASK - is there a better way of returning multiple values in Java? |
|
|
/* TOASK - is there a better way of returning multiple values in Java? |
|
|
à la python, ie: return (quotient, remainder) |
|
|
à la python, ie: return (quotient, remainder) |
|
|
*/ |
|
|
*/ |
|
|