]> granicus.if.org Git - icinga2/blob - itl/hangman
Implement URL handler for /v1
[icinga2] / itl / hangman
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 if (!globals.irc) {
21         globals.irc = globals.log
22 }
23
24 hm = {
25         max_errors = 6
26
27         h_word = null
28         h_arr = null
29         guesses = 0
30         errors = 0
31         a_arr = null
32         misses = ""
33
34         function str2arr(str) { 
35                 var arr = []
36
37                 for (i in range(str.len())) { 
38                         arr.add(str.substr(i, 1))
39                 }
40
41                 return arr
42         }
43
44         function init(s) {
45                 s = s.upper()
46                 h_word = s
47                 h_arr = str2arr(h_word)
48                 guesses = 0
49                 errors = 0
50                 a_arr = h_arr.clone()
51                 misses = ""
52
53                 for (x in range(a_arr.len())) {
54                         a_arr[x] = (h_arr[x] == " ")
55                 }
56         }
57
58         function print_progress() {
59                 var ir
60
61                 for (x in range(a_arr.len())) {
62                         if (a_arr[x]) {
63                                 ir += h_arr[x]
64                         } else {
65                                 ir += "_"
66                         }
67
68                         ir += " "
69                 }
70
71                 irc(ir)
72                 irc(errors + "/" + (max_errors + 1) + " errors made: " + misses)
73         }
74
75         function create_hint() {
76                 var r = Math.floor(Math.random() * a_arr.len())
77                 if (!a_arr[r]) {
78                         a_arr[r] = true
79                         for (x in range(h_arr.len())) {
80                                 if (h_arr[x] == h_arr[r]) {
81                                         a_arr[x]=true
82                                 }
83                         }
84                 } else {
85                         if (a_arr.contains(false)) {
86                                 create_hint()
87                         } else {
88                                 winner()
89                         }
90                 }
91
92                 if (!a_arr.contains(false)) {
93                         winner()
94                 }
95         }
96
97         function hint(i) {
98                 for (x in range(i)) {
99                         create_hint()
100                 }
101
102                 print_progress()
103         }
104
105         function winner() {
106                 if (h_word) {
107                         irc("Congratulations, you are a winner in " + guesses + " guesses.")
108                         h_word = null
109                 }
110         }
111
112         function guess(s) {
113                 if (!h_word) {
114                         irc("Please set a word with hm.init(\"word\")")
115                         return
116                 }
117
118                 s = s.upper()
119
120                 if (s.len() != 1) {
121                         irc("NEIN!")
122                         return
123                 }       
124
125                 var correct = false
126                 for (x in range(h_arr.len())) {
127                         if (h_arr[x] == s) {
128                                 a_arr[x] = true
129                                 correct = true
130                         }
131                 }
132
133                 if (!correct) {
134                         misses += s + " "
135                         errors += 1
136                 }
137                 
138                 print_progress()
139
140                 guesses += 1
141
142                 if (!a_arr.contains(false)) {
143                         winner()
144                         return
145                 }
146
147                 if (errors > max_errors) {
148                         irc("You died...")
149                         irc(" ________")
150                         irc(" |/      |")
151                         irc(" |      (_)")
152                         irc(" |      \\|/")
153                         irc(" |       |")
154                         irc(" |      / \\")
155                         irc(" |")
156                         irc("_|___")
157                         remove("h_word")
158                         return
159                 }
160         }
161
162         function clone() {
163                 var n = Dictionary.prototype.clone.call(this)
164
165                 if (h_arr) {
166                         n.h_arr = h_arr.clone()
167                 }
168
169                 if (a_arr) {
170                         n.a_arr = a_arr.clone()
171                 }
172
173                 return n
174         }
175 }
176