Question

In: Computer Science

If one player rolls doubles (i.e., two fours or two sixes, etc.) and is a winner...

If one player rolls doubles (i.e., two fours or two sixes, etc.) and is a winner for that round, he or she should get double points. If both players roll doubles then the one with the highest double gets double points. What do I need to do to my coding to make it happen?

<!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>
<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;
let enemy_points = rand3 + rand4;
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)
document.getElementById("message").src='diceimages/youloss.png';
return

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

Solutions

Expert Solution

Hello buddy!

I'll give you a description, what you need to do in order to implement this functionality.

But hey!, I am assuming that your HTML part is working correctly and the only problem is to add the required functionality.

Just modify your function -----> function who_won(rand1,rand2,rand3,rand4)

In this function you are calculating the sum of points for each player , right?
let player_points = rand1 + rand2;
let enemy_points = rand3 + rand4;

so, you just need to add some code after these two lines, to calculate the score according to your objective.

If one player rolls doubles (i.e., two fours or two sixes, etc.) and is a winner for that round, he or she should get double points. If both players roll doubles then the one with the highest double gets double points.

After adding the code in who_won() function -

Your function who_won(rand1,rand2,rand3,rand4) will look like this-

function who_won(rand1,rand2,rand3,rand4){
let player_points = rand1 + rand2;
let enemy_points = rand3 + rand4;

//Start of snippet of code to implement desired functionality

//if both players have double numbers, the one with higher double total,will get double points
if(rand1===rand2 && rand3===rand4){
    if(rand1+rand2 > rand3+rand4){
        player_points = player_points*2;
    }
    else{
        enemy_points = enemy_points*2;
    }
}
//if first player has double numbers and higher score , he will get double points
else if (rand1===rand2 && rand3!=rand4){
    if(rand1+rand2 > rand3+rand4){
        player_points = player_points*2;
    }
}
// if enemy player has double numbers and higher score, he will get double points.
else if (rand1!=rand2 && rand3===rand4){
    if(rand1+rand2 < rand3+rand4){
        enemy_points = enemy_points*2;
    }
}
//end of snippet of code


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";
}
}



Happy coding buddy!

thumbs up ! if it helps ;)


Related Solutions

In a dice game a player first rolls two dice. If the two numbers are l...
In a dice game a player first rolls two dice. If the two numbers are l ≤ m then he wins if the third roll n has l≤n≤m. In words if he rolls a 5 and a 2, then he wins if the third roll is 2,3,4, or 5, while if he rolls two 4’s his only chance of winning is to roll another 4. What is the probability he wins?
At a charity event, a player rolls a pair of dice. If the player roles a...
At a charity event, a player rolls a pair of dice. If the player roles a pair (same number on each die), the player wins $10. If the two are exactly one number a part (like a five and a six), the player wins $6. IF the player roles a one and a six, they win $15. Otherwise, they lose. If it cost $5 to play, find the expected value. Write a complete sentence to explain what your answer means...
A game works as follows: the player rolls one dice once (let's call it ''n'') and...
A game works as follows: the player rolls one dice once (let's call it ''n'') and then he rolls it second time (let's call this second roll "m'') and receives nm points . A)What is the probability that n = 1 knowing that the player has received more than 20 points. b) What is the probability that m = 1 knowing that the player has received more than 20 points. c) (What is the probability that the player receives more...
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice....
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice. The shooter immediately loses if the sum of the dice is 2, 3, or 12 and immediately wins if the sum of the dice is 7 or 11 on the first roll. If the sum is anything else (4, 5, 6, 8, 9, or 10), that number becomes the point and the shooter rolls again. The shooter now wins by rolling that same point...
1)A casino wants to introduce a new game. In this game a player rolls two 4-sided...
1)A casino wants to introduce a new game. In this game a player rolls two 4-sided dice and their winnings are determined by the following rules: Otherwise, the player wins nothing. If 3 ≤ sum ≤ 5 the player wins $25. If sum ≥ 6 the player wins $100. a)Determine the probability distribution for this gamble. Probability Distribution X P (X) b)How much should the casino charge for this game if they want to make a profit in the long...
You make a carnival game, where the player rolls two fair dice (in a single roll)...
You make a carnival game, where the player rolls two fair dice (in a single roll) and attempts to roll doubles (meaning both dice show the same number). The player puts down a dollar to play the game. If the player loses, they lose their dollar. If the player wins, they win $3 (and do not lose their original dollar). Answer the following (5 pts total). If you are running the game, what is the expected value of how much...
A game works as follows: each player rolls one dice (let's call it ''n''). After both...
A game works as follows: each player rolls one dice (let's call it ''n''). After both players have made the first roll, they have the choice to leave or not. If a player exits, he automatically looses; if not, he rolls a second time (let's call this second roll "m'') and receives nm points . The player with the most points wins, in case of a tie no one wins. a) Give the fundamental space of the two throws. b)...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are lots of ways to win: (1, 6), (2, 5), and so on. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people’s eyes glaze over at...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die has six faces, which contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. (i) If the sum is 2 or 10 on the first throw, the player wins. (ii) If the sum is 3, 7 or 12 on the first throw, the player loses. (iii)...
Please come up with a diagram (i.e. using a two-player decision matrix such as the Prisoner's...
Please come up with a diagram (i.e. using a two-player decision matrix such as the Prisoner's Dilemma example in the Learning Notes) for an original game theory/prisoner's dilemma scenario (either in business, politics, or your own personal life), and explain what would be the most likely outcome of the scenario you have chosen. more than 200 words please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT