https://github.com/harshitrv/dice-game
A simple Dice game to settle your arguments. I build it to test and improve my JS skills.
https://github.com/harshitrv/dice-game
dice-game
Last synced: 16 days ago
JSON representation
A simple Dice game to settle your arguments. I build it to test and improve my JS skills.
- Host: GitHub
- URL: https://github.com/harshitrv/dice-game
- Owner: HarshitRV
- Created: 2021-05-25T18:04:07.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-01T07:05:46.000Z (over 3 years ago)
- Last Synced: 2025-02-17T14:44:46.192Z (3 months ago)
- Topics: dice-game
- Language: HTML
- Homepage: https://github.com/lucifer00911/Dice-Game
- Size: 20.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DICE GAME
### A simple dice game .### Play the game here : [PLAY](https://lucifer00911.github.io/Dice-Game/)
#
# JAVASCRIPT LOGIC
```
//Array of images that are used
const arrImg = ["images/1_dice.png","images/2_dice.png","images/3_dice.png","images/4_dice.png","images/5_dice.png","images/6_dice.png"]//Function to select one of the images from the array
let newDice = (arrImg) => {
let random = Math.floor(arrImg.length*Math.random())
return arrImg[random]
}//Assign the images to a first dice variable
var randomDice1 = newDice(arrImg);
//Another variable to save the number part from the image text of first dice
var dice1Num = Number(randomDice1[7]);//Assign the images to a second dice variable
var randomDice2 = newDice(arrImg);
//Another variable to save the number part from the image text of second dice
var dice2Num = Number(randomDice2[7]);//Changing the
src attribute according to random image
document.getElementById("dice1").setAttribute("src",randomDice1)
document.getElementById("dice2").setAttribute("src",randomDice2)//Logic to check which player won ?
if(dice1Num === dice2Num){
document.getElementById("refresh").textContent = "Draw"
}
else if(dice1Num > dice2Num){
document.getElementById("refresh").textContent = "Player 1 Wins"
}
else{
document.getElementById("refresh").textContent = "Player 2 Wins"
}console.log(randomDice)
```
#