Skip to content

Commit

Permalink
and call it on each iteration of a game loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Volchenko committed Aug 19, 2018
1 parent f0e6d43 commit cda6a4a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.js
@@ -1,19 +1,23 @@
import { GameState, getWinner, turn } from './game-state.js';
import { GameState, getWinner, turn, isGameFinished } from './game-state.js';
import { setupCanvas } from './canvas-setup.js';
import { draw } from './renderer.js';

function* gameLoop(gameState) {
let winner = -1;

while (winner < 0) {
while (winner < 0 && !isGameFinished(gameState)) {
const [rowIndex, colIndex] = yield;
turn(gameState, rowIndex, colIndex);

winner = getWinner(gameState);
}

setTimeout(() => {
alert(`Congratulations, ${['X', 'O'][winner]}! You won!`);
if (winner < 0) {
alert(`It's a draw`);
} else {
alert(`Congratulations, ${['X', 'O'][winner]}! You won!`);
}
});
}

Expand Down

0 comments on commit cda6a4a

Please sign in to comment.