-
Notifications
You must be signed in to change notification settings - Fork 0
/
HangPlugin.java
116 lines (99 loc) · 2.72 KB
/
HangPlugin.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
import java.io.*;
import java.util.*;
import net.GUIpsp.GuiBot.*;
public class HangPlugin extends BasePlugin {
public void main() throws Throwable {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(
"/home/guipsp/Desktop/GuiBot/plugins/wordlist.txt")));
String tmp = reader.readLine();
while (tmp != null) {
choices.add(tmp);
tmp = reader.readLine();
}
reader.close();
registerCmd(
"guess_start",
getClass().getMethod("start", String.class, String.class,
String.class, String.class, String.class), this,
"Starts a new hangman game");
registerCmd(
"guess",
getClass().getMethod("guess", String.class, String.class,
String.class, String.class, String.class), this,
"Guesses a letter in the hangman game");
}
public String version() {
return "V1";
}
public String pluginName() {
return "Stupid Hangman without the man";
}
public String author() {
return "Rotten194";
}
public void start(String channel, String sender, String login,
String hostname, String message) {
if (isRunning == true) {
Main.bot.sendMessage(channel, "A game is already running!");
return;
} else {
newGame();
Main.bot.sendMessage(channel, "Started a game! " + print());
}
}
public void guess(String channel, String sender, String login,
String hostname, String message) {
if (isRunning)
guess(message, channel);
if (!isRunning) {
Main.bot.sendMessage(channel, "Guessing where? No game running");
} else if (!won() && isRunning == true) {
isRunning = false;
Main.bot.sendMessage(channel, "The word was \"" + word + "\", "
+ sender + " got it!");
} else {
Main.bot.sendMessage(channel, "Status: " + print());
}
}
public boolean isRunning;
public String word;
public String mask;
public ArrayList<String> choices = new ArrayList<String>();
public void newGame() {
isRunning = true;
word = choices.get(new Random().nextInt(choices.size()));
mask = "";
for (int i = 0; i < word.length(); i++)
mask += "*";
}
public void guess(String guess, String channel) {
if (guess.length() > 1)
Main.bot.sendMessage(channel, "Can't guess a word!");
else {
int cor = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess.charAt(0)) {
cor++;
mask = mask.substring(0, (i)) + " " + mask.substring(i + 1);
}
}
Main.bot.sendMessage(channel, (cor > 0 ? "You got " + cor
+ " characters!" : "Didn't find anything"));
}
}
public String print() {
String s = "";
for (int i = 0; i < (word.length()); i++) {
if (mask.charAt(i) == '*') {
s += "*";
} else {
s += word.charAt(i);
}
}
return s;
}
public boolean won() {
return mask.contains("*");
}
}