This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
forked from jmor1591/Game-of-Nim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.java
167 lines (144 loc) · 5.8 KB
/
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.util.Random;
import java.util.Scanner;
public class Game {
private Player player1;
private Player player2;
private Player activePlayer;
private boolean finished; // initial value is false; we don't need to include it in constructor
private int currPlayer;
// Constructor for Game
public Game() {
player1 = new Player();
player2 = new Player();
}
public void play() {
// Adds the pieces to play Nim
Board.populate();
// Sets the maximum guess a player can make
Board.setMaxGuess();
// Choose first player using randomizer
Random random = new Random();
currPlayer = random.nextInt(2) + 1;
// Sets current player based on results of randomizer
if (currPlayer == 1) {
activePlayer = player1;
System.out.println(player1.getName() + " was chosen to go first!");
} else {
activePlayer = player2;
System.out.println(player2.getName() + " was chosen to go first!");
}
// Loops until there is one piece left
while (Board.getPieces() > 1) {
int pieces = Board.getPieces();
int maxGuess = Board.getMaxGuess();
// initialize new scanner that listens to system input
Scanner sc = new Scanner(System.in);
System.out.println("It is " + activePlayer.getName() + "'s turn.");
System.out.println("There are " + pieces + " pieces remaining.");
// If else statements for grammatically correct responses
if (maxGuess == 1) {
System.out.println("You can remove only " + maxGuess + " piece.");
} else {
System.out.println("You can remove up to " + maxGuess + " pieces.");
}
// Prompt user for guess
System.out.println("How many pieces would you like to remove?");
// Store guess in variable
int guess = sc.nextInt();
// If the guess is valid
if (isValid(guess)) {
// Remove pieces, set max guess, and advance turn
Board.removePieces(guess);
Board.setMaxGuess();
advanceTurn();
} else {
// While the guess is invalid, reprompt user for response
while (!isValid(guess)) {
System.out.println("Sorry, that isn't a valid value.");
System.out.println(
"Please type a guess up to " + Board.getMaxGuess() + " pieces.");
guess = sc.nextInt();
}
// Remove pieces, set max guess, and advance turn
Board.removePieces(guess);
Board.setMaxGuess();
advanceTurn();
}
}
// If the current player is even, player1 wins the round. Otherwise, player2 wins the round
if (currPlayer % 2 == 0) {
player1.incrScore();
System.out.println(player1.getName() + " won the round!");
} else {
player2.incrScore();
System.out.println(player2.getName() + " won the round!");
}
// Prompts user to play again
finished = isFinished();
// If they want to play again, run Game.play() again
if (finished == false) {
play();
} else {
// Print out scores
System.out.println(player1.getName() + ": " + player1.getScore());
System.out.println(player2.getName() + ": " + player2.getScore());
// Compare scores and print out the corresponding winner. Otherwise, print that it's a
// tie
if (player1.getScore() > player2.getScore()) {
System.out.println(player1.getName() + " won the game!");
} else if (player2.getScore() > player1.getScore()) {
System.out.println(player2.getName() + " won the game!");
} else {
System.out.println("It's a tie!");
}
}
}
private boolean isFinished() {
// Ask user if they want to play again
System.out.println("Would you like to play again? (Y/N)");
// Initialize scanner
Scanner sc = new Scanner(System.in);
// Read next line for response
String userInput = sc.nextLine();
// While the input is different from the provided responses, reprompt to play
// again
while (!(userInput.trim().toUpperCase().equals("Y")
|| userInput.trim().toUpperCase().equals("N"))) {
System.out.println("Input was incorrect.\n\nWould you like to play again? (Y/N)");
userInput = sc.nextLine();
}
// If they do, return true
if (userInput.trim().toUpperCase().equals("Y")) {
System.out.println("Response: Yes");
return false;
}
// If they don't, return false
if (userInput.trim().toUpperCase().equals("N")) {
System.out.println("Response: No");
return true;
}
// If none of this code works it defaults to true
System.out.println("Code didn't work lol");
return true;
}
private void advanceTurn() {
// Increase currPlayer
currPlayer++;
// If currPlayer is odd, the active player is player1, otherwise the active player is
// player2
if (currPlayer % 2 == 1) {
activePlayer = player1;
} else {
activePlayer = player2;
}
}
private boolean isValid(int num) {
// If the number is more than the max amount of guesses, return true. Otherwise, return
// false
if (num <= Board.getMaxGuess()) {
return true;
} else {
return false;
}
}
}