-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
300 lines (238 loc) · 7.72 KB
/
board.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
window.BOARDSIZE=5;
window.MAXSHIPCOUNT=window.BOARDSIZE;
// enum board type
var BoardStatus = {
SEA : 0,
SHIP : 1,
MISS : 2,
BOOM : 3
};
Board = function() {
this.playerId=-1;
this.isOwner=false;
this.currentShipCount=0;
//this.board[window.BOARDSIZE][window.BOARDSIZE];
//TO-DO find a intuitive way to init 2-d array
//this.board=new Array(window.BOARDSIZE);
//for (i=0;i<window.BOARDSIZE;i++ ){
// this.board[window.BOARDSIZE]=new Array(window.BOARDSIZE);
//}
// init 5*5 array
this.board= [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
];
};
Board.prototype = {
getBoardLength: function(){
return this.board.length;
},
setPlayerId : function(palyerId) {
this.playerId=palyerId;
},
getPlayerId : function() {
return this.playerId;
},
setIsOwner : function (isOwner){
this.isOwner=isOwner
},
getIsOwner : function() {
return this.isOwner;
},
setCurrentShipCount: function(count){
this.currentShipCount=count;
},
getCurrentShipCount: function(){
return this.currentShipCount;
},
setBoardStatus : function( i, j, status){
//this.board[posX][posY]=status;
this.board[i][j]=status;
},
getBoardStatus : function( i, j ){
//return this.board[posX][posY];
return this.board[i][j];
},
getBoardEntireStatus : function(){
var entireStatus = "";
for (i=0;i<window.BOARDSIZE;i++){
for(j=0;j<window.BOARDSIZE;j++){
entireStatus+= this.getBoardStatus(i,j).toString();
}
}
return entireStatus;
},
importBoard: function( entireStatus ){
for (i=0;i<entireStatus.length; i++){
this.setBoardStatus( Math.floor( i/window.BOARDSIZE ), (i % window.BOARDSIZE), parseInt( entireStatus.charAt(i) ));
}
},
initBoard : function(){
for (i=0;i<window.BOARDSIZE;i++){
for(j=0;j<window.BOARDSIZE;j++){
this.setBoardStatus( i, j, BoardStatus.SEA);
}
}
},
placeShipOnBoard : function( i, j ) {
//this.setBoardStatus( posX, posY, BoardStatus.SHIP);
this.setBoardStatus( i, j, BoardStatus.SHIP);
},
throwBomb : function ( i, j ){
//if (this.getBoardStatus( posX, posY ) == BoardStatus.SEA){
// this.setBoardStatus(posX, posY, BoardStatus.MISS);
//}else if(this.getBoardStatus( posX, posY ) == BoardStatus.SHIP){
// this.setBoardStatus(posX, posY, BoardStatus.BOOM);
//}
if (this.getBoardStatus( i, j ) == BoardStatus.SEA){
this.setBoardStatus(i, j, BoardStatus.MISS);
}else if(this.getBoardStatus( i, j ) == BoardStatus.SHIP){
this.setBoardStatus(i, j, BoardStatus.BOOM);
}
},
checkGameOver : function() {
var boomCount=0;
for (i=0;i<window.BOARDSIZE;i++){
for(j=0;j<window.BOARDSIZE;j++){
if (this.getBoardStatus( i, j) == BoardStatus.BOOM){
boomCount++;
}
}
}
// all ship has been bombed.
if (boomCount == window.MAXSHIPCOUNT){
return true;
}
return false;
}
}
BoardUI= function(board1, board2){
this.board1=board1;
this.board2=board2;
//TO-DO init array from this.board.getBoardLength()
this.board1CanModify=[
[true,true,true,true,true],
[true,true,true,true,true],
[true,true,true,true,true],
[true,true,true,true,true],
[true,true,true,true,true]
];
this.board2CanModify=[
[true,true,true,true,true],
[true,true,true,true,true],
[true,true,true,true,true],
[true,true,true,true,true],
[true,true,true,true,true]
];
};
BoardUI.prototype= {
drawUI: function(){
// set players board wrapper
$("#textarea").after("<br>\n"+
"<div id='boardUI'>\n"+
"</div>\n");
wrapWidth=(this.board1.getBoardLength()*2*100)+100;
wrapHeight=(this.board1.getBoardLength()*100)+100;
$("#boardUI").css({'width': wrapWidth.toString()+'px',
'height': wrapHeight.toString()+'px',
'margin': "0 auto"});
// set palyer board column
$("#boardUI").append("<div id='player1'></div>"+
"<div id='player2'></div>");
columnWidth=this.board1.getBoardLength()*100;
$("#player1").css({ 'width': columnWidth.toString(),
'height':wrapHeight.toString()+'px',
'float': "left"});
$("#player2").css({ 'width': columnWidth.toString(),
'height':wrapHeight.toString()+'px',
'float': "right"});
$("#player1").append("<h1>player1</h1><br>");
$("#player2").append("<h1>player2</h1><br>");
for (i=0;i<window.BOARDSIZE;i++){
for (j=0;j<window.BOARDSIZE;j++){
$("#player1").append("<input type='image'"+"id='player1_"+i+j+"' src='picture/point.jpg'>");
$("#player2").append("<input type='image'"+"id='player2_"+i+j+"' src='picture/point.jpg'>");
}
$("#player1").append("<br>");
$("#player2").append("<br>");
}
},
destroyUI: function(){
$("#boardUI").remove();
},
changeIcon: function(playerId, i, j){
// check which board will be change, player1 or player2.
var playerBoard = null;
if (playerId == "1"){
playerBoard= this.board1;
}else if (playerId == "2"){
playerBoard= this.board2;
}else{
setMessage("Not a valid playerId!! on changeIcon()");
return false;
}
// change the icon according to the board status.
var status=playerBoard.getBoardStatus(i,j);
if (status == BoardStatus.SEA){
$("#player"+playerId+"_"+i+j).attr("src","picture/point.jpg");
}else if(status == BoardStatus.SHIP){
$("#player"+playerId+"_"+i+j).attr("src","picture/ship.jpg");
}else if(status == BoardStatus.MISS){
$("#player"+playerId+"_"+i+j).attr("src","picture/miss.jpg");
}else if(status == BoardStatus.BOOM){
$("#player"+playerId+"_"+i+j).attr("src","picture/boom.jpg");
}else{
setMessage("Not a valid status on "+"player"+playerId+"_"+i+j+" with status: "+status);
return false;
}
},
showAllIcon : function(){
for(i = 0 ; i<window.BOARDSIZE;i++){
for(j = 0;j<window.BOARDSIZE;j++){
this.changeIcon("1", i, j);
this.changeIcon("2", i, j);
}
}
},
combineUIAndSetShip: function(playerId){
// which board will be set, player1 or player2 .
//var playerIdPrefix="player"+window.playerId+"_";
var playerIdPrefix="player"+playerId+"_";
for(i = 0; i < window.MAXSHIPCOUNT ;i++){
for(j = 0; j < window.MAXSHIPCOUNT;j++){
$("#"+playerIdPrefix+i+j).on('click', {posI: i, posJ: j}, function(event){
testAndSetShip(event.data.posI,event.data.posJ);
});
}
}
},
combineUIAndThrowBomb: function(playerId){
/*var competitorPlayerId=null;
if (window.playerId == "1"){
competitorPlayerId="2";
}else if (window.playerId == "2"){
competitorPlayerId="1";
}*/
var playerIdPrefix="player"+playerId+"_";
for(i = 0; i < window.MAXSHIPCOUNT ;i++){
for(j = 0; j < window.MAXSHIPCOUNT;j++){
$("#"+playerIdPrefix+i+j).on('click', {posI: i, posJ: j}, function(event){
testThrowBomb(event.data.posI,event.data.posJ);
});
}
}
},
disableClickEvent: function(playerId){
// which board will be clear, player1 or player2 .
var playerIdPrefix="player"+playerId+"_";
for(i = 0; i < window.MAXSHIPCOUNT ;i++){
for(j = 0; j < window.MAXSHIPCOUNT;j++){
// clear all delegated click handlers.
$("#"+playerIdPrefix+i+j).off();
}
}
}
};