This repository was archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboard.js
461 lines (439 loc) · 14.3 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
const B64 = '0123456789:;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')//boardstate alphabet
const B20 = 'ABCDEFGHIJKLMNOPQRST'.split('')//move position alphabet
function parseRule(rulestring){
rules = rulestring.split('/');//split into birth and survive
births = [];
survive = [];
for(var i=0; i<rules[0].length; i++){
var num = parseInt(rules[0].charAt(i))
if (!isNaN(num)){
births.push(num);
}
}
for(var i=0; i<rules[1].length; i++){
var num = parseInt(rules[1].charAt(i))
if (!isNaN(num)){
survive.push(num)
}
}
return [births,survive]
}
function stringToBoard(boardString,size){
grid=[];
states = [];
counter = 0;
for (var y=0; y<size; y++) {
grid.push([]);
for (var x=0; x<size; x++) {
grid[y].push({currentState: 0, nextState: 0});//fill board
}
}
for (var i=0; i<boardString.length; i++){
var num = B64.indexOf(boardString[i])
for (var j=2; j>=0; j--){
var state = Math.floor(num/Math.pow(4,j))//read states into 1d array
state %= 4
states.push(state)
}
}
for (var y=0; y<size; y++) {
for (var x=size-1; x>=0; x--) {
grid[y][x].currentState = states[counter];//put states onto board
counter++;
}
}
return grid;
}
function newBoard(density,size){
var board= [];
for (var y = 0; y < Math.floor(size/2); y++) {//half the board
board.push([]);
for (var x = 0; x < size; x++) {
var val;
var r = Math.random();
if ((2*r) < density){
val = 1;
}else if (r < density){
val = 2;
}else{
val = 0;
}
board[y].push({currentState: val, nextState: 0});
}
}
for (y = 0; y < Math.ceil(size/2); y ++) {//fill other half
board.push([]);
}
for (y = 0; y < Math.floor(size/2); y++) {//rotate board
for (x = 0; x < size; x++) {
if (board[y][size - x - 1].currentState == 2)
val = 1;
else if (board[y][size - x - 1].currentState == 1)
val = 2;
else
val = 0;
board[size-y-1].push({currentState: val, nextState: 0});
}
}
if (size%2==1){//odd case
centre = []
centFlip = []
for(var i=0; i < Math.floor(size/2); i++){//create array
var val;
var r = Math.random();
if ((2*r) < density){
centre.push({currentState: 1, nextState: 0});
centFlip.push({currentState: 2, nextState: 0});
}else if (r < density){
centre.push({currentState: 2, nextState: 0});
centFlip.push({currentState: 1, nextState: 0});
}else{
centre.push({currentState: 0, nextState: 0});
centFlip.push({currentState: 0, nextState: 0});
}
}
final = centre.concat([{currentState: 0, nextState: 0}]).concat(centFlip.reverse())
board[Math.floor(size/2)]=final
}
return board
}
function boardToString(board){
var count =0;
var value =0;
var string = '';
for(var x=0;x<board.length;x++){
for(var y=board[0].length-1; y>=0; y--){//invert y axis
count++;
value+= board[x][y].currentState*Math.pow(4,3-count);//base 4 cellstate stuff
if (count==3){//base 4, 64 bit alphabet, 3 cells per digit
count=0;
string+=B64[value];
value = 0;
}
}
}
if(count!=0){
string+=B64[value];
}
return string
}
function getColouredNeighbours(grid, x, y, colour) {
var colouredNeighbours = 0;
for (var dx = -1; dx < 2; dx++) {
for (var dy = -1; dy < 2; dy++) {
if (x + dx >= 0 && x + dx < grid.length && y + dy >= 0 && y + dy < grid[0].length) {
if (!(dx == 0 && dy == 0)) {
if (grid[x + dx][y + dy].currentState == colour || grid[x + dx][y + dy].currentState == colour+3) {
colouredNeighbours += 1;
}
}
}
}
}
return colouredNeighbours;
}
function iterate(grid,birth,survive){
if (typeof survive == "undefined"){
survive = birth[1];
birth = birth[0];
}
for (var x = 0; x < grid.length; x++) {
for (var y = 0; y < grid[0].length; y++) {
var rn = getColouredNeighbours(grid, x, y, 1);
var bn = getColouredNeighbours(grid, x, y, 2);
var n = rn+bn;
if (birth.includes(n) && grid[x][y].currentState == 0) {
if (rn>bn){
grid[x][y].nextState = 1;
}else if(bn>rn){
grid[x][y].nextState = 2;
}else if(rn==bn){
grid[x][y].nextState = 3;
}
}
else if (!(survive.includes(n)) && grid[x][y].currentState != 0) {
grid[x][y].nextState = 0;
}
else {
if (grid[x][y].currentState <= 3)
grid[x][y].nextState = grid[x][y].currentState;
else
grid[x][y].nextState = grid[x][y].currentState - 3;
}
}
}
for (var x = 0; x < grid.length; x++) {
for (var y = 0; y < grid[0].length; y++) {
//console.log(gridTiles[x][y].currentState,gridTiles[x][y].nextState);
if (grid[x][y].currentState != grid[x][y].nextState) {
grid[x][y].currentState = grid[x][y].nextState;
}
}
}
return grid;
}
function doMoves(board, moves, rules, player){//player is the player who's turn it is before any of the moves have been made
var size = board.length-1;
for(var i=0; i<moves.length; i++){
if(moves[i] === ''){
continue;
}
var move = moves[i].split('+')[0];
var type = move.slice(-1);
if(['A','B','C'].includes(type)){//kill or sacrifice
var x = B20.indexOf(moves[i].charAt(0));
var y = B20.indexOf(moves[i].charAt(1));
board[x][y].currentState = 0;
}
else if(type === 'D'){//summon
var x = B20.indexOf(moves[i].charAt(0));
var y = B20.indexOf(moves[i].charAt(1));
board[x][y].currentState = player;
}
else if(type === 'E'){
board = iterate(board,rules);
player = player%2+1;
}
}
return board;
}
function countItems(array, item){
var count = 0;
for (var i=0; i<array.length; i++){
if (array[i] === item){
count++;
}
}
return count;
}
function checkLegit(gamestring, board, player, move){//player is the player requesting the move
var parts = gamestring.split(',');
var rules = parseRule(parts[0]);
var size = parseInt(parts[1]);
var testBoard = stringToBoard(parts[5],size);
var moves = parts.slice(6);
var movesMade = countItems(moves,'E');
var turn = movesMade%2+1;
var type = move.slice(-1);
var turnMoves = [];
var turnMoveTypes = [];
for (var i=moves.length-1; i>=0; i--){
if (moves[i] != ''){
if (moves[i]=='E'){
break;
}else{
turnMoves.push(moves[i]);
turnMoveTypes.push(moves[i].slice(-1))
}
}
}
turnMoves = turnMoves.reverse();
if (turn!=player){
console.log('Wrong player');
return false
}
if (board.length != size || board[0].length != size){//different size than specified
console.log('Wrong size');
return false;
}
testBoard = doMoves(testBoard, moves, rules, 1);//game starts with player 1
if (!checkEqual(testBoard,board)){//board does not represent gamestring
console.log('Wrong board');
return false;
}
if (turnMoves.length > 3){
console.log(turnMoves);
console.log('too many moves either way');
return false;
}
if (type === 'A'){
if (turnMoves.length!=0){
console.log(turnMoves);
console.log('too many moves to kill');
return false;
}
try{
var x = B20.indexOf(move.charAt(0));
var y = B20.indexOf(move.charAt(1));
if (board[x][y].currentState==0){
console.log('can\'t kill dead cell');
return false;
}
}catch(err){//no coords are specified
console.log('no coords kill');
return false;
}
}else if(type === 'B'){
if (turnMoves.length!=1 || turnMoves[0].slice(-1)!='D'){//wrong preceding moves
console.log('need summon for 1st sacrifice');
return false;
}
try{
var x = B20.indexOf(move.charAt(0));
var y = B20.indexOf(move.charAt(1));
if (board[x][y].currentState!=player){//can only sacrifice self
console.log('1st sacrafice must be self');
return false;
}
}catch(err){//no coords are specified
console.log('no coords sac 1');
return false;
}
}else if(type === 'C'){
if (turnMoves.length!=2 ||
turnMoves[0].slice(-1)!='D' || turnMoves[1].slice(-1)!='B'){//wrong preceding moves
console.log('must have summon and first sacrifice');
return false;
}
try{
var x = B20.indexOf(move.charAt(0));
var y = B20.indexOf(move.charAt(1));
if (board[x][y].currentState!=player){//can only sacrifice self
console.log('2nd sacrifice must be self');
return false;
}
}catch(err){//no coords are specified
console.log('no coords sac 2');
return false;
}
}else if(type === 'D'){
if (turnMoves.length!=0){//wrong preceding moves
console.log('can only sum once');
return false;
}
try{
var x = B20.indexOf(move.charAt(0));
var y = B20.indexOf(move.charAt(1));
if (board[x][y].currentState!=0){//can only sacrifice self
console.log('can\'t summon non dead');
return false;
}
}catch(err){//no coords are specified
console.log('no coords sum');
return false;
}
}else if(type === 'E'){
if (turnMoves.length===3){
if (!(turnMoveTypes.includes('B')&&turnMoveTypes.includes('C')&&turnMoveTypes.includes('D'))){
console.log('summon not enough');
return false;
}
}else if(turnMoves.length===1){
if (!turnMoveTypes.includes('A')){
console.log('kill not enough');
return false;
}
}else{
console.log('wrong number of moves');
return false;
}
}else{
console.log('not a move');
return false;
}
return true;
}
function checkEqual(board1, board2){
if (board1.length != board2.length){
console.log('bad length',board1.length,board2.length);
return false;
}
if (board1[0].length != board2[0].length){
console.log('bad width')
return false;
}
for (var i=0; i<board1.length; i++){
for (var j=0; j<board1[0].length; j++){
if (board1[i][j].currentState != board2[i][j].currentState){
console.log('unequal cell',i,j)
return false;
}
}
}
return true;
}
function remakeBoard(gamestring){
var parts = gamestring.split(",");
var rules = parseRule(parts[0]);
var size = parseInt(parts[1]);
var board = stringToBoard(parts[5], size);
var moves = parts.slice(6);
board = doMoves(board, moves, rules, 1);
return board;
}
function tryUndo(gamestring, undo, player){
var parts = gamestring.split(',');
var moves = parts.slice(6);
var index = moves.indexOf("");
if (index!=-1){
moves.splice(index, 1);
}
var turn = countItems(moves,'E')%2+1;
if (turn!=player || moves.slice(-1)[0]=='E'){//move commited or move just started
return gamestring;
}
var turnMoves = [];
for (var i=moves.length-1; i>=0; i--){
if (moves[i] != ''){
if (moves[i]=='E'){
break;
}else{
turnMoves.push(moves[i]);
}
}
}
turnMoves.reverse();
moves.splice(-turnMoves.length,turnMoves.length)
if(undo=='all'){
turnMoves = [];
}else if(undo.length == 2){
for(var i=0; i<turnMoves.length; i++){
if (turnMoves[i].slice(0,2)==undo){
index = i;
break;
}
}
var type = turnMoves[i].slice(-1)[0];
if (type=='A' || type=='D'){
turnMoves = [];
}else if(type=='C' || type=='B'){
turnMoves.splice(index,1);
for (var i=0; i<turnMoves.length; i++){
if (turnMoves[i].slice(-1)[0] == 'C'){
turnMoves[i] = turnMoves[i].slice(0,2)+'B';
}
}
}
}
parts = parts.slice(0,6).concat(moves).concat(turnMoves);
return parts.join()+",";
}
function getCellsCount(grid) {
var redCells = 0;
var blueCells = 0;
var whiteCells = 0;
for (var y = 0; y < grid.length; y++) {
for (var x = 0; x < grid[0].length; x++) {
if (grid[x][y].currentState == 1)
redCells ++;
else if (grid[x][y].currentState == 2)
blueCells ++;
else if(grid[x][y].currentState == 3){
whiteCells ++;
}
}
}
return {red: redCells, blue: blueCells, white: whiteCells};
}
try{
module.exports.parseRule = parseRule;
module.exports.newBoard = newBoard;
module.exports.stringToBoard = stringToBoard;
module.exports.boardToString = boardToString;
module.exports.iterate = iterate;
module.exports.checkLegit = checkLegit;
module.exports.doMoves = doMoves;
module.exports.tryUndo = tryUndo;
module.exports.remakeBoard = remakeBoard;
module.exports.getCellsCount = getCellsCount;
}catch(err){}