-
Notifications
You must be signed in to change notification settings - Fork 1
/
play_area.php
2527 lines (2520 loc) · 132 KB
/
play_area.php
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
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="refresh" content="5" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin:0;
}
.image {
position: fixed;
width: 100%;
height: 100%;
background: black;
background-image: url('Homepage.png');
background-size: cover;
}
table, th, td {
border: 2px solid black;
border-collapse: collapse;
!padding: 20px;
}
table.pos_fixed_1 {
position: absolute;
top: 100px;
left: 200px;
}
table.pos_fixed_center {
position: absolute;
top: 140px;
left: 510px;
}
table.pos_fixed_2 {
position: absolute;
left: 850px;
top: 100px;
}
table.pos_fixed_3 {
position: absolute;
top: 650px;
left: 520px;
}
input.pos_fixed_ac {
position: fixed;
top: 50px;
left: 50px
}
input.pos_fixed_cr {
position: fixed;
top: 50px;
left: 100px
}
input.pos_fixed_sb {
position: fixed;
top: 250px;
left: 100px
}
input.pos_fixed_ds {
position: fixed;
top: 300px;
left: 50px
}
input.pos_fixed_co {
position: fixed;
top: 10px;
left: 50px;
}
p.pos_fixed_do {
position: fixed;
top: 14px;
left: 50px;
}
.table {
background: transparent;
display: inline-block;
border: 2px;
border-style: solid;
border-color: #586171;
height: 36px;
width: 36px;
}
p.pos_fixed_pl1 {
position: absolute;
top: 12px;
left: 330px;
font-family: "Times New Roman", Times, serif;
font-style: normal;
font-size: 40px;
color: red;
}
p.pos_fixed_pl2 {
position: absolute;
top: 12px;
right: 330px;
font-family: "Times New Roman", Times, serif;
font-style: normal;
font-size: 40px;
color: red;
}
p.pos_fixed_pl3 {
position: absolute;
top: 562px;
left: 630px;
font-family: "Times New Roman", Times, serif;
font-style: normal;
font-size: 40px;
color: red;
}
p.demo {
position: absolute;
top: 25px;
right: 20px;
font-size: 20px;
color: white;
}
p.above_demo {
position: absolute;
top: 5px;
right: 20px;
font-size: 20px;
color: white;
}
img.loading {
position: fixed;
z-index: 1;
top: 0px;
left: 0px;
size: cover;
height: 100%;
width: 100%;
}
img.GameOverP1 {
position: absolute;
top: 100px;
left: 200px;
height: 400px;
width: 400px;
}
img.GameOverP2 {
position: absolute;
top: 100px;
left: 850px;
height: 400px;
width: 400px;
}
img.GameOverP3 {
position: absolute;
top: 650px;
left: 520px;
height: 400px;
width: 400px;
}
img.WinnerP1 {
position: absolute;
top: 100px;
left: 200px;
height: 400px;
width: 400px;
}
img.WinnerP2 {
position: absolute;
top: 100px;
left: 850px;
height: 400px;
width: 400px;
}
img.WinnerP3 {
position: absolute;
top: 650px;
left: 520px;
height: 400px;
width: 400px;
}
form.center {
position: absolute;
top: 500px;
right: 290px;
height: 100px;
width: 200px;
}
</style>
<script type="text/javascript">
function place(a, b, selected, orient){
// The variables fnl, lf and tp are defined so that we are able to place the image of the ship in the correct position on the screen.
var fnl = ""
lf = parseInt("201") + parseInt(b) * parseInt("40");
lf = eval(lf);
tp = 101 + a * 40;
fnl += "top: " + tp + "px; left: " + lf + "px; position: absolute"
// The validity of the functions has already been checked, all we need to do is put the imaage on the screen, which is again done by the variable ac_h.
if ((selected == "ac") && (orient == "Horizontal")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Aircraft_Carrier_True.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("ac");
}
if ((selected == "cr") && (orient == "Horizontal")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Cruiser_True.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("cr");
}
if ((selected == "sb") && (orient == "Horizontal")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Submarine_True.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("sb");
}
if ((selected == "ds") && (orient == "Horizontal")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Destroyer_True.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("ds");
}
if ((selected == "ac") && (orient == "Vertical")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Aircraft_Carrier_False.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("ac");
}
if ((selected == "cr") && (orient == "Vertical")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Cruiser_False.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("cr");
}
if ((selected == "sb") && (orient == "Vertical")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Submarine_False.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("sb");
}
if ((selected == "ds") && (orient == "Vertical")) {
var ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Destroyer_False.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
completed.push("ds");
}
}
function put_my_ships(ship_id, orient, cell_number){
// This function converts the ship_id and cell_number into the required format to call the function place.
a = Math.floor(cell_number / 10);
b = cell_number % 10 - 1;
if (b == -1) {
b = 9;
a = a - 1;
}
if (ship_id == 5){
ship = "ac";
}
if (ship_id == 4){
ship = "cr";
}
if (ship_id == 3){
ship = "sb";
}
if (ship_id == 2){
ship = "ds";
}
place(a, b, ship, orient);
}
function hit_or_miss(a, b, hit, id, last_hit) {
// Ths function, given the cell location and whether it has been hit or not, and then appends the image onto the screen. The variables tp, lf and fnl are used in the same ways as before.
var fnl = "";
if (id == 1){ // Me
tp = 101 + a * 40;
lf = 201 + b * 40;
fnl += "top: " + tp + "px; left: " + lf + "px; position: absolute;"
if (hit != 0){
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
else {
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Miss_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (id == 2){ // Other Player
tp = 101 + a * 40;
lf = 851 + b * 40;
fnl += "top: " + tp + "px; left: " + lf + "px; position: absolute;";
if (hit != 0){
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
else {
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Miss_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (id == 3) { // Computer
tp = 651 + a * 40;
lf = 521 + b * 40;
fnl += "top: " + tp + "px; left: " + lf + "px; position: absolute;"
if (hit != 0){
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
else {
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Miss_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
}
function check_cell(cell_number, h_or_m, player_id, last_hit){
// This function converts the cell number into the required format to call the function hit_or_miss.
a = Math.floor(cell_number / 10);
b = cell_number % 10 - 1;
if (b == -1) {
b = 9;
a = a - 1;
}
hit_or_miss(a, b, h_or_m, player_id, last_hit);
}
function show_turn(id) {
// This function sees who's turn it is and appends a small image over the player's name, to indicate to that player that it's their turn using the variable ac_h.
if (id == 0) {
return;
}
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Turn.png");
if (id == 1){ // Player 1's Turn
ac_h.setAttribute("style", "top: 50px; left: 465px; position: absolute");
}
if (id == 2) { // Player 2's Turn
ac_h.setAttribute("style", "top: 50px; left: 1120px; position: absolute");
}
if (id == 3) { // Computer's Turn
ac_h.setAttribute("style", "top: 605px; left: 800px; position: absolute");
}
document.body.appendChild(ac_h);
}
function ch2_delay(cell, id1, id2, id3, turn, lost) {
// This function helps in the playing the animation of the cell blowing up.
location.href = 'check_hit.php?cell=' + cell + '&id1=' + id1 + '&id2=' + id2 + '&id3=' + id3 + '&B=2';
}
function ch3_delay(cell, id1, id2, id3, turn, lost) {
// This function helps in the playing the animation of the cell blowing up.
location.href = 'check_hit.php?cell=' + cell + '&id1=' + id1 + '&id2=' + id2 + '&id3=' + id3 + '&B=3';
}
function check_hit_2(cell, id1, id2, id3, turn, lost){
// This function is what is called when you click on a button on the other player's board. It calls the php file which allows us to see whether there is actually a ship there or not.
if(turn == id1 && lost != id2) {
a = Math.floor(cell / 10);
b = cell % 10 - 1;
if (b == -1) {
b = 9;
a = a - 1;
}
var fnl = '';
tp = 65 + a * 40;
lf = 848 + b * 40;
fnl += "top: " + tp + "px; left: " + lf + "px; position: absolute;" + "width: 100px; height: 100px;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "blow.gif");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
setTimeout(ch2_delay, 750, cell, id1, id2, id3, turn, lost);
}
}
function check_hit_3(cell, id1, id2, id3, turn, lost){
// This function is what is called when you click on a button on the cpmputer's board. It calls the php file which allows us to see whether there is actually a ship there or not.
if(turn == id1 && lost != id3){
a = Math.floor(cell / 10);
b = cell % 10 - 1;
if (b == -1) {
b = 9;
a = a - 1;
}
var fnl = '';
tp = 615 + a * 40;
lf = 518 + b * 40;
fnl += "top: " + tp + "px; left: " + lf + "px; position: absolute;" + "width: 100px; height: 100px;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "blow.gif");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
setTimeout(ch3_delay, 750, cell, id1, id2, id3, turn, lost);
}
}
function show_hits_on_side(player_id, ship_id, no_of_hits){
// This function takes in as input the player, ship, and the number of hits that ship has taken, and then it displays on screen this information.
if (player_id == 1) {
if (ship_id == 5) {
lf = 20;
for (i = 0; i < no_of_hits; i++) {
tp = 100 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 4) {
lf = 70;
for (i = 0; i < no_of_hits; i++) {
tp = 100 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 3) {
lf = 70;
for (i = 0; i < no_of_hits; i++) {
tp = 270 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 2) {
lf = 20;
for (i = 0; i < no_of_hits; i++) {
tp = 310 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
}
if (player_id == 2) {
if (ship_id == 5) {
lf = 1380;
for (i = 0; i < no_of_hits; i++) {
tp = 100 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 4) {
lf = 1330;
for (i = 0; i < no_of_hits; i++) {
tp = 100 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 3) {
lf = 1330;
for (i = 0; i < no_of_hits; i++) {
tp = 270 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 2) {
lf = 1380;
for (i = 0; i < no_of_hits; i++) {
tp = 310 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
}
if (player_id == 3
) {
if (ship_id == 5) {
lf = 420;
for (i = 0; i < no_of_hits; i++) {
tp = 650 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 4) {
lf = 470;
for (i = 0; i < no_of_hits; i++) {
tp = 650 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 3) {
lf = 470;
for (i = 0; i < no_of_hits; i++) {
tp = 820 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
if (ship_id == 2) {
lf = 420;
for (i = 0; i < no_of_hits; i++) {
tp = 860 + i * 40;
fnl = "top: " + tp + "px; left: " + lf + "px; position: absolute;";
ac_h = document.createElement("IMG");
ac_h.setAttribute("src", "Hit_cell.png");
ac_h.setAttribute("style", fnl);
document.body.appendChild(ac_h);
}
}
}
}
var passing = [];
var i;
for (i = 1; i < 101; i++) {
passing[i] = 0;
}
function check_validity_and_place(ship_id, orient, a, b){
// This is a function which is used to check whether a given ship in a given orientation is valid in it's positioning or not. The method used is the same as in setup.html.
if ((ship_id == 5) && (orient == "Horizontal")) {
if (b > 5) {
return false;
}
passing[10*a + b + 1] = 5;
passing[10*a + b + 2] = 5;
passing[10*a + b + 3] = 5;
passing[10*a + b + 4] = 5;
passing[10*a + b + 5] = 5;
return true;
}
if ((ship_id == 4) && (orient == "Horizontal")) {
if (b > 6) {
return false;
}
if (passing[10*a + b + 1] != 0) {
return false;
}
if (passing[10*a + b + 2] != 0) {
return false;
}
if (passing[10*a + b + 3] != 0) {
return false;
}
if (passing[10*a + b + 4] != 0) {
return false;
}
passing[10*a + b + 1] = 4;
passing[10*a + b + 2] = 4;
passing[10*a + b + 3] = 4;
passing[10*a + b + 4] = 4;
return true;
}
if ((ship_id == 3) && (orient == "Horizontal")) {
if (b > 7){
return false;
}
if (passing[10*a + b + 1] != 0) {
return false;
}
if (passing[10*a + b + 2] != 0) {
return false;
}
if (passing[10*a + b + 3] != 0) {
return false;
}
passing[10*a + b + 1] = 3;
passing[10*a + b + 2] = 3;
passing[10*a + b + 3] = 3;
return true;
}
if ((ship_id == 2) && (orient == "Horizontal")) {
if (b > 8){
return false;
}
if (passing[10*a + b + 1] != 0) {
return false;
}
if (passing[10*a + b + 2] != 0) {
return false;
}
passing[10*a + b + 1] = 2;
passing[10*a + b + 2] = 2;
return true;
}
if ((ship_id == 5) && (orient == "Vertical")) {
if (a > 5){
return false;
}
passing[10*a + b + 1] = 5;
passing[10*a + b + 11] = 5;
passing[10*a + b + 21] = 5;
passing[10*a + b + 31] = 5;
passing[10*a + b + 41] = 5;
return true;
}
if ((ship_id == 4) && (orient == "Vertical")) {
if (a > 6){
return false;
}
if (passing[10*a + b + 1] != 0) {
return false;
}
if (passing[10*a + b + 11] != 0) {
return false;
}
if (passing[10*a + b + 21] != 0) {
return false;
}
if (passing[10*a + b + 31] != 0) {
return false;
}
passing[10*a + b + 1] = 4;
passing[10*a + b + 11] = 4;
passing[10*a + b + 21] = 4;
passing[10*a + b + 31] = 4;
return true;
}
if ((ship_id == 3) && (orient == "Vertical")) {
if (a > 7){
return false;
}
if (passing[10*a + b + 1] != 0) {
return false;
}
if (passing[10*a + b + 11] != 0) {
return false;
}
if (passing[10*a + b + 21] != 0) {
return false;
}
passing[10*a + b + 1] = 3;
passing[10*a + b + 11] = 3;
passing[10*a + b + 21] = 3;
return true;
}
if ((ship_id == 2) && (orient == "Vertical")) {
if (a > 8){
return false;
}
if (passing[10*a + b + 1] != 0) {
return false;
}
if (passing[10*a + b + 11] != 0) {
return false;
}
passing[10*a + b + 1] = 2;
passing[10*a + b + 11] = 2;
return true;
}
}
function create_comp_board(){
// This function creates a randomly generated board for the computer to use. This is almost randomly generated such that there is no ships in the central 4x4 grid.
var i;
var direction = [];
direction[0] = "Horizontal";
direction[1] = "Vertical";
var random_1 = [0, 1, 2, 7, 8, 9];
var random_2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (i = 5; i > 1; i--) {
var flag = false;
while (flag == false){
var dir = direction[Math.floor(Math.random() * direction.length)];
var rand_1 = random_1[Math.floor(Math.random() * random_1.length)];
var rand_2 = random_2[Math.floor(Math.random() * random_2.length)];
if (dir == "Horizontal") {
var ok = check_validity_and_place(i, dir, rand_1, rand_2);
if (ok == true){
flag = true;
}
}
if (dir == "Vertical") {
var ok = check_validity_and_place(i, dir, rand_2, rand_1);
if (ok == true){
flag = true;
}
}
}
}
}
function send_board(id1, id2){
location.href = 'send_comp_board.php?id1=' + id1 + '&id2=' + id2 + '&array=' + passing;
}
function comp_hit_cell(id, id1, id2, id3, turn){
var place = [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];
if(id == 1)
{
var cell = place[Math.floor(Math.random() * place.length)];
location.href = 'check_hit_comp.php?cell=' + cell + '&id1=' + id1 + '&id2=' + id2 + '&id3=' + id3 + '&B=1';
}
else if(id == 2)
{
var cell = place[Math.floor(Math.random() * place.length)];
location.href = 'check_hit_comp.php?cell=' + cell + '&id1=' + id1 + '&id2=' + id2 + '&id3=' + id3 + '&B=2';
}
}
function evaluate_board(board){
// This function, upon given a board, calculates how many times (and in which places) the ship has been hit and which cells have been hit on and missed.
var i;
var ac_hits;
var cr_hits;
var sb_hits;
var ds_hits;
var misses;
var ac_loc;
var cr_loc;
var sb_loc;
var ds_loc;
var ms_loc;
ac_hits = 0;
cr_hits = 0;
sb_hits = 0;
ds_hits = 0;
misses = 0;
ac_loc = [];
cr_loc = [];
sb_loc = [];
ds_loc = [];
ms_loc = [];
for (i = 1; i < 101; i++) {
if (board[i] == 5) {
ac_loc[ac_hits] = i;
ac_hits = ac_hits + 1;
}
if (board[i] == 4) {
cr_loc[cr_hits] = i;
cr_hits = cr_hits + 1;
}
if (board[i] == 3) {
sb_loc[sb_hits] = i;
sb_hits = sb_hits + 1;
}
if (board[i] == 2) {
ds_loc[ds_hits] = i;
ds_hits = ds_hits + 1;
}
if (board[i] == 0) {
ms_loc[misses] = i;
misses = misses + 1;
}
}
var return_list;
return_list = [];
return_list[0] = ac_hits;
return_list[1] = cr_hits;
return_list[2] = sb_hits;
return_list[3] = ds_hits;
return_list[4] = misses;
return_list[5] = ac_hits + cr_hits + sb_hits + ds_hits;
return_list[6] = ac_loc;
return_list[7] = cr_loc;
return_list[8] = sb_loc;
return_list[9] = ds_loc;
return_list[10] = ms_loc;
return return_list;
}
function compute_guesses(cell_number) {
// This function, upon given a cell number, copmutes the cells on the grid which surround it.
var r;
r = []
a = Math.floor(cell_number / 10);
b = cell_number % 10 - 1;
if (b == -1) {
b = 9;
a = a - 1;
}
if (cell_number == 1) {
r[0] = 2;
r[1] = 11;
}
else if (cell_number == 10) {
r[0] = 9;
r[1] = 20;
}
else if (cell_number == 91) {
r[0] = 81;
r[0] = 92;
}
else if (cell_number == 100) {
r[0] = 99;
r[0] = 90;
}
else if (a == 0) {
r[0] = cell_number - 1;
r[1] = cell_number + 1;
r[2] = cell_number + 10;
}
else if (a == 9) {
r[0] = cell_number - 10;
r[1] = cell_number - 1;
r[2] = cell_number + 1;
}
else if (b == 0) {
r[0] = cell_number - 10;
r[1] = cell_number + 1;
r[2] = cell_number + 10;
}
else if (b == 9) {
r[0] = cell_number - 10;
r[1] = cell_number - 1;
r[2] = cell_number + 10;
}
else {
r[0] = cell_number - 10;
r[1] = cell_number - 1;
r[2] = cell_number + 1;
r[3] = cell_number + 10;
}
return r;
}
function minimum_of_list(list) {
// This function calculates the smallest element of the list.
var i;
var minimum = list[0];
for (i = 1; i < list.length; i++) {
if (list[i] < minimum) {
minimum = list[i];
}
}
return minimum;
}
function maximum_of_list(list) {
// This function calculates the largest element of the list.
var i;
var maximum = list[0];
for (i = 1; i < list.length; i++) {
if (list[i] > maximum) {
maximum = list[i];
}
}
return maximum;
}
function the_computer_plays(board_1, board_2, computer_board, id1, id2, id3) {
/* This function calculates the next guess of the computer. This has 2 parts:
1: Who to attack
2: Where to attack
*/
pl_1_array = evaluate_board(board_1);
pl_2_array = evaluate_board(board_2);
comp_array = evaluate_board(computer_board);
var who_to_hit = 0;
var guess = 0;
if (pl_1_array[5] == 14) {
who_to_hit = 2;
}
else if (pl_2_array[5] == 14) {
who_to_hit = 1;
}
else if (comp_array[5] >= pl_1_array[5] && comp_array[5] >= pl_2_array[5]) {
if (pl_1_array[5] > pl_2_array[5]) {
who_to_hit = 1;
}
else if (pl_1_array[5] < pl_2_array[5]){
who_to_hit = 2;
}
else {
var mb = [1, 2];
var who_to_hit = mb[Math.floor(Math.random() * mb.length)];
}
}
else if (pl_1_array[0] != 5 && pl_1_array[0] != 0) {
who_to_hit = 1;
}
else if (pl_2_array[0] != 5 && pl_2_array[0] != 0) {
who_to_hit = 2;
}
else if (pl_1_array[1] != 4 && pl_1_array[1] != 0) {
who_to_hit = 1;
}
else if (pl_2_array[1] != 4 && pl_2_array[1] != 0) {
who_to_hit = 2;
}
else if (pl_1_array[2] != 3 && pl_1_array[2] != 0) {
who_to_hit = 1;
}
else if (pl_2_array[2] != 3 && pl_2_array[2] != 0) {
who_to_hit = 2;
}
else if (pl_1_array[3] != 2 && pl_1_array[3] != 0) {
who_to_hit = 1;
}
else if (pl_2_array[3] != 2 && pl_2_array[3] != 0) {
who_to_hit = 2;
}
else if (comp_array[5] >= 9) {
if (pl_1_array[5] > pl_2_array[5]) {
who_to_hit = 1;
}
else if (pl_1_array[5] < pl_2_array[5]){
who_to_hit = 2;
}
else {
var mb = [1, 2];
var who_to_hit = mb[Math.floor(Math.random() * mb.length)];
}
}
else {
if (pl_1_array[5] > pl_2_array[5]) {
who_to_hit = 2;
}
else if (pl_2_array[5] > pl_1_array[5]){
who_to_hit = 1;
}
else {
var mb = [1, 2];
var who_to_hit = mb[Math.floor(Math.random() * mb.length)];
}
}
if (who_to_hit == 1) {
var ac_hits = pl_1_array[0];
var cr_hits = pl_1_array[1];
var sb_hits = pl_1_array[2];
var ds_hits = pl_1_array[3];
var misses = pl_1_array[4];
var ac_loc = pl_1_array[6];
var cr_loc = pl_1_array[7];
var sb_loc = pl_1_array[8];
var ds_loc = pl_1_array[9];
var ms_loc = pl_1_array[10];
if ((ac_hits != 0) && (ac_hits != 5)) {
if (ac_hits == 1) {
var possible_hits;
possible_hits = [];
current_hit = ac_loc[0];
possible_hits = compute_guesses(current_hit);
var j;
for (j = 0; j < possible_hits.length; j++) {
if (board_1[possible_hits[j]] == -1) {
guess = possible_hits[j];
break;
}
}
}
else {
var possible_hits;
possible_hits = [];
var current_hit_1 = ac_loc[0];
var current_hit_2 = ac_loc[1];
var a = Math.floor((minimum_of_list(ac_loc) - 1) / 10);
var b = (minimum_of_list(ac_loc)-1) % 10;
if (((current_hit_1 - current_hit_2) % 10) == 0) {
if (a != 0 && board_1[minimum_of_list(ac_loc) - 10] == 0) {
possible_hits[0] = minimum_of_list(ac_loc) + 10;
possible_hits[1] = minimum_of_list(ac_loc) + 20;
possible_hits[2] = minimum_of_list(ac_loc) + 30;
possible_hits[3] = minimum_of_list(ac_loc) + 40;
}
else if (a != 9 && board_1[maximum_of_list(ac_loc) + 10] == 0) {
possible_hits[0] = maximum_of_list(ac_loc) - 10;
possible_hits[1] = maximum_of_list(ac_loc) - 20;
possible_hits[2] = maximum_of_list(ac_loc) - 30;
possible_hits[3] = maximum_of_list(ac_loc) - 40;
}
else {
possible_hits[1] = current_hit_1 + 20;
possible_hits[3] = current_hit_1 + 30;
possible_hits[5] = current_hit_1 + 40;
possible_hits[0] = current_hit_1 - 10;
possible_hits[2] = current_hit_1 - 20;
possible_hits[4] = current_hit_1 - 30;
}
}
else {
if (b != 0 && board_1[minimum_of_list(ac_loc) - 1] == 0) {
possible_hits[0] = minimum_of_list(ac_loc) + 1;
possible_hits[1] = minimum_of_list(ac_loc) + 2;
possible_hits[2] = minimum_of_list(ac_loc) + 3;
possible_hits[3] = minimum_of_list(ac_loc) + 4;