Repository navigation
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
136 lines (116 loc) · 4.55 KB
/
Copy pathRockPaperScissors.java
File metadata and controls
136 lines (116 loc) · 4.55 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
/*
* This is a game of rock paper scissors that collects the player's input and compares it to a random choice of the computer.
* It uses String and arrays to store the data of the player and computer choice as well as their wins and loses.
*/
public class RockPaperScissors {
private String playerChoice, computerChoice;
private String winner;
private int amtPlayerWins = 0, amtComputerWins = 0;
private String[] playerWins = new String[3];
private String[] computerWins = new String[3];
private boolean playAgain;
private int round = 0;
private boolean tie = false;
private Input input = new Input();
// Acts as the "main" method for this game class:
public void PlayGame() throws InterruptedException {
do {
ResetGame();
System.out.println("\n~~ Welcome to a game of Rock Paper Scissors! ~~");
System.out.println("You will play against us for a chance to win. Highest of three: wins!");
do {
System.out.println("\nRound " + (round + 1));
System.out.println("----------");
GetPlayerChoice();
AssignCompChoice();
tie = CheckPlay(round);
round++;
System.out.println("\nOur weapon: " + computerChoice);
System.out.println(winner);
// If there is a tie, the round will repeat:
if (tie) {
if (round == 1) {
round = 0;
} else {
round--;
}
}
} while (round < 3);
CheckWinner();
DisplayPlayerScores();
playAgain = input.PlayAgain();
} while (playAgain);
}
// Method to collect and evaluate the player's input:
public void GetPlayerChoice() {
do {
System.out.println("Choose your weapon:");
System.out.println("Rock, Paper, or Scissors.");
playerChoice = input.sc.nextLine();
if (!playerChoice.equalsIgnoreCase("Rock") && !playerChoice.equalsIgnoreCase("Paper")
&& !playerChoice.equalsIgnoreCase("Scissors")) {
System.out.println("Invalid weapon choice.\n");
playerChoice = "InvalidChoice";
}
} while (playerChoice.equals("InvalidChoice"));
}
// Assigns a choice for the computer based on a random number:
public void AssignCompChoice() {
switch (input.GetRandomNum(0, 2)) {
case 0 -> computerChoice = "Rock";
case 1 -> computerChoice = "Paper";
case 2 -> computerChoice = "Scissors";
}
}
// Checks for round winner based on player input and computer choice:
public boolean CheckPlay(int round) {
if (playerChoice.equalsIgnoreCase(computerChoice)) {
winner = "Tie!";
return true;
} else if (playerChoice.equalsIgnoreCase("Rock") && computerChoice.equals("Scissors")) {
PlayerWin(round);
return false;
} else if (playerChoice.equalsIgnoreCase("Paper") && computerChoice.equals("Rock")) {
PlayerWin(round);
return false;
} else if (playerChoice.equalsIgnoreCase("Scissors") && computerChoice.equals("Paper")) {
PlayerWin(round);
return false;
} else {
winner = "We win this round.";
computerWins[round] = "Win";
amtComputerWins++;
playerWins[round] = "Lose";
return false;
}
}
public void PlayerWin(int round) {
winner = "You win this round! ~ *";
playerWins[round] = "Win";
amtPlayerWins++;
computerWins[round] = "Lose";
}
// Checks for final winner based on the number of wins from player and computer:
public void CheckWinner() {
if (amtPlayerWins > amtComputerWins) {
System.out.println("\n* ~ You Win! ~ *");
} else {
System.out.println("\nx ~ We Won! Better luck next time. ~ x");
}
}
// Method displays the player's game scores to show which rounds they won and
// lost:
public void DisplayPlayerScores() {
System.out.println("\nYour Game Scores:");
for (int i = 0; i < playerWins.length; i++) {
System.out.println("Round " + (i + 1) + ": " + playerWins[i]);
}
}
public void ResetGame() {
round = 0;
amtComputerWins = 0;
amtPlayerWins = 0;
winner = " ";
playAgain = false;
}
}