-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRockPaperScissors.java
52 lines (44 loc) · 1.4 KB
/
RockPaperScissors.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
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class RockPaperScissors
{
public static int getIndexOf(String[] strings, String item)
{
for (int i = 0; i < strings.length; i++)
{
if (item.equals(strings[i])) return i;
}
return -1;
}
public static int input(String[] strings)
{
Scanner input = new Scanner(System.in);
String choice = "null";
while (! Arrays.asList(strings).contains(choice))
{
if (choice != "null") System.out.println("Invalid input");
choice = input.next();
}
return getIndexOf(strings,choice);
}
public static void main(String args[])
{
String[] options = {"Rock" , "Paper" , "Scissors"};
String[] boolReply = {"No" , "Yes"};
String[] endStates = {"Tied" , "Lose" , "Win"};
Random rand = new Random();
Boolean repeat = true;
while (repeat)
{
int atkMove = rand.nextInt(3);
System.out.println("Rock, Paper, Scissors, Shoot! (Type Rock, Paper, or Scissors)");
int defMove = input(options);
System.out.printf("Your opponent chose %s!%n", options[atkMove]);
System.out.printf("You %s!%n",endStates[(9+atkMove-defMove)%3]);
System.out.println("Would you like to go again?(Yes/No)");
repeat = (input(boolReply) != 0);
}
System.out.println("Well thanks for playing!");
}
}