forked from tensorflow/tfjs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_graphics.js
177 lines (159 loc) · 5.34 KB
/
snake_graphics.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
175
176
177
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {SnakeGame} from './snake_game';
/**
* Render the state of the snake game on an HTML canvas element.
*
* @param {HTMLCanvasElement} canvas The canvas to render the game in.
* @param {SnakeGame} game The game to render.
* @param {Float32Array} qValues Q-values of the current step, optional.
* If provided, will overlay Q-values for possible actions on the rendered
* graph.
*/
export function renderSnakeGame(canvas, game, qValues) {
const width = canvas.width;
const height = canvas.height;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, width, height);
const state = game.getState();
const gameWidth= game.width;
const gameHeight = game.height;
const gridWidth = width / gameWidth;
const gridHeight = height / gameHeight;
// Draw the grid.
ctx.strokeStyle = '#aaa';
ctx.lineWidth = '0';
for (let i = 0; i <= gameHeight; ++i) {
ctx.moveTo(0, i * gridHeight);
ctx.lineTo(width, i * gridHeight);
ctx.stroke();
}
for (let i = 0; i <= gameWidth; ++i) {
ctx.moveTo(i * gridWidth, 0);
ctx.lineTo(i * gridWidth, height);
ctx.stroke();
}
// Draw the snake.
state.s.forEach((yx, i) => {
const [y, x] = yx;
ctx.fillStyle = i === 0 ? 'orange' : 'blue';
ctx.beginPath();
ctx.rect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
ctx.fill();
if (i === 0) {
ctx.strokeStyle = 'black';
ctx.lineWidth = '2';
ctx.beginPath();
ctx.moveTo((x + 0.25) * gridWidth, (y + 0.5) * gridHeight);
ctx.lineTo((x + 0.75) * gridWidth, (y + 0.5) * gridHeight);
ctx.stroke();
ctx.beginPath();
ctx.arc(
(x + 0.5) * gridWidth, (y + 0.5) * gridHeight, gridWidth * 0.25,
0, Math.PI);
ctx.stroke();
}
});
// Draw the fruit.
state.f.forEach(yx => {
const [y, x] = yx;
ctx.fillStyle = 'green';
ctx.beginPath();
ctx.rect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
ctx.fill();
ctx.strokeStyle = 'black';
ctx.lineWidth = '2';
ctx.beginPath();
ctx.arc(
(x + 0.5) * gridWidth, (y + 0.5) * gridHeight, gridWidth * 0.25,
0, 2 * Math.PI);
ctx.stroke();
});
if (qValues != null) { // If qNet is provided, render the q-values.
if (qValues.length !== 3) {
throw new Error(
`Expected qValues to be of length 3, ` +
`but got length ${qValues.length}`);
}
const [headY, headX] = state.s[0];
let qW;
let qN;
let qE;
let qS;
if (game.snakeDirection === 'l') {
qW = qValues[0];
qS = qValues[1];
qN = qValues[2];
} else if (game.snakeDirection === 'u') {
qN = qValues[0];
qW = qValues[1];
qE = qValues[2];
} else if (game.snakeDirection === 'r') {
qE = qValues[0];
qN = qValues[1];
qS = qValues[2];
} else if (game.snakeDirection === 'd') {
qS = qValues[0];
qE = qValues[1];
qW = qValues[2];
}
const {qWNormalized, qNNormalized, qENormalized, qSNormalized} =
normalizeQValuesForDisplay(qW, qN, qE, qS);
drawQValueOverlay(ctx, qW, qWNormalized,
(headX - 1) * gridWidth, headY * gridHeight, gridWidth, gridHeight);
drawQValueOverlay(ctx, qN, qNNormalized,
headX * gridWidth, (headY - 1) * gridHeight, gridWidth, gridHeight);
drawQValueOverlay(ctx, qE, qENormalized,
(headX + 1) * gridWidth, headY * gridHeight, gridWidth, gridHeight);
drawQValueOverlay(ctx, qS, qSNormalized,
headX * gridWidth, (headY + 1) * gridHeight, gridWidth, gridHeight);
}
}
function normalizeQValuesForDisplay(qW, qN, qE, qS) {
const scores = [qW, qN, qE, qS].filter(x => x != null);
const min = Math.min(...scores);
const max = Math.max(...scores);
const normalize = q => (q - min) / (max - min);
return {
qWNormalized: qW == null ? qW : normalize(qW),
qNNormalized: qN == null ? qN : normalize(qN),
qENormalized: qE == null ? qE : normalize(qE),
qSNormalized: qS == null ? qE : normalize(qS)
};
}
function drawQValueOverlay(context, q, qNormalized, x, y, w, h) {
if (q == null) {
return;
}
context.globalAlpha = 0.2;
let r = Math.floor((1 - qNormalized) * 255);
let g = 255;
let b = Math.floor((1 - qNormalized) * 255);
context.fillStyle = `rgb(${r},${g},${b})`;
context.beginPath();
context.rect(x, y, w, h);
context.fill();
context.globalAlpha = 1;
context.font = '13px sans serif';
r = Math.floor((1 - qNormalized) * 100 + 64);
g = Math.floor((1 - qNormalized) * 100 + 64);
b = Math.floor((1 - qNormalized) * 100 + 64);
context.fillStyle = `rgb(${r},${g},${b})`;
context.beginPath();
context.fillText(q.toFixed(1), x + 0.15 * w, y + 0.55 * h);
context.fill();
}