-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHangmanGame.java
More file actions
166 lines (152 loc) · 5.66 KB
/
HangmanGame.java
File metadata and controls
166 lines (152 loc) · 5.66 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
157
158
159
160
161
162
163
164
165
166
// HangmanGame.java - plays the Hangman class
// A.Garg
// March 2022
import java.util.Scanner;
import java.util.ArrayList;
public class HangmanGame
{
// Data Fields
private Hangman player;
private Scanner scan = new Scanner(System.in);
private Scanner numScan = new Scanner(System.in);
//Constructor
public HangmanGame()
{
resetWord();
}
/**
* ----------------------------METHODS-------------------------------
*/
// Resets the word
public void resetWord()
{
boolean firstScan = false, secondScan = false;
while (firstScan == false)
{
System.out.println("Would you like to choose your own word? (y/n): ");
String userChoice = scan.nextLine().toLowerCase();
if (userChoice.equals("y"))
{
System.out.println("What is your word? ");
player = new Hangman(scan.nextLine());
firstScan = true;
}
else if(userChoice.equals("n"))
{
while(secondScan == false)
{
System.out.println("Would you like a word about fruits(1), countries(2), sports(3), or randomly chosen(4)? ");
int userNum = numScan.nextInt() - 1;
if (userNum < 0 || userNum > 3)
System.out.println("Invalid Input");
else
{
if(userNum == 3)
player = new Hangman((int) (Math.random() * 3));
else
player = new Hangman(userNum);
secondScan = true;
}
}
firstScan = true;
}
else
System.out.println("Invalid Input");
}
}
// plays the game
public boolean playGame()
{
boolean gameDone = false, didWin = false;
String playerWord = player.getWord();
ArrayList<Character> guessedChar = new ArrayList<Character>(), incorrectGuesses = new ArrayList<Character>();
System.out.println(player);
for(int i = 0; i < playerWord.length(); i++)
{
if(playerWord.charAt(i) == ' ')
System.out.print(" ");
else
System.out.print("_ ");
}
while(gameDone == false)
{
System.out.println("\n\nGuess a word or a letter: ");
String guess = scan.nextLine();
if (guess.length() == 1)
{
char charGuess = Character.toLowerCase(guess.charAt(0));
if(player.guessLetter(guess))
{
System.out.println("\n--===============================--\n" + "'" + guess + "'" + " is in the word");
guessedChar.add(Character.toLowerCase(charGuess));
}
else
{
System.out.println("\n--===============================--\n" + "'" + guess + "'" + " is not in the word");
if(!(incorrectGuesses.contains(charGuess)))
incorrectGuesses.add(charGuess);
player.takeLife();
}
}
else
{
if(player.guessWord(guess))
{
System.out.println("\n--===============================--\n" + "'" + guess + "'" + " is the word");
for(int i = 0; i < playerWord.length(); i ++)
guessedChar.add(Character.toLowerCase(playerWord.charAt(i)));
didWin = true;
gameDone = true;
}
else
{
System.out.println("\n--===============================--\n" + "'" + guess + "'" + " is not the word");
player.takeLife();
}
}
System.out.println("\nIncorrect Guesses: ");
for(int i = 0; i < incorrectGuesses.size(); i++)
{
if(i != incorrectGuesses.size() - 1)
System.out.print(incorrectGuesses.get(i) + ", ");
else
System.out.println(incorrectGuesses.get(i));
}
System.out.println();
System.out.println(player);
for(int i = 0; i < playerWord.length(); i++)
{
if(playerWord.charAt(i) == ' ')
System.out.print(" ");
else if(guessedChar.contains(Character.toLowerCase(playerWord.charAt(i))))
System.out.print(playerWord.charAt(i) + " ");
else
System.out.print("_ ");
}
if(player.hasGuessedWord(guessedChar))
{
gameDone = true;
didWin = true;
}
if(player.getLife() == 0)
{
gameDone = true;
didWin = false;
}
if(gameDone == true)
{
if(didWin == true)
System.out.println("\nYOU WON!!\n\nLives Left: " + player.getLife());
else
System.out.println("\nYOU LOST\nThe Word Was: " + player.getWord());
}
}
return didWin;
}
// Closes the Scanners - Do after game is done
public void endGame()
{
scan.close();
numScan.close();
}
}