Skip to content

Commit

Permalink
Merge pull request #702 from hars-21/surrender
Browse files Browse the repository at this point in the history
Surrender Button added
  • Loading branch information
ayush-t02 authored Aug 5, 2024
2 parents 39a1a8c + 383666f commit 128d035
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 26 additions & 0 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ class Game {
this.invokeEvent("playerSwitch");
}
}

surrender() {
if (confirm("Are you sure you want to surrender?")) {
this.players.splice(this.currentPlayerIndex, 1);

if (this.currentPlayerIndex >= this.players.length) {
this.currentPlayerIndex = 0;
}

this.currentPlayer = this.players[this.currentPlayerIndex];

this.addPlayersUI();
this.updatePlayerNameUI();
this.updatePlayerScoreUI();

if (this.players.length == 1) {
this.invokeEvent("playerWin");
}
}
}
}

// Declaring Global Variables
Expand Down Expand Up @@ -570,6 +590,12 @@ document.getElementById("setting-btn").addEventListener("click", () => {
menu.classList.toggle("menu-open");
});

// Surrender Button
const flag = document.getElementById("surrender");
flag.addEventListener("click", () => {
game.surrender();
});

// Help Button
const help = document.getElementById("help");
help.addEventListener("click", () => {
Expand Down
4 changes: 2 additions & 2 deletions pages/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ <h2>Settings</h2>
<button id="help" class="link-btn">
Help <i class="fa-solid fa-circle-question"></i>
</button>
<!-- <button id="surrender" class="link-btn">
<button id="surrender" class="link-btn">
Surrender <i class="fa-solid fa-flag"></i>
</button> -->
</button>
</div>
<div class="menu-buttons">
<div id="restart" class="icon-btn">
Expand Down

0 comments on commit 128d035

Please sign in to comment.