Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey there, I need help with this, it must be done in javascript 1 2 4 is one of

ID: 3669642 • Letter: H

Question

Hey there, I need help with this, it must be done in javascript

1 2 4 is one of very few dice games not built for gambling. It's a game for any number of players. Two to four is probably best. It's a friendly way to pass some time (2 seconds to maybe 10 minutes per game), since it mostly requires you to keep track of your own progress, and trust others to do the same. It moves quickly, so the more players, the harder to keep track of others' play.

So the assignment is

1.Use arrays to help you build a game of 1 2 4 for one person.

An array named roll can hold the three numbers of each roll.
(For 1 extra point, use a two-dimensional array to keep track of all numbers rolled throughout the game.)

Feel free to use other arrays if you can find a place for them.

2.Follow the rules of the game.

A. Use Math.floor(Math.random) to simulate each roll of the dice (three times for three dice). Store the results in an array.

B.Did you roll 1 2 4? You win! (Congratulations. Play again?) Otherwise:

C. Did you roll 3 of a kind? Time to start over. Otherwise:

D.Compare all the dice and all the combinations to the required point. (Did you get the point you were looking for?)

If you got the point, check to see if you got the next. Repeat.

If you did not get the point, pass play to the next player. (Note that when there's only 1 player, you just pass to yourself.)

E. When you get all the points through 1 2 3 4 5 6 7 6 5 4 3 2 1, you win. Play again?

F. Display every roll, along with results. You can use a table such as the one above, or use your own layout, but for every roll, show

the point,

the numbers revealed on the dice, and

the result.

NOTE that the instructions you to display an entire game, all at once.

Explanation / Answer

var playerOne = null; var playerTwo = null; var currentPlayer = null; var currentRoundScores = []; function Player(name) { this.name = name; this.score = 0; } function getCurrentRoundScore() { var total = 0; currentRoundScores.forEach(function(score) { total += score; }); return total; } function updatePlayerScores() { updateTextOnPage("#player-1-score", playerOne.score); updateTextOnPage("#player-2-score", playerTwo.score); } function updateRoundScore() { $("#current-round-score").text(getCurrentRoundScore()); } function getRandomPlayer(playerOne, playerTwo) { var players = [playerOne, playerTwo]; var randomPlayer = players[Math.round(Math.random())]; $("#current-player").text(randomPlayer.name); return randomPlayer; } function switchCurrentPlayer() { if (currentPlayer === playerOne) { currentPlayer = playerTwo; } else { currentPlayer = playerOne; } $("#current-player").text(currentPlayer.name); currentRoundScores = []; updateRoundScore(); disableEndTurnBtn(); } function setMessage(message) { $("#message").text(message); } function clearMessage() { setMessage(""); } function updateTextOnPage(selector, text) { $(selector).text(text); } function resetGame() { playerOne.score = 0; playerTwo.score = 0; updatePlayerScores(); enableRollBtn(); currentPlayer = getRandomPlayer(playerOne, playerTwo); setMessage("A new game begins! " + currentPlayer.name + " was randomly selected to start this round."); } function disableEndTurnBtn() { $("#end-turn").prop('disabled', true); } function enableEndTurnBtn() { $("#end-turn").prop('disabled', false); } function disableRollBtn() { $("#roll-dice").prop('disabled', true); } function enableRollBtn() { $("#roll-dice").prop('disabled', false); } function updateBackground(url) { $("body").css({"background": "url('./images/" + url + "')", "background-size": "100% 100%"}); } function rolledOne(dieResult1, dieResult2) { if (variation !== "big-pig" && dieResult1 !== dieResult2) { switchCurrentPlayer(); setMessage("Oh no! You rolled a 1, you lose your current points and end your turn. You're up, " + currentPlayer.name + "!"); disableEndTurnBtn(); setRandomFailBackground(); currentRoundScores = [] $(".dice-image1").effect("shake", {times: 8}, 800); $(".dice-image2").effect("shake", {times: 8}, 800); } } function setRandomFailBackground() { var failImages = ["fail1.gif", "fail2.gif", "fail3.gif", "fail4.gif", "fail5.gif", "fail6.gif"]; updateBackground(failImages[Math.floor(Math.random() * failImages.length)]); } function twoPig(dieResult1, dieResult2) { if (dieResult1 === 1 && dieResult2 === 1) { currentPlayer.score = 0; switchCurrentPlayer(); setMessage("Oh no! Snake Eyes, you just lost all your points. You're up, " + currentPlayer.name + "!"); disableEndTurnBtn(); setRandomFailBackground(); updatePlayerScores(); $(".dice-image1").effect("explode", {times: 8}, 800); $(".dice-image2").effect("explode", {times: 8}, 800); } if (dieResult1 === dieResult2 && dieResult1 !== 1) { setMessage("You rolled doubles! You must roll again! Good luck with that!"); disableEndTurnBtn(); $(".dice-image1").effect("explode", {times: 8}, 800); $(".dice-image2").effect("explode", {times: 8}, 800); } } function bigPig(dieResult1, dieResult2) { if (dieResult1 === 1 && dieResult2 === 1) { currentRoundScores.push(25 - (currentRoundScores[currentRoundScores.length - 1] + currentRoundScores[currentRoundScores.length - 2])); $("#current-round-score").text(getCurrentRoundScore()); setMessage("Snake Eyes! 25 points to you! Sweeeeet!"); } if (dieResult1 === dieResult2 && dieResult1 !== 1) { currentRoundScores.push((currentRoundScores[currentRoundScores.length - 1])*2); $("#current-round-score").text(getCurrentRoundScore()); setMessage("Doubles! Double the flavor, Double the points!") } } $(function() { disableEndTurnBtn(); var variation = NaN; $(".dice-result-wrapper *").hide(); var diceImage = $(".dice-image"); var diceImage2 =$(".dice-image2"); $("form#add-players").submit(function(event) { event.preventDefault(); var playerOneName = $("#player-1-name").val(); var playerTwoName = $("#player-2-name").val(); playerOne = new Player(playerOneName); playerTwo = new Player(playerTwoName); updateTextOnPage(".player-1-name", playerOne.name + "'s"); updateTextOnPage(".player-2-name", playerTwo.name + "'s"); $(this).parent().hide(); variation = $("#variation").val(); if (variation !== "traditional") { $(".dice-result-wrapper").append(''); diceImage2 = $(".dice-image2"); } $("#show-game").show(); currentPlayer = getRandomPlayer(playerOne, playerTwo); $("#current-player").text(currentPlayer.name); setMessage(currentPlayer.name + " was randomly selected to go first. Good luck!"); }); $("#roll-dice").click(function() { updateBackground(""); clearMessage(); enableEndTurnBtn(); var diceResult1 = Math.floor(Math.random() * 6) + 1; var diceResult2 = Math.floor(Math.random() * 6) + 1; currentRoundScores.push(diceResult1); $("#current-round-score").text(getCurrentRoundScore()); diceImage.removeClass().addClass("dice"); if (diceResult1 === 1) { diceImage.addClass("dice-one"); rolledOne(diceResult1, diceResult2); } else if (diceResult1 === 2) { diceImage.addClass("dice-two"); } else if (diceResult1 === 3) { diceImage.addClass("dice-three"); } else if (diceResult1 === 4) { diceImage.addClass("dice-four"); } else if (diceResult1 === 5) { diceImage.addClass("dice-five"); } else if (diceResult1 === 6) { diceImage.addClass("dice-six"); } if (variation !== "traditional") { if (diceResult1 !== 1) {currentRoundScores.push(diceResult2);} $("#current-round-score").text(getCurrentRoundScore()); diceImage2.removeClass().addClass("dice"); if (diceResult2 === 1) { diceImage2.addClass("dice-one"); rolledOne(diceResult1, diceResult2); } else if (diceResult2 === 2) { diceImage2.addClass("dice-two"); } else if (diceResult2 === 3) { diceImage2.addClass("dice-three"); } else if (diceResult2 === 4) { diceImage2.addClass("dice-four"); } else if (diceResult2 === 5) { diceImage2.addClass("dice-five"); } else if (diceResult2 === 6) { diceImage2.addClass("dice-six"); } if (variation === "two-pig") { twoPig(diceResult1, diceResult2); } else { bigPig(diceResult1, diceResult2); } } $(".dice-result-wrapper *").show(); }); $("#end-turn").click(function() { setMessage("Nice work! " + currentPlayer.name + " just gained " + getCurrentRoundScore() + " points."); currentPlayer.score += getCurrentRoundScore(); updatePlayerScores(); $(".dice-result-wrapper *").hide(); if (currentPlayer.score >= 100) { setMessage("Congratulations " + currentPlayer.name + "! You win!"); $("#message").append(" Click here to play again!"); updateBackground("success1.gif"); disableRollBtn(); $(".clear-scores").click(function() { resetGame(); }); } switchCurrentPlayer(); }); });