-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS.java
178 lines (147 loc) · 5.37 KB
/
RPS.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* This program plays a computerized version of Rock-Paper-Scissors(RPS),
* between the computer and a single player.
* And show a summary about winning rate at the end.
*/
import java.util.Random;
import java.util.Scanner;
public class RPS {
static Scanner in1 = new Scanner(System.in);
static Scanner in2 = new Scanner(System.in);
static Scanner in0 = new Scanner(System.in);
// function: Get player's name from console.
// arguments: (void)
// return-value: (void)
public static void getName(){
System.out.print("> Input your name: ");
String name = in1.nextLine();
System.out.println("Hello, " + name + "!");
System.out.println("Let's play Rock-Paper-Scissors!");
System.out.println();
}
// function: Get player's throw(int, not string) from console.
// arguments: (void)
// return-value: number_player
public static int getPlayerThrow(){
System.out.print("> Throw and enters a word(Rock, Paper, Scissors): ");
Boolean isBadWord = true;
Boolean isFirstTry = true;
int number_player = -1;
while(isBadWord){
if(!isFirstTry){
System.out.println("You did not say Rock, Paper, or Scissors! No fair!");
System.out.println();
System.out.print("> Throw and enters a word: ");
}
String throw_player = in2.nextLine();
number_player = RPSToNumber(throw_player);
isFirstTry = false;
if(number_player != -1){
isBadWord = false;
}
}
return number_player;
}
// function: Get computer's throw(int, not string) using Random.
// arguments: (void)
// return-value: number_computer
public static int getComputerThrow(){
Random r = new Random();
int number_computer = r.nextInt(3);
return number_computer;
}
// function: Convert a single number to a RPS throw, just like 0 -> "Rock".
// arguments: number
// return-value: throw_s
public static String numberToRPS(int number){
String throw_s = "";
if(number == 0){
throw_s = "Rock";
}else if(number == 1){
throw_s = "Paper";
}else if(number == 2){
throw_s = "Scissors";
}
return throw_s;
}
// function: Convert a RPS throw to a single number, just like "Rock" -> 0.
// arguments: throw_s
// return-value: number
public static int RPSToNumber(String throw_s){
int number = 0;
if(throw_s.equalsIgnoreCase("Rock")){
number = 0;
}else if(throw_s.equalsIgnoreCase("Paper")){
number = 1;
}else if(throw_s.equalsIgnoreCase("Scissors")){
number = 2;
}else{
number = -1;
}
return number;
}
// function: Print player's and computer's throw.
// arguments: playerThrow, computerThrow
// return-value: (void)
public static void printThrow(int playerThrow, int computerThrow){
System.out.println("* The computer picked: " + numberToRPS(computerThrow));
System.out.println("* You picked: " + numberToRPS(playerThrow));
}
// function: Decide winner and return the winner-flag.
// arguments: playerThrow, computerThrow
// return-value: result as winner-flag.
public static int decideWinnner(int playerThrow, int computerThrow){
int result = 0;
int temp = playerThrow - computerThrow;
if(temp == 0){
result = 0;
}else if(temp == 1 || temp == -2){
result = 1;
}else if(temp == -1 || temp == 2){
result = -1;
}
return result;
}
// function: Print the winner in console.
// arguments: result
// return-value: (void)
public static void printWinnner(int result){
if(result == 0){
System.out.println("a Tie");
}else if(result == -1){
System.out.println("Computer wins!");
}else if(result == 1){
System.out.println("You win!");
}
}
// function: Calculate and show the summary of the game in console.
// arguments: countTotalTimes, countYouWin
// return-value: (void)
public static void summary(int countTotalTimes, int countYouWin){
double winningRatePlayer = (1.0 *countYouWin) / (1.0 * countTotalTimes) * 100;
System.out.println();
System.out.println("===== Summary ====");
System.out.println("You've tried " + countTotalTimes +" times.");
System.out.println("Your winning rate: "+winningRatePlayer+ "%");
}
public static void main(String[] args){
getName();
System.out.print("> Input the times you want to play: ");
int countTotalTimes = in0.nextInt();
int countYouWin = 0;
for(int i = 0; i < countTotalTimes; i++){
System.out.println();
System.out.println("[No." + (i+1) + " Time]");
int playerThrow = getPlayerThrow();
int computerThrow = getComputerThrow();
printThrow(playerThrow, computerThrow);
int winner = decideWinnner(playerThrow, computerThrow);
if(winner == 1){
countYouWin++;
}
printWinnner(winner);
}
summary(countTotalTimes, countYouWin);
System.out.println("Goodbye:)");
}
}