Java Kernel for Jupyter Notebooks.
[Install Java kernel readme}(https://github.com/SpencerPark/IJava). Java will require an independent kernel in Jupyter Notebooks. The instruction performed by the Teacher follows, but look to readme if you have troubles.
1
2
3
4
5
6
7
(base) id:~$ wget https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip # download IJava kernel as zip
(base) id:~$ unzip ijava-1.3.0.zip # unzip downloaded IJava kernel
(base) id:~$ python install.py --user # install IJava kernel
(base) id:~$ jupyter kernelspec list # list kernels
Available kernels:
java /home/shay/.local/share/jupyter/kernels/java
python3 /home/shay/.local/share/jupyter/kernels/python3
Console Game Menu
College Boards Units #1, #3, and #4 and Free Response Methods and Control Structures are built into these labs. Of course, these games are very popular in beginning programming. They are here for reference, as they were shared by a student.
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class ConsoleGame {
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color
public ConsoleGame() {
Scanner sc = new Scanner(System.in); // using Java Scanner Object
boolean quit = false;
while (!quit) {
this.menuString(); // print Menu
try {
int choice = sc.nextInt(); // using method from Java Scanner Object
System.out.print("" + choice + ": ");
quit = this.action(choice); // take action
} catch (Exception e) {
sc.nextLine(); // error: clear buffer
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
public void menuString(){
String menuText = ""
+ "\u001B[35m___________________________\n"
+ "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
+ "|\u001B[0m Menu! \u001B[35m|\n"
+ "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
+ "| 0 - Exit |\n"
+ "| 1 - Rock Paper Scissors |\n"
+ "| 2 - Higher or Lower |\n"
+ "| 3 - Tic Tac Toe |\n"
+ "|_________________________| \u001B[0m\n"
+ "\n"
+ "Choose an option.\n"
;
System.out.println(menuText);
}
private boolean action(int selection) {
boolean quit = false;
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye, World!");
quit = true;
break;
case 1:
rps();
break;
case 2:
horl();
break;
case 3:
ticTacToe();
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
public void horl(){
System.out.println("Higher or Lower");
System.out.println("You have three guesses to guess the number I am thinking of between 1-8.");
System.out.println("If you guess the number correctly, you win!");
Scanner scHL = new Scanner(System.in);
int randomG = (int) (Math.random() * 8) + 1;
int guess = scHL.nextInt();
for(int i = 3; i > 0; i--){
if(guess == randomG){
System.out.println("You win!");
break;
}
else if(guess > randomG){
System.out.println("The number is lower");
}
else if(guess < randomG){
System.out.println("The number is higher");
}
guess = scHL.nextInt();
}
System.out.println("Game over.");
scHL.close();
}
public void rps(){
System.out.println("Rock Paper Scissors");
System.out.println("Type r for rock, p for paper, or s for scissors");
Scanner scRPS = new Scanner(System.in);
String userChoice = scRPS.nextLine().toLowerCase();
Boolean quit = false;
int random = (int) (Math.random() * 3);
while(quit == false){
if(userChoice.equals("r")){
if(random == 1){
System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
}
else if(random == 2){
System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
}
else{
System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
}
quit = true;
}
else if(userChoice.equals("p")){
if(random == 1){
System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
}
else if(random == 2){
System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
}
else{
System.out.println("You chose paper \nThe computer chose rock \nYou win!");
}
quit = true;
}
else if(userChoice.equals("s")){
if(random == 1){
System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
}
else if(random == 2){
System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
}
else{
System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
}
quit = true;
}
else{
System.out.println("Invalid input, try again");
userChoice = scRPS.nextLine();
}
}
scRPS.close();
}
public void ticTacToe(){
System.out.println("Tic Tac Toe");
Scanner scTTT = new Scanner(System.in);
String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
String player = "X";
String player2 = "O";
int turn = 0;
Boolean quit = false;
System.out.println("Do you want to play against a friend or the computer?");
System.out.println("Type 1 for friend, 2 for computer");
int choice = scTTT.nextInt();
//make tic tac toe using player1 and player2
if(choice == 1){
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Player 2's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = scTTT.nextInt();
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = player2;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
}
}
}
}
}
if(choice == 2){
String computer = "O";
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Computer's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = (int)(Math.random() * 9) + 1;
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = computer;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
}
}
}
}
}
scTTT.close();
}
static public void main(String[] args) {
new ConsoleGame(); // starting Menu object
}
}
ConsoleGame.main(null);
1
2
3
4
5
6
7
8
9
10
11
[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
Hacks
To start the year, I want you to consider a simple Java console game or improve on the organization and presentation of the games listed.
- Make RPS, Tic-Tack-Toe, and Higher Lower into different cells and objects. Document each cell in Jupyter Notebooks.
- Simplify logic, particularly T-T-T. What could you do to make this more simple? Java has HashMap (like Python Dictionary), Arrays (fixed size), ArraLists (Dynamic Size).
- Run the menu using recursion versus while loop. Try to color differently.
- Look over 10 units for College Board AP Computer Science A. In your reorganized code blocks and comments identify the Units of Code Used.
- Answer why you think this reorganization and AP indetification is important?