-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWOF2.java
More file actions
156 lines (139 loc) · 5.3 KB
/
WOF2.java
File metadata and controls
156 lines (139 loc) · 5.3 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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
// concrete implementation of WOF for a human player
public class WOF2 extends GuessingGame {
protected String phrase;
protected StringBuilder secret;
protected String pastGuesses;
protected int chances;
protected String incorrectGuesses;
protected ArrayList<WOFPlayer> players;
protected List<String> phraseList;
protected AllGamesRecord allRecords;
protected ArrayList<String> usedPhrases;
private String id;
public WOF2(int maxAttempts) { // constructor initializes phrase list, scanner, and max attempts
super(maxAttempts);
this.scanner = new Scanner(System.in);
this.phraseList = readFile(); // load phrases from file
this.usedPhrases = new ArrayList<>();
this.phraseList = readFile();
this.id = id;
this.chances = maxAttempts;
this.pastGuesses = "";
this.incorrectGuesses = "";
}
public List<String> readFile() { // reads phrases from file and returns as a list
List<String> phrases = new ArrayList<>();
try {
phrases = Files.readAllLines(Paths.get("phrases.txt"));
} catch (IOException e) {
System.out.println("Error reading phrases file: " + e.getMessage());
}
return phrases;
}
public String randomPhrase() { // separated from original randomPhrase so that each call gets new phrase without reading file again
Random rand = new Random();
int r = rand.nextInt(phraseList.size()); // safer phrase list selection
return phraseList.get(r);
}
private StringBuilder generateHiddenPhrase(String phrase) { // generates hidden version of the phrase
StringBuilder hidden = new StringBuilder();
for (char c : phrase.toCharArray()) {
if (c == ' ') {
hidden.append(" ");
} else {
hidden.append("*");
}
}
return hidden;
}
@Override
public GameRecord play() { // plays one game and returns a game record
playGame();
return new GameRecord(chances, id);
}
// game execution
public void playGame() {
while (!secret.toString().equals(phrase)) {
String g = super.getUserGuess();
if (pastGuesses.contains(g)) {
System.out.println("You've already guessed this one! Try another");
continue;
}
pastGuesses += g;
processGuess(g);
endGame();
}
System.out.println("<----Congrats! All done---->");
}
@Override
public boolean playNext() { // checks if the player wants to play another round
System.out.println("Enter 1 to start");
String response = scanner.nextLine();
if (response.indexOf('1') == -1) {
System.out.println("Sad to see you go :( Here are your results: ");
System.out.println(allRecords);
System.out.println("Exiting...");
System.exit(0);
}
System.out.println("Starting a new round...");
return true;
}
@Override
protected boolean isGameWon() { // checks if player has guessed the full phrase
return secret.toString().equals(phrase);
}
@Override
protected void endGame(){
if (chances == 0) {
System.out.println("Oops too many wrong guesses, game over!");
System.out.println("<----Game Over---->");
System.exit(1);
}
}
@Override
protected void processGuess(String guess) {
char g = guess.charAt(0);
if (updateGuess(phrase, secret, g)) {
System.out.println("Correct!");
System.out.println("Current progress: " + secret);
} else {
chances--;
incorrectGuesses += g;
System.out.println("Try again! You have " + chances + " chances left");
System.out.println("Your past incorrect guesses: " + incorrectGuesses);
System.out.println("Current progress: " + secret);
}
}
public Boolean updateGuess(String phrase, StringBuilder secret, char guess) {
// returns T/F for whether letter is a match and modifies hidden phrase and counts score
boolean found = false;
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) == guess || phrase.charAt(i) == Character.toUpperCase(guess)) {
secret.setCharAt(i, phrase.charAt(i));
found = true;
}
}
return found;
}
@Override
public void printInstructions() { // prints game instructions for user
System.out.println("<---- Game Start ---->");
phrase = randomPhrase();
secret = generateHiddenPhrase(phrase);
System.out.println("Instructions: This is a hangman-like game with a phrase.");
System.out.println("Guess one letter at a time. Only the first letter will be recognized.");
System.out.println("Hidden phrase: " + secret);
}
// main method to initiate the game
public static void main(String[] args) {
WOF2 game = new WOF2(5); // max 5 attempts per game
game.playAll();
}
}