-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTalker.java
123 lines (106 loc) · 4.41 KB
/
Talker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package rockPaperScissors;
import java.util.Scanner;
/**
* This class handles i/o: prompts for user input, generates output based on
* parameters
*/
public final class Talker {
/**
* Method prompts for user to enter in a character and handles errors in
* user input. (multiple characters could be entered but only the first
* matters.) If an invalid character is entered the method prompts the user
* again and again until a valid character is entered.
*
* @return The character is returned as a String
*/
public static String promptInput() {
// variable for user input
Scanner keyboard = new Scanner(System.in);
boolean proceed = false;
String moveString = "";
while (!proceed) {
try {
// Prompt for input and store said input in a variable
System.out.print("Throw either 'R', 'P', 'S', 'L', or 'K' (enter 'Z' to end): ");
moveString = keyboard.next();
moveString = filter(moveString);
proceed = true;
} catch (IllegalArgumentException e) {
System.out.println("Your input is invalid, try again.");
proceed = false;
}
}
return moveString;
}
/**
* prints out welcome text and instructions
*/
public static void printWelcomeText() {
System.out.println("Welcome to Rock, Paper, Scissors, Lizard, Spock");
System.out.println("\nHere are the rules:\n");
System.out.println("1. Rock beats scissors and Lizard");
System.out.println("2. Paper beats spock and Rock");
System.out.println("3. Scissors beats paper and lizard");
System.out.println("4. Lizard beats spock and paper");
System.out.println("5. Spock beats rock and scissors");
System.out.println("6. If two of the same move are played, its a tie");
System.out.println("\nHave fun!\n");
}
public static void printEndgameInfo() {
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
System.out.println("\nThanks for playing.\n");
System.out.println("----------------------------------------");
}
/**
* Prints output based on the outcome of a particular round
*
* @param humanMove
* @param computerMove
* @param result
* @param reason
*/
public static void printRoundResult(Move humanMove, Move computerMove, String result, String reason) {
// Output player's moves and the result, move histories, and overall
// win-ties-loss
System.out.println("YOUR MOVE: " + humanMove.name + "; AI MOVE: " + computerMove.name + "; RESULT: " + result);
System.out.println("Reason: " + reason);
System.out.println("----------------------------------------------------");
}
/**
* Prints out results based on results
*
* @param wins
* @param ties
* @param losses
* @param rounds
*/
public static void printResults(int wins, int ties, int losses, double rounds) {
// Results
System.out.println("----------------------------------------");
System.out.println("ROUNDS PLAYED: " + rounds);
System.out.println("WINS: " + wins + " TIES: " + ties + " LOSSES: " + losses);
System.out.println("WINS/ROUNDS: " + ((double) wins / rounds) * 100 + "%");
System.out.println("TIES/ROUNDS: " + ((double) ties / rounds) * 100 + "%");
System.out.println("AI WIN-RATE: " + ((double) losses / rounds) * 100 + "%");
}
/**
* Converts the string entered to uppercase
*
* @param s
*/
public static String filter(String s) {
int firstCharToInt = (int) s.charAt(0);
// Check if its a letter
if (!(firstCharToInt >= 97 && firstCharToInt <= 122) && !(firstCharToInt >= 65 && firstCharToInt <= 90))
throw new IllegalArgumentException();
// If its a lowercase letter convert it to uppercase
if (firstCharToInt >= 97 && firstCharToInt <= 122)
firstCharToInt -= 32;
String firstCharAsString = "" + (char) firstCharToInt;
if (!firstCharAsString.equals("Z") && !Move.ALL_MOVES.contains(firstCharAsString))
throw new IllegalArgumentException();
// convert back to String
return "" + (char) firstCharToInt;
}
}