Skip to content

Commit

Permalink
Decompose click handler
Browse files Browse the repository at this point in the history
  • Loading branch information
cutiful committed Mar 28, 2020
1 parent 06fa2a8 commit d3d9cbf
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,21 @@ class FourInLine {
drawText(this.ctx, this.width, this.height, "No moves left!", 32, "white", "black");
}

_clickHandler(e) {
if (this.paused) return;

if (this._noMoves || this._winner.team) {
this.reset.call(this);
this._active = true;
this._currentTurn = this._firstTurn === 1 ? 2 : 1;
this._firstTurn = this._currentTurn;
this.draw.call(this);
return;
}

if (!this._active) return;

const col = this._selectedColumn - 1;
static getMoveRow(circles, col) {
let row = -1;
for (let i = this._circles.length - 1; i >= 0; i--) {
if (this._circles[i][col]) continue;
for (let i = circles.length - 1; i >= 0; i--) {
if (circles[i][col]) continue;

row = i;
break;
}

return row;
}

processMove(col) {
const row = FourInLine.getMoveRow(this._circles, col);

if (row === -1) return;
this._active = false;
this.animating = true;
Expand All @@ -124,6 +116,22 @@ class FourInLine {
});
}

_clickHandler(e) {
if (this.paused) return;

if (this._noMoves || this._winner.team) {
this.reset.call(this);
this._active = true;
this._currentTurn = this._firstTurn === 1 ? 2 : 1;
this._firstTurn = this._currentTurn;
this.draw.call(this);
return;
}

if (!this._active) return;
this.processMove.call(this, this._selectedColumn - 1);
}

_mousemoveHandler(e) {
const column = Math.ceil(e.offsetX / (this.width / columns));
if (column !== this._selectedColumn) {
Expand Down

0 comments on commit d3d9cbf

Please sign in to comment.