Skip to content

Commit

Permalink
Make it possible to add an external player to the game
Browse files Browse the repository at this point in the history
  • Loading branch information
cutiful committed Mar 29, 2020
1 parent 01c4a3a commit e7b77c1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { rows, columns } from "./config.js";
import { checkWinningCombinations, checkAvailableMoves } from "./rules.js";
import { drawWinnerScreen, drawText } from "./text.js";
import { hasHover } from "./window.js";
import { copyCircles } from "./misc.js";

class FourInLine {
constructor(ctx, width, height) {
Expand All @@ -26,6 +27,9 @@ class FourInLine {
this._animating = false;
this._animationWaiters = [];

this._externalPlayer = 0;
this._externalPlayerFn = undefined;

for (let i = 0; i < rows; i++) {
const row = [];
for (let i = 0; i < columns; i++) {
Expand Down Expand Up @@ -114,6 +118,9 @@ class FourInLine {

this.animating = false;
this.draw.call(this);

if (this._active)
setTimeout(this.checkExternalPlayer.bind(this));
});
}

Expand All @@ -126,6 +133,9 @@ class FourInLine {
this._currentTurn = this._firstTurn === 1 ? 2 : 1;
this._firstTurn = this._currentTurn;
this.draw.call(this);

setTimeout(this.checkExternalPlayer.bind(this));

return;
}

Expand Down Expand Up @@ -195,6 +205,27 @@ class FourInLine {
this._animationWaiters.push(resolve);
});
}

setExternalPlayer(player, fn) {
this._externalPlayer = player;
this._externalPlayerFn = fn;

if (this._active)
setTimeout(this.checkExternalPlayer.bind(this));
}

resetExternalPlayer() {
this._externalPlayer = 0;
this._externalPlayerFn = undefined;
}

checkExternalPlayer() {
if (this._externalPlayer && this._currentTurn === this._externalPlayer) {
this._active = false;
this._externalPlayerFn(copyCircles(this._circles))
.then(this.processMove.bind(this));
}
}
}

export default FourInLine;

0 comments on commit e7b77c1

Please sign in to comment.