Repository navigation
Expand file tree
/
Copy pathHangman.java
More file actions
139 lines (117 loc) · 3.08 KB
/
Copy pathHangman.java
File metadata and controls
139 lines (117 loc) · 3.08 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
package Hangman;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
public class Hangman {
private String word;
private String progress;
private int lives;
private Dictionary d;
private ImageIcon[] images;
private ImageIcon win;
private String brokenword;
private String filename;
private String name;
private int numCorrect;
public Hangman(String filename) {
// Scanner in = new Scanner(System.in);
// System.out.print("Type your name: ");
// this.name = in.nextLine();
this.filename = filename;
this.lives = 6;
this.progress = "";
this.newWord();
this.word = this.word.toUpperCase();
this.brokenword = "";
for (int i = 0; i < this.word.length(); i++) {
this.brokenword += this.word.charAt(i) + " ";
}
this.images = new ImageIcon[7];
for (int i = images.length - 1; i >= 0; i--) {
images[i] = new ImageIcon("img/" + i + ".png");
}
this.win = new ImageIcon("img/win.png");
// in.close();
}
public String getName() {
return this.name;
}
public int getNumCorrect() {
return this.numCorrect;
}
public void newWord() {
// makes a dictionary and creates the censored word
this.d = new Dictionary(filename);
this.word = this.d.getWord();
for (int i = 0; i < this.word.length(); i++) {
if (this.word.charAt(i) == ' ')
this.progress += " ";
else this.progress += "_ ";
}
}
public boolean checkLetter(String letter){
int count = 0;
String n = "";
for (int i = 0; i < brokenword.length(); i++) {
if (brokenword.charAt(i) == letter.charAt(0)) {
// adds the correct letter to the new progress
n += brokenword.charAt(i);
count++;
} else {
if (this.progress.charAt(i) == '_') {
// if they missed add a _ to progress
n += "_";
} else {
// adds the char to the new string if it has already been
// found
n += brokenword.charAt(i);
}
}
}
this.progress = n;
// records score
if (count == 0){
lives--;
try {
playSound("img/no.wav");
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} else return true;
}
public int getLives() {
return lives;
}
public boolean wordEquals() {
return this.brokenword.equals(this.progress);
}
public String getWord() {
return word;
}
public ImageIcon[] getImages() {
return images;
}
public String getProgress() {
return progress;
}
public ImageIcon getImage() {
if (wordEquals())
return win;
return images[lives];
}
public void playSound(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException{
File soundFile = new File(file);
AudioInputStream input = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(input);
clip.start();
}
}