Skip to content

Commit

Permalink
[Fix] Bug on issue #4 and some of possible draw/tie
Browse files Browse the repository at this point in the history
  • Loading branch information
FOWMind committed Nov 30, 2022
1 parent a5e2ac8 commit 3c81221
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/modules/EventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import Utils from "./Utils.js";
export default class EventManager {
constructor() {
this.uiManager = new UIManager();
this.utils = new Utils();
this.gameManager = new GameManager();
this.audioManager = new AudioManager();
this.gameManager = GameManager.instance();
this.utils = new Utils();
console.log("EventManager instanciado");
}

/**
Expand Down
58 changes: 33 additions & 25 deletions src/modules/GameManager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import AudioManager from "./AudioManager.js";
import Utils from "./Utils.js";

let currentTurnTimer;
let currentTurnTimeout;

export default class GameManager {
currentTurnTimer;
currentTurnTimeout;
players = {
one: "1",
two: "2",
Expand All @@ -14,15 +13,27 @@ export default class GameManager {
timePerTurn = 30; // seconds
gameStarted = false;

static newInstance = new GameManager();

/**
* Singleton
*/
static instance() {
if (!GameManager.newInstance) {
GameManager.newInstance = new GameManager();
}
return this.newInstance;
}

constructor() {
this.gameOver = false;
this.utils = new Utils();
this.audioManager = new AudioManager();
}

clearTimers() {
clearInterval(currentTurnTimer);
clearTimeout(currentTurnTimeout);
clearInterval(this.currentTurnTimer);
clearTimeout(this.currentTurnTimeout);
}

clearTurn() {
Expand All @@ -31,7 +42,8 @@ export default class GameManager {
this.updateTurnTime(this.timePerTurn);
}

resetDiscs(discs) {
resetDiscs() {
const discs = this.utils.getAllDiscs();
discs.forEach((disc) =>
disc.classList.remove(
"filled",
Expand All @@ -46,10 +58,7 @@ export default class GameManager {
* Stop the current game session.
*/
stopGame() {
const discs = this.utils.getAllDiscs();
const someDiscFilled = discs.some((disc) => {
return disc.classList.contains("filled");
});
const someDiscFilled = this.utils.someDiscFilled();

// Avoid stop if not necessary
if (!someDiscFilled) return;
Expand All @@ -60,7 +69,7 @@ export default class GameManager {
this.gameOver = false;
this.clearTimers();
this.clearTurn();
this.resetDiscs(discs);
this.resetDiscs();
}

/**
Expand Down Expand Up @@ -95,39 +104,39 @@ export default class GameManager {

/**
* Updates turn time every second and shows it on screen.
* @returns The turn interval identifier.
*/
updateTurnTimeEachSecond() {
let timeLeft = this.timePerTurn;
const timer = setInterval(() => {
this.currentTurnTimer = setInterval(() => {
// each second
timeLeft--;
this.updateTurnTime(timeLeft);
}, 1000);
return timer;
}

/**
* Changes the current turn to opponent when turn time ends.
* @returns The turn timeout identifier.
*/
changeTurnAfterTimeout() {
const timeout = setTimeout(() => {
this.currentTurnTimeout = setTimeout(() => {
this.handleTurnChange();
}, this.timePerTurn * 1000);
return timeout;
}

/**
* Handles what happen with the game turns.
*/
handleTurnChange() {
if (this.gameOver) return;
if (this.utils.allDiscFilled()) {
// TODO: Do something elaborate
this.stopGame();
return;
}
this.changeTurnToOpponent();
clearInterval(currentTurnTimer);
clearTimeout(currentTurnTimeout);

currentTurnTimer = this.updateTurnTimeEachSecond();
currentTurnTimeout = this.changeTurnAfterTimeout();
this.clearTimers();
this.updateTurnTimeEachSecond();
this.changeTurnAfterTimeout();
}

/**
Expand All @@ -146,9 +155,8 @@ export default class GameManager {

setWin() {
this.gameOver = true;
console.log("Player " + this.playerWithTurn + " wins!");
this.clearTimers();
this.audioManager.playSound("win");

console.log({ currentTurnTimer, currentTurnTimeout });
console.log("Player " + this.playerWithTurn + " wins!");
}
}
3 changes: 2 additions & 1 deletion src/modules/UIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default class UIManager {
this.rows = 6;
this.discSize = 50; // pixels
this.root = document.getElementById("root");
this.gameManager = new GameManager();
this.gameManager = GameManager.instance();
console.log("UIManager instanciado");
}
/**
* Handles the game render.
Expand Down
28 changes: 23 additions & 5 deletions src/modules/Utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import GameManager from "./GameManager.js";

const initialLastDiscPositionY = 6;
let lastDiscPositionY = initialLastDiscPositionY;
let currentPlayerWithTurn;
Expand All @@ -14,17 +16,30 @@ export default class Utils {
return [...discs];
}

someDiscFilled() {
const discs = this.getAllDiscs();
return discs.some((disc) => disc.classList.contains("filled"));
}

allDiscFilled() {
const discs = this.getAllDiscs();
const someDiscUnfilled = discs.some(
(disc) => !disc.classList.contains("filled")
);
return !someDiscUnfilled;
}

/**
* Checks if a disc is filled.
* @param {HTMLElement} disc - The disc to check if it's filled.
* @param {boolean} checkForCurrentPlayer - Check or not if the passed disc is filled by current player.
* @param {boolean} playerWithTurn - Current player with turn to check if the a disc is filled by that player.
* @returns Whether the disc is filled or not.
*/
isFilled(disc, checkForCurrentPlayer) {
isFilled(disc, playerWithTurn) {
const filled = disc.classList.contains("filled");
if (!checkForCurrentPlayer) return filled;
if (!playerWithTurn) return filled;
const filledByCurrentPlayer = disc.classList.contains(
"filled-" + currentPlayerWithTurn
"filled-" + playerWithTurn
);
return filled && filledByCurrentPlayer;
}
Expand Down Expand Up @@ -155,7 +170,10 @@ export default class Utils {
);
if (!sibling) continue;

const filledByCurrentPlayer = this.isFilled(sibling, true);
const filledByCurrentPlayer = this.isFilled(
sibling,
currentPlayerWithTurn
);
if (!filledByCurrentPlayer) continue;
allSibling.push(sibling);
}
Expand Down

0 comments on commit 3c81221

Please sign in to comment.