-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
229 lines (209 loc) · 6.69 KB
/
script.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
"using strict";
var BOARD_SIZE = 9;
var player1array = new Array(BOARD_SIZE + 1);
var player2array = new Array(BOARD_SIZE + 1);
var playerTurn = null;
var turn = 0;
var movedShip = null;
var shipType = null;
var player = null;
$(function() {
for (var i = 0; i < player1array.length; i++) {
player1array[i] = new Array(BOARD_SIZE + 1);
player2array[i] = new Array(BOARD_SIZE + 1);
}
for (var i = 0; i < player1array.length; i++) {
for (var j = 0; j < player1array.length; j++) {
player1array[i][j] = false;
player2array[i][j] = false;
}
}
/**
* Create the boards
*/
var board1 = document.getElementById('player1-board');
var board2 = document.getElementById('player2-board');
for (var i = 1; i <= BOARD_SIZE; i++) {
for (var j = 1; j <= BOARD_SIZE; j++) {
var div1 = document.createElement('div');
div1.id = i + "" + j + "-1";
div1.style.width = (100 / BOARD_SIZE) + "%";
div1.style.height = (100 / BOARD_SIZE) + "%";
board1.appendChild(div1);
var div2 = document.createElement('div');
div2.id = i + "" + j + "-2";
div2.style.width = (100 / BOARD_SIZE) + "%";
div2.style.height = (100 / BOARD_SIZE) + "%";
board2.appendChild(div2);
}
}
/**
* Pick a ship to place
*/
$('.ship').click(function(e) {
movedShip = e.target;
shipType = movedShip.className.split(" ")[1];
if ($(e.target).parents('#left').length)
player = 1;
else
player = 2;
$('.board > div').not('.ship').removeClass();
$('.board > div').not('.ship').addClass('place-ship ' + shipType);
if ($(movedShip).hasClass('vertical'))
$('.board > div').not('.ship').addClass('vertical');
});
/**
* Place a ship on the board
*/
$('body').on('click', '.place-ship', function(e) {
if (validPlacement(player, movedShip, shipType, e.target)) {
var parent = (player == 1 ? "#left" : "#right");
// Remove ship from picking area
$(parent + ' .ship-placeholder > .' + shipType).css('display', 'none');
// Remove hover classes from empty squares
$('.board > div').not('.ship').removeClass();
var row = e.target.id.substr(0, 1);
var col = e.target.id.substr(1, 1);
var shipLength = shipType.substr(shipType.length-1, 1);
if ($(movedShip).hasClass('vertical')) {
$('#' + row + col + "-" + player).addClass('ship ' + shipType + ' vertical');
for (var i = 1; i < shipLength; i++) {
$('#' + (parseInt(row)+i) + col + "-" + player).addClass('ship');
}
}
else {
$('#' + row + col + "-" + player).addClass('ship ' + shipType);
for (var i = 1; i < shipLength; i++) {
$('#' + row + (parseInt(col)+i) + "-" + player).addClass('ship');
}
}
if (player === 1) {
if ($('#left .ship[style="display: none;"]').length === 10) {
$('button#' + player).removeClass('hidden');
}
}
else {
if ($('#right .ship[style="display: none;"]').length === 10) {
$('button#' + player).removeClass('hidden');
}
}
/*movedShip = null;
shipType = null;
player = null;*/
}
});
/**
* Ready: hide ships and start game
*/
$('button').click(function(e) {
storeShips(e.target.id);
$(e.target).hide();
if ($('button[style]').length == 2) {
$('#player1-board').addClass('shoot');
$('.score').show();
playerTurn = 1;
}
});
/**
* Reload the page
*/
$('#reset').click(function() {
location.reload();
});
/**
* Shoot ship
*/
$('body').on('click', '.shoot > div', function(e) {
var row = e.target.id.substr(0, 1);
var col = e.target.id.substr(1, 1);
if (!$(e.target).hasClass('bomb')) {
if (playerTurn === 1) {
if (player2array[row][col]) {
$(e.target).addClass('ship bomb');
$('#left #shots').html("Shots: " + $('#left .bomb').length);
$('#left #hits').html("Hits: " + $('#left .ship.bomb').length);
}
else {
$(e.target).addClass('bomb');
$('#left #shots').html("Shots: " + $('#left .bomb').length);
}
}
else {
if (player1array[row][col]) {
$(e.target).addClass('ship bomb');
$('#right #shots').html("Shots: " + $('#right .bomb').length);
$('#right #hits').html("Hits: " + $('#right .ship.bomb').length);
}
else {
$(e.target).addClass('bomb');
$('#right #shots').html("Shots: " + $('#right .bomb').length);
}
}
if ($('#player' + playerTurn + '-board .ship.bomb').length === 17) {
$('#p' + playerTurn).html("Winner").css('color', '#AFB42B');
$('.shoot').addClass('winner');
$('.shoot').removeClass('shoot');
}
else {
$('#player' + playerTurn + '-board').removeClass('shoot');
playerTurn = (++turn) % 2 + 1;
$('#player' + playerTurn + '-board').addClass('shoot');
}
}
});
});
/**
* Checks if the ship placement is valid, e.g. not outside of the board,
* not on another ship, etc.
* @param player the player placing the ship
* @param movedShip the ship that will be placed on the board
* @param shipType the type of ship
* @param box the square to place the ship in
* @return true if it is a valid placement
*/
function validPlacement(player, movedShip, shipType, box) {
var shipLength = shipType.substr(shipType.length-1, 1);
var row = box.id.substr(0, 1);
var col = box.id.substr(1, 1);
if ($(movedShip).hasClass('vertical')) {
// Vertical ship
if (BOARD_SIZE - shipLength < row - 1)
return false;
for (var i = 0; i < shipLength; i++) {
if ($('#' + (parseInt(row)+i) + col + "-" + player).hasClass('ship'))
return false;
}
}
else {
// Horizontal ship
if (BOARD_SIZE - shipLength < col - 1)
return false;
for (var i = 0; i < shipLength; i++) {
if ($('#' + row + (parseInt(col)+i) + "-" + player).hasClass('ship'))
return false;
}
}
return true;
}
/**
* Stores the ships in a matrix and hides them from the board.
* @param player the player board to store and hide
*/
function storeShips(player) {
console.log(player);
for (var i = 1; i <= BOARD_SIZE; i++) {
for (var j = 1; j <= BOARD_SIZE; j++) {
if ($('#' + i + j + "-" + player).hasClass('ship')) {
if (player == 1)
player1array[i][j] = true;
else
player2array[i][j] = true;
}
}
}
$('#player' + player + '-board > div').removeClass();
if (player == 1)
$('#left .ship').removeClass();
else
$('#right .ship').removeClass();
}