-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNimGame.java
More file actions
221 lines (173 loc) · 5.68 KB
/
NimGame.java
File metadata and controls
221 lines (173 loc) · 5.68 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Date: 10/14/2020
* Title: Nim Game
* Author: Andrey Zinovyev
* Description: A game in which 2 players take turns to remove starts from a pile,
* once someone has to take the last one they lose!
*/
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class NimGame {
public static void main(String[] args) {
// Make scanner.
Scanner reader = new Scanner(System.in);
// Create a random object to randomize the pile sizes.
Random rn = new Random();
// Variables.
String playerOne, playerTwo, pickPile;
int currentPlayer = 1;
int A, B, C;
int remove = 0;
// Number of nims limit.
int nim = 10;
// Randomly generate numbers from 1-9 in each pile.
A = rn.nextInt(nim) + 1;
B = rn.nextInt(nim) + 1;
C = rn.nextInt(nim) + 1;
System.out.println("Welcome to Nim! Please type in your names!");
// Get player names.
System.out.print("Player 1, what is your name? ");
playerOne = reader.nextLine();
System.out.print("Player 2, what is your name? ");
playerTwo = reader.nextLine();
// Loop which will keep the game going until there are no piles left.
while (A != 0 || B != 0 || C != 0) {
// Fancy display (row).
System.out.print("A: ");
for (int j = 0; j < A; j++) {
System.out.print("*");
}
System.out.print(" (" + A + ")");
System.out.println();
System.out.print("B: ");
for (int j = 0; j < B; j++) {
System.out.print("*");
}
System.out.print(" (" + B + ")");
System.out.println();
System.out.print("C: ");
for (int j = 0; j < C; j++) {
System.out.print("*");
}
System.out.print(" (" + C + ")");
System.out.println();
// System.out.println("A: " + A + " B: " + B + " C: " + C);
// Asks player chronologically which pile to take and how much.
if (currentPlayer == 1) {
System.out.print(playerOne + ", please choose a pile: ");
}
else if (currentPlayer == 2) {
System.out.print(playerTwo + ", please choose a pile: ");
}
pickPile = reader.nextLine();
pickPile = pickPile.toUpperCase();
// Check is user said to take 0 from pile.
while (pickPile.equals("A") && A == 0 || pickPile.equals("B") && B == 0|| pickPile.equals("C") && C == 0) {
System.out.println("Nice try, but that pile is already empty!");
pickPile = reader.nextLine();
pickPile = pickPile.toUpperCase();
}
// Check is user did not enter a pile.
while (!pickPile.equals("A") && !pickPile.equals("B") && !pickPile.equals("C")) {
System.out.println("You have entered an invalid input, try again.");
pickPile = reader.nextLine();
pickPile = pickPile.toUpperCase();
}
pickPile = pickPile.toUpperCase();
//
boolean mayError = true;
boolean errorCaught = false;
// Check if user has typed in a non-int when removing from pile.
while (mayError) {
try {
System.out.println("How much do you want to remove from pile " + pickPile + "?");
remove = reader.nextInt();
errorCaught = false;
mayError = false;
}
catch (InputMismatchException e) {
errorCaught = true;
}
if (errorCaught == false) {
mayError = false;
}
if (errorCaught == true) {
System.out.println("That is a not a number! Please enter an Integer");
reader.nextLine();
}
}
// Check if the number entered is not in range.
while ((pickPile.equals("A") && remove > A) || (pickPile.equals("B") && remove > B) || (pickPile.equals("C") && remove > C) || remove <= 0 ) {
System.out.println("Make sure you are entering a number in the range of the pile!");
mayError = true;
errorCaught = false;
while (mayError) {
// Ask for user's input, if it is oka
try {
System.out.println("How much do you want to remove from pile " + pickPile + "?");
remove = reader.nextInt();
errorCaught = false;
mayError = false;
}
// Catch if the user inputs a wrong type of input and ask again
catch (InputMismatchException e) {
errorCaught = true;
}
if (errorCaught == false) {
mayError = false;
}
if (errorCaught == true) {
System.out.println("That is a not a number! Please enter an Integer");
reader.nextLine();
}
}
}
// Update the piles.
if (pickPile.equals("A")) {
A -= remove;
}
else if (pickPile.equals("B")) {
B -= remove;
}
else if (pickPile.equals("C")) {
C -= remove;
}
// Alternate players.
if (currentPlayer == 1) {
currentPlayer = 2;
}
else if (currentPlayer == 2) {
currentPlayer = 1;
}
// Check if there is only one nim left to check for loser.
int pileSum = A + B + C;
if (pileSum == 1) {
// Update fancy display.
System.out.print("A: ");
for (int j = 0; j < A; j++) {
System.out.print("*");
}
System.out.print(" (" + A + ")");
System.out.println();
System.out.print("B: ");
for (int j = 0; j < B; j++) {
System.out.print("*");
}
System.out.print(" (" + B + ")");
System.out.println();
System.out.print("C: ");
for (int j = 0; j < C; j++) {
System.out.print("*");
}
System.out.print(" (" + C + ")");
System.out.println();
System.out.println();
System.out.print("Player " + currentPlayer + " you have to pick the last NIM, you lose.");
break;
}
reader.nextLine();
}
reader.close();
}
}