-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGo.html
144 lines (122 loc) · 3.77 KB
/
Go.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
135
136
137
138
139
140
141
142
143
144
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Go</title>
<meta name="description" content="JS Go">
<meta name="author" content="Ben Grabow">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div id="game">
<div id="game-board">
<img class="grid" src="./grid_648.png"/>
</div>
<div id="game-status">
<div id="current-player">
<span>Current player: </span>
<span id="current-player-value"></span>
</div>
<button id="pass-button" class="button">Pass</button>
<div id="game-over"></div>
</div>
</div>
<script src="./js/scripts.js"></script>
<script>
window.onload = () => {
buildCellDivs();
var game = new GoGame();
game.subscribe({
notify: (stateJSON) => render(JSON.parse(stateJSON))
});
var cellDivs = document.querySelectorAll('.cell');
forEachNodeIn(cellDivs, cellDiv => {
cellDiv.addEventListener('click', () => {
notifyGameOfClick(cellDiv.dataset.col, cellDiv.dataset.row, game);
});
});
document.getElementById("pass-button").addEventListener('click', ()=>{
notifyGameOfPass(game);
})
}
const CELL_WIDTH = 36;
const CELL_HEIGHT = 36;
const BOARD_SIZE = 19;
function render(state) {
renderGameBoard(state);
renderGameStatus(state);
}
function renderGameBoard(state) {
forEachNodeIn(document.querySelectorAll('.cell'), cellDiv => {
var col = cellDiv.dataset.col;
var row = cellDiv.dataset.row;
var newColor = state.cells[key(col, row)] || Stone.empty;
setColor(cellDiv, newColor);
function key(col, row) {
return [col, row].toString();
}
});
}
function renderGameStatus(state) {
showCurrentPlayer(state.currentPlayer);
showGameStatus(state.gameOver);
}
function showCurrentPlayer(currentPlayer) {
setColor(document.getElementById("current-player-value"), currentPlayer);
}
function showGameStatus(gameOver) {
var text = gameOver ? "Game over!" : "";
document.getElementById('game-over').innerText = text;
}
function setColor(container, color) {
setEmpty(container);
if(color !== Stone.empty) {
var stone = document.createElement('div');
stone.className = `${color} stone`;
stone.dataset.color = color;
container.appendChild(stone);
}
}
function setEmpty(container) {
while(container.firstChild) {
container.removeChild(container.firstChild);
}
}
function forEachNodeIn(nodeList, callback) {
Array.prototype.forEach.call(nodeList, callback);
}
function notifyGameOfClick(col, row, game) {
game.currentPlayerSelects(col, row);
}
function notifyGameOfPass(game) {
game.currentPlayerPasses();
}
function buildCellDivs() {
var game = document.getElementById("game-board");
for (var row=0; row < BOARD_SIZE; row++) {
for (var col=0; col < BOARD_SIZE; col++) {
game.appendChild(createCellAt(col, row));
}
}
}
function createCellAt(col, row) {
var cell = document.createElement('div')
cell.className = 'cell';
cell.id = cellId(col, row);
cell.dataset.col = col;
cell.dataset.row = row;
cell.style.left = CELL_WIDTH * col + 'px';
cell.style.top = CELL_HEIGHT * row + 'px';
cell.style.width = CELL_WIDTH + 'px';
cell.style.height = CELL_HEIGHT + 'px';
return cell;
}
function cellId(col, row) {
return `cell-#{col}-#{row}`;
}
</script>
</body>
</html>