forked from countable/cats-of-jasnah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cats-of-jasnah.js
202 lines (187 loc) · 5.14 KB
/
cats-of-jasnah.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const COLOR_ATTS = ['red', 'blue', 'yellow']
const MOTION_ATTS = ['bouncing', 'spinning']
const ANIMAL_ATTS = ['ducks']
const ALL_ATTS = COLOR_ATTS.concat(MOTION_ATTS).concat(ANIMAL_ATTS)
let ATTS
let cur_atts = {}
let cur_level = 0
let clue
const pick_rand = function(seq) {
return seq[Math.floor(Math.random() * seq.length)]
}
const set_level = function(level) {
// initialization
$('.instructions').hide()
$('#forkongithub').hide()
$('.number-bar').show()
$('.level-display').show()
$('.level-number').text(level)
cur_level = level
make_cats()
}
// returns whether a given value is to be negated.
const level_get_negation = function(level) {
if (level === 2) return false
if (level === 6) return true
if (level >= 5) return Math.random() > 0.4
return true
}
const cats_for_level = function(level) {
min_cats = level + 1
max_cats = level + 5
return Math.floor(Math.random() * (max_cats + 1 - min_cats)) + min_cats
}
const set_avail_atts = function() {
if (cur_level == 0) {
ATTS = []
} else if (cur_level <= 2) {
ATTS = [pick_rand(ALL_ATTS)]
} else if (cur_level <= 5) {
ATTS = [pick_rand(COLOR_ATTS), pick_rand(MOTION_ATTS)]
} else {
ATTS = [
pick_rand(COLOR_ATTS),
pick_rand(MOTION_ATTS),
pick_rand(ANIMAL_ATTS)
]
}
}
const get_level_num_adjectives = function(level, keys) {
// get the number of categories used as adjectives, given a list of
// categories. More is generally
// easier.
let pos = Math.floor(Math.random() * (keys.length + 1))
if (level == 3) pos = keys.length // combination level
if (level == 4) pos = 0 // conjunction level
if (level < 7) {
// all but one an adjective before level 7
if (pos > keys.length - 1) {
pos = keys.length - 1
}
} else {
pos = 0
}
return pos
}
const permute_atts = function() {
cur_atts = {}
for (let i = 0; i < ATTS.length; i++) {
if (Math.random() > 0.2) {
cur_atts[ATTS[i]] = level_get_negation(cur_level)
}
}
}
const chance_for_level = function(level) {
return 0.5 + Math.random() * (level / 40) // increased match chance as levels progress, sometimes.
}
const speak = function(text, opts) {
opts = opts || {}
$('p').text(text)
responsiveVoice.speak(text, 'US English Female', opts)
}
var make_cats = function() {
set_avail_atts()
permute_atts()
is_reversed = Math.random() < 0.5
let text = 'how many '
const keys = Object.keys(cur_atts)
const prefix_pos = get_level_num_adjectives(cur_level, keys)
const prefix_keys = keys.slice(0, prefix_pos)
if (prefix_keys.length) {
let prefix_words = []
for (let att in prefix_keys) {
prefix_words.push(
(cur_atts[prefix_keys[att]] ? '' : 'not-') + prefix_keys[att]
)
}
text += prefix_words.join(', ') + ' '
// ducks is just 'duck' when used as an adjective
text = text.replace('ducks', 'duck')
}
postfix_keys = keys.slice(prefix_pos)
if (postfix_keys.length) {
text += 'cats are '
let conjunctions = []
for (let att in postfix_keys) {
conjunctions.push(
(cur_atts[postfix_keys[att]] ? '' : 'not ') + postfix_keys[att]
)
}
console.log(conjunctions)
text += conjunctions.join(' and are ')
} else {
text += 'cats are here'
}
text += '?'
// substitution.
if (Math.random() < 0.5) text = text.replace(/not.blue/, 'white')
if (Math.random() < 0.5) text = text.replace(/not.red/, 'white')
if (Math.random() < 0.5) text = text.replace(/not.yellow/, 'white')
clue = text
speak(text)
// remove existing cats and add new ones for the current level.
$('svg:gt(0)').remove()
var num_cats = cats_for_level(cur_level)
for (var i = 0; i < num_cats; i++) {
$('svg')
.eq(0)
.clone()
.appendTo('body')
.each(function(svg) {
const $t = $(this)
$(this).removeClass('hidden')
for (var att = 0; att < ATTS.length; att++) {
if (cur_atts[ATTS[att]] === true) {
chance = chance_for_level(cur_level)
} else if (cur_atts[ATTS[att]] === false) {
chance = 1 - chance_for_level(cur_level)
} else {
chance = 0.5
}
console.log(chance)
if (Math.random() < chance) $t.addClass(ATTS[att])
}
})
}
if (get_answer().length > 9) {
console.log('Too many cats, generate a new puzzle.')
make_cats()
}
}
const sound = function(s) {
var snd = new Audio(s + '.mp3')
snd.play()
}
$('body').keyup(function(e) {
if (!/\d/.test(e.key)) return
submit(parseInt(e.key))
})
const get_answer = function() {
return $('svg:visible').filter(function(svg) {
let match = true
for (let att in cur_atts) {
match =
match &&
(cur_atts[att] ? $(this).hasClass(att) : !$(this).hasClass(att))
}
return match
})
}
const submit = function(value) {
let answer = get_answer()
answer.addClass('circle')
console.log('answer:', answer.length)
if (value === answer.length) {
speak("That's right, " + answer.length, {
onend: function() {
make_cats()
}
})
} else {
speak("Good try, but that's wrong.", {
onend: function() {
speak(clue)
}
})
}
}