-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
174 lines (164 loc) · 3.74 KB
/
js.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
var red = 'r';
var yellow = 'y';
var current = red;
var gameOver = false;
var board;
var rows = 6;
var columns = 7;
var currColumns = [5,5,5,5,5,5,5]; //keeps track of which row each column is at.
window.onload = function() {
setGame();
}
function setGame() {
board = [];
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
// JS
row.push(' ');
// HTML
let tile = document.createElement("div");
tile.id = r.toString() + "-" + c.toString();
tile.classList.add("tile");
tile.addEventListener("click", setPiece);
document.getElementById("board").append(tile);
}
board.push(row);
}
}
function setPiece(){
if(gameOver)
return ;
let coords =this.id.split("-");
let r = parseInt(coords[0]);
let c = parseInt(coords[1]);
r = currColumns[c];
board[r][c] = current; //update JS board
console.log(r+" " +c +" "+currColumns[c],+" "+this);
let tile =document.getElementById(r.toString() + "-" + c.toString());
// let tile = this;
// let tile = "5-2";
if(current == red){
tile.classList.add("red-piece");
current = yellow;
}else{
tile.classList.add("yellow-piece");
current = red;
}
currColumns[c]--;
if(checkWinner(r,c ,current))
{
if(current == red)
document.getElementById("winnery").innerHTML = "Yellow";
else
document.getElementById("winnerr").innerHTML = "Red";
}
}
function checkWinner(r,c ,kk )
{
var count = 0;
if(kk == red)
kk= yellow;
else kk= red;
for(i = 5 ; i >= 0 ;i-- )
{
console.log(board[i][c] +" " +i+" "+c + " " + count);
if(board[i][c] == kk){
count++;
if(count >= 4)
{
gameOver = true;
return true;
}
}
else
count=0;
}
count=0;
for(i = 6 ; i >= 0 ;i-- )
{
console.log(board[r][i] +" " +r+" "+i + " "+count);
if(board[r][i] == kk)
{ count++;
if(count >= 4)
{
gameOver = true;
return true;
}
}
else
count = 0;
}
count = 0;
i=r;
j=c;
while(i<6 && j<7){
if(board[i][j]==kk)
{
count++;
console.log(board[i][j] +" zzz " +i+" "+j + " " + count);
i++;
j++;
}else
break;
}
if(count >= 4)
{
gameOver = true;
return true;
}
i = r ;
j = c;
count--;
while( i>=0 && j>=0){
if(board[i][j]==kk)
{
count++;
console.log(board[i][j] +" zzz " +i+" "+j + " " + count);
i--;
j--;
}else
break;
}
if(count >= 4)
{
gameOver = true;
return true;
}
i=r;
j=c;
count=0;
while( i >= 0 && j < 7){
if(board[i][j]==kk)
{
count++;
console.log(board[i][j] +" vv " +i+" "+j + " " + count);
i--;
j++;
}else
break;
}
i=r;
j=c;
if(count >= 4)
{
gameOver = true;
return true;
}
count--;
while(i < 6 && j >= 0){
if(board[i][j]==kk)
{
count++;
console.log(board[i][j] +" vv " +i+" "+j + " " + count);
i++;
j--;
}else
break;
}
if(count >= 4)
{
gameOver = true;
return true;
}
}