-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathRock_Paper_Scissor.java
67 lines (59 loc) · 2.16 KB
/
Rock_Paper_Scissor.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
import java.util.*;
public class Game
{
public static final String ROCK = "ROCK";
public static final String PAPER = "PAPER";
public static final String SCISSORS = "SCISSORS";
public static void main(String args[])
{
System.out.println("Enter any one of the following inputs: ");
System.out.println("ROCK");
System.out.println("PAPER");
System.out.println("SCISSORS");
System.out.println();
String playerMove = getPlayerMove();
String computerMove = getComputerMove();
//Rules of the Game Applied Below:
/*if both playerMove and computerMove
produces the same formation, then
Game is a tie*/
if (playerMove.equals(computerMove))
System.out.println("Game is Tie !!");
// if playerMove is ROCK
else if (playerMove.equals(Game.ROCK))
System.out.println(computerMove.equals(Game.PAPER) ? "Computer Wins": "Player wins");
// if playerMove is PAPER
else if (playerMove.equals(Game.PAPER))
System.out.println(computerMove.equals(Game.SCISSORS) ? "Computer Wins": "Player wins");
// if playerMove is SCISSORS
else
System.out.println(computerMove.equals(Game.ROCK) ? "Computer Wins": "Player wins");
}
/* Get Computer's move using Random
class nextInt() method */
public static String getComputerMove()
{
String computermove;
Random random = new Random();
int input = random.nextInt(3)+1;
if (input == 1)
computermove = Game.ROCK;
else if(input == 2)
computermove = Game.PAPER;
else
computermove = Game.SCISSORS;
System.out.println("Computer move is: " + computermove);
System.out.println();
return computermove;
}
/* Get Player's move using Scanner
class */
public static String getPlayerMove()
{
Scanner in = new Scanner(System.in);
String input = in.next();
String playermove = input.toUpperCase();
System.out.println("Player move is: "+ playermove);
return playermove;
}
}