-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-four.html
134 lines (115 loc) · 3.32 KB
/
connect-four.html
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
<head>
<style>
td { width: 30px; height: 30px; background: #eee;}
table button { background: lightblue; width: 30px; height: 30px; }
div {width: 30px; height: 30px; border-radius: 50%;}
.red { background: red;}
.black { background: black; }
</style>
<body>
<p id="msg">Red: It is your turn</p>
<table>
<tr>
<th><button data-col=0></button>
<th><button data-col=1></button>
<th><button data-col=2></button>
<th><button data-col=3></button>
<th><button data-col=4></button>
<tbody id="board">
<tr> <td><div></div> <td><div></div> <td><div></div> <td><div></div> <td><div></div>
<tr> <td><div></div> <td><div></div> <td><div></div> <td><div></div> <td><div></div>
<tr> <td><div></div> <td><div></div> <td><div></div> <td><div></div> <td><div></div>
<tr> <td><div></div> <td><div></div> <td><div></div> <td><div></div> <td><div></div>
<tr><td><div></div> <td><div></div> <td><div></div> <td><div></div> <td><div></div>
</table>
<p id="msg">To place a piece, click one of the buttons at the top of the board.</p>
<button id="start_over">Start over</button>
<script>
let currentPlayer = 'red';
let msg = document.getElementById('msg');
let board = [
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
];
let falling = false;
function switchPlayer() {
currentPlayer = (currentPlayer == 'red') ? 'black' : 'red';
msg.innerText = currentPlayer + ': It is your turn';
}
function render() {
let tbody = document.querySelector('#board');
for (let row=0; row<5; row++)
for (let col=0; col<5; col++)
tbody.children[row].children[col].children[0].className = board[row][col];
}
function addPiece(col, color) {
if (board[0][col]) return 'Blocked';
board[0][col] = color;
render();
falling = true;
}
document.querySelectorAll('table button').forEach(b => b.addEventListener('click', (e) => {
let col = e.target.dataset['col'];
addPiece(col, currentPlayer);
switchPlayer();
}));
function gravity() {
falling = false;
for (let row=4; row>=1; row--)
for (let col=4; col>=0; col--) {
if (board[row][col] == '' &&
board[row-1][col] != '') {
board[row][col] = board[row-1][col];
board[row-1][col] = '';
falling = true;
}
}
if (falling) render();
}
function check4(r, c, dr, dc) {
if (board[r][c] == '') return;
let color = board[r][c];
if (board[r + 1 * dr][c + 1 * dc] == color &&
board[r + 2 * dr][c + 2 * dc] == color &&
board[r + 3 * dr][c + 3 * dc] == color) {
msg.innerText = color + ' has won!'
}
}
function checkWinner() {
if (falling) return;
for (let r=0; r<5; r++) {
check4(r, 0, 0, 1);
check4(r, 1, 0, 1);
}
for (let c=0; c<5; c++) {
check4(0, c, 1, 0);
check4(1, c, 1, 0);
}
for (let r=0; r<2; r++) {
check4(r, 0, 1, 1);
check4(r, 1, 1, 1);
}
for (let r=3; r<5; r++) {
check4(r, 0, -1, 1);
check4(r, 1, -1, 1);
}
}
function gameloop() {
gravity();
checkWinner();
}
setInterval(gameloop, 1000);
document.getElementById('start_over').addEventListener('click', (e) => {
switchPlayer();
for (let i = 0; i < 6; i++) {
setTimeout(() => {
board[4] = ['', '', '', '', ''];
gravity(); render();},
500 * i);
}
});
</script>
</body?