forked from harishsinghbisht8/snapdeal_hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
86 lines (76 loc) · 1.78 KB
/
logic.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
/*
var obj = {
currentMove: 1,
x: 2,
y: 0,
matrix: [[1,2,1],
[0,1,2],
[0,0,0]],
moveCount: 1
}*/
module.exports = function(obj) {
var matrix = obj.matrix;
var x = obj.x;
var y =obj.y;
var currentMove = obj.currentMove;
var n = matrix.length;
var moveCount = obj.moveCount;
if(matrix[x][y] == 0){
matrix[x][y] = currentMove;
}
var resultdata = {
"result" : "progress",
"matrix": matrix
}
//check end conditions
//check col
for(var i = 0; i < n; i++){
if(matrix[x][i] != currentMove)
break;
if(i == n-1){
resultdata.result = "won";
console.log("won");
//report win for s
}
}
//check row
for(var i = 0; i < n; i++){
if(matrix[i][y] != currentMove)
break;
if(i == n-1){
resultdata.result = "won";
console.log("won");
//report win for s
}
}
//check diag
if(x+y==(n-1) || x==y){
//on a diagonal
for(var i = 0; i < n; i++){
if(matrix[i][i] != currentMove)
break;
if(i == n-1){
resultdata.result = "won";
console.log("won");
//report win for s
}
}
//check anti diag
for(var i = 0;i<n;i++){
if(matrix[i][(n-1)-i] != currentMove)
break;
if(i == n-1){
resultdata.result = "won";
console.log("won");
//report win for s
}
}
}
//check draw
if(moveCount == (n*n)){
resultdata.result = "draw";
console.log('draw game over')
//report draw
}
return resultdata;
}