-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirst_try
535 lines (463 loc) · 17.7 KB
/
first_try
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* ************************************************************************* *\
* Programmierung 1 HS 2018 - Serie 4-1
Raphaela Seeger 16-113-441
Anastasija Novikova 16-825-390 *
\* ************************************************************************* */
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class GuessOrc {
// Store the orc chieftain position.
private int mineShaftId;
// Amount of attempts we get to stop the orcs.
// Use 'MAX_ATTEMPTS'' instead of coding a number
// directly into your program.
public final int MAX_ATTEMPTS = 6;
// Number of mine shafts from which orcs can arrive.
public final int MINE_SHAFTS = 12;
// Magic numbers to tell us when something was not set.
public final int NO_MOVE_MADE = -1;
public final int NO_HINT = -1;
// Could not parse the given input.
public final int PARSING_FAILED = -1;
// SET DEBUG MODE.
public final boolean DEBUG = false;
// That's what the orc horde looks like.
public final String ORCS = " ┗(`Д゚┗(`゚Д゚´)┛゚Д´)┛ ";
// If you use WINDOWS (or the above line just looks wrong) change it to this:3
// public final String ORCS = " - - - ORC--ARMY - - - ";
//Do not modify or access!
private int[] history = new int[ MAX_ATTEMPTS ];
private int[] historyHelpers = new int[ MAX_ATTEMPTS ];
private final String[] HINTS = new String[] {
"to the left",
"to the right",
"under dirt",
"under stone",
"very far away"
};
// DO NOT CHANGE THIS METHOD!
public static void main ( String[] args ) {
GuessOrc game = new GuessOrc();
game.resetHistory();
game.start();
}
/**
* Implement your hint calculation here.
*
* @param column the guessed mine shaft.
* @return the id for a hint in HINT.
*/
public int calculateHint( int column ) {
// Your code goes here;
//create helpTextId to feed into printMap
//switch creates Array of Strings, then random method to print one of them?
ArrayList<Integer> possibleHints = new ArrayList<Integer>();
//create numbers for switch statement to identify different cases
int i = 0;
int currentGuess = movesMade();
boolean isThereStone = false;
if ((mineShaftId + movesMade()) % 2 == 1) {
possibleHints.add(2);
}
else {
possibleHints.add (3);
}
if (column > mineShaftId)
{ i=1;
possibleHints.add (0);}
if (column < mineShaftId)
{i=0;
possibleHints.add (1);}
if (column +5 <= mineShaftId)
{i=4;
possibleHints.add (4);}
if (column -5 >= mineShaftId)
{i=4;
possibleHints.add (4);}
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(possibleHints.size());
int helpTextId = possibleHints.get(index);
return helpTextId;
}
/**
* Implement the game logic here.
*
*/
public void start() {
// Your code goes here
//print out remaining rows as well
System.out.println(generateOrcIntroScreen());
System.out.println (" ");
//randomize mineShaftId
randomizeMineShaftId();
//loop to go through six times, break if won, default "You loose!"
for (int currentMove = 0; currentMove <= 5; currentMove ++)
{
boolean checkWin = false;
boolean getNextMove = true;
//while infinite loop tests input for validity and asks to re-enter guessed column until input valid
while (getNextMove == true) {
//asks for guessedMineShaft with right turn (determined through switch statement)
String x;
switch (currentMove)
{case 0:
x = "first";
break;
case 1:
x = "second";
break;
case 2:
x = "third";
break;
case 3:
x = "fourth";
break;
case 4:
x = "fifth";
break;
case 5:
x = "final";
break;
default:
x = "debug";
}
System.out.println ("Please enter your " + x +" guess:");
//scanning the first character of the input
Scanner scan = new Scanner (System.in);
char guessedMineShaft = scan.next().charAt(0);
//converting input-guess to lower case letter
guessedMineShaft = Character.toLowerCase(guessedMineShaft);
//converting guessed mineShaft into Int using helper methode for better comparing
int column = getColumnAsInt (guessedMineShaft);
//int currentGuess = movesMade();
int helpTextId = calculateHint(column);
printMap(column, helpTextId);
//set a boolean to true if Won -> to acess outside and break out of for loop
if(column == mineShaftId)
{
checkWin = true;
}
if(checkWin == true)
{
System.out.println ("You won, congratulations!");
getNextMove = false;
}
// break while loop if input is valid
if (column == 0 || column == 1 || column ==2 || column ==3 || column ==4 || column ==5 || column ==6 || column ==7 || column ==8 || column ==9 || column ==10 || column ==11)
{
break;
}
System.out.println ("Your entry was invalid.");
}
if(getNextMove == false)
{
break;
}
}
//feeds the mineShaft Char to the resolution
char mineShaft;
mineShaft = getMineShaftAsChar (mineShaftId);
//in case you need more than 6 moves, might already be defined later...
if (movesMade() == MAX_ATTEMPTS){
System.out.println ("You loose! The orcs attacked from mine shaft '" +mineShaft +"'");
}
}
//helper methode random number for mineShaftId
private int randomizeMineShaftId()
{
//randomizing mineShaftId Number between 0-11
int cutoffUpper = 11;
int cutoffLower =0;
mineShaftId = ThreadLocalRandom.current().nextInt(cutoffLower, cutoffUpper++);
return mineShaftId;
}
// --- Helper methods ---
/**
* Resets the game's history.
*
* DO NOT CHANGE THIS METHOD!
*/
private void resetHistory() {
history = new int[ MAX_ATTEMPTS ];
Arrays.fill( history, NO_MOVE_MADE );
historyHelpers = new int[ MAX_ATTEMPTS ];
Arrays.fill( historyHelpers, NO_HINT );
}
/**
* Calculates the number of moves.
*
* @return the number of moves already made.
*
* DO NOT CHANGE THIS METHOD!
*/
private int movesMade() {
int currentMove = 0;
while (currentMove < MAX_ATTEMPTS && history[currentMove] != NO_MOVE_MADE){
currentMove++;
}
return currentMove;
}
/**
* Prints the map.
* If you hand it something else than NO_MOVE_MADE and
* NO_HINT it will also update the history.
*
* @param column a chosen column to flush
* @param helpTextId a chosen helptextID (from HINTS) or
*
* DO NOT CHANGE THIS METHOD!
*/
private void printMap( int column, int helpTextId ) {
// Update history
// This would not write anything even if we did an update
if( column != NO_MOVE_MADE ) {
history[movesMade()] = column;
}
if( helpTextId != NO_HINT ) {
historyHelpers[movesMade() - 1] = helpTextId;
}
boolean hasWon = column == mineShaftId;
int currentMove = movesMade();
System.out.println();
if( movesMade() > 0 )
System.out.println( "You flush mine shaft " + getMineShaftAsChar(column) );
if( column == mineShaftId ){ // Did we get the orcs?
System.out.println();
System.out.println( "You have stopped the orc chieftain!" );
System.out.println();
} else {
System.out.println();
System.out.println();
}
// Build and print the header.
String header = "|";
for(int col = 0; col < MINE_SHAFTS; col++){
header += String.valueOf( getMineShaftAsChar( col ) ) + "|";
}
System.out.println( header + "\n" + getLine() );
// Build the map
int row = MAX_ATTEMPTS;
while( row > 0 ){
printRow( column, MAX_ATTEMPTS - row, currentMove);
row -= 1;
}
System.out.println( getLine() );
if ( !hasWon && movesMade() > 0 && movesMade() < MAX_ATTEMPTS){
System.out.println(
"Your Seismometer tells you that the orcs are "
+ HINTS[ helpTextId ] );
} else if ( movesMade() == MAX_ATTEMPTS ){
System.out.println( ORCS ); // They got you.
}
// Print history:
System.out.println( getLine() );
if( movesMade() > 1 ) {
System.out.println("History: ");
}
for( int historyEntry = 0; historyEntry < MAX_ATTEMPTS; historyEntry++ ) {
int hintId = historyHelpers[ historyEntry ];
if( hintId < HINTS.length && hintId >= 0 ) {
System.out.println( ( historyEntry + 1 ) + ".: " + HINTS[ hintId ] );
}
}
}
/**
* A line we can use to structure the map.
*
* @ return a String made of "-"
*/
private String getLine() {
// Add a line of (more or less) correct length.
char[] line = new char[ 2*MINE_SHAFTS + 1 ];
Arrays.fill( line, '-' );
return new String( line );
}
/**
* Prints a row of the map.
*
* DO NOT CHANGE THIS METHOD UNLESS the special characters don't work on your system!
* (Document it if that's the case)
*
* @param column The column which the player chose.
* @param row The row we want to print.
* @param currentGuess The number of the current guess.
*/
private void printRow( int column, int row, int currentGuess) {
if( row == currentGuess ){
printOrcRow( row );
}
else {
String output = "|";
for (int col = 0; col < MINE_SHAFTS; col++) {
// Did the player check this mine shaft?
boolean checked = false;
for (int i : history) {
if (col == i) { //
checked = true;
}
}
// Is there stone ?
boolean isThereStone = false;
if ((col + row) % 2 == 0) {
isThereStone = true;
}
String emptyField = isThereStone ? "#" : " "; // How we draw empty terrain
String hitTerrain = isThereStone ? "▓" : "░"; // How we draw terrain we have flushed.
if (row < currentGuess) { // Are we behind the orcs?
output += (checked ? hitTerrain : "▒");
} else {
// Have we hit the chieftain? Celebrate with special character!
if (column == mineShaftId && col == column && currentGuess == movesMade()) {
hitTerrain = "■";
}
output += (checked ? hitTerrain : emptyField);
}
output += "|";
}
System.out.println( output );
}
}
/**
* Print the row where the orcs are right now.
*
* DO NOT CHANGE THIS METHOD UNLESS the special characters don't work on your stystem!
*
* If DEBUG is set to true, print the exact location instead.
* @param row The row where the orcs are supposed to be right now.
*/
private void printOrcRow(int row) {
if (!DEBUG) {
System.out.println(ORCS);
} else {
String output = "|";
for (int col = 0; col < MINE_SHAFTS; col++) {
// Did the player check this mine shaft?
boolean checked = false;
for (int i : history) {
if (col == i) { //
checked = true;
}
}
// Is there stone ?
boolean isThereStone = false;
if ((col + row) % 2 == 0) {
isThereStone = true;
}
String emptyField = isThereStone ? "#" : " "; // How we draw empty terrain
String hitTerrain = isThereStone ? "▓" : "░";
// Is the chieftain there? Special character
if (col == mineShaftId) {
hitTerrain = "■";
}
output += hitTerrain;
output += "|";
}
System.out.println(output);
}
}
/**
* Determines column's number from its code.
* Here 'a' is 1st column and 'l' is 12th
* returns 0 on unrecognised codes.
*
* DO NOT CHANGE THIS METHOD!
*/
private int getColumnAsInt( char column ) {
switch ( column ) {
case 'a':
return 0;
case 'b':
return 1;
case 'c':
return 2;
case 'd':
return 3;
case 'e':
return 4;
case 'f':
return 5;
case 'g':
return 6;
case 'h':
return 7;
case 'i':
return 8;
case 'j':
return 9;
case 'k':
return 10;
case 'l':
return 11;
default:
return PARSING_FAILED;
}
}
/**
* Determines column's code from its integer
* Here 'a' is int 0 and 'l' is 11
* returns 'z' on unrecognised integers.
*
* DO NOT CHANGE THIS METHOD
*/
private char getMineShaftAsChar( int mineShaft ) {
switch ( mineShaft ) {
case 0:
return 'a';
case 1:
return 'b';
case 2:
return 'c';
case 3:
return 'd';
case 4:
return 'e';
case 5:
return 'f';
case 6:
return 'g';
case 7:
return 'h';
case 8:
return 'i';
case 9:
return 'j';
case 10:
return 'k';
case 11:
return 'l';
default:
return 'z';
}
}
/**
* Use this to print the intro screen.
*
* DO NOT CHANGE THIS METHOD UNLESS the special characters don't work on your stystem!
* (Document it if that's the case)
*
* @return a String made of orc
*/
private String generateOrcIntroScreen() {
String orc = " __,='`````'=/__\n" +
" '// /-\\ /-\\ \\ `' _,-,\n" +
" //| ,_) (`\\ ,-'`_,-\\\n" +
" ,-~~~\\ /||\\ /-, \\==```` \\__\n" +
" / `----' `\\ \\ \\/\n" +
" ,-` , \\ ,.-\\ \\\n" +
" / , \\,-`\\`_,-`\\_,..--'\\\n" +
" ,` ,/, ,>, ) \\--`````\\\n" +
" ( `\\`---'` `-,-'`_,< \\ \\_,.--'`\n" +
" `. `--. _,-'`_,-` | \\\n" +
" [`-.___ <`_,-'`------( /\n" +
" (`` _,-\\ \\ --`````````|--`\n" +
" >-`_,-`\\,-` , |\n" +
" <`_,' , /\\ /\n" +
" ` \\/\\,-/ `/ \\/`\\_/V\\_/\n" +
" ( ._. ) ( .__. ) **************\n" +
" | | | | Stop The Orcs!\n" +
" \\,---_| |_---./ **************\n" +
" ooOO(_) (_)OOoo" +
"\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
return orc;
}
}