Roll The Dice Game

Veröffentlicht am: 25 Januar 2022
auf dem Kanal: JavaScript Daily
3,035
60

This is just a basic structure and there are many ways you can customize and add more features to the game.
1. First, import the random module to generate random numbers for the dice rolls.
2. Define a function that will simulate a dice roll. This function should generate a random number between 1 and 6, which represents the number on the face of the dice.
3. Define a function that will ask the player if they want to roll the dice. If they say yes, call the dice roll function from step 2 and display the result. If they say no, end the game.
4. Define a function that will keep track of the player's score. Each time the player rolls the dice, add the result to their score.
5. Define a function that will check if the player has won or lost. If their score is greater than or equal to 20, they win. If their score is less than 20, they lose.
6. Put all of these functions together in a while loop that will continue until the player wins or loses. At each iteration of the loop, ask the player if they want to roll the dice, update their score, and check if they have won or lost.
7. Once the game is over, display a message to the player indicating whether they won or lost and their final score.


In this article, we will introduce you to important DOM concepts by building roll the dice game. This article will be divided into five core sections so that you can follow along.
So as to fully understand how to code rolling the dice game, let us be familiar with the rule of the game.

The game has 2 players, playing in rounds
In each turn, a player rolls a dice as many times as he whishes. Each result get added to his ROUND score
BUT, if the player rolls a 1, all his ROUND score gets lost. After that, it's the next player's turn
The player can choose to 'Hold', which means that his ROUND score gets added to his GLOBAL score. After that, it's the next player's turn
The first player to reach 50 points on GLOBAL score wins the game
We will, now, work on building our Dice game and learn DOM concepts along the way.

Step 1 of this app: create a random number
Step 2 of this app: create first click event
Step 3 of this app: complement first click event
Step 4 of this app: create second click event
Step 5 of this app: create third click event

Social Media

Twitter
  / afghanelyas1  

GitHub
https://github.com/afghanelyas

FaceBook
  / jscodedaily  

Instagram
  / javascriptdaily01  


Auf dieser Seite können Sie das Online-Video Roll The Dice Game mit der Dauer online in guter Qualität ansehen, das der Benutzer JavaScript Daily 25 Januar 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 3,035 Mal angesehen und es wurde von 60 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!