This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HangMan.java
147 lines (133 loc) · 3.32 KB
/
HangMan.java
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
import java.util.*;
/**
* {@code HangMan} class is the actual brain of the HangMan game and and
* provides all associated utilities for the same. It is a partially independent
* class, though it requires {@code Word} for obtaining the word being played.
* This class does not implement nor extend any other interfaces and classes
* respectively.
*
* <p>
* Collections framework has been extensively used for providing resizabiltiy
* without needing to hardcode the size based on the word.
* </p>
*
* <p>
* The class is a dependency for the {@code Game} class.
* </p>
*
* <p>
* Copyright (c) 2021. All rights reserved to Emmanuel Jojy. Use is subject to
* the above conditions.
* </p>
*
* @author Emmanuel Jojy
*
*/
public class HangMan {
/**
* ArrayList of correct letters guessed. Is updated each time a letter needs to
* be valiated. {@code Game} class makes sure no duplicate letters reach for
* validation.
*/
ArrayList<Character> crt;
/**
* ArrayList of wrong letters guessed. Is updated each time a letter needs to be
* valiated. {@code Game} class makes sure no duplicate letters reach for
* validation.
*/
ArrayList<Character> wrg;
/**
* Total number of the letter guessed was wrong. Maximum allowable tries is 6.
* Valur ranges from 0 to 6.
*/
int tries;
/**
* Check if the user guessed the complete word. Checked each time a letter is
* sent for validation.
*/
boolean win;
/**
* The guessed word. Initially contains '{@code _}' replacing all characters of
* original word.
*/
String guess;
/**
* Reference to {@code Main.word}. The application creates a copy when the
* instance of {@code HangMan} is created.
*/
String word;
/**
* Default constructor. Initializes all fields with default values.
*/
public HangMan() {
word = Main.word;
crt = new ArrayList<Character>();
wrg = new ArrayList<Character>();
tries = 0;
guess = " ";
win = false;
for (int i = 0; i < word.length(); i++) {
guess += "_ ";
}
}
private boolean isGuessed(char letter) {
char ch;
for (int i = 0; i < crt.size(); i++) {
ch = crt.get(i).charValue();
if (ch == letter) {
return true;
}
}
return false;
}
private boolean check(char letter) {
Character ch = Character.valueOf(letter);
boolean res;
if (word.indexOf(letter) != -1) {
crt.add(ch);
res = true;
} else {
wrg.add(ch);
tries++;
res = false;
}
update();
if (guess.indexOf('_') == -1)
win = true;
return res;
}
private void update() {
int i;
char letter;
guess = "";
for (i = 0; i < word.length(); i++) {
letter = word.charAt(i);
if (isGuessed(letter))
guess += letter;
else
guess += '_';
guess += ' ';
}
}
private void debug() {
System.out.println("try = " + tries);
System.out.println("crt = " + crt);
System.out.println("wrg = " + wrg);
System.out.println("gus = " + guess);
System.out.println("\n");
}
/**
* Validates a letter guessed by the {@code Game} class. The only directly
* callable method of {@code HangMan}.
* <p>
* All other methods are private and are chained to {@code validate()}
* </p>
*
* @param letter The letter the user guesses. The caller must ensure no
* duplictaes reach the method for a specific session.
*/
public void validate(char letter) {
check(letter);
debug();
}
}