-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors
More file actions
72 lines (57 loc) · 2.07 KB
/
RockPaperScissors
File metadata and controls
72 lines (57 loc) · 2.07 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
import java.util.*;
/* designer Chris Forrest
* This is my first program
* java or otherwise
*/
public class RockPaperScissors {
public static void main(String[] args) {
Scanner Input = new Scanner (System.in);
System.out.println("Hello this my first attempt at building a game from scratch.");
System.out.println("I'm keeping it simple and just doing a rock,paper,scissors game.");
System.out.print("Let's get started please type your name.");
String name = Input.next();
game( name );
System.out.println("Would you like to play again yes or no.");
String replay = Input.next();
while (replay.equals("yes")) {
game( name );
System.out.println( " Would you like to play again yes or no.");
replay = Input.next();
}
if ( replay.equals( "no")) {
System.out.println("Thanks for trying my game. I hope you enjoyed it.");
}
}
public static void game(String name ) {
Random rand = new Random ();
int computerNumber = rand.nextInt(9);
int result = 0 ;
if ( computerNumber <= 2 ) {
result = 1;
}
else if ( computerNumber <= 5 && computerNumber > 2 ) {
result = 2;
}
else if ( computerNumber <= 9 && computerNumber > 5 ) {
result = 3;
}
int compChoice = result;
Scanner Input = new Scanner (System.in);
System.out.println("Alright " + name + " please type a 1 for rock, 2 for paper or 3 for scissors.");
int playerChoice = Input.nextInt();
System.out.println("The computer plays " + result + ".");
if( playerChoice == compChoice ) {
System.out.println( "It's a draw." );
}
if ( playerChoice == 1 && compChoice == 3 ||
playerChoice == 3 && compChoice == 2 ||
playerChoice == 2 && compChoice == 1 ) {
System.out.println("Congratulations " + name + " you win!");
}
if( playerChoice == 1 && compChoice == 2 ||
playerChoice == 2 && compChoice == 3 ||
playerChoice == 3 && compChoice == 1) {
System.out.println("The computer wins.");
}
}
}