-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasin.js
188 lines (172 loc) · 4.62 KB
/
Basin.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
"use strict;"
/**
* Part of Model that represents the game field.
* Squares may be: "u":unknown,"e":empty,"s":ship,"m":miss,"h":hit,"w":wreck,"c":buoy
* @see Board
* @constructor
* @param string mode "public" for tracking board (no ships, only strikes) or "private" for primary board
*/
function Basin(mode) {
this._isPublic=(mode==="public");
this._fill = this._isPublic ? "u" : "e";
this._arr=new Array(DIM);
for (var i = 0; i < DIM; i++) {
this._arr[i] = createArray(DIM, this._fill);
}
this.get=function(row,col) {
if( row instanceof Array ) {
var col=row[1];
var row=row[0];
}
if( row<0 || row>=DIM || col<0 || col>=DIM ) return(false);
return ( this._arr[row][col] );
};
this.put=function(val,row,col) {
if( row instanceof Array ) {
var col=row[1];
var row=row[0];
}
if( row<0 || row>=DIM || col<0 || col>=DIM ) return(false);
this._arr[row][col]=val;
};
this.clear=function() {
var range=new Seq2d();
var rc;
while ( rc=range.go() ) {
this.put(this._fill,rc)
}
};
/**
* Takes a point [row,col] and tells if it is available for a strike.
* @param integer|array row row or point [row,col]
* @param integer col
* @returns boolean
*/
this.checkStrikable=function(row,col) {
var val;
if( row===false ) return(false);// continue to the outside check
if( row instanceof Array ) {
//alert("array!");
col=row[1];
row=row[0];
}
if( row<0 || row>=DIM || col<0 || col>=DIM ) return(false);
val=this._arr[row][col];
return ( val=="u" || val=="s" || val=="e" );
};
/**
* Needed for Harvester. In other situations is not used.
* @see Fleet::checkHit()
*/
this.checkHit=function(row,col) {
var val;
if( row instanceof Array ) {
var col=row[1];
var row=row[0];
}
if( row<0 || row>=DIM || col<0 || col>=DIM ) return(false);
val=this._arr[row][col];
return ( val=="s" );
};
/**
* Takes a point [row,col] and finds all neighbouring points available for a strike.
* @param {integer|array} row row or pair [row,col]
* @param integer col
* @param string mode "all" for orthogonal and diagonal neighbours, "cross" for only othogonal
* @see arrayUtils::adjAll
* @see arrayUtils::adjCross
* @returns array of points
*/
this.adjStrikable=function(row,col,mode) {
var i, adjacent=[], res=[];
if( row instanceof Array ) {
col=row[1];
row=row[0];
}
if( row<0 || row>=DIM || col<0 || col>=DIM ) return(false);
if ( mode=="all" ) adjacent=adjAll(row,col);
else if ( mode=="cross" ) adjacent=adjCross(row,col);
else throw new Error("Basin::adjStrikable: invalid mode:"+mode);
for (var i=0;i<adjacent.length;i++) {
if ( this.checkStrikable(adjacent[i]) ) res.push( adjacent[i] );
}
return(res);
};
/**
* Checks if the given ship is all-dead.
* @param array ship
* @return boolean
*/
this.checkSunk=function(ship) {
var i,rc=[];
for ( i=0; i<ship.length; i++ ){
rc=ship[i];
if( this.get(rc) !== 'h') {
//alert ( "Checked -- alive "+this.get(rc[0],rc[1]) );
return false;
}
}
return true;
};
/**
* Marks all ship squares as Wreck.
* @param array ship
* @return nothing
*/
this.markSunk=function(ship) {
var i,rc=[];
for ( i=0; i<ship.length; i++ ) {
rc=ship[i];
this.put('w',rc);
}
};
/**
* Marks all Unknown or Empty squares around the ship as Buoy.
* @param array ship
* @return nothing
*/
this.markAround=function(ship) {
var rc,c,j;
var ar=around(ship);
for ( j=0; j<ar.length; j++ ) {
rc=ar[j];
c=this.get(rc);
if ( c=="u" || c=="e" ) {
this.put("c",rc);
}
}
};
this.markShip=function(ship) {
var i,rc;
for ( i=0; i<ship.length; i++ ){
rc=ship[i];
this.put('s',rc);
}
};
/**
* Used after Harvester::search().
*/
this.cleanUp=function() {
if(this._isPublic) throw new Error("Basin::cleanUp: only private Basin is supported");
var range=new Seq2d();
var rc;
var c,cc;
while ( rc=range.go() ) {
c=this.get(rc);
if(c=="h" || c=="w") cc="s";
else cc="e";
this.put(cc,rc);
}
};
/**
* Used to import sheeps from a Fleet object. Opposite to Harvester::search().
* @param array ships
*/
this.takeShips=function(ships) {
var i;
if ( !(ships instanceof Array) || !(ships[0] instanceof Array)) throw new Error("Basin::takeShips: invalid argument "+ships);
for (var i=0;i<ships.length;i++) {
this.markShip(ships[i]);
}
};
}// end Basin