-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (111 loc) · 2.52 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
var Game = function(){
this.board = [[],[],[],[],[],[],[]];
this.lastPosition = new Array;
};
Game.prototype.checkFull = function(num){
return (this.board[num].length === 6) ? true: false;
}
Game.prototype.drop = function(num, color){
if (!this.checkFull(num)) {
var column = this.board[num];
column.push(color);
};
this.lastPosition = [parseInt(num), this.board[num].length-1];
}
Game.prototype.checkWin = function(){
var x = this.lastPosition[0];
var y = this.lastPosition[1];
//calculate south
var south = 0;
for (var i = 1; i < (y + 1); i++ ) {
if (this.board[x][y-i] !== this.board[x][y]) {
break;
}
south++;
};
//calculate west
var west = 0;
for (var i = 1; i < (x + 1); i++ ) {
if (this.board[x-i][y] !== this.board[x][y]) {
break;
}
west++;
};
//calculate east
var east = 0;
for (var i = 1; i < (7 - x); i++ ) {
if (this.board[x+i][y] !== this.board[x][y]) {
break;
}
east++;
};
//calculate northWest
var northWest = 0;
for (var i = 1; i < (x + 1); i++ ) {
if (this.board[x-i][y+i] !== this.board[x][y]) {
break;
}
northWest++;
};
//calculate northEast
var northEast = 0;
for (var i = 1; i < (7 - x); i++ ) {
if (this.board[x+i][y+i] !== this.board[x][y]) {
break;
}
northEast++;
};
//calculate southEast
var southEast = 0;
for (var i = 1; i < (y + 1); i++ ) {
if (this.board[x+i][y-i] !== this.board[x][y]) {
break;
}
southEast++;
};
//calculate southWest
var southWest = 0;
for (var i = 1; i < (y + 1); i++ ) {
if (this.board[x-i][y-i] !== this.board[x][y]) {
break;
}
southWest++;
};
//check south
if (south >= 3){
return true;
};
//check east + west
if (east + west >= 3){
return true;
};
//check northWest + southEast
if (northWest + southEast >= 3){
return true;
};
//check northEast + southWest
if (northEast + southWest >= 3){
return true;
};
//return false because no winner
return false;
};
// game.drop(1, 'black');
// game.drop(2, 'red');
// game.drop(2, 'black');
// game.drop(3, 'red');
// game.drop(3, 'red');
// game.drop(3, 'black');
// game.drop(4, 'red');
// game.drop(4, 'red');
// game.drop(4, 'red');
// console.log(game.board);
// console.log(game.checkWin())
// game.drop(4, 'black');
// console.log(game.board);
// console.log(game.checkWin())
// game.drop(1, 'black');
// game.drop(2, 'black');
// console.log(game.checkWin())
// game.drop(3, 'black');
//console.log(game.lastPosition);