-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluedo.js
1767 lines (1554 loc) · 51 KB
/
cluedo.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// cluedo.js
//
// Automatic Cluedo Player
// by Saumil Shah
// globals
var persons; // array for storing person names
var weapons; // array for storing weapon names
var rooms; // array for storing room names
var cards; // concatenation of persons+weapons+rooms
var tuples; // all tuple combinations
var players; // player details. name, current room, shadowing whom, etc
var probability; // probability table cards x players+murder_column
var marked; // boolean matrix for modifications to the probability table
var command_input;
var commands;
var textarea;
var command_stack;
var last_command;
var me; // my player index
var queries; // list of queries asked
var game_started; // once the game is started, changes are locked out
var tuple_sort; // function for sorting the tuples
var murder_column; // value = players.length
var murder_person_found;
var murder_weapon_found;
var murder_room_found;
var murder_card_row_cell;
var card_row; // array for the row of cards
var probability_fix = false; // EXPERIMENTAL
var image_dir = "card_images";
// initialise everything
// invoked after document is loaded
function init() {
persons = [
"Scarlett",
"Plum",
"Mustard",
"Green",
"Peacock",
"White"
];
weapons = [
"Dagger",
"Revolver",
"Rope",
"Candlestick",
"LeadPipe",
"Spanner"
];
rooms = [
"Hall",
"Lounge",
"DiningRoom",
"Conservatory",
"Ballroom",
"Kitchen",
"BilliardRoom",
"Library",
"Study"
];
game_started = false;
murder_person_found = false;
murder_weapon_found = false;
murder_room_found = false;
me = -1;
// populate the cards array and the held_by array
var allcards = persons.concat(weapons.concat(rooms));
cards = new Array(allcards.length);
for(var i = 0; i < allcards.length; i++) {
cards[i] = new Object;
cards[i].name = allcards[i]; // card name
cards[i].held_by = -1; // who holds this card?
cards[i].queried = 0; // how many times was it queried by?
}
// bind the Enter key to click the GO button
// and eventually invoke the command parser
command_input = document.getElementById("command");
command_input.value = "";
var run = document.getElementById("run");
document.getElementById("slowmotion").checked = true;
run.onclick = function() { parser(command_input.value);};
command_stack = new Array();
last_command = 0;
command_input.addEventListener("keyup", function(event) {
event.preventDefault();
switch(event.keyCode) {
case 13: // enter
run.click();
this.value = "";
break;
case 38: // up arrow
if(last_command > 0) {
this.value = command_stack[--last_command];
}
break;
case 40: // down arrow
if(last_command < command_stack.length - 1) {
this.value = command_stack[++last_command];
}
break;
case 27: // escape
last_command = command_stack.length;
this.value = "";
}
});
textarea = document.getElementById("command_history");
// setup command table and make room for display rows
// accordingly in the textarea
textarea.rows = setup_command_table() + 2;
textarea.value = "";
textarea.readOnly = "true";
// set the query history to empty
queries = new Array();
// bind the Sort By radio buttons
var radio_pass = document.getElementById("pass");
var radio_respond = document.getElementById("respond");
var radio_murder = document.getElementById("murder");
radio_murder.checked = true;
tuple_sort = sort_by_murder;
radio_pass.onchange = radio_change;
radio_respond.onchange = radio_change;
radio_murder.onchange = radio_change;
// show the help text first
cmd_help();
console.log("init end");
};
// function to draw the row of cards
function draw_row_of_cards() {
if(typeof(players) == "undefined") {
return;
}
// initialise the card_row array
card_row = new Array(players.length + 1);
for(var i = 0; i < players.length; i++) {
card_row[i] = new Array(players[i].card_count);
}
card_row[murder_column] = new Array(3);
var output = dom_clear("row_of_cards");
var table = document.createElement("table");
var tr = document.createElement("tr");
var p = 0;
var c = 0;
for(var i = 0; i < cards.length; i++) {
var td = document.createElement("td");
var image = document.createElement("img");
image.className = "autoresizeimage";
image.src = cardface("unknown");
td.appendChild(image);
card_row[p][c++] = image;
if(c == card_row[p].length) {
c = 0;
p++;
}
tr.appendChild(td);
}
table.appendChild(tr);
tr = document.createElement("tr");
for(var i = 0; i < players.length; i++) {
var td = document.createElement("td");
td.colSpan = card_row[i].length;
td.className = "goldenyellow";
var div1 = document.createElement("div");
var div2 = document.createElement("div");
div1.innerHTML = players[i].name;
td.appendChild(div1);
td.appendChild(div2);
players[i].card_row_cell = td;
tr.appendChild(td);
}
td = document.createElement("td");
td.colSpan = 3;
td.className = "background-orange";
var div1 = document.createElement("div");
var div2 = document.createElement("div");
div1.innerHTML = "Murder Cards";
td.appendChild(div1);
td.appendChild(div2);
murder_card_row_cell = td;
tr.appendChild(td);
table.appendChild(tr);
output.appendChild(table);
}
// function to assemble an image URL and return it
function cardface(card) {
var image_src = image_dir + "/" + card.toLowerCase() + ".png";
return(image_src);
}
// function to clear the output DOM element by ID
// and return it
function dom_clear(id) {
var retval = document.getElementById(id);
while(retval.firstChild)
retval.removeChild(retval.firstChild);
return(retval);
}
var radio_change = function() {
var radio_pass = document.getElementById("pass");
var radio_respond = document.getElementById("respond");
var radio_murder = document.getElementById("murder");
if(radio_pass.checked) {
tuple_sort = sort_by_passing;
}
else {
if(radio_respond.checked) {
tuple_sort = sort_by_responding;
}
else {
tuple_sort = sort_by_murder;
}
}
recalc_tuple_probabilities();
}
function setup_command_table() {
var counter = 0;
commands = [
[
"help", // [0] command name
false, // [1] parameters required
cmd_help, // [2] function to invoke
"- list available commands", // [3] help text
false, // [4] add to command history stack
true // [5] display in the help command list
],
[
"set players",
true,
cmd_set_players,
"<list of player names beginning with the dealer>",
true,
true
],
[
"i am",
true,
cmd_who_am_i,
"<player name that will be played by the computer>",
true,
true
],
[
"i have",
true,
cmd_i_have_card,
"<name of card I have in my hand>",
true,
true
],
[
"assign person",
true,
cmd_assign_person,
"<person player>",
true,
true
],
[
"begin play",
false,
cmd_begin_play,
"- Once play starts, changes are locked out",
true,
true
],
[
"my room",
true,
cmd_set_my_room,
"<name of the room I am in during this turn>",
true,
false
],
[
"enter room",
true,
cmd_set_room,
"<player room>",
true,
true
],
[
"exit room",
true,
cmd_leave_room,
"<player>",
true,
true
],
[
"query by",
true,
cmd_query_by,
"<player/me person weapon room>",
true,
true
],
[
"response by",
true,
cmd_response_by,
"<player card>",
true,
true
],
[
"response passed",
false,
cmd_response_passed,
"- when no one has any card belonging to the query",
true,
true
],
[
"show history",
false,
cmd_print_history,
"- prints the command history [info]",
false,
true
],
[
"print tuples",
false,
cmd_print_tuples,
"- prints possible murder combinations still remaining [info]",
false,
false
],
[
"print probabilities",
false,
cmd_print_probability_table,
"- prints the probability table [info]",
false,
false
],
[
"print queries",
false,
cmd_print_queries,
"- prints the query history [info]",
false,
false
],
[
"initialise probabilities",
false,
cmd_initialise_probability_table,
"- warning will reset the whole game! [debug]",
false,
false
]
];
for(var i = 0; i < commands.length; i++) {
if(commands[i][5]) {
counter++;
}
}
return(counter);
}
// can submit a semicolon separated list of multiple commands
// useful especially when testing an entire game script
function parser(command_string) {
var command_list = command_string.replace(/\s*\/\*[^\*]*\*\/\s*/g, "")
.replace(/\s*;\s+/g, ";")
.replace(/;+/g, ";")
.replace(/\s+/g, " ")
.replace(/(\s*;$)|(^\s*;)/g, "").split(/;/);
if(command_list[0] == "") {
console.log("empty command string");
return;
}
console.log(command_list.length + " commands submitted");
var interval = 0;
var slowmotion = document.getElementById("slowmotion").checked;
for(var i = 0; i < command_list.length; i++) {
var f = function(x) {run_single_command(x);};
setTimeout(f, interval, command_list[i]);
interval += slowmotion ? 1000 : 100; // 1 second intervals
}
}
// run a single Cluedo command
var run_single_command = function(command_string) {
var command_array = command_string.replace(/[^A-Za-z0-9]/g, " ")
.replace(/\s+/g, " ")
.replace(/\s$/g, "").split(/ /);
var command = command_array[0];
// "help" is the only command that has no parameters
// all other commands are two words long.
if(command != "help") {
command += " " + command_array[1];
}
command_array.shift();
command_array.shift();
var params = command_array.join(" ");
for(var i = 0; i < commands.length; i++) {
if(command.toLowerCase() == commands[i][0]) {
var params_required = commands[i][1];
var command_function = commands[i][2];
var response;
if(params_required) {
response = command_function(params);
}
else {
response = command_function();
}
if(commands[i][4]) {
// if the command has to be recorded in the command history
add_command_history(commands[i][0], params, response);
}
return;
}
}
console.log(command + ": not a valid command");
}
function add_command_history(command, params, response) {
var command_string = command;
var command_string;
if(params != "") {
command_string += " " + params;
}
command_string += " /* " + response + " */";
command_stack.push(command_string);
// record the command in the history box
textarea.value = command_string + "\n" + textarea.value;
last_command = command_stack.length;
}
// create an array of all possible tuples
// this will be called only after the computer
// is mapped to a player using the "I am" command
function generate_combinations() {
tuples = [];
for(var i = 0; i < persons.length; i++) {
for(var j = 0; j < weapons.length; j++) {
for(var k = 0; k < rooms.length; k++) {
var tuple = new Object;
tuple.person = i;
tuple.weapon = j;
tuple.room = k;
tuple.eliminated = false;
tuple.pass_weight = 0;
tuple.show_weight = 0;
tuple.query_sum = 0; // sum of queries against each card
tuple.asked_by = -1;
tuple.candidate = false; // candidate for next query
// create an array of likelihoods of passing by players
tuple.pass_probability = new Array(players.length);
tuple.show_probability = new Array(players.length);
for(var p = 0; p < players.length; p++) {
tuple.pass_probability[p] = -1;
tuple.show_probability[p] = -1;
}
tuples.push(tuple);
}
}
}
}
// sort function for sorting tuples in descending order
// based on the probability of a player passing
var sort_by_passing = function(a, b) {
return(b.pass_weight - a.pass_weight);
}
// sort function for sorting tuples in ascending order
// based on the probability of a player responding
var sort_by_responding = function(a, b) {
return(a.show_weight - b.show_weight);
}
// sort function for sorting tuples in descending order
// based on the total probability of the murder cards
var sort_by_murder = function(a, b) {
if(b.murder_weight == a.murder_weight) {
return(a.query_sum - b.query_sum);
}
return(b.murder_weight - a.murder_weight);
}
// function to search for a tuple
function tuple_search(card1, card2, card3) {
var person = -1, weapon = -1, room = -1;
if(persons.indexOf(card1) != -1) { person = persons.indexOf(card1); }
if(persons.indexOf(card2) != -1) { person = persons.indexOf(card2); }
if(persons.indexOf(card3) != -1) { person = persons.indexOf(card3); }
if(weapons.indexOf(card1) != -1) { weapon = weapons.indexOf(card1); }
if(weapons.indexOf(card2) != -1) { weapon = weapons.indexOf(card2); }
if(weapons.indexOf(card3) != -1) { weapon = weapons.indexOf(card3); }
if(rooms.indexOf(card1) != -1) { room = rooms.indexOf(card1); }
if(rooms.indexOf(card2) != -1) { room = rooms.indexOf(card2); }
if(rooms.indexOf(card3) != -1) { room = rooms.indexOf(card3); }
for(var i = 0; i < tuples.length; i++) {
if(tuples[i].person == person &&
tuples[i].weapon == weapon &&
tuples[i].room == room) {
return(tuples[i]);
}
}
return(null);
}
// function to check whether the last query was
// answered or not
function query_outstanding() {
if(queries.length > 0) {
return(!queries[queries.length - 1].answered);
}
return(false);
}
// function to recalculate the remaining probabilities
// in the probability table
function recalc_remaining_probabilities() {
var changed = false;
var count, sum, p;
for(var i = 0; i < cards.length; i++) {
count = 0;
sum = 1;
for(var j = 0; j < players.length + 1; j++) {
if(!marked[i][j]) {
count++;
}
else {
sum -= probability[i][j];
}
}
p = sum / count;
for(var j = 0; j < players.length + 1; j++) {
if(!marked[i][j]) {
if(probability[i][j] != p) {
changed = true;
}
probability[i][j] = p;
if(p == 1) {
//cards[i].held_by = j;
mark_held_by(i, j);
}
}
}
}
// total up the player columns once again to
// see if all the cards have been inferred
for(var j = 0; j < players.length; j++) {
validate_player_cards(j);
}
// total up the murder column to see if
// a murder card is inferred
recalc_murder_probability(persons, get_person);
recalc_murder_probability(weapons, get_weapon);
recalc_murder_probability(rooms, get_room);
return(changed);
}
// function to mark a card as held by a specific player
// if the player turns out to be the murder column, then
// mark the boolean flags per card group (i.e. person/weapon/room)
// as discovered
//
// this function might get called multiple times for the same card, player
// pair
function mark_held_by(card, player) {
// if card is already marked as held by, then return
if(cards[card].held_by == player) {
return;
}
// show the card face in the card row
if(player != murder_column) {
card_row[player][players[player].cards_guessed].src =
cardface(cards[card].name);
}
else {
// ugly search
card_row[murder_column][get_murder_cards_guessed()].src =
cardface(cards[card].name);
}
// mark the card as held by the player/murder column
cards[card].held_by = player;
if(player == murder_column) {
if(persons.indexOf(cards[card].name) != -1) {
murder_person_found = true;
return;
}
if(weapons.indexOf(cards[card].name) != -1) {
murder_weapon_found = true;
return;
}
if(rooms.indexOf(cards[card].name) != -1) {
murder_room_found = true;
return;
}
}
}
// function to calculate probabilities for a tuple
// being a murder tuple. take the average probability
// of the room, person and weapon
function recalc_tuple_probabilities() {
var p_person, p_weapon, p_room;
var index_person, index_weapon, index_room;
var coefficient;
var highest_weight = 0;
var highest_murder_weight = 0;
var lowest_weight = Math.pow(10, players.length);
for(var i = 0; i < tuples.length; i++) {
index_person = get_person(tuples[i].person);
index_weapon = get_weapon(tuples[i].weapon);
index_room = get_room(tuples[i].room);
tuples[i].eliminated = false; // we shouldn't need to reset it, but..
tuples[i].candidate = false;
// calculate total murder probability of all the three cards
p_person = probability[index_person][murder_column];
p_weapon = probability[index_weapon][murder_column];
p_room = probability[index_room][murder_column];
// heuristic hack
// if the murder person/weapon/room has been discovered, then any
// person/weapon/room card that we hold can be set to an equal
// weight when used in a query
if(murder_person_found && cards[index_person].held_by == me) {
p_person = 1;
}
if(murder_weapon_found && cards[index_weapon].held_by == me) {
p_weapon = 1;
}
if(murder_room_found && cards[index_room].held_by == me) {
p_room = 1;
}
// heuristic hack
// sum up the number of times each card has been queried in a tuple.
// "Hot" cards are those that have been queried quite often.
// Prefer to use "cooler" cards in the query
tuples[i].query_sum = cards[index_person].queried +
cards[index_weapon].queried +
cards[index_room].queried;
tuples[i].murder_weight = p_person + p_weapon + p_room;
// if all tuple cards are held by me or in the murder column
// then eliminate that tuple from the query possibilities
if(me_or_murderer(index_person) && me_or_murderer(index_weapon) &&
me_or_murderer(index_room)) {
tuples[i].eliminated = true;
}
// if I have asked this query before, it's stupid of me to ask it
// again [THIS CASE SHOULD NEVER HAPPEN]
/*
if(tuples[i].asked_by == me) {
tuples[i].eliminated = true;
}
*/
// find the likelihood of player[x] passing to the query
// where x != me
var will_pass = new Array(players.length);
var will_show = new Array(players.length);
var next_player = get_next_responder(me);
coefficient = Math.pow(10, players.length - 1);
tuples[i].pass_weight = 0;
tuples[i].show_weight = 0;
while(me != -1 && next_player != me) {
p_person = probability[index_person][next_player];
p_weapon = probability[index_weapon][next_player];
p_room = probability[index_room][next_player];
will_pass[next_player] = (1-p_person) * (1-p_weapon) * (1-p_room);
will_show[next_player] = maximum(p_person, p_weapon, p_room);
var j = get_next_responder(me);
tuples[i].pass_probability[next_player] = will_pass[next_player];
tuples[i].show_probability[next_player] = will_show[next_player];
while(j != next_player) {
tuples[i].pass_probability[next_player] *= will_pass[j];
tuples[i].show_probability[next_player] *= will_pass[j];
j = get_next_responder(j);
}
tuples[i].pass_weight += tuples[i].pass_probability[next_player] *
coefficient;
tuples[i].show_weight += tuples[i].show_probability[next_player] *
coefficient;
coefficient = coefficient / 10;
if(tuple_sort == sort_by_passing) {
if(tuples[i].pass_probability[next_player] == 0) {
tuples[i].eliminated = true;
}
}
else { // we assume that the same tuples will get eliminated
if(tuples[i].show_probability[next_player] == 1) {
tuples[i].eliminated = true;
}
}
next_player = get_next_responder(next_player);
}
// update the lowest and highest weights observed
if(!tuples[i].eliminated) {
if(tuples[i].show_weight < lowest_weight) {
lowest_weight = tuples[i].show_weight;
}
if(tuples[i].pass_weight > highest_weight) {
highest_weight = tuples[i].pass_weight;
}
if(tuples[i].murder_weight > highest_murder_weight) {
highest_murder_weight = tuples[i].murder_weight;
}
}
}
// sort the tuple array
tuples.sort(tuple_sort);
// Mark the candidate tuples based on the lowest or highest weights
for(var i = 0; i < tuples.length; i++) {
if(!tuples[i].eliminated) {
switch(tuple_sort) {
case sort_by_passing:
if(tuples[i].pass_weight == highest_weight) {
tuples[i].candidate = true;
}
break;
case sort_by_responding:
if(tuples[i].show_weight == lowest_weight) {
tuples[i].candidate = true;
}
break;
default:
if(tuples[i].murder_weight == highest_murder_weight) {
tuples[i].candidate = true;
}
}
}
}
cmd_print_tuples();
}
function me_or_murderer(card) {
if(me != -1 && (cards[card].held_by == me ||
cards[card].held_by == murder_column)) {
return(true);
}
return(false);
}
function maximum(a, b, c) {
var retval = a;
if(b > retval) { retval = b; }
if(c > retval) { retval = c; }
return(retval);
}
// function to calculate card index for person number
var get_person = function(r) { return(r); }
// function to calculate card index for weapon number
var get_weapon = function(r) { return(persons.length + r); }
// function to calculate card index for room number
var get_room = function(r) { return(persons.length + weapons.length + r); }
function mark_probability(card, player, p) {
probability[card][player] = p;
marked[card][player] = true;
if(p == 1) {
//cards[card].held_by = player;
mark_held_by(card, player);
}
}
// function to set the probability of a given
// card,player to a 1 and set the remaining
// probabilities to zero
function link(card, player) {
if(players[player].cards_guessed == players[player].card_count) {
if(probability[card][player] != 1) {
return("No you don't");
}
}
for(var j = 0; j < players.length + 1; j++) {
mark_probability(card, j, 0);
}
mark_probability(card, player, 1);
validate_player_cards(player);
if(persons.indexOf(cards[card].name) != -1) {
recalc_murder_probability(persons, get_person);
}
if(weapons.indexOf(cards[card].name) != -1) {
recalc_murder_probability(weapons, get_weapon);
}
if(rooms.indexOf(cards[card].name) != -1) {
recalc_murder_probability(rooms, get_room);
}
cmd_print_probability_table();
return(cards[card].name + " held by " + players[player].name);
}
// function to validate a player's cards
// if all the player's cards have been guessed then
// set the remainder cards' probability to zero
function validate_player_cards(player) {
players[player].cards_guessed = 0;
for(var i = 0; i < cards.length; i++) {
if(probability[i][player] == 1) {
players[player].cards_guessed++;
}
}
if(players[player].cards_guessed == players[player].card_count) {
for(var i = 0; i < cards.length; i++) {
if(probability[i][player] != 1) {
mark_probability(i, player, 0);
}
}
}
}
// function to run the probability totals of the murder column
function recalc_murder_probability(set, get_index) {
var suspects = set.length;
for(var i = 0; i < set.length; i++) {
if(probability[get_index(i)][murder_column] == 0) {
suspects--;
}
// if we find the suspected card probability = 1
// then set the probability of all other cards in that group to zero
if(probability[get_index(i)][murder_column] == 1) {
for(var j = 0; j < set.length; j++) {
if(i != j) {
mark_probability(get_index(j), murder_column, 0);
}
}
return;
}
}
// if only one suspect remains
if(suspects == 1) {
for(var i = 0; i < set.length; i++) {
if(probability[get_index(i)][murder_column] != 0) {
mark_probability(get_index(i), murder_column, 1);
return;
}
}
}
}
// function to get the next responder
function get_next_responder(turn) {
return((turn + 1) % players.length);
}
// function to generate a TD element
function make_cell(s) {
var cell = document.createElement("td");
cell.innerHTML = s;
return(cell);
}
// function to highlight a card row
function highlight(card) {
document.getElementById(card).className = "highlight";
}
// function to print the help text
var cmd_help = function() {
textarea.value = "Multiple commands can be concatenated with ';'\n\n" +
textarea.value;
for(var i = commands.length - 1; i >= 0; i--) {
if(commands[i][5]) {
textarea.value = commands[i][0] + " " + commands[i][3] + "\n" +
textarea.value;
}
}
textarea.value = "\n" + textarea.value;
return("OK");
}
// the first player is assumed to be the dealer
// the rest of the players are enumerated clockwise
// argument is a string of names separated by spaces
var cmd_set_players = function(player_list) {
if(game_started) {
return("Game has already started");
}
players = [];
var player_names = player_list.replace(/[^A-Za-z0-9]+$/, "")
.replace(/ +/g, " ").split(/ /);
if(player_names.length > 6 || player_names.length < 2) {
return("This game requires 2-6 players only");
}
for(var i = 0; i < player_names.length; i++) {
var p = new Object;
p.name = player_names[i]; // player's actual name
p.room = -1; // current room the player is in
p.person = -1; // character that the player is shadowing
p.card_count = 0; // number of cards that player is dealt
p.cards_guessed = 0; // number of cards inferred
p.card_row_cell = {};
players.push(p);
}
var dealing_to = 1; // player 0 = dealer, player 1 = starts game
// 3 cards are taken away as murder cards
for(var i = 0; i < cards.length - 3; i++) {
players[dealing_to].card_count++;
dealing_to = (dealing_to + 1) % players.length;
}
murder_column = players.length;
draw_row_of_cards();
cmd_initialise_probability_table();
return(players.length + " players registered");
}
// Create a probability table
// and initialise the probabilities equally
var cmd_initialise_probability_table = function() {
if(typeof(players) == "undefined") {
return("No players registered");
}
generate_combinations();
probability = new Array(cards.length);
marked = new Array(cards.length);
for(var i = 0; i < cards.length; i++) {
probability[i] = new Array(players.length + 1);
marked[i] = new Array(players.length + 1);
for(var j = 0; j < players.length + 1; j++) {