-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.java
More file actions
127 lines (109 loc) · 4.28 KB
/
Copy pathHangman.java
File metadata and controls
127 lines (109 loc) · 4.28 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
// little Hangman game with one or two players (one player is the computer with a list of words)
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")) {
// computer chooses word from list
Scanner scanner = new Scanner(new File("/Users/crixos/Documents/Courses/Java/Hangman/Words.txt"));
List<String> words = new ArrayList<>();
while (scanner.hasNext()){
words.add(scanner.nextLine()); // add each word to the list
}
Random rand = new Random();
word = words.get(rand.nextInt(words.size())); // choose a random word from the list
}
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\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\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\n\n\n\n\n");
System.out.println("Ready for player 2! Have fun!");
}
//System.out.println(word);
List<Character> playerGuesses = new ArrayList<>(); // list of player's guesses
int wrongCount = 0; // number of wrong guesses
while(true) {
printHangedMan(wrongCount);
if(wrongCount >= 6){ // if player has guessed wrong 6 times, they lose
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(int wrongCount) { // prints the separate parts of the hanged man
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) {
// gets player's guess and adds it to the list of guesses
System.out.println("Please enter a new letter: ");
String letterGuess = keyboard.nextLine();
playerGuesses.add(letterGuess.charAt(0));
return word.contains(letterGuess);
}
private static boolean printWordState(String word, List<Character> playerGuesses) {
// prints the word with the letters that have been guessed
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);
}
}