]> granicus.if.org Git - icinga2/blob - itl/hangman
Merge pull request #7210 from Icinga/bugfix/boost-asio-deprecated
[icinga2] / itl / hangman
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 if (!globals.contains("irc")) {
4         globals.irc = log
5 }
6
7 hm = {
8         max_errors = 6
9
10         h_word = null
11         h_arr = null
12         guesses = 0
13         errors = 0
14         a_arr = null
15         misses = ""
16
17         if (irc) {
18                 hangman_output = irc
19         } else {
20                 hangman_output = log
21         }
22
23         function str2arr(str) { 
24                 var arr = []
25
26                 for (i in range(str.len())) { 
27                         arr.add(str.substr(i, 1))
28                 }
29
30                 return arr
31         }
32
33         function init(s) {
34                 s = s.upper()
35                 h_word = s
36                 h_arr = str2arr(h_word)
37                 guesses = 0
38                 errors = 0
39                 a_arr = h_arr.clone()
40                 misses = ""
41
42                 for (x in range(a_arr.len())) {
43                         a_arr[x] = (h_arr[x] == " ")
44                 }
45         }
46
47         function print_progress() {
48                 var ir
49
50                 for (x in range(a_arr.len())) {
51                         if (a_arr[x]) {
52                                 ir += h_arr[x]
53                         } else {
54                                 ir += "_"
55                         }
56
57                         ir += " "
58                 }
59
60                 hangman_output(ir)
61                 hangman_output(errors + "/" + (max_errors + 1) + " errors made: " + misses)
62         }
63
64         function create_hint() {
65                 var r = Math.floor(Math.random() * a_arr.len())
66                 if (!a_arr[r]) {
67                         a_arr[r] = true
68                         for (x in range(h_arr.len())) {
69                                 if (h_arr[x] == h_arr[r]) {
70                                         a_arr[x]=true
71                                 }
72                         }
73                 } else {
74                         if (a_arr.contains(false)) {
75                                 create_hint()
76                         } else {
77                                 winner()
78                         }
79                 }
80
81                 if (!a_arr.contains(false)) {
82                         winner()
83                 }
84         }
85
86         function hint(i) {
87                 for (x in range(i)) {
88                         create_hint()
89                 }
90
91                 print_progress()
92         }
93
94         function winner() {
95                 if (h_word) {
96                         hangman_output("Congratulations, you are a winner in " + guesses + " guesses.")
97                         h_word = null
98                 }
99         }
100
101         function guess(s) {
102                 if (!h_word) {
103                         hangman_output("Please set a word with hm.init(\"word\")")
104                         return
105                 }
106
107                 s = s.upper()
108
109                 if (s.len() != 1) {
110                         hangman_output("NEIN!")
111                         return
112                 }       
113
114                 var correct = false
115                 for (x in range(h_arr.len())) {
116                         if (h_arr[x] == s) {
117                                 a_arr[x] = true
118                                 correct = true
119                         }
120                 }
121
122                 if (!correct) {
123                         misses += s + " "
124                         errors += 1
125                 }
126                 
127                 print_progress()
128
129                 guesses += 1
130
131                 if (!a_arr.contains(false)) {
132                         winner()
133                         return
134                 }
135
136                 if (errors > max_errors) {
137                         hangman_output("You died...")
138                         hangman_output(" ________")
139                         hangman_output(" |/      |")
140                         hangman_output(" |      (_)")
141                         hangman_output(" |      \\|/")
142                         hangman_output(" |       |")
143                         hangman_output(" |      / \\")
144                         hangman_output(" |")
145                         hangman_output("_|___")
146                         remove("h_word")
147                         return
148                 }
149         }
150
151         function clone() {
152                 var n = Dictionary.prototype.clone.call(this)
153
154                 if (h_arr) {
155                         n.h_arr = h_arr.clone()
156                 }
157
158                 if (a_arr) {
159                         n.a_arr = a_arr.clone()
160                 }
161
162                 return n
163         }
164 }
165