Question

In: Computer Science

I have a working code for a dice game. What is the best way to add...

I have a working code for a dice game. What is the best way to add coding to keep track how many points each player makes? When they tie both players gets no points. When they reach to 100 the game ends or unless the player quit. How would I make it work?

<!Doctype html>
<html>
<head>

<meta charset="UTF-8">
<title>Dice Game</title>
<link rel="stylesheet" type="text/css" href="dice.css">
</head>
<body>
<div class="row" align="center">
<div class="col-4">
<h3>Your Dice</h3>
<img src="diceimages/m1.png" width="100" height="100" alt="roll: 1" id="mydice1"/>
<img src="diceimages/m1.png" width="100" height="100" alt="roll: 1" id="mydice2"/>
</div>

<div class="col-4">
<h3>Opponent's Dice</h3>
<img src="diceimages/o1.png" width="100" height="100" alt="roll: 1" id="opponentdice1"/>
<img src="diceimages/o1.png" width="100" height="100" alt="roll: 1" id="opponentdice2"/>
<div id="enemy_score"></div>
</div>

<div class="col-4" align="center">
<img src="diceimages/goodluck.png" width="150" height="150" alt="roll: 1" id="message"/><br>
<button class="roll" onClick="throwdice()">Roll Dice</button>
</div>
</div>

<script>
// var sides = ["m1.png", "m2.png", "d3.png", "d4.png", "d5.png", "d6.png"];
var side_alt = ["roll: 1", "roll: 2", "roll: 3", "roll: 4", "roll: 5", "roll: 6"];

function throwdice(){
// Create Random Number between 1 and 6
var rand1 = Math.round(Math.random()*5) + 1;  
var rand2 = Math.round(Math.random()*5) + 1;
var rand3 = Math.round(Math.random()*5) + 1;
var rand4 = Math.round(Math.random()*5) + 1;

// Set Images src
document.getElementById("mydice1").src = "diceimages/m" + rand1 + ".png";
document.getElementById("mydice2").src = "diceimages/m" + rand2 + ".png";
document.getElementById("opponentdice1").src = "diceimages/o" + rand3 + ".png";
document.getElementById("opponentdice2").src = "diceimages/o" + rand4 + ".png";

//Set Images alt
document.getElementById("mydice1").alt = side_alt[rand1];
document.getElementById("mydice2").alt = side_alt[rand2];
document.getElementById("opponentdice1").alt = side_alt[rand3];
document.getElementById("opponentdice2").alt = side_alt[rand4];

who_won(rand1,rand2,rand3,rand4);
}

function who_won(rand1,rand2,rand3,rand4){
let player_points = rand1 + rand2 + 2;
let enemy_points = rand3 + rand4 + 2;
let result = winner(player_points,enemy_points);
document.getElementById("message").innerHTML = result;
}

//If Statements
function winner(player, enemy) {
if (player < enemy) {
return "diceimages/youloss.png";

}  
if (enemy < player) {
return "diceimages/youwon.png"
}
if (player == enemy) {
return "diceimages/equal.png"
}
}
</script>
</body>
</html>

Solutions

Expert Solution

<!Doctype html>
<html>
<head>

<meta charset="UTF-8">
<title>Dice Game</title>
<link rel="stylesheet" type="text/css" href="dice.css">
</head>
<body>
<div class="row" align="center">
<div class="col-4">
<h3>Your Dice</h3>
<img src="diceimages/m1.png" width="100" height="100"  id="mydice1"/>
<img src="diceimages/m1.png" width="100" height="100"  id="mydice2"/>
</div>

<div class="col-4">
<h3>Opponent's Dice</h3>
<img src="diceimages/o1.png" width="100" height="100"  id="opponentdice1"/>
<img src="diceimages/o1.png" width="100" height="100"  id="opponentdice2"/>
<div id="enemy_score"></div>
</div>

<div class="col-4" align="center">
<img src="diceimages/goodluck.png" width="150" height="150"  id="message"/><br>
<button class="roll" onClick="throwdice()">Roll Dice</button>
<div>
    <h3>
        Total Points
    </h3>
    <div>Player points:<div id="playerPoints"></div></div>
    <div>Enemy points:<div id="enemyPoints"></div></div>
    <div>Won:<h2 id="won"></h2></div>
</div>
</div>
</div>

<script>
// var sides = ["m1.png", "m2.png", "d3.png", "d4.png", "d5.png", "d6.png"];
var side_alt = ["roll: 1", "roll: 2", "roll: 3", "roll: 4", "roll: 5", "roll: 6"];
var totalPlayerpoints = 0,totalEnemypoints = 0
function throwdice(){
// Create Random Number between 1 and 6
var rand1 = Math.round(Math.random()*5) + 1;  
var rand2 = Math.round(Math.random()*5) + 1;
var rand3 = Math.round(Math.random()*5) + 1;
var rand4 = Math.round(Math.random()*5) + 1;

// Set Images src
document.getElementById("mydice1").src = "diceimages/m" + rand1 + ".png";
document.getElementById("mydice2").src = "diceimages/m" + rand2 + ".png";
document.getElementById("opponentdice1").src = "diceimages/o" + rand3 + ".png";
document.getElementById("opponentdice2").src = "diceimages/o" + rand4 + ".png";

//Set Images alt
document.getElementById("mydice1").alt = side_alt[rand1];
document.getElementById("mydice2").alt = side_alt[rand2];
document.getElementById("opponentdice1").alt = side_alt[rand3];
document.getElementById("opponentdice2").alt = side_alt[rand4];

who_won(rand1,rand2,rand3,rand4);
}

function who_won(rand1,rand2,rand3,rand4){
let player_points = rand1 + rand2 + 2;
let enemy_points = rand3 + rand4 + 2;
let result = winner(player_points,enemy_points);
document.getElementById("message").innerHTML = result;
if(totalPlayerpoints<100 && totalEnemypoints<100)
{
    document.getElementById("playerPoints").innerHTML = totalPlayerpoints;
    document.getElementById("enemyPoints").innerHTML = totalEnemypoints;
}
else{
    if(totalPlayerpoints>totalEnemypoints)
    document.getElementById("won").innerHTML = "You won";
    else
    document.getElementById("won").innerHTML = "You lose";
}
}

//If Statements
function winner(player, enemy) {
if (player < enemy) {
    
totalEnemypoints = totalEnemypoints + enemy; //updating total points of enemy
console.log(totalEnemypoints)
return "diceimages/youloss.png";

}  
if (enemy < player) {
    
totalPlayerpoints = totalPlayerpoints + player; //updating total points of player
console.log(totalPlayerpoints)
return "diceimages/youwon.png"
}
if (player == enemy) {
return "diceimages/equal.png"
}
}
</script>
</body>
</html>

Commented all the required task.


Related Solutions

I am currently working on creating a dice game. I have not figured out how to...
I am currently working on creating a dice game. I have not figured out how to make it work? What should I do to make it work? Here is what I have so far: <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Dice Game</title> <link rel="stylesheet" type="text/css" href="dice.css"> </head> <body> <div class="row" align="center"> <div class="col-4"> <h3>Your Dice</h3> <img src="dice images/m1.png" width="100" height="100" alt="roll: 1" id="mydice1"/> <img src="dice images/m1.png" width="100" height="100" alt="roll: 1" id="mydice2"/> </div> <div class="col-4"> <h3>Opponent's Dice</h3> <img src="dice images/o1.png" width="100"...
Game of dice - fair dice: If I throw a number >= 5 I win. If...
Game of dice - fair dice: If I throw a number >= 5 I win. If he throws a number =< 4 he wins. I throw the first dice. Given that he loses(throws a number >4) what is the probability of me winning ? What is the expected number of throws before either of us wins?
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
MATLAB code for the following question. In the game of Monopoly, a pair of dice are...
MATLAB code for the following question. In the game of Monopoly, a pair of dice are rolled to move a player's piece around the board. If a double is rolled (the dice show the same number), the player receives another roll of the dice. If a double is rolled a second time, a third roll of the dice is received. If a double is rolled on the third occasion, the player forfeits their turn (and goes to Jail). Write a...
I need to add this checkpoint to an existing code that i have in python Checkpoint...
I need to add this checkpoint to an existing code that i have in python Checkpoint 1: Once you have created and tested the Bank Account Class create subclasses to the BankAccount class. There should be two subclasses, CheckingAccount and SavingsAccount. You should add interest_rate to the parent BankAccount class. To the CheckingAccount add variables called per_check_fee default to false and allow_overdraft default to True. To SavingsAccount add the variable transactions_per_month, default it to 5. Create instances of CheckingAccount and...
This is the assignment I have, I need the best game theory problem example for it...
This is the assignment I have, I need the best game theory problem example for it which is difficult. Thank you:) Think of an example of a decision problem or a game theory problem. This time you will have to submit the source of the story. You can take it from a newspaper, from Internet, magazine, book... 1. Submit a photocopy or a printed version of your problem. 2. Simplify the problem and represent it using the concepts of game...
Dice Game Rules: 2 - 4 players Each player has 5 Dice. The dice have 6...
Dice Game Rules: 2 - 4 players Each player has 5 Dice. The dice have 6 sides. Each player rolls their dice, and the dice statistics are reported: Sum, number of pairs (and of what), and "straights" - (all dice in order - e.g. 1,2,3,4,5 or 2,3,4,5,6) Player 1 might roll 2,2,3,4,4 so the results would be: Sum: 15, 1 pair (2), 1 pair (4) Player 2 might roll 1, 1, 4, 6, 6 so the results would be: Sum:...
Hey, I have a code that I am working on to make a garden plot calculator,...
Hey, I have a code that I am working on to make a garden plot calculator, however I am reaching an error that I cannot seem to get past. Please help. import math # GarednPlot contains all the utility functions class GardenPlot: # constructor function: Welcomes the user and sets default values def __init__(self): print("Welcome!") self.length = 0 self.radius = 0 self.depth = 0 # Sets the length of the GardenPlot and returns that length def setLength(self): self.length = float(input("Enter...
I have this MIPS code and I need to find: add f,g,h add f,f,i sub f,f,j...
I have this MIPS code and I need to find: add f,g,h add f,f,i sub f,f,j a. How many bits does it take to encode ? b. How many bits needed in register file to store the data?
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT