Skip to content

Commit 52a99db

Browse files
committed
init
1 parent a148e51 commit 52a99db

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import java.io.IOException;
2+
import java.nio.file.Files;
3+
import java.nio.file.Path;
4+
import java.util.List;
5+
import java.util.Arrays;
6+
import java.util.Random;
7+
import java.util.Scanner;
8+
9+
public class WordQuestDemov2 {
10+
11+
public static void main(String[] args) throws IOException {
12+
13+
// Define the secret word and maximum allowed attempts
14+
String fileName = "data/sample-words.txt";
15+
String secretWord = getRandomWord(fileName);
16+
17+
// NOTE: only for testing purposes
18+
System.out.println("TESTING ONLY: secretWord=" + secretWord);
19+
System.out.println();
20+
21+
int maxAttempts = 10;
22+
23+
// Create a game board with underscores to represent unrevealed letters
24+
char[] gameBoard = new char[secretWord.length()];
25+
26+
// Initialize game board with underscores to represent unrevealed letters
27+
final char EMPTY_PLACE_HOLDER = '-';
28+
Arrays.fill(gameBoard, EMPTY_PLACE_HOLDER);
29+
30+
// Scanner to read player input
31+
Scanner scanner = new Scanner(System.in);
32+
33+
System.out.println("Welcome to Word Quest!");
34+
35+
// Flag to check if the word has missing letters
36+
boolean hasMissingLetters = true;
37+
38+
// Main game loop: runs while there are attempts left and word has missing letters
39+
while (maxAttempts > 0 && hasMissingLetters) {
40+
41+
System.out.print("Current word: ");
42+
System.out.println(gameBoard);
43+
44+
System.out.println();
45+
System.out.print("Guess a letter: ");
46+
47+
// Read the user's input, take the first character and convert it to uppercase
48+
String userInput = scanner.next().toUpperCase();
49+
char guess = userInput.charAt(0);
50+
51+
// Track if the guess is correct
52+
boolean isGuessCorrect = false;
53+
54+
// Loop through each letter in the secret word and check if it matches the guess
55+
for (int i=0; i < secretWord.length(); i++) {
56+
57+
if (secretWord.charAt(i) == guess) {
58+
// Reveal the correctly guessed letter on the game board
59+
gameBoard[i] = guess;
60+
isGuessCorrect = true;
61+
}
62+
}
63+
64+
// Update game status based on the guess
65+
if (isGuessCorrect) {
66+
System.out.println("Good job! You found a match!");
67+
68+
// Check if the word has missing letters
69+
hasMissingLetters = contains(gameBoard, EMPTY_PLACE_HOLDER);
70+
} else {
71+
System.out.println("Incorrect!");
72+
73+
// Decrement attempts for an incorrect guess
74+
maxAttempts--;
75+
}
76+
77+
// Display remaining attempts after each guess
78+
System.out.println("Attempts remaining: " + maxAttempts);
79+
System.out.println();
80+
}
81+
82+
// End of game message based on whether the word has missing letters
83+
if (hasMissingLetters) {
84+
System.out.println("You've run out of attempts. The secret word was: " + secretWord);
85+
} else {
86+
System.out.println("Success!!! You've revealed the secret word: " + secretWord);
87+
}
88+
89+
scanner.close();
90+
}
91+
92+
private static boolean contains(char[] gameBoard, char EMPTY_PLACE_HOLDER) {
93+
for (char temp : gameBoard) {
94+
if (temp == EMPTY_PLACE_HOLDER) {
95+
return true;
96+
}
97+
}
98+
return false;
99+
}
100+
101+
private static String getRandomWord(String fileName) throws IOException {
102+
// String[] words = {"Java", "Airplane", "Friend"};
103+
104+
// read in all lines from the file
105+
List<String> linesList = Files.readAllLines(Path.of(fileName));
106+
107+
// convert the List to an Array
108+
String[] words = linesList.toArray(new String[0]);
109+
110+
Random random = new Random();
111+
int index = random.nextInt(words.length);
112+
113+
String theWord = words[index];
114+
115+
return theWord.toUpperCase();
116+
}
117+
}

0 commit comments

Comments
 (0)