Skip to content

Commit 55e31b3

Browse files
committed
git commit -m "Implement Hangman game using Java and file input"
1 parent a27de2a commit 55e31b3

3 files changed

Lines changed: 241 additions & 0 deletions

File tree

games/HangMan/Main.java

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package HangMan;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.Random;
9+
import java.util.Scanner;
10+
11+
public class Main {
12+
13+
public static void main(String[] args) {
14+
15+
// HANG-MAN GAME
16+
17+
String filePath = "HangMan/words.txt";
18+
19+
ArrayList<String> words = new ArrayList<>();
20+
21+
try(BufferedReader reader = new BufferedReader(new FileReader(filePath))){
22+
String line;
23+
24+
while ((line = reader.readLine()) != null) {
25+
words.add(line.trim());
26+
}
27+
}
28+
29+
catch(FileNotFoundException e){
30+
System.out.println("Could not find file.");
31+
}
32+
33+
catch(IOException e){
34+
System.out.println("Something went wrong.");
35+
}
36+
37+
Random random = new Random();
38+
String word = words.get(random.nextInt(words.size()));
39+
40+
Scanner scanner = new Scanner(System.in);
41+
42+
ArrayList<Character> wordState = new ArrayList<>();
43+
int wrongGuesses = 0;
44+
45+
for(int i = 0; i < word.length(); i++){
46+
wordState.add('_');
47+
}
48+
49+
System.out.println("-------------------");
50+
System.out.println("Welcome to Hangman!");
51+
System.out.println("-------------------");
52+
53+
while (wrongGuesses < 6){
54+
55+
System.out.println(getHangmanArt(wrongGuesses));
56+
57+
System.out.print("Word: ");
58+
59+
for(char c : wordState){
60+
System.out.print(c + " ");
61+
}
62+
63+
System.out.println();
64+
65+
System.out.print("Guess a letter: ");
66+
char guess = scanner.next().toLowerCase().charAt(0);
67+
68+
if(word.indexOf(guess) >= 0){
69+
System.out.println("Correct guess!");
70+
71+
for(int i = 0; i < word.length(); i++){
72+
if(word.charAt(i) == guess){
73+
wordState.set(i, guess);
74+
}
75+
}
76+
77+
if(!wordState.contains('_')){
78+
79+
System.out.println(getHangmanArt(wrongGuesses));
80+
System.out.println("You WIN!");
81+
System.out.println("The word was: " + word);
82+
break;
83+
}
84+
}
85+
86+
else{
87+
wrongGuesses++;
88+
System.out.println("Wrong guess!");
89+
}
90+
}
91+
92+
if(wrongGuesses >= 6){
93+
94+
System.out.println(getHangmanArt(wrongGuesses));
95+
System.out.println("Game OVER!");
96+
System.out.println("The word was: " + word);
97+
}
98+
99+
scanner.close();
100+
}
101+
102+
static String getHangmanArt(int wrongGuesses){
103+
104+
return switch(wrongGuesses){
105+
106+
case 0 -> """
107+
108+
109+
110+
""";
111+
112+
case 1 -> """
113+
o
114+
115+
116+
""";
117+
118+
case 2 -> """
119+
o
120+
|
121+
122+
""";
123+
124+
case 3 -> """
125+
o
126+
/|
127+
128+
""";
129+
130+
case 4 -> """
131+
o
132+
/|\\
133+
134+
""";
135+
case 5 -> """
136+
o
137+
/|\\
138+
/
139+
""";
140+
case 6 -> """
141+
o
142+
/|\\
143+
/ \\
144+
""";
145+
146+
default -> "";
147+
};
148+
}
149+
}

games/HangMan/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 🎮 Hangman Game (Java)
2+
3+
A simple console-based **Hangman game** written in Java.
4+
The program randomly selects a word from a file and allows the player to guess letters with a limited number of mistakes.
5+
6+
This project focuses on:
7+
8+
- File handling
9+
- Collections
10+
- Control flow
11+
- Basic game logic
12+
- Clean console interaction
13+
14+
---
15+
16+
## 📌 Features
17+
18+
- Reads words from an external text file (`words.txt`)
19+
- Random word selection
20+
- ASCII-art Hangman display
21+
- Tracks wrong guesses (up to 6)
22+
- Win and lose conditions
23+
- Case-insensitive input
24+
25+
---
26+
27+
## 📂 Project Structure
28+
29+
games/
30+
├─ HangMan/
31+
│ ├─ Main.java
32+
│ └─ words.txt
33+
└─ README.md
34+
35+
36+
---
37+
38+
## ▶️ How to Run
39+
40+
### 1. Compile
41+
From the **project root directory** (`nuke2`):
42+
43+
```bash
44+
javac HangMan/Main.java
45+
java HangMan.Main
46+
47+
📄 words.txt Format:-
48+
49+
- Place words.txt inside the HangMan folder
50+
- One word per line
51+
- No special formatting required
52+
53+
Example:-
54+
apple
55+
orange
56+
.
57+
.
58+
.
59+
.
60+
61+
🧠 How the Game Works:-
62+
63+
- The program loads words from words.txt
64+
- A random word is selected
65+
- The player guesses one letter at a time
66+
- Correct guesses reveal letters
67+
- Incorrect guesses add to the hangman
68+
- The game ends when:
69+
- The word is fully guessed (Win)
70+
- 6 wrong guesses are made (Game Over)
71+
72+
⚠️ Notes:-
73+
74+
- The program uses relative file paths
75+
- words.txt must exist and must not be empty
76+
- Java searches for files relative to where the program is executed, not where the class is located
77+
78+
🛠️ Technologies Used:-
79+
80+
- Java
81+
- ArrayList
82+
- Random
83+
- Scanner
84+
- BufferedReader

games/HangMan/words.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apple
2+
orange
3+
grapes
4+
banana
5+
coconut
6+
pineapple
7+
lime
8+
lemon

0 commit comments

Comments
 (0)