-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
443 lines (404 loc) · 11.8 KB
/
app.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
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
// var http = require('http')
// var io = require('socket.io');
var app = express();
// var server = http.Server(app);
// var websocket = io(server);
//
// websocket.on('connection', (socket) => {
// console.log('A client just joined on', socket.id);
// });
//
// io.on('updateBoard', (newBoard) => {
// // Save the message document in the `messages` collection.
// // db.collection('messages').insert(message);
//
// // The `broadcast` allows us to send to all users but the sender.
// socket.broadcast.emit('updateBoard', board);
// });
// server.listen(3000, () => console.log('listening on *:3000'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// app.use(logger('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
var newBoard;
var initialBoardLayout=[]
var isDefeated = false;
var winner;
if (!gameFull){
console.log('in gamefull')
var gameFull = false;
}
if (!numPlayers) {
var numPlayers = 0
}
var spy = {value: 'S', team: 'red'}
var att = {value: 6, team: 'blue'}
var def = {value: 3, team: 'red'}
var bomb = {value: 'B', team: 'blue'}
var emptyBoard= [ ["", "","","","","","",""],
["", "","","","","","",""],
["", "","","","","","",""],
["", "","","","","","",""],
["", "","","","","","",""],
["", "","","","","","",""],
["", "","","","","","",""],
["", "","","","","","",""],
]
if (!board){
console.log('in Set Board')
var board = emptyBoard
}
if (!currentPlayer){
console.log('in current Player')
var currentPlayer = 'red';
}
var twoDCopy = function(arr, arrLen) {
var copy = []
for (let k = 0; k < arr.length; k++) {
copy.push(arr[k].slice());
}
return copy
}
var transpose=function(arr,arrLen) {
var copy = []
for (let k = 0; k < arr.length; k++) {
copy.push(arr[k].slice());
}
for (var i = 0; i < arrLen; i++) {
for (var j = 0; j <i; j++) {
//swap element[i,j] and element[j,i]
var temp = copy[i][j];
copy[i][j] = copy[j][i];
copy[j][i] = temp;
}
}
return copy;
}
var nextPlayer = function(){
return (currentPlayer === 'red') ? 'blue' : 'red'
}
var updatePos = function(position1, position2){
// console.log('update')
console.log('1: ',position1)
console.log('2: ', position2)
initialPiece = board[position1.row][position1.col];
board[position2.row][position2.col] = initialPiece;
board[position1.row][position1.col] = "";
// console.log(board);
return board;
}
var battle = function(position1, position2) {
var attacker = board[position1.row][position1.col];
var defender = board[position2.row][position2.col];
// console.log('attacker', attacker)
// console.log('defender', defender)
if (defender.piece === 'F') {
isDefeated = true;
var winner = (defender.team === 'red') ? 'blue':'red'
return attacker.piece
}
if (defender.piece === 'B') {
if(parseInt(attacker.piece) === 3) {
board[position2.row][position2.col] = "";
updatePos(position1, position2);
return attacker.piece.toString()
}else{
board[position1.row][position1.col] = "";
return defender.piece.toString()
}
}
if (attacker.piece === 'S') {
board[position2.row][position2.col] = "";
updatePos(position1, position2);
return attacker.piece.toString()
}
if (defender.piece === 'S') {
// console.log('spy defender')
board[position2.row][position2.col] = "";
updatePos(position1, position2);
return attacker.piece.toString()
}
if (parseInt(attacker.piece) > parseInt(defender.piece)) {
board[position2.row][position2.col] = "";
updatePos(position1, position2);
return attacker.piece.toString()
} else {
board[position1.row][position1.col] = "";
return defender.piece.toString()
}
}
var makeMove = function(position, direction, position2){
// console.log('direction:',direction)
if (board[position.row][position.col].piece === '2') {
if (board[position2.row][position2.col] === ""){
updatePos(position, position2)
}
battle(position, position2)
}
if (direction === 'up') {
console.log('up')
if (board[position.row - 1][position.col] === "") {
return updatePos({row: position.row, col: position.col},{row: position.row - 1, col: position.col});
} else{
return battle({row: position.row, col: position.col}, {row: position.row - 1, col: position.col})
}
} if (direction === 'down') {
console.log('down')
if (board[position.row + 1][position.col] === "") {
return updatePos({row: position.row, col: position.col},{row: position.row + 1, col: position.col});
} else{
return battle({row: position.row, col: position.col}, {row: position.row + 1, col: position.col})
}
} if (direction === 'left') {
console.log('left')
if(board[position.row][position.col-1] === "") {
return updatePos({row: position.row, col: position.col},{row: position.row, col: position.col - 1});
} else{
return battle({row: position.row, col: position.col}, {row: position.row , col: position.col - 1})
}
} if (direction === 'right') {
console.log('right')
if(board[position.row][position.col+1] === "") {
return updatePos({row: position.row, col: position.col},{row: position.row, col: position.col + 1});
} else{
return battle({row: position.row, col: position.col}, {row: position.row, col: position.col + 1})
}
}
}
var checkClearPath = function(pos1, pos2, direction){
console.log('step 2')
if (direction === 'up'){
console.log('step 3')
pos1.row--;
if (pos1.row !== pos2.row) {
console.log('inside if statement')
if (board[pos1.row][pos1.col] === ""){
return checkClearPath(pos1 , pos2, direction);
} else {
return false;
}
} else {
console.log('returning true')
return true;
}
}
if (direction === 'down'){
console.log('step 3')
pos1.row++;
if (pos1.row !== pos2.row) {
console.log('inside if statement')
if (board[pos1.row][pos1.col] === ""){
return checkClearPath(pos1 , pos2, direction);
} else {
return false;
}
} else {
console.log('returning true')
return true;
}
}
if (direction === 'left'){
console.log('step 3')
pos1.col--;
if (pos1.col !== pos2.col) {
console.log('inside if statement')
if (board[pos1.row][pos1.col] === ""){
return checkClearPath(pos1 , pos2, direction);
} else {
return false;
}
} else {
console.log('returning true')
return true;
}
}
if (direction === 'right'){
console.log('step 3')
pos1.col++;
if (pos1.col !== pos2.col) {
console.log('inside if statement')
if (board[pos1.row][pos1.col] === ""){
return checkClearPath(pos1 , pos2, direction);
} else {
return false;
}
} else {
console.log('returning true')
return true;
}
}
}
var checkValidMove=function(pos1,pos2) {
if (board[pos1.row][pos1.col].piece !== '2') {
if (pos2.row-pos1.row === 1 && pos2.col - pos1.col === 0) {
return 'down'
}
if (pos2.row-pos1.row === -1 && pos2.col - pos1.col === 0) {
return 'up'
}
if (pos2.col-pos1.col === 1 && pos2.row - pos1.row === 0) {
return 'right'
}
if (pos2.col-pos1.col === -1 && pos2.row - pos1.row === 0) {
return 'left'
}
return 'illegal move'
} else {
ogPos = {row: pos1.row, col: pos1.col}
if ((pos2.row-pos1.row > 0) && (pos2.col - pos1.col === 0)) {
var direction = 'down'
console.log(direction)
if (checkClearPath(ogPos,pos2, direction)){
return direction
}
}
if ((pos2.row-pos1.row < 0) && (pos2.col - pos1.col === 0)) {
console.log('step 1')
console.log(direction)
var direction = 'up'
if (checkClearPath(ogPos,pos2, direction)){
console.log('returning', direction)
return direction
}
}
if ((pos2.col-pos1.col > 0) && (pos2.row - pos1.row === 0)) {
var direction = 'right'
console.log(direction)
if (checkClearPath(ogPos,pos2, direction)){
return direction
}
}
if ((pos2.col-pos1.col < 0) && (pos2.row - pos1.row === 0)) {
var direction = 'left'
console.log(direction)
if (checkClearPath(ogPos,pos2, direction)){
return direction
}
}
}
console.log('iligal move');
return 'illegal move'
}
app.get('/',function(req,res) {
res.send('hello world')
})
app.post('/test', function(req, res){
console.log('body', req.body)
res.json({
final: 'hey'
})
})
app.get('/joingame', function(req, res){
var team;
// console.log('num ', numPlayers)
if (!gameFull) {
team = (numPlayers === 0 ? 'red' : 'blue')
numPlayers++
if (numPlayers === 2) {
gameFull = true;
}
} else {
team = 'Game is Full'
}
// if
// numPlayers = 1;
// res.json({
// myTeam: 'red'
// })
// }
// if(numPlayers === 1 ){
// numPlayers = 2;
// res.json({
// myTeam: 'blue'
// })
// console.log('2 ',myTeam)
// } else if (numPlayers = 2){
// res.json({
// myTeam: 'Game Full'
// })
// }
res.json({
myTeam: team
})
// console.log('3', myTeam)
});
app.post('/stateupdate', function(req, res){
// if (board[0][0] !== "" && board[7][7] !== "")
// var bd = req.body.reqBoard
// board = transpose(board, 8);
//console.log('BOARD:',board)
var x = board.slice()
var returnBoard = transpose(x, 8);//transpose(board, 8);
//console.log('transposed board::',returnBoard)
var response = {board: returnBoard, currPlayer: currentPlayer};
res.json(response)
});
app.post('/setupboard',function(req,res) {
newBoard = req.body.board;
//console.log('NEWBOARD B4 transpose', newBoard);
var thisTeam = req.body.team;
var boardTranspose = transpose(newBoard, 8);
// console.log('BOARDTRANSPOSE ',boardTranspose)
//console.log('after transpose:',newBoard)
if (thisTeam === 'blue') {
console.log('I AM TEAM ', thisTeam)
board[0] = boardTranspose[0];
board[1] = boardTranspose[1];
board[2] = boardTranspose[2];
} else if (thisTeam === 'red') {
console.log('I AM TEAM ', thisTeam)
board[5] = boardTranspose[5];
board[6] = boardTranspose[6];
board[7] = boardTranspose[7];
}
// console.log('This is the board After', board)
var flipBoard = [...board]
// console.log('Post setupboard ********', board)
var newboardTranspose = (transpose(flipBoard, 8));
// console.log('sending transpose::',newboardTranspose)
res.json({board: newboardTranspose});
})
app.post('/makemove', function(req, res) {
// console.log('in');
// console.log('BOARD BEFORE MOVE IS MADE:',board)
newBoard = req.body.board;
boardTranspose = transpose(newBoard, 8);
board = boardTranspose;
var moves = req.body.move;
var copyBoard = [...board];
var pos2 = moves[1];
var pos1 = moves[0];
var direction = checkValidMove(pos1,pos2);
if(direction==='illegal move') {
('dir = ', direction)
copyBoard = transpose(copyBoard, 8)
// console.log('COPY', copyBoard)
res.json({
board: copyBoard,
currentPlayer: currentPlayer,
move: [pos1]
});
} else {
// console.log('legal move')
makeMove(pos1,direction,pos2);
var newboard = twoDCopy(board, 8);
// console.log("New BOARD", newboard)
var newboard2 = transpose(newboard, 8)
var nextPlayerVal = nextPlayer();
currentPlayer = nextPlayerVal;
// console.log('MaDE MOVE', nextPlayerVal);
res.json({
board: newboard2,
currentPlayer: nextPlayerVal,
move: [],
gameWon: isDefeated
});
}
})
app.listen(process.env.PORT||3000)