MCPcopy
hub / github.com/tensorflow/tfjs-examples / step

Function step

snake-dqn/index.js:64–87  ·  view source on GitHub ↗

* Play a game for one step. * * - Use the current best action to forward one step in the game. * - Accumulate to the cumulative reward. * - Determine if the game is over and update the UI accordingly. * - If the game has not ended, calculate the current Q-values and best action. * - Render the

()

Source from the content-addressed store, hash-verified

62 * - Render the game in the canvas.
63 */
64async function step() {
65 const {reward, done, fruitEaten} = game.step(bestAction);
66 invalidateQValuesAndBestAction();
67 cumulativeReward += reward;
68 if (fruitEaten) {
69 cumulativeFruits++;
70 }
71 gameStatusSpan.textContent =
72 `Reward=${cumulativeReward.toFixed(1)}; Fruits=${cumulativeFruits}`;
73 if (done) {
74 gameStatusSpan.textContent += '. Game Over!';
75 cumulativeReward = 0;
76 cumulativeFruits = 0;
77 if (autoPlayIntervalJob) {
78 clearInterval(autoPlayIntervalJob);
79 autoPlayStopButton.click();
80 }
81 autoPlayStopButton.disabled = true;
82 stepButton.disabled = true;
83 }
84 await calcQValuesAndBestAction();
85 renderSnakeGame(gameCanvas, game,
86 showQValuesCheckbox.checked ? currentQValues : null);
87}
88
89let currentQValues;
90let bestAction;

Callers 1

initGameFunction · 0.85

Calls 4

renderSnakeGameFunction · 0.90
calcQValuesAndBestActionFunction · 0.85
stepMethod · 0.80

Tested by

no test coverage detected