Browse Source

(feat) Exercise 6.9 - let user play rock-paper-scissors

We'll let the AI keep playing too

Takes user input as number or word or q to quit
main
Rob Hallam 1 year ago
parent
commit
b3bef68db2
1 changed files with 56 additions and 7 deletions
  1. +56
    -7
      week1.java

+ 56
- 7
week1.java View File

@@ -6,8 +6,8 @@ import java.util.concurrent.ThreadLocalRandom;
class runLabs {
public static void main(String[] args) {
// new Lab4();
new Lab5();
// new Lab6();
// new Lab5();
new Lab6();
// new Lab7();
}
}
@@ -416,7 +416,7 @@ class Lab6 extends Lab {
new six7();
new six8();
new six9();
new six5();
// new six5();
}

/**
@@ -630,15 +630,63 @@ class Lab6 extends Lab {

public six9() {
super(9);
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();
System.out.println(String.format("Playing three games to warm up:"));
playRockPaperScissors(player1, player2);
player2 = randomThrow();
playRockPaperScissors(player1, player2);
player2 = randomThrow();
playRockPaperScissors(player1, player2);
System.out.println(String.format("Grand. Now it's your turn!"));
player1 = getUserThrow();
playRockPaperScissors(player1, player2);
}
}

public Throw getUserThrow() {
Scanner userInput = new Scanner(System.in);
// Throw userThrow = null;

while (true) {
System.out.println(String.format("Please enter your throw:"));
System.out.println(String.format("\t0 (rock), 1 (paper), 2 (scissors)"));
System.out.println(String.format("\tor the word corresponding to the throw you'd like"));
System.out.println(String.format("\t[q / quit] quits"));

// check for number
if (userInput.hasNextInt()) {
int choiceNumber = userInput.nextInt();
if ((choiceNumber < 0) || (choiceNumber > 2)) {
continue;
} else {
userInput.close();
Throw[] throwsArray = Throw.values();
return throwsArray[choiceNumber];
}
} else if (userInput.hasNext()) {
String choiceString = userInput.nextLine();
switch (choiceString.toLowerCase()) {
case "rock":
case "stone":
userInput.close();
return Throw.ROCK;
case "paper":
userInput.close();
return Throw.PAPER;
case "scissors":
userInput.close();
return Throw.SCISSORS;
case "q":
case "quit":
userInput.close();
System.exit(0);
default:
continue;
}
}
}

}

public void playRockPaperScissors (Throw player1, Throw player2) {
@@ -646,6 +694,7 @@ class Lab6 extends Lab {
// for plays in RPS
String result;
// early out - TOASK why this doesn't work when it works in a REPL!!
// answered - umm, I forgot the else >_<
if (player1 == player2) {
result = "a draw";
} else {
@@ -681,9 +730,9 @@ class Lab6 extends Lab {
}

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));
System.out.printf("Player 1 throws... %s\t", getThrowName(player1));
System.out.printf("Player 2 throws... %s\t", getThrowName(player2));
System.out.println(String.format("The result is... %s", result));
}

enum Throw {


Loading…
Cancel
Save