forked from mrobock/steph_max_battleship
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.js
More file actions
149 lines (135 loc) · 5.6 KB
/
Copy pathcontroller.js
File metadata and controls
149 lines (135 loc) · 5.6 KB
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
$(document).ready(function() {
createBoard();
buildTable();
setXBlockShips(5, 1);
setXBlockShips(4, 2);
setXBlockShips(3, 2);
setXBlockShips(2, 2);
setXBlockShips(1, 1);
showShipCount();
// displayShips(); // take this off to hide ships until click
$("td").on("click", function() {
clickHitMiss(this); //checks if the cell is a hit or miss
showTorpedoes(); //shows number of torpedoes left
showSunkShips(this); //shows number of ships remaining
if ((((fiveBlockCount === 0 && fourBlockCount === 0) && threeBlockCount === 0) && twoBlockCount === 0) && oneBlockCount === 0) { //if all ships counts are zero
$("td").off("click"); //turn off clicking
$("#torpedoCount").text("Mission accomplished! You WIN! "); //set message
}
$(this).off();
if (torpedoes === 0 && (fiveBlockCount > 0 || fourBlockCount > 0 || threeBlockCount > 0 || twoBlockCount > 0 || oneBlockCount > 0)) { //if torpedo count is zero
$("td").off("click"); //turn off clicking
$("#torpedoCount").text("Game over. You LOSE!"); //set message
displayShips(this); //then display all ships
}
});
$("#start").on("click", function() {
$("td").off();
newGame();
$("td").removeClass("hit");
$("td").removeClass("miss");
setXBlockShips(5, 1);
setXBlockShips(4, 2);
setXBlockShips(3, 2);
setXBlockShips(2, 2);
setXBlockShips(1, 1);
// displayShips();
countUnsunkShips();
$("#fiveblock").text(fiveBlockCount);
$("#fourblock").text(fourBlockCount);
$("#threeblock").text(threeBlockCount);
$("#twoblock").text(twoBlockCount);
$("#oneblock").text(oneBlockCount);
$("td").on("click", function() {
clickHitMiss(this); //checks if the cell is a hit or miss
showTorpedoes(); //shows number of torpedoes left
showSunkShips(this); //shows number of ships remaining
if ((((fiveBlockCount === 0 && fourBlockCount === 0) && threeBlockCount === 0) && twoBlockCount === 0) && oneBlockCount === 0) { //if all ships counts are zero
$("td").off("click"); //turn off clicking
$("#torpedoCount").text("Mission accomplished! You WIN! "); //set message
}
$(this).off();
if (torpedoes === 0 && (fiveBlockCount > 0 || fourBlockCount > 0 || threeBlockCount > 0 || twoBlockCount > 0 || oneBlockCount > 0)) { //if torpedo count is zero
$("td").off("click"); //turn off clicking
$("#torpedoCount").text("Game over. You LOSE!"); //set message
displayShips(this); //then display all ships
}
});
});
function createBoard() {
for (var i = 0; i < 10; i++) { //first loop creates the TRs with id of i
$("#table").append("<tr class='board' id=" + i +">");
for (var j = 0; j < 10; j++) { //loop creates TDs with id of ij and board class
$("#"+i).append("<td class='board' id=" + i + j + "></td>");
}
$("#table").append("</tr>");//closes the TRs after TD loop runs
}
}
function showTorpedoes() {
countTorpedoes(); //calls countTorpedoes function
if (torpedoes === 1) { //handles plural vs singular
$("#torpedoCount").text("Torpedo remainging: " + torpedoes);
} else {
$("#torpedoCount").text("Torpedoes remaining: " + torpedoes); //sets message
}
}
function clickHitMiss(thing) {
var position = $(thing).attr("id").split(""); //takes the id attribute and splits it into 2 numbers
row = position[0];
column = position[1];
if (board[row][column] === 0) { //finds the value at the board/array position of the two numbers
$(thing).addClass("miss"); //class miss is added if there is no ship (value = 0)
} else {
$(thing).addClass("hit"); //class hit is added if there is a ship (value > 0)
// remainingShips();
}
}
function displayShips() {
for (var i = 0; i < board.length; i++) { //loops through 10 rows
for (var j = 0; j < board[i].length; j++) { //loops through 10 columns in each row
if (board[i][j] > 0) { //if the backend array has a value greater than 0, there's a ship
$("#" + i + j).addClass("hit"); //add hit class to any cell with a ship in it
}
}
}
}
function showShipCount() {
countUnsunkShips();
if (fiveBlockCount === 0) { //if the count of fiveblock ships is zero
$("#fiveblock").text(""); //set text to nothing
$("#fiveblock").prepend('<img class="boomImage" src="images/explosion.png"/>') //append explosion img
} else {
$("#fiveblock").text(fiveBlockCount); //otherwise, set the count
}
if (fourBlockCount === 0) {
$("#fourblock").text("");
$("#fourblock").prepend('<img class="boomImage" src="images/explosion.png"/>')
} else {
$("#fourblock").text(fourBlockCount);
}
if (threeBlockCount === 0) {
$("#threeblock").text("");
$("#threeblock").prepend('<img class="boomImage" src="images/explosion.png"/>')
} else {
$("#threeblock").text(threeBlockCount);
}
if (twoBlockCount === 0) {
$("#twoblock").text("");
$("#twoblock").prepend('<img class="boomImage" src="images/explosion.png"/>')
} else {
$("#twoblock").text(twoBlockCount);
}
if (oneBlockCount === 0) {
$("#oneblock").text("");
$("#oneblock").prepend('<img class="boomImage" src="images/explosion.png"/>')
} else {
$("#oneblock").text(oneBlockCount);
}
}
function showSunkShips(thing) {
var position = $(thing).attr("id").toString(); //takes the id attr and ensures its a string
// console.log(position);
checkSunkShip(position); //passes position into checkSunkShip function
showShipCount(); //runs show ship count to update the board with sunk ships
}
});