-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStonePaperScissors.java
More file actions
152 lines (136 loc) · 5.82 KB
/
Copy pathStonePaperScissors.java
File metadata and controls
152 lines (136 loc) · 5.82 KB
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
import java.util.Random;
import java.util.Scanner;
public class StonePaperScissors {
public static String name(int c) {
Scanner sc = new Scanner(System.in);
String player = "";
if (c == 1) {
System.out.print("\nType your name: ");
player = sc.nextLine();
} else {
String[] players = new String[2];
System.out.print("\nType name of player 1: ");
players[0] = sc.nextLine();
System.out.print("Type name of player 2: ");
players[1] = sc.nextLine();
player = players[0] + "," + players[1];
}
return player;
}
public static void play(int c, String player) {
String[] lst = {"stone", "paper", "scissor"};
int score1 = 0, score2 = 0;
Random rand = new Random();
Scanner sc = new Scanner(System.in);
if (c == 1) {
int a = 1;
while (a == 1) {
System.out.println("\n1. stone\n2. paper\n3. scissor");
System.out.print("Enter your choice [1/2/3]: ");
int playerChoice = sc.nextInt();
// Computer's choice (randomly generated)
int compChoice = rand.nextInt(3) + 1;
System.out.println("Computer chose: " + lst[compChoice - 1]);
System.out.println("You chose: " + lst[playerChoice - 1]);
if (playerChoice == compChoice) {
System.out.println("It is a tie");
} else if (playerChoice > compChoice) {
if (playerChoice == 3 && compChoice == 1) {
System.out.println("Computer won");
score2++;
} else {
System.out.println("You won");
score1++;
}
} else {
if (playerChoice == 1 && compChoice == 3) {
System.out.println("You won");
score1++;
} else {
System.out.println("Computer won");
score2++;
}
}
System.out.print("Do you wish to continue? (yes-->1 , no-->2): ");
a = sc.nextInt();
if (a == 2) {
System.out.println("Your score = " + score1);
System.out.println("Computer score = " + score2);
if (score1 > score2) {
System.out.println("Congratulations " + player + ", you won");
} else if (score2 > score1) {
System.out.println("Better luck next time");
} else {
System.out.println("It is a tie");
}
}
}
} else {
int a = 1;
String player1 = player.split(",")[0];
String player2 = player.split(",")[1];
while (a == 1) {
System.out.println("\n1. stone\n2. paper\n3. scissor");
System.out.print(player1 + " Enter your choice [1/2/3]: ");
int player1Choice = sc.nextInt();
System.out.print(player2 + " Enter your choice [1/2/3]: ");
int player2Choice = sc.nextInt();
System.out.println(player1 + " chose: " + lst[player1Choice - 1]);
System.out.println(player2 + " chose: " + lst[player2Choice - 1]);
if (player1Choice == player2Choice) {
System.out.println("It is a tie");
} else if (player1Choice > player2Choice) {
if (player1Choice == 3 && player2Choice == 1) {
System.out.println(player2 + " won");
score2++;
} else {
System.out.println(player1 + " won");
score1++;
}
} else {
if (player1Choice == 1 && player2Choice == 3) {
System.out.println(player1 + " won");
score1++;
} else {
System.out.println(player2 + " won");
score2++;
}
}
System.out.print("Do you wish to continue? (yes-->1 , no-->2): ");
a = sc.nextInt();
if (a == 2) {
System.out.println(player1 + " score = " + score1);
System.out.println(player2 + " score = " + score2);
if (score1 > score2) {
System.out.println("Congratulations " + player1 + ", you won");
} else if (score2 > score1) {
System.out.println("Congratulations " + player2 + ", you won");
} else {
System.out.println("It is a tie");
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
System.out.println("Welcome to stone paper scissor!");
int ch;
System.out.print("(1) 1 player\n(2) 2 player\nEnter your choice[1/2]: ");
ch = sc.nextInt();
if (ch != 1 && ch != 2) {
System.out.print("Invalid choice\n");
System.out.print("Enter your choice[1/2]: ");
ch = sc.nextInt();
}
String player;
if (ch == 1) {
player = name(ch);
play(ch, player);
} else {
player = name(ch);
play(ch, player);
}
}
}