-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHangman.java
More file actions
131 lines (116 loc) · 4.83 KB
/
Hangman.java
File metadata and controls
131 lines (116 loc) · 4.83 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
// Hangman.java - the class for the hangman game
// Akshit Garg
// March 2022
import java.util.ArrayList;
public class Hangman
{
private String word;
private String[] fruits = {"apple", "banana", "pepper", "tomato", "lychee", "dragonfruit", "pear", "orange", "lemon", "lime", "avocado", "pumpkin", "watermelon", "cucumber", "peaches", "apricots", "strawberry", "lingonberry", "blackberry", "blueberry", "raspberry", "grapes", "cranberry"};
private String[] countries = {"United States", "Canada", "Mexico", "Brazil", "Colombia", "Argentina", "Ukraine", "Russia", "United Kingdom", "France", "Spain", "Switzerland", "Germany", "Italy", "Greece", "Poland", "Sweden", "Ireland", "India", "China", "Taiwan", "Japan", "Rwanda", "Pakistan"};
private String[] sports = {"tennis", "soccer", "football", "badminton", "rugby", "volleyball", "tchoukball", "wrestling", "chess", "cricket", "basketball", "baseball", "ultimate frisbee", "field hockey", "ice hockey", "water skiing", "snoboarding", "paddleboarding", "skiing", "pole vaulting", "track and field"};
private int life = 6;
// Constructors
public Hangman(int answer)
{
findWord(answer);
}
// Another constructor
public Hangman(String userWord)
{
findWord(userWord);
}
/**
* ---------------------------------------METHODS---------------------------------------------
*/
// Determines what the word will be based on premade lists
private void findWord(int answer)
{
if(answer == 0)
word = fruits[(int) (Math.random() * fruits.length)];
else if(answer == 1)
word = countries[(int) (Math.random() * countries.length)];
else if(answer == 2)
word = sports[(int) (Math.random() * sports.length)];
}
// Determines what the word will be based on user input
private void findWord(String userWord)
{
word = userWord;
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");
}
// Takes a life away
public void takeLife()
{
life--;
}
// Returns the lives of the player
public int getLife()
{
return life;
}
// Sees if the full word has been guessed
public boolean hasGuessedWord(ArrayList<Character> guess)
{
boolean didGuess = false;
int length = word.length();
for(int i = 0; i < length; i++)
{
if(word.charAt(i) != ' ')
{
if(guess.contains(Character.toLowerCase(word.charAt(i))))
didGuess = true;
else
{
didGuess = false;
i = word.length();
}
}
}
return didGuess;
}
// Guess a word
public boolean guessWord(String wordGuess)
{
boolean didGuess = false;
if (wordGuess.toLowerCase().equals(word.toLowerCase()))
didGuess = true;
else
return didGuess;
return didGuess;
}
// Guess a letter
public boolean guessLetter(String letter)
{
boolean didGuess = false;
if (word.toLowerCase().indexOf(letter.toLowerCase()) != -1)
didGuess = true;
else
return didGuess;
return didGuess;
}
// Returns the word
public String getWord()
{
return word;
}
// the toString method - Prints the man based on the lives left
public String toString()
{
String result = "";
if (life == 6)
result = " +----+ \n | | \n | \n | \n | \n | \n | \n *****\n *******\n";
else if(life == 5)
result = " +----+ \n | | \n 0 | \n | \n | \n | \n | \n *****\n *******\n";
else if(life == 4)
result = " +----+ \n | | \n 0 | \n | | \n | \n | \n | \n *****\n *******\n";
else if(life == 3)
result = " +----+ \n | | \n 0 | \n /| | \n | \n | \n | \n *****\n *******\n";
else if(life == 2)
result = " +----+ \n | | \n 0 | \n /|\\ | \n | \n | \n | \n *****\n *******\n";
else if(life == 1)
result = " +----+ \n | | \n 0 | \n /|\\ | \n | | \n / | \n | \n *****\n *******\n";
else if(life == 0)
result = " +----+ \n | | \n 0 | \n /|\\ | \n | | \n / \\ | \n | \n *****\n *******\n";
return result;
}
}