-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender-state.js
41 lines (36 loc) · 990 Bytes
/
render-state.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
const render = (state, options) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tic-tac-toe</title>
</head>
<body>
<table align="center">
<tr><td>${renderCaption(state)}</td></tr>
<tr><td>${renderTable(state)}</td></tr>
<tr><td align="center"><a href="../index.html">Back</a></td></tr>
</table>
</body>
</html>`;
const renderCaption = state => {
if(state.winner){
if(state.winner == "tie"){
return `It's a tie!`;
}else{
return `Player ${state.winner.toUpperCase()} wins!`;
}
}
return `Player ${state.player.toUpperCase()}'s turn.`;
}
const renderTable = state => {
let cells = state.field.slice();
Object.keys(state.moves).forEach(move => {
cells[+move] = renderLink(state.moves[move]);
});
return `<table align="center">${[0, 1, 2].map(i => `<tr>
${[0, 1, 2].map(j => `<td>${cells[3 * i + j]}</td>`).join("")}
</tr>`).join("")}</table>`
}
const renderLink = key => `<a href="./${key}.html">_</a>`;
module.exports = render;