In: Computer Science
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>
<!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.