Skip to content

Commit

Permalink
Score limit 100
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
an4s911 committed Jul 26, 2023
1 parent 87534d8 commit ee012e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Binary file added dice-roll.mp3
Binary file not shown.
32 changes: 31 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const turnScoreElement = document.querySelector("#turn-score > p");
const playerScoreElement = document.querySelector("#your-score > p");
const computerScoreElement = document.querySelector("#computer-score > p");

const winningScore = 100;

let user = {
playerScore: 0,
pronoun: "Your",
Expand Down Expand Up @@ -77,6 +79,10 @@ function turnOver() {
currentTurn.score = 0;
}, 800);

if (currentTurn.player.score >= winningScore) {
playerWins(currentTurn.player);
}

currentTurn.switchPlayer();
}

Expand All @@ -85,7 +91,10 @@ function rollAndValidate() {
const validationResult = rollDetails.validateRoll();
if (validationResult === 1) {
currentTurn.score += rollDetails.rollValue;
if (currentTurn.player === computer) {

if (currentTurn.player.score + currentTurn.score >= winningScore) {
turnOver();
} else if (currentTurn.player === computer) {
if (currentTurn.score >= 20) {
turnOver();
} else {
Expand All @@ -101,7 +110,28 @@ function rollAndValidate() {
}
}

function playerWins(player) {
const name = player === user ? "You" : "Computer";
setTimeout(() => {
alert(name + " Won!!! with " + player.score + " points");
}, 100);
setTimeout(() => {
resetGame();
}, 100);
}

function resetGame() {
user.score = 0;
computer.score = 0;
rollBtn.removeAttribute("disabled");
turnOverBtn.removeAttribute("disabled");
currentTurn.player = user;
currentTurn.score = 0;
}

function rollDice() {
let audio = new Audio("./dice-roll.mp3");
audio.play();
return {
die1Value: rollDie(dice[0]),
die2Value: rollDie(dice[1]),
Expand Down

0 comments on commit ee012e9

Please sign in to comment.