-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
54 lines (44 loc) · 1.66 KB
/
GuessingGame.java
File metadata and controls
54 lines (44 loc) · 1.66 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
import java.util.Scanner;
// superclass for general game functionality, extends game
public abstract class GuessingGame extends Game {
protected int maxAttempts; // max number of attempts allowed
protected int attempts; // current number of attempts
protected Scanner scanner; // scanner for user input
protected int score;
protected String id;
public GuessingGame(int maxAttempts) { // constructor to initialize attempts and scanner
super();
this.maxAttempts = maxAttempts;
this.attempts = 0;
this.scanner = new Scanner(System.in);
this.score = score;
this.id = id;
}
// plays one game
@Override
public GameRecord play() {
attempts = 0; // reset attempts for each game
while (attempts < maxAttempts && !isGameWon()) {
System.out.println("Attempt " + (attempts + 1) + " of " + maxAttempts);
String guess = getUserGuess();
processGuess(guess);
attempts++;
}
return new GameRecord(score, id);
}
// checks if the next game should be played, to be overridden by subclasses
@Override
public abstract boolean playNext();
// gets a guess from the user
protected String getUserGuess() {
System.out.print("Enter your guess: ");
return scanner.nextLine().trim().toUpperCase();
}
// checks if the game is won
protected abstract boolean isGameWon();
// processes a guess
protected abstract void processGuess(String guess);
// ends the game and prints game record for the game result
protected abstract void endGame();
public abstract void printInstructions();
}