]> granicus.if.org Git - icinga2/blob - itl/hangman
ITL: Fix missing double quote in ssl-cert CheckCommand
[icinga2] / itl / hangman
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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         if (irc) {
35                 hangman_output = irc
36         } else {
37                 hangman_output = log
38         }
39
40         function str2arr(str) { 
41                 var arr = []
42
43                 for (i in range(str.len())) { 
44                         arr.add(str.substr(i, 1))
45                 }
46
47                 return arr
48         }
49
50         function init(s) {
51                 s = s.upper()
52                 h_word = s
53                 h_arr = str2arr(h_word)
54                 guesses = 0
55                 errors = 0
56                 a_arr = h_arr.clone()
57                 misses = ""
58
59                 for (x in range(a_arr.len())) {
60                         a_arr[x] = (h_arr[x] == " ")
61                 }
62         }
63
64         function print_progress() {
65                 var ir
66
67                 for (x in range(a_arr.len())) {
68                         if (a_arr[x]) {
69                                 ir += h_arr[x]
70                         } else {
71                                 ir += "_"
72                         }
73
74                         ir += " "
75                 }
76
77                 hangman_output(ir)
78                 hangman_output(errors + "/" + (max_errors + 1) + " errors made: " + misses)
79         }
80
81         function create_hint() {
82                 var r = Math.floor(Math.random() * a_arr.len())
83                 if (!a_arr[r]) {
84                         a_arr[r] = true
85                         for (x in range(h_arr.len())) {
86                                 if (h_arr[x] == h_arr[r]) {
87                                         a_arr[x]=true
88                                 }
89                         }
90                 } else {
91                         if (a_arr.contains(false)) {
92                                 create_hint()
93                         } else {
94                                 winner()
95                         }
96                 }
97
98                 if (!a_arr.contains(false)) {
99                         winner()
100                 }
101         }
102
103         function hint(i) {
104                 for (x in range(i)) {
105                         create_hint()
106                 }
107
108                 print_progress()
109         }
110
111         function winner() {
112                 if (h_word) {
113                         hangman_output("Congratulations, you are a winner in " + guesses + " guesses.")
114                         h_word = null
115                 }
116         }
117
118         function guess(s) {
119                 if (!h_word) {
120                         hangman_output("Please set a word with hm.init(\"word\")")
121                         return
122                 }
123
124                 s = s.upper()
125
126                 if (s.len() != 1) {
127                         hangman_output("NEIN!")
128                         return
129                 }       
130
131                 var correct = false
132                 for (x in range(h_arr.len())) {
133                         if (h_arr[x] == s) {
134                                 a_arr[x] = true
135                                 correct = true
136                         }
137                 }
138
139                 if (!correct) {
140                         misses += s + " "
141                         errors += 1
142                 }
143                 
144                 print_progress()
145
146                 guesses += 1
147
148                 if (!a_arr.contains(false)) {
149                         winner()
150                         return
151                 }
152
153                 if (errors > max_errors) {
154                         hangman_output("You died...")
155                         hangman_output(" ________")
156                         hangman_output(" |/      |")
157                         hangman_output(" |      (_)")
158                         hangman_output(" |      \\|/")
159                         hangman_output(" |       |")
160                         hangman_output(" |      / \\")
161                         hangman_output(" |")
162                         hangman_output("_|___")
163                         remove("h_word")
164                         return
165                 }
166         }
167
168         function clone() {
169                 var n = Dictionary.prototype.clone.call(this)
170
171                 if (h_arr) {
172                         n.h_arr = h_arr.clone()
173                 }
174
175                 if (a_arr) {
176                         n.a_arr = a_arr.clone()
177                 }
178
179                 return n
180         }
181 }
182