-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.java
More file actions
130 lines (106 loc) · 3.23 KB
/
Hangman.java
File metadata and controls
130 lines (106 loc) · 3.23 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Hangman {
public static void main(String[] args) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
System.out.println("1 or 2 players?");
String players = keyboard.nextLine();
String word;
if (players.equals("1")) {
Scanner scanner = new Scanner(new File("/home/josh/Downloads/words.json"));
List<String> words = new ArrayList<>();
while (scanner.hasNext()) {
words.add(scanner.nextLine());
}
Random rand = new Random();
word = words.get(rand.nextInt(words.size()));
}
else {
System.out.println("Player 1, please enter your word:");
word = keyboard.nextLine();
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
System.out.println("Ready for player 2! Good luck!");
}
//System.out.println(word);
List<Character> playerGuesses = new ArrayList<>();
Integer wrongCount = 0;
while(true) {
printHangedMan(wrongCount);
if (wrongCount >= 5) {
System.out.println("You lose!");
System.out.println("The word was: " + word);
break;
}
printWordState(word, playerGuesses);
if (!getPlayerGuess(keyboard, word, playerGuesses)) {
wrongCount++;
}
if(printWordState(word, playerGuesses)) {
System.out.println("You win!");
break;
}
System.out.println("Please enter your guess for the word:");
if(keyboard.nextLine().equals(word)) {
System.out.println("You win!");
break;
}
else {
System.out.println("Nope! Try again.");
}
}
}
private static void printHangedMan(Integer wrongCount) {
System.out.println(" -------");
System.out.println(" | |");
if (wrongCount >= 1) {
System.out.println(" O");
}
if (wrongCount >= 2) {
System.out.print("\\ ");
if (wrongCount >= 3) {
System.out.println("/");
}
else {
System.out.println("");
}
}
if (wrongCount >= 4) {
System.out.println(" |");
}
if (wrongCount >= 5) {
System.out.print("/ ");
if (wrongCount >= 6) {
System.out.println("\\");
}
else {
System.out.println("");
}
}
System.out.println("");
System.out.println("");
}
private static boolean getPlayerGuess(Scanner keyboard, String word, List<Character> playerGuesses) {
System.out.println("Please enter a letter:");
String letterGuess = keyboard.nextLine();
playerGuesses.add(letterGuess.charAt(0));
return word.contains(letterGuess);
}
private static boolean printWordState(String word, List<Character> playerGuesses) {
int correctCount = 0;
for (int i = 0; i < word.length(); i++) {
if (playerGuesses.contains(word.charAt(i))) {
System.out.print(word.charAt(i));
correctCount++;
}
else {
System.out.print("-");
}
}
System.out.println();
return (word.length() == correctCount);
}
}