-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
115 lines (96 loc) · 3.99 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
package game.main;
/**
* Main entry point of the game
*/
public class Game {
private Player player;
private Word word;
private Gallows gallows;
private String inputName;
private String wordToGuess;
private boolean guessed = false;
public static void main(String[] args) {
Game game = new Game();
// Display menu options in a loop.
int option = -1;
do {
try {
System.out.println();
System.out.println("---------------------------------------------------------");
System.out.println("1. Create player");
System.out.println("2. Get player score");
System.out.println("3. Start game");
System.out.println("9. Quit");
option = Helper.getInt("\nEnter option => ");
switch (option) {
case 1:
createPlayer(game);
break;
case 2:
getScoreOfPlayer(game);
break;
case 3:
startGame(game);
playGame(game);
break;
case 9:
// Quit program
break;
default:
System.out.println("Invalid option selected.");
}
} catch (Exception ex) {
System.out.println("An exception occurred: " + ex.getMessage());
}
} while (option != 9);
}
private static void createPlayer(Game game) {
game.player = new Player();
game.inputName = Helper.getString("Enter player name: ");
game.player.setName(game.inputName);
System.out.println("Created player " + game.player.getName());
}
private static void getScoreOfPlayer(Game game) {
if (game.player == null) {
System.out.println("Could not determine score of player, please create a player first.");
return;
}
System.out.println("Score of player " + game.player.getName() + " is: " + game.player.getScore());
}
private static void startGame(Game game) {
System.out.println("Starting game..");
int inputLength = Helper.getInt("Enter length of word you want to guess (minimum: 2, maximum: 35): ");
System.out.println("Obtaining word of length " + inputLength + " from dictionary.");
// create Dictionary object to read text file with words and to get a word from that list
Dictionary dict = new Dictionary();
game.wordToGuess = dict.getRandomWordFromWordList(inputLength);
// create Word object to handle the updating of the guessed word
game.word = new Word(game.wordToGuess);
System.out.println(game.word.getWordGuessed());
// create a Gallows object to draw a gallows
game.gallows = new Gallows();
}
private static void playGame(Game game) {
while (!game.guessed) {
String guessInput = Helper.getString("Guess a letter: ");
String wordGuessed = game.word.guessLetter(guessInput);
String guessedLetters = game.word.getGuessedLetters();
System.out.println(wordGuessed);
// call drawGallows function if guessed letter is not correct
if (!game.word.isLetterCorrect()) {
System.out.println(game.gallows.drawGallows());
}
if (game.gallows.isGameOver()) {
System.out.println(String.format("Game over! The word was %s", game.word.getWordToGuess()));
break;
}
System.out.println("You already used the following characters: " + guessedLetters);
if (game.word.isGuessed()) {
game.guessed = true;
System.out.println("Good job! You found the correct word.");
} else {
game.guessed = false;
}
}
}
}