Review Algorithmic Thinking by Building a Dice Game step 14

Who can give me a solution for step 14; I can’t solve this problem;

Question as follows:

Step 14

For the last portion of the game, you will need to create an algorithm that checks for the presence of a straight. A small straight is when four of the dice have consecutive values in any order (Ex. 1234) resulting in a score of 30 points. A large straight is when all five dice have consecutive values in any order (Ex. 12345) resulting in a score of 40 points.

Declare a checkForStraights function which accepts an array of numbers. If the user gets a large straight, update the fifth radio button with a score of 40. If the user gets a small straight, update the fourth radio button with a score of 30. If the user gets no straight, update the last radio button to display 0.

Call your checkForStraights function when the rollDiceBtn is clicked to complete your dice game!

My previous codes for the step1- 13:
/* file: script.js */

// User Editable Region

const listOfAllDice = document.querySelectorAll(“.die”);
const scoreInputs = document.querySelectorAll(“#score-options input”);
const scoreSpans = document.querySelectorAll(“#score-options span”);
const currentRound = document.getElementById(“current-round”);
const currentRoundRolls = document.getElementById(“current-round-rolls”);
const totalScore = document.getElementById(“total-score”);
const scoreHistory = document.getElementById(“score-history”);
const rollDiceBtn = document.getElementById(“roll-dice-btn”);
const keepScoreBtn = document.getElementById(“keep-score-btn”);
const rulesContainer = document.querySelector(“.rules-container”);
const rulesBtn = document.getElementById(“rules-btn”);

let diceValuesArr = ;
let isModalShowing = false;
let score = 0;
let total = 0;
let round = 1;
let rolls = 0;

const rollDice = () => {
diceValuesArr = ;

for (let i = 0; i < 5; i++) {
const randomDice = Math.floor(Math.random() * 6) + 1;
diceValuesArr.push(randomDice);
};

listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];
});
};

const updateStats = () => {
currentRoundRolls.textContent = rolls;
currentRound.textContent = round;
};

const updateRadioOption = (index, score) => {
scoreInputs[index].disabled = false;
scoreInputs[index].value = score;
scoreSpans[index].textContent = , score = ${score};
};

const updateScore = (selectedValue, achieved) => {
score += parseInt(selectedValue);
totalScore.textContent = score;

scoreHistory.innerHTML += <li>${achieved} : ${selectedValue}</li>;
};

const getHighestDuplicates = (arr) => {
const counts = {};

for (const num of arr) {
counts[num] = counts[num] ? counts[num] + 1 : 1;
}

let highestCount = 0;

for (const num in counts) {
if (counts[num] >= 3 && counts[num] > highestCount) {
highestCount = counts[num];
}
if (counts[num] >= 4 && counts[num] > highestCount) {
highestCount = counts[num];
}
}

const sumOfAllDice = arr.reduce((a, b) => a + b, 0);

if (highestCount >= 4) {
updateRadioOption(1, sumOfAllDice);
}

if (highestCount >= 3) {
updateRadioOption(0, sumOfAllDice);
}

updateRadioOption(5, 0);
};

const detectFullHouse = (arr) => {
const counts = {};

for (const num of arr) {
counts[num] = counts[num] ? counts[num] + 1 : 1;
}

const hasThreeOfAKind = Object.values(counts).includes(3);
const hasPair = Object.values(counts).includes(2);

if (hasThreeOfAKind && hasPair) {
updateRadioOption(2, 25);
}

updateRadioOption(5, 0);
};

const checkForStraights = (arr) => {
let counter = 1;
let ans = 1;

arr.sort((a, b) => a - b);

for (let i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
if (arr[i] - arr[i - 1] == 1) {
counter++;
} else {
ans = Math.max(ans, counter);
counter = 1;
}
}
}
counter = Math.max(ans, counter);

console.log(“sorted”, arr);
console.log(“counter”, counter);

if (counter === 4) {
updateRadioOption(3, 30);
}

if (counter === 5) {
updateRadioOption(4, 40);
}

updateRadioOption(5, 0);
};

const resetRadioOptions = () => {
scoreInputs.forEach((input) => {
input.disabled = true;
input.checked = false;
});

scoreSpans.forEach((span) => {
span.textContent = “”;
});
};

const resetGame = () => {
diceValuesArr = [0, 0, 0, 0, 0];
score = 0;
round = 1;
rolls = 0;

/* file: script.js */

// User Editable Region

const listOfAllDice = document.querySelectorAll(“.die”);
const scoreInputs = document.querySelectorAll(“#score-options input”);
const scoreSpans = document.querySelectorAll(“#score-options span”);
const currentRound = document.getElementById(“current-round”);
const currentRoundRolls = document.getElementById(“current-round-rolls”);
const totalScore = document.getElementById(“total-score”);
const scoreHistory = document.getElementById(“score-history”);
const rollDiceBtn = document.getElementById(“roll-dice-btn”);
const keepScoreBtn = document.getElementById(“keep-score-btn”);
const rulesContainer = document.querySelector(“.rules-container”);
const rulesBtn = document.getElementById(“rules-btn”);

let diceValuesArr = ;
let isModalShowing = false;
let score = 0;
let total = 0;
let round = 1;
let rolls = 0;

const rollDice = () => {
diceValuesArr = ;

for (let i = 0; i < 5; i++) {
const randomDice = Math.floor(Math.random() * 6) + 1;
diceValuesArr.push(randomDice);
};

listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];
});
};

const updateStats = () => {
currentRoundRolls.textContent = rolls;
currentRound.textContent = round;
};

const updateRadioOption = (index, score) => {
scoreInputs[index].disabled = false;
scoreInputs[index].value = score;
scoreSpans[index].textContent = , score = ${score};
};

const updateScore = (selectedValue, achieved) => {
score += parseInt(selectedValue);
totalScore.textContent = score;

scoreHistory.innerHTML += <li>${achieved} : ${selectedValue}</li>;
};

const getHighestDuplicates = (arr) => {
const counts = {};

for (const num of arr) {
counts[num] = counts[num] ? counts[num] + 1 : 1;
}

let highestCount = 0;

for (const num in counts) {
if (counts[num] >= 3 && counts[num] > highestCount) {
highestCount = counts[num];
}
if (counts[num] >= 4 && counts[num] > highestCount) {
highestCount = counts[num];
}
}

const sumOfAllDice = arr.reduce((a, b) => a + b, 0);

if (highestCount >= 4) {
updateRadioOption(1, sumOfAllDice);
}

if (highestCount >= 3) {
updateRadioOption(0, sumOfAllDice);
}

updateRadioOption(5, 0);
};

const detectFullHouse = (arr) => {
const counts = {};

for (const num of arr) {
counts[num] = counts[num] ? counts[num] + 1 : 1;
}

const hasThreeOfAKind = Object.values(counts).includes(3);
const hasPair = Object.values(counts).includes(2);

if (hasThreeOfAKind && hasPair) {
updateRadioOption(2, 25);
}

updateRadioOption(5, 0);
};

const checkForStraights = (arr) => {
let counter = 1;
let ans = 1;

arr.sort((a, b) => a - b);

for (let i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
if (arr[i] - arr[i - 1] == 1) {
counter++;
} else {
ans = Math.max(ans, counter);
counter = 1;
}
}
}
counter = Math.max(ans, counter);

console.log(“sorted”, arr);
console.log(“counter”, counter);

if (counter === 4) {
updateRadioOption(3, 30);
}

if (counter === 5) {
updateRadioOption(4, 40);
}

updateRadioOption(5, 0);
};

const resetRadioOptions = () => {
scoreInputs.forEach((input) => {
input.disabled = true;
input.checked = false;
});

scoreSpans.forEach((span) => {
span.textContent = “”;
});
};

const resetGame = () => {
diceValuesArr = [0, 0, 0, 0, 0];
score = 0;
round = 1;
rolls = 0;

listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];
});

totalScore.textContent = score;
scoreHistory.innerHTML = “”;

currentRoundRolls.textContent = rolls;
currentRound.textContent = round;

resetRadioOptions();
};

rollDiceBtn.addEventListener(“click”, () => {
if (rolls === 3) {
alert(“You have made three rolls this round. Please select a score.”);
} else {
rolls++;
resetRadioOptions();
rollDice();
updateStats();
getHighestDuplicates(diceValuesArr);
detectFullHouse(diceValuesArr);
checkForStraights(diceValuesArr);
}
});

rulesBtn.addEventListener(“click”, () => {
isModalShowing = !isModalShowing;

if (isModalShowing) {
rulesBtn.textContent = “Hide Rules”;
rulesContainer.style.display = “block”;
} else {
rulesBtn.textContent = “Show Rules”;
rulesContainer.style.display = “none”;
}
});

keepScoreBtn.addEventListener(“click”, () => {
let selectedValue;
let achieved;

for (const radioButton of scoreInputs) {
if (radioButton.checked) {
selectedValue = radioButton.value;
achieved = radioButton.id;
break;
}
}

if (selectedValue) {
rolls = 0;
round++;
updateStats();
resetRadioOptions();
updateScore(selectedValue, achieved);
if (round > 6) {
setTimeout(() => {
alert(Game Over! Your total score is ${score});
resetGame();
}, 500);
}
} else {
alert(“Please select an option or roll the dice”);
}
});

listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];
});

totalScore.textContent = score;
scoreHistory.innerHTML = “”;

currentRoundRolls.textContent = rolls;
currentRound.textContent = round;

resetRadioOptions();
};

rollDiceBtn.addEventListener(“click”, () => {
if (rolls === 3) {
alert(“You have made three rolls this round. Please select a score.”);
} else {
rolls++;
resetRadioOptions();
rollDice();
updateStats();
getHighestDuplicates(diceValuesArr);
detectFullHouse(diceValuesArr);
checkForStraights(diceValuesArr);
}
});

rulesBtn.addEventListener(“click”, () => {
isModalShowing = !isModalShowing;

if (isModalShowing) {
rulesBtn.textContent = “Hide Rules”;
rulesContainer.style.display = “block”;
} else {
rulesBtn.textContent = “Show Rules”;
rulesContainer.style.display = “none”;
}
});

keepScoreBtn.addEventListener(“click”, () => {
let selectedValue;
let achieved;

for (const radioButton of scoreInputs) {
if (radioButton.checked) {
selectedValue = radioButton.value;
achieved = radioButton.id;
break;
}
}

if (selectedValue) {
rolls = 0;
round++;
updateStats();
resetRadioOptions();
updateScore(selectedValue, achieved);
if (round > 6) {
setTimeout(() => {
alert(Game Over! Your total score is ${score});
resetGame();
}, 500);
}
} else {
alert(“Please select an option or roll the dice”);
}
});

We cannot write solution code for you.

Please post what you have tried so far and describe how you are stuck. Thanks

1 Like

i am a beginner and don’t know to create an algorithm that checks for the presence of a straight. A small straight is when four of the dice have consecutive values in any order (Ex. 1234 ) resulting in a score of 30 points. A large straight is when all five dice have consecutive values in any order (Ex. 12345 ) resulting in a score of 40 points.

try to work out any type of solution that will get you even part-way to the answer.
for eg. if you wanted to check for just 1 and 2 in the dice roll, how would you do that. (if you can do that, expand your method to include 3 numbers then 4 then 5)