-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameHandler.js
152 lines (135 loc) · 3.96 KB
/
gameHandler.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
let sizes = [2, 3, 3, 4, 5] // Ship sizes - 2, 3, 3, 4, 5 - source: wikipedia
let shipNames = ["Patrol Boat", "Submarine", "Destroyer", "Battleship", "Carrier"]; // Source - wikipedia
function onCellClick(tile) {
if (inSetup) {
addSetupShip(tile)
} else {
if (player.turn) {
player.turn = false;
playerTurn(tile);
}
}
}
function onCellRightClick(tile) {
if (inSetup) {
destroySetupShip(tile);
}
}
function onCellHoverIn(tile) {
if (inSetup) {
setupHoverIn(tile);
}
}
function onCellHoverOut(tile) {
// if (inSetup) {
// setupHoverOut(tile)
// }
}
function startGame() {
inSetup = false;
let playerShips = [];
for (let size of sizes) {
for (let ship of setup.ships) {
if (size === ship.length && !playerShips.includes(ship)) {
playerShips.push(ship)
break
}
}
}
setup.ships = playerShips;
player = new Board(setup.ships, "Your", ["A", "B"], true) // Creates boards and 2D arrays that contain ship positions to keep track of which ones are down
player.turn = true;
enemy = new Board(generateShips(), "The enemy's", ["C", "D"])
enemy.foundHits = [[]];
updateGrid(player.shipGrid, document.getElementById("shipGrid"), "B", player)
}
function takeTurn(toShoot, tileName, current, other) { // Where code shared between player and enemy is ran
let code = 0;
let tileShot = shoot(other.shipGrid[toShoot[0]][toShoot[1]]) // tileShot = miss (2) or hit (3)
current.shootGrid[toShoot[0]][toShoot[1]] = tileShot; // Update grids
other.shipGrid[toShoot[0]][toShoot[1]] = tileShot;
let destroyedText = "";
let destroyCheck = [];
destroyCheck = checkShips(other); // Check if any ship has been destroyed, if so add it to the text display so the player knows
let textToAdd = current.name + " shot was fired at " + tileName + "! It was a ";
if (tileShot === 2) {
textToAdd += "miss...";
} else {
textToAdd += "hit!";
code = 1;
}
for (let ship of destroyCheck) {
if (!other.destroyed.includes(ship)) {
destroyedText = " " + other.name + " " + shipNames[ship] + " has sunk!";
other.destroyed.push(ship);
code = 2;
}
}
textToAdd += destroyedText;
document.getElementById("result").innerHTML = textToAdd;
if (other.destroyed.length === other.ships.length) {
current.won = true;
}
return code;
}
function shoot(tile) { // Checks if the tile chosen is a hit or a miss
switch (tile) {
case 0: // Empty
return 2; // Miss
case 1: // Ship
return 3; // Hit
default:
return tile; // Change nothing if same tile was hit more than once
}
}
function createShip(first, last) {
let predicted = [];
predicted.push(last);
let direction = false;
if (first[1] > last[1]) {
direction = 2;
} else if (first[1] < last[1]) {
direction = -2;
} else if (first[0] > last[0]) {
direction = 1;
} else {
direction = -1;
}
let i = 0;
let lastPoint = last;
while (true) {
lastPoint = predicted[predicted.length - 1];
if (lastPoint[0] == first[0] && lastPoint[1] == first[1]) {
break;
}
if (direction === 2) { // right
predicted.push([lastPoint[0], lastPoint[1] + 1]);
} else if (direction === -2) { // left
predicted.push([lastPoint[0], lastPoint[1] - 1]);
}
if (direction === 1) { // up
predicted.push([lastPoint[0] + 1, lastPoint[1]]);
} else if (direction === -1) { // down
predicted.push([lastPoint[0] - 1, lastPoint[1]]);
}
i++;
if (i > 10) {
return false;
}
}
return predicted;
}
function checkWinner() {
let displayText = "";
if (player.won && !enemy.won) {
displayText += "Congratulations! You have won!";
} else if (!player.won && enemy.won) {
displayText += "The enemy has won! Better luck next time...";
} else if (player.won && enemy.won) {
displayText += "It is a draw!"; // Not actually possible
} else {
return false;
}
document.getElementById("result").innerHTML = displayText;
return true;
}