-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
128 lines (111 loc) · 3.34 KB
/
script.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
var playerRed = "R";
var playerYellow = "Y";
var currPlayer = playerRed;
var gameOver = false;
var board;
var rows = 6;
var columns = 7;
var currColumns = []; //keeps track of which row each column is at.
window.onload = function() {
setGame();
}
function setGame() {
board = [];
currColumns = [5, 5, 5, 5, 5, 5, 5];
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;
}
//get coords of that tile clicked
let coords = this.id.split("-");
let r = parseInt(coords[0]);
let c = parseInt(coords[1]);
// figure out which row the current column should be on
r = currColumns[c];
if (r < 0) { // board[r][c] != ' '
return;
}
board[r][c] = currPlayer; //update JS board
let tile = document.getElementById(r.toString() + "-" + c.toString());
if (currPlayer == playerRed) {
tile.classList.add("red-piece");
currPlayer = playerYellow;
}
else {
tile.classList.add("yellow-piece");
currPlayer = playerRed;
}
r -= 1; //update the row height for that column
currColumns[c] = r; //update the array
checkWinner();
}
function checkWinner() {
// horizontal
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns - 3; c++){
if (board[r][c] != ' ') {
if (board[r][c] == board[r][c+1] && board[r][c+1] == board[r][c+2] && board[r][c+2] == board[r][c+3]) {
setWinner(r, c);
return;
}
}
}
}
// vertical
for (let c = 0; c < columns; c++) {
for (let r = 0; r < rows - 3; r++) {
if (board[r][c] != ' ') {
if (board[r][c] == board[r+1][c] && board[r+1][c] == board[r+2][c] && board[r+2][c] == board[r+3][c]) {
setWinner(r, c);
return;
}
}
}
}
// anti diagonal
for (let r = 0; r < rows - 3; r++) {
for (let c = 0; c < columns - 3; c++) {
if (board[r][c] != ' ') {
if (board[r][c] == board[r+1][c+1] && board[r+1][c+1] == board[r+2][c+2] && board[r+2][c+2] == board[r+3][c+3]) {
setWinner(r, c);
return;
}
}
}
}
// diagonal
for (let r = 3; r < rows; r++) {
for (let c = 0; c < columns - 3; c++) {
if (board[r][c] != ' ') {
if (board[r][c] == board[r-1][c+1] && board[r-1][c+1] == board[r-2][c+2] && board[r-2][c+2] == board[r-3][c+3]) {
setWinner(r, c);
return;
}
}
}
}
}
function setWinner(r, c) {
let winner = document.getElementById("winner");
if (board[r][c] == playerRed) {
winner.innerText = "Red Wins";
} else {
winner.innerText = "Yellow Wins";
}
gameOver = true;
}