-
Notifications
You must be signed in to change notification settings - Fork 0
/
StonePaperScissor.java
74 lines (62 loc) · 2.48 KB
/
StonePaperScissor.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
// Building a game, to play stone paper and scissor
// Stone : 1
// Paper : 0
// Scissor : -1
// For win = 2 points
import java.util.Random;
import java.util.Scanner;
public class Stone_Paper_Scissor_Game {
public static void main(String[] args) {
Scanner sh = new Scanner(System.in);
int you = 0;
String compChoice = "";
int cpoints = 0, ypoints = 0;
System.out.println("STONE----PAPER----SCISSOR");
for (int i = 0; i < 5; i++) {
System.out.print("Choose your response (Stone, paper or Scissor): ");
String choice = sh.next();
if (choice.equalsIgnoreCase("stone")) {
you = 1;
} else if (choice.equalsIgnoreCase("paper")) {
you = 0;
} else if (choice.equalsIgnoreCase("scissor")) {
you = -1;
} else {
System.out.println("Invalid input. Please choose Stone, Paper, or Scissor.");
i--; // Repeat this round
continue;
}
Random rand = new Random();
int computer = rand.nextInt(3) - 1;
if (computer == 1) {
compChoice = "Stone";
} else if (computer == 0) {
compChoice = "Paper";
} else {
compChoice = "Scissor";
}
System.out.println("You chose: " + choice.toUpperCase());
System.out.println("Computer chose: " + compChoice.toUpperCase());
if (you == computer) {
System.out.println("It's a draw.");
} else if ((you == 1 && computer == -1) || (you == 0 && computer == 1) || (you == -1 && computer == 0)) {
System.out.println("You WIN, Congratulations.");
ypoints += 2;
} else {
System.out.println("You lose, try again...");
cpoints += 2;
}
}
System.out.print("Final Score: ");
System.out.println("You: " + ypoints + " points");
System.out.println("Computer: " + cpoints + " points");
if (ypoints > cpoints) {
System.out.printf("Voila! You WIN this game.\nCongratulations and Celebrations.");
} else if (ypoints < cpoints) {
System.out.printf("Sorry, you lost.\nIt's a matter of luck,\nI'm sure you will win next time.");
} else {
System.out.println("Unfortunately, it is a draw. Try hard next time.");
}
sh.close();
}
}