-
Notifications
You must be signed in to change notification settings - Fork 2
/
jeopardy.html
363 lines (327 loc) · 14.8 KB
/
jeopardy.html
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="include/bootstrap.min.css">
<script src="include/jquery-1.11.0.min.js"></script>
<script src="include/bootstrap.min.js"></script>
<style>
body { background:url(3840x2400.png) center center; }
.question{
height:100px;
vertical-align: middle;
text-align: center;
font-size: 1.5em;
padding-top: 35px;
cursor: pointer;
}
.unanswered:hover, .demo {
background-color:#8ADF82;
}
.origdouble { text-decoration:line-through; color:rgba(0,0,0,0.3); }
.modal-dialog { width:900px; }
.modal-dialog img { text-align:center; }
.modal-header h4 { text-shadow: 1px 1px 3px white; }
.modal-footer { font-size:2em; }
#question { font-size:3em; }
.panel { background:rgba(255,255,255,0.4); }
.panel-default > .panel-heading { background:rgba(245,245,245,0.6); }
.panel-footer { text-align: center; font-size:1.5em; background:rgba(245,245,245,0.5); }
pre { background:none; border:none; font-size:.7em; }
pre.small { font-size:.4em; }
.player { display:none; margin:15px; border-bottom: 5px solid; }
.player.well { background-color: white; }
.playernumber { float: right; font-size: small; padding-top: 0em; color: #bbb; }
#player1 { border-bottom-color: #27D500; } .answered1 { background-color: #27D500; } /* green */
#player2 { border-bottom-color: #D50000; } .answered2 { background-color: #D50000; } /* red */
#player3 { border-bottom-color: #00D5CB; } .answered3 { background-color: #00D5CB; } /* turquoise */
#player4 { border-bottom-color: #0060D5; } .answered4 { background-color: #0060D5; } /* blue */
#player5 { border-bottom-color: #D5B200; } .answered5 { background-color: #D5B200; } /* ochre */
#player6 { border-bottom-color: #D200D5; } .answered6 { background-color: #D200D5; } /* violet */
#player7 { border-bottom-color: #D57900; } .answered7 { background-color: #D57900; } /* orange */
#player8 { border-bottom-color: #009A93; } .answered8 { background-color: #009A93; } /* dark turquoise */
#player9 { border-bottom-color: #EE8EFE; } .answered9 { background-color: #EE8EFE; } /* pink */
</style>
</head>
<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading"></div>
<div class="panel-body" id="main-board"></div>
<div class="panel-footer">
<div id="player1" class="player col-md-2 well panel-body"><span class="playernumber">1</span><span id="playername1"></span><br /><span id="score1">0</span></div>
<div id="player2" class="player col-md-2 well panel-body"><span class="playernumber">2</span><span id="playername2"></span><br /><span id="score2">0</span></div>
<div id="player3" class="player col-md-2 well panel-body"><span class="playernumber">3</span><span id="playername3"></span><br /><span id="score3">0</span></div>
<div id="player4" class="player col-md-2 well panel-body"><span class="playernumber">4</span><span id="playername4"></span><br /><span id="score4">0</span></div>
<div id="player5" class="player col-md-2 well panel-body"><span class="playernumber">5</span><span id="playername5"></span><br /><span id="score5">0</span></div>
<div id="player6" class="player col-md-2 well panel-body"><span class="playernumber">6</span><span id="playername6"></span><br /><span id="score6">0</span></div>
<div id="player7" class="player col-md-2 well panel-body"><span class="playernumber">7</span><span id="playername7"></span><br /><span id="score7">0</span></div>
<div id="player8" class="player col-md-2 well panel-body"><span class="playernumber">8</span><span id="playername8"></span><br /><span id="score8">0</span></div>
<div id="player9" class="player col-md-2 well panel-body"><span class="playernumber">9</span><span id="playername9"></span><br /><span id="score9">0</span></div>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="modal fade" id="question-modal" role="dialog" aria-labelledby="question-modal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">
<span class="pull-right" id="player"></span>
<span id="currentquestion"></span> <span id="currentvalue"></h4>
</div>
<div class="modal-body">
<p id="question"></p>
</div>
<div class="modal-footer ">
<div id="answers" class="text-left">
</div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<audio src="include/think-music.mp3" type="audio/mp3"></audio>
<script>
var showmaster = true; // true: a showmaster enters the answers with + and - (answers not displayed) // false: display answers in multiple choice mode
boardfile = location.search.substr(1);
if (!boardfile) boardfile = 'board.json';
$(function(){
$.ajax({//ajax method to load the board.json and call the loadBoard() function on success
'async': false,
'global': false,
type:'GET',
dataType:'json',
url:boardfile,
success:function(data){
map = data;
loadBoard();
}
});
$('.unanswered').click(function(){//event bound to clicking on a tile. it grabs the data from the click event, populates the modal, fires the modal, and binds the answer method
var category = $(this).parent().data('category');
var question = $(this).data('question');
var isdouble = $(this).data('double');
var value = map[category].questions[question].value;
currenttileorigvalue = value;
if (isdouble) {
value = parseInt(prompt(" *** Double Jeopardy! ***\n\nHow many points do you want to risk?", value));
}
currenttilevalue = value;
currenttile = $(this);
var answers = $('#answers');
$('#currentquestion').empty().html(map[category].name)
if (isdouble) {
$('#currentvalue').empty().html('<span class="origdouble">' + currenttileorigvalue + '</span> ' + value );
} else {
$('#currentvalue').empty().html(value);
}
$('#question').empty().html(map[category].questions[question].question);
answers.empty();
$.each(map[category].questions[question].answers, function(i, answer){//loop over the answers and make buttons for each
if (showmaster) {
if (answer.correct) {
answers.append(answer.text);
$('.modal-footer').css('display', 'none');
}
} else {
answers.append(
'<button class="btn btn-danger answer" ' +
'data-category="'+category+'"' +
'data-question="'+question+'"' +
'data-value="'+value+'"' +
'data-correct="'+answer.correct+'"' +
'>'+ answer.text+'</button><br><br>'
)
}
});
$('#question-modal').modal('show');//fire modal
console.log(category, question);
console.log(map[category].questions[question]);
handleAnswer(); //bind answer onclick to newly created buttons
});
$(document).keypress(function(event){
keycode = event.which;
if (keycode >= 49 && keycode <= 57) { // 1..9
player = event.which - 48;
playerBuzzed(player);
} else if (keycode == 27 || keycode == 120) { // escape or x - hide question
hideQuestion();
setCurrentPlayer(0);
} else if (keycode == 43) { // +
rightAnswer();
} else if (keycode == 45) { // -
wrongAnswer();
} else if (keycode == 97) { // a
showAnswer();
} else if (keycode == 110) { // n
noAnswer();
} else if (keycode == 109) { // m
playMusic(); // requires include/think-music.mp3
} else if (keycode == 115) { // s
stopMusic();
} else if (keycode == 48) { // 0
setCurrentPlayer(0); // if a player accidently buzzed
} else if (keycode == 100) { // d
demomode = 1 - demomode; // switch demoRunner on/off
demoRunner();
} else if (keycode == 0 || keycode == 108) { // function key etc. or l
alert('board and input locked');
} else {
console.log('unhandled key pressed, keycode ' + keycode);
}
return false; // cancel any keypress events
});
});
var players = [];
var currentplayer = 0;
var score = [];
var map;
var currenttile;
var currenttilevalue = 0;
var currenttileorigvalue = 0;
var demomode = 0;
function playerBuzzed(player) {
if (! players[player]) { // Player not registered yet, ask for name
var playername = prompt("Player " + player + ", please enter your name", "Player " + player);
if (playername != null) {
players[player] = playername;
score[player] = 0;
$('#playername' + player).text(playername);
$('#player' + player).css("display", "block");
// document.getElementById("demo").innerHTML = "Hello " + person + "! How are you today?";
}
} else { // known player buzzed
// check if question is taken. if not, take it
if (currentplayer == 0) {
if ($('#question-modal').css("display") == "block") { // only make player active if a question is displayed
setCurrentPlayer(player);
}
}
}
}
function setCurrentPlayer(player) {
if (player == 0) {
if (currentplayer != 0) {
$('#player' + currentplayer).css("background-color", "white");
$('.modal-header').css("background-color", "white");
}
playername = '';
} else {
playername = players[player];
bgcolor = $('#player' + player).css("border-bottom-color");
$('#player' + player).css("background-color", bgcolor);
$('.modal-header').css("background-color", bgcolor);
}
currentplayer = player;
$('#player').text(playername);
stopMusic();
}
function noAnswer() {
if (currenttilevalue == 0) { return; }
// mark tile as not answered
currenttile.empty().removeClass('unanswered').unbind().css('cursor','not-allowed');
hideQuestion();
setCurrentPlayer(0);
}
function rightAnswer() {
if (currentplayer == 0) { return; }
// mark tile as answered
currenttile.empty().removeClass('unanswered').unbind().css('cursor','not-allowed');
currenttile.addClass('answered' + currentplayer);
// add score for currentplayer
score[currentplayer] += currenttilevalue;
updateScore();
hideQuestion();
setCurrentPlayer(0);
}
function wrongAnswer() {
if (currentplayer == 0) { return; }
score[currentplayer] -= currenttilevalue;
currenttilevalue = currenttileorigvalue;
$('#currentvalue').html(currenttilevalue);
updateScore();
setCurrentPlayer(0);
}
function loadBoard(){//function that turns the board.json (loaded in the the map variable) into a jeopardy board
var board = $('#main-board');
var columns = map.length;
var column_width = parseInt(12/columns); //get the width/12 rounded down, to use the bootstrap column width appropriate for the number of categories
var tile_count = 0;
console.log(columns);
$.each(map, function(i,category){
//load category name
var header_class = 'text-center col-md-' + column_width;
if (i === 0 && columns % 2 != 0){ //if the number of columns is odd, offset the first one by one to center them
header_class += ' col-md-offset-1';
}
$('.panel-heading').append(
'<div class="'+header_class+'"><h4>'+category.name+'</h4></div>'
);
//add column
var div_class = 'category col-md-' + column_width;
if (i === 0 && columns % 2 != 0){
div_class += ' col-md-offset-1';
}
board.append('<div class="'+div_class+'" id="cat-'+i+'" data-category="'+i+'"></div>');
var column = $('#cat-'+i);
$.each(category.questions, function(n,question){
//add questions
tile_count ++;
double = 0;
if (question.double) { double = 1; }
column.append('<div class="well question unanswered" id="tile' + tile_count + '" data-question="'+n+'" data-double="' + double + '">'+question.value+'</div>')
});
});
$('.panel-heading').append('<div class="clearfix"></div>')
}
function showAnswer(){
$('.modal-footer').show(500);
}
function hideQuestion(){
$('#question-modal').modal('hide');
currenttile = null;
currenttilevalue = 0;
}
function updateScore(){
$('#score' + currentplayer).text(score[currentplayer]);
}
function handleAnswer(){
$('.answer').click(function(){// hide empty the tile, mike it unclickable, update the score if correct, and hide the modal
var tile= $('div[data-category="'+$(this).data('category')+'"]>[data-question="'+$(this).data('question')+'"]')[0];
$(tile).empty().removeClass('unanswered').unbind().css('cursor','not-allowed');
if ($(this).data('correct')){
score[currentplayer] += parseInt($(this).data('value'));
$(tile).addClass('answered' + currentplayer);
}
hideQuestion();
updateScore();
setCurrentPlayer(0);
})
}
function playMusic() {
$('audio')[0].play();
}
function stopMusic() {
$('audio')[0].pause(); // Stop playing
$('audio')[0].currentTime = 0; // Reset time
}
// Order of fields in demo mode. Made for a 5x5 field, needs adjustment to look good at other field sizes.
var demo_order = [1,6,11,16,21,22,23,24,25,20,15,10,5,4,3,2,7,12,17,18,19,14,9,8,13,13,18,19,14,9,8,7,12,17,18,19,20,15,10,5,4,3, // spiral
2,1,6,11,16,21,22,17,12,7,2,3,8,13,18,23,24,19,14,9,4,5,10,15,20,25, // horicontal zigzag
24,23,22,21,16,17,18,19,20,15,14,13,12,11,6,7,8,9,10,5,4,3,2 // vertical zigzag
];
var demo_last_tile = -1;
function demoRunner(){
$('#tile' + demo_order[demo_last_tile]).removeClass('demo');
demo_last_tile ++;
if (demo_last_tile > demo_order.length) demo_last_tile = 0;
if (demomode) {
$('#tile' + demo_order[demo_last_tile]).addClass('demo');
setTimeout(function(){ demoRunner(); }, 100);
}
}
</script>
</body>
</html>
<!--
vim:expandtab
-->