2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-02-25 10:17:26 +01:00
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (!globals.contains("irc")) {
|
|
|
|
globals.irc = log
|
2015-02-25 10:17:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hm = {
|
|
|
|
max_errors = 6
|
|
|
|
|
|
|
|
h_word = null
|
|
|
|
h_arr = null
|
|
|
|
guesses = 0
|
|
|
|
errors = 0
|
|
|
|
a_arr = null
|
|
|
|
misses = ""
|
|
|
|
|
2016-09-01 15:28:56 +02:00
|
|
|
if (irc) {
|
|
|
|
hangman_output = irc
|
|
|
|
} else {
|
|
|
|
hangman_output = log
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:17:26 +01:00
|
|
|
function str2arr(str) {
|
|
|
|
var arr = []
|
|
|
|
|
|
|
|
for (i in range(str.len())) {
|
|
|
|
arr.add(str.substr(i, 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
function init(s) {
|
|
|
|
s = s.upper()
|
|
|
|
h_word = s
|
|
|
|
h_arr = str2arr(h_word)
|
|
|
|
guesses = 0
|
|
|
|
errors = 0
|
|
|
|
a_arr = h_arr.clone()
|
|
|
|
misses = ""
|
|
|
|
|
|
|
|
for (x in range(a_arr.len())) {
|
|
|
|
a_arr[x] = (h_arr[x] == " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function print_progress() {
|
|
|
|
var ir
|
|
|
|
|
|
|
|
for (x in range(a_arr.len())) {
|
|
|
|
if (a_arr[x]) {
|
|
|
|
ir += h_arr[x]
|
|
|
|
} else {
|
|
|
|
ir += "_"
|
|
|
|
}
|
|
|
|
|
|
|
|
ir += " "
|
|
|
|
}
|
|
|
|
|
2016-09-01 15:28:56 +02:00
|
|
|
hangman_output(ir)
|
|
|
|
hangman_output(errors + "/" + (max_errors + 1) + " errors made: " + misses)
|
2015-02-25 10:17:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function create_hint() {
|
|
|
|
var r = Math.floor(Math.random() * a_arr.len())
|
|
|
|
if (!a_arr[r]) {
|
|
|
|
a_arr[r] = true
|
|
|
|
for (x in range(h_arr.len())) {
|
|
|
|
if (h_arr[x] == h_arr[r]) {
|
|
|
|
a_arr[x]=true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (a_arr.contains(false)) {
|
|
|
|
create_hint()
|
|
|
|
} else {
|
|
|
|
winner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!a_arr.contains(false)) {
|
|
|
|
winner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hint(i) {
|
|
|
|
for (x in range(i)) {
|
|
|
|
create_hint()
|
|
|
|
}
|
|
|
|
|
|
|
|
print_progress()
|
|
|
|
}
|
|
|
|
|
|
|
|
function winner() {
|
|
|
|
if (h_word) {
|
2016-09-01 15:28:56 +02:00
|
|
|
hangman_output("Congratulations, you are a winner in " + guesses + " guesses.")
|
2015-02-25 10:17:26 +01:00
|
|
|
h_word = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function guess(s) {
|
|
|
|
if (!h_word) {
|
2016-09-01 15:28:56 +02:00
|
|
|
hangman_output("Please set a word with hm.init(\"word\")")
|
2015-02-25 10:17:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s = s.upper()
|
|
|
|
|
|
|
|
if (s.len() != 1) {
|
2016-09-01 15:28:56 +02:00
|
|
|
hangman_output("NEIN!")
|
2015-02-25 10:17:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var correct = false
|
|
|
|
for (x in range(h_arr.len())) {
|
|
|
|
if (h_arr[x] == s) {
|
|
|
|
a_arr[x] = true
|
|
|
|
correct = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!correct) {
|
|
|
|
misses += s + " "
|
|
|
|
errors += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
print_progress()
|
|
|
|
|
|
|
|
guesses += 1
|
|
|
|
|
|
|
|
if (!a_arr.contains(false)) {
|
|
|
|
winner()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors > max_errors) {
|
2016-09-01 15:28:56 +02:00
|
|
|
hangman_output("You died...")
|
|
|
|
hangman_output(" ________")
|
|
|
|
hangman_output(" |/ |")
|
|
|
|
hangman_output(" | (_)")
|
|
|
|
hangman_output(" | \\|/")
|
|
|
|
hangman_output(" | |")
|
|
|
|
hangman_output(" | / \\")
|
|
|
|
hangman_output(" |")
|
|
|
|
hangman_output("_|___")
|
2015-02-25 10:17:26 +01:00
|
|
|
remove("h_word")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clone() {
|
|
|
|
var n = Dictionary.prototype.clone.call(this)
|
|
|
|
|
|
|
|
if (h_arr) {
|
|
|
|
n.h_arr = h_arr.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a_arr) {
|
|
|
|
n.a_arr = a_arr.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|