How to resolve the algorithm Pig the dice game step by step in the JavaScript programming language
How to resolve the algorithm Pig the dice game step by step in the JavaScript programming language
Table of Contents
Problem Statement
The game of Pig is a multiplayer game played with a single six-sided die. The object of the game is to reach 100 points or more. Play is taken in turns. On each person's turn that person has the option of either:
Create a program to score for, and simulate dice throws for, a two-person game.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pig the dice game step by step in the JavaScript programming language
Explanation:
This JavaScript code simulates a simple dice rolling game with two players.
- Initialization:
- It initializes an array
players
with two objects, each representing a player with an emptyname
and a score of 0. - It initializes the current player index to 1 (second player) and
gameOver
tofalse
. - It prompts the user to enter names for each player and uppercases them.
- Roll Function:
- The
roll
function simulates rolling a dice by generating a random number between 1 and 6 (inclusive).
- Round Function:
- The
round
function takes a player object as an argument and simulates a round of the game for that player. - It initializes
curSum
(current sum) to 0,quit
tofalse
, anddice
(the rolled number) to 0. - It alerts the player's name and current score.
- It enters a loop that continues as long as
quit
isfalse
.- It generates a dice roll and stores it in
dice
. - If the roll is 1, it alerts the player that they rolled a 1 and quits the loop.
- Otherwise, it adds the rolled number to
curSum
and asks the player if they want to roll again using aconfirm
. - If the player chooses to quit, it updates the player's score with
curSum
. - If the player's score reaches or exceeds 100, it sets
gameOver
totrue
.
- It generates a dice roll and stores it in
- Game Loop:
- The main game loop continues as long as
gameOver
isfalse
. - It alternates between the two players (0 and 1) and calls the
round
function for the current player. - If
gameOver
becomestrue
, it alerts the winning player's name and score.
Gameplay:
The game starts with the first player taking a turn. They roll the dice, and if they roll a number other than 1, they can choose to roll again and add the rolled numbers to their score. However, if they roll a 1, they lose their turn and their current score is reset to 0. Players take turns until one of them reaches or exceeds 100 points, in which case they win the game.
Source code in the javascript programming language
let players = [
{ name: '', score: 0 },
{ name: '', score: 0 }
];
let curPlayer = 1,
gameOver = false;
players[0].name = prompt('Your name, player #1:').toUpperCase();
players[1].name = prompt('Your name, player #2:').toUpperCase();
function roll() { return 1 + Math.floor(Math.random()*6) }
function round(player) {
let curSum = 0,
quit = false,
dice;
alert(`It's ${player.name}'s turn (${player.score}).`);
while (!quit) {
dice = roll();
if (dice == 1) {
alert('You roll a 1. What a pity!');
quit = true;
} else {
curSum += dice;
quit = !confirm(`
You roll a ${dice} (sum: ${curSum}).\n
Roll again?
`);
if (quit) {
player.score += curSum;
if (player.score >= 100) gameOver = true;
}
}
}
}
// main
while (!gameOver) {
if (curPlayer == 0) curPlayer = 1; else curPlayer = 0;
round(players[curPlayer]);
if (gameOver) alert(`
${players[curPlayer].name} wins (${players[curPlayer].score}).
`);
}
You may also check:How to resolve the algorithm URL decoding step by step in the Factor programming language
You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Set of real numbers step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Binary search step by step in the UnixPipes programming language