-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC9 Rock paper scissors.java
99 lines (94 loc) · 2.13 KB
/
C9 Rock paper scissors.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
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner scan = new Scanner (System.in);
System.out.println ("[- Rock Paper Scissors -]");
System.out.println ("Are you ready to play ? (y or n):");
String choice = scan.next ();
switch (choice)
{
case "y":
int gotDraw = draw ();
System.out.println
("Enter Your choice: \nRock (R), Paper (P), Scissors (S): ");
String userDraw = scan.next ();
checkWin (gotDraw, userDraw);
break;
case "n":System.out.println ("Game closed!");
System.exit (0);
break;
default:System.exit (0);
}
}
public static int draw ()
{
double draw = (Math.random () * 3) + 1;
return (int) draw;
}
// num == Integer.parseInt(str) convert string to num.
public static void checkWin (int gotDraw, String userDraw)
{
int uDraw = 0;
switch (userDraw)
{
case "R":
uDraw = 1;
break;
case "P":
uDraw = 2;
break;
case "S":
uDraw = 3;
break;
default:
System.out.println ("User's choice is invalid!");
System.exit (0);
}
System.out.println ("Your Choice: " + userDraw);
switch (gotDraw)
{
case 1:
System.out.println ("Computer Choice: R (Rock)");
break;
case 2:
System.out.println ("Computer Choice: P (Paper)");
break;
case 3:
System.out.println ("Computer Choice: S (Scissor)");
break;
default:
System.exit (0);
}
System.out.println ("\n\n\n");
if (gotDraw == uDraw)
{
System.out.println ("Game Draw!");
}
else if (gotDraw == 1 && uDraw == 2)
{
System.out.println ("Congrats!, You won!");
}
else if (gotDraw == 1 && uDraw == 3)
{
System.out.println ("Sorry!, You lose!");
}
else if (gotDraw == 2 && uDraw == 1)
{
System.out.println ("Sorry!, You lose!");
}
else if (gotDraw == 2 && uDraw == 3)
{
System.out.println ("Congrats!, You won!");
}
else if (gotDraw == 3 && uDraw == 1)
{
System.out.println ("Congrats!, You won!");
}
else if (gotDraw == 3 && uDraw == 2)
{
System.out.println ("Sorry!, You lose!");
}
}
}