-
Notifications
You must be signed in to change notification settings - Fork 8
/
rock_paper_scissors_game.java
116 lines (86 loc) · 3.66 KB
/
rock_paper_scissors_game.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//Java program for popular Rock, Paper, Scissors Game
//Importing the Random class of util package
import java.util.Random;
//Importing the Scanner class of util package
import java.util.Scanner;
//Main Class of the program
public class Main{
//Function to generate the computer choice
public static String generateComputerChoice( Random random){
int wordNumber;
//Choosing a random number using the inbuilt function
wordNumber = random.nextInt( 3 ) + 1;
String computerChoice = "";
//Getting computer choice on the random number
if( wordNumber == 1 ){
computerChoice = "rock";
}else if( wordNumber == 2 ){
computerChoice = "paper";
}else if( wordNumber == 3 ){
computerChoice = "scissors";
}
System.out.println( "\nThe Computer already made it's choice" );
return computerChoice;
}
//Function to show the list of available options
public static void showMenu(){
System.out.println( "Options to choose from :\n1.Rock\n2.Paper\n3.Scissors" );
}
//function to get the user choice
public static String getUserChoice( Scanner scanner ){
String userWordChoice = "";
System.out.print( "\nPlease make yours : " );
userWordChoice = scanner.nextLine();
//Returning the user choice
return userWordChoice;
}
//Function to get the user
public static String chooseWinner( String computerChoice, String userChoice ){
String winner = "No Winner";
String customMessage = "Both choose same";
String finalMessage = "";
String rockCustomMessage = "The rock smashes the scissor";
String scissorsCustomMessage = "Scissors cuts paper";
String paperCustomMessage = "Paper wraps rock";
//Winner Logic for the game start
if( computerChoice.equals( "rock" ) && userChoice.equalsIgnoreCase( "scissors" ) ){
winner = "Computer";
customMessage = rockCustomMessage;
}else if( userChoice.equalsIgnoreCase( "rock" ) && computerChoice.equals( "scissors" ) ){
winner = "User";
customMessage = rockCustomMessage;
}
if( computerChoice.equals( "scissors" ) && userChoice.equalsIgnoreCase( "paper" ) ){
winner = "Computer";
customMessage = scissorsCustomMessage;
}else if( userChoice.equalsIgnoreCase( "scissors" ) && computerChoice.equals( "paper" ) ){
winner = "User";
customMessage = scissorsCustomMessage;
}
if( computerChoice.equals( "paper" ) && userChoice.equalsIgnoreCase( "rock" ) ){
winner = "Computer";
customMessage = scissorsCustomMessage;
}else if( userChoice.equalsIgnoreCase( "paper" ) && computerChoice.equals( "rock" ) ){
winner = "User";
customMessage = paperCustomMessage;
}
//Winner Logic for the game ends
finalMessage = winner + " won ( " + customMessage + " )";
//Returning the final message
return finalMessage;
}
//Main Function of the program
public static void main( String [] args ){
Random random = new Random();
Scanner scanner = new Scanner(System.in);
String computerChoice;
String userChoice;
String winner;
showMenu();
computerChoice = generateComputerChoice( random );
userChoice = getUserChoice( scanner );
winner = chooseWinner( computerChoice, userChoice );
System.out.println( "\nYou choose : " + userChoice + "\nComputer choose : " +computerChoice );
System.out.println( winner );
}
}