So basically im trying to achieve a running total for each player in my game of war. Idk how to achieve this I have already tried creating a variable which holds a total but i just refreshes and sets it to a new total.
HTML CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Card Game</title>
<link href="styles/normalilze.css" s rel="stylesheet" />
<link href="styles/my_style.css" rel="stylesheet" />
</head>
<body>
<div id="main">
<h1>Card Game</h1>
<div id="left">
<div id="pHand">
<br />
<!-- Using JavaScript replace the word 'Player' with the name of
the person playing the game-->
<h2 id="pNames">Player</h2>
</div>
<div id="cHand">
<h2>Slick</h2>
</div>
<div id="but">
<button id="newPlayer">New player</button>
<button id="deal">Deal</button>
<button id="play">Play</button>
</div>
</div>
<section class="player">
<div id="right">
<h2>Card Count:</h2>
<!-- Using JavaSCript enter the number of remaining cards in the 'Players' hand
in the p tag below-->
<p id="cardCount"></p>
<br />
<!-- Using JavaSCript replace the word 'Player' with the name of
the person playing the game-->
<h2 id="pName">Player</h2>
<h2 id="pName2">:</h2>
<!-- Using jQuery insert the current 'Player' score in the p tag
below -->
<p id="pScore"></p>
<br />
<h2 id="cName">Slick:</h2>
<!-- Using jQuery insert the current 'Slick' score in the p tag
below -->
<p id="cScore"></p>
<!-- add and style winning message in the
following div -->
<div id="win"></div>
</div>
</div>
</section>
<script src="scripts/jquary-3.2.1.min.js"></script>
<script src="scripts/jQ-War.js"></script>
</body>
</html>
Here is the javascript code and I added the problem which i am facing.
JavaScript Code
$(document).ready(function () {
var i = 0;
var running = false;
$(function(){
$("#deal").click(function(){
if (!running){
War();
addTotals();
running = true;
setTimeout(()=>{running = false}, 2200);
}
});
});
function War(){
if (i <= 26)
{
var carding = [Hearts, Spades, Diamonds, Clubs];
var slickCards = carding[Math.floor(Math.random() * carding.length)];
var playerCards = carding[Math.floor(Math.random() * carding.length)];
$("<img>")
.attr('src', playerCards [playerScore = (value = Math.floor(Math.random() * 12))].picture)
.appendTo("#pHand").hide().fadeIn("slow").delay("1000").fadeOut("slow");
$("<img>")
.attr('src', slickCards [slickScore = (value = Math.floor(Math.random() * 12))].picture)
.appendTo("#cHand").hide().fadeIn("slow").delay("1000").fadeOut("slow");
i++;
}
}
function addTotals(){
var totalPlayerScore = 0;
totalPlayerScore = playerScore + totalPlayerScore;
document.getElementById("pScore").innerText = totalPlayerScore;
}
function card(name,suit,value, picture) {
this.name = name;
this.suit = suit;
this.value = value;
this.picture = picture;
}
var Hearts = [
new card('Ace', 'Hearts',1, "./images/Hearts/Ace.jpg"),
new card('Two', 'Hearts',2, "./images/Hearts/Two.jpg"),
new card('Three', 'Hearts',3, "./images/Hearts/Three.jpg"),
new card('Four', 'Hearts',4, "./images/Hearts/Four.jpg"),
new card('Five', 'Hearts',5, "./images/Hearts/Five.jpg"),
new card('Six', 'Hearts',6, "./images/Hearts/Six.jpg"),
new card('Seven', 'Hearts',7, "./images/Hearts/Seven.jpg"),
new card('Eight', 'Hearts',8, "./images/Hearts/Eight.jpg"),
new card('Nine', 'Hearts',9, "./images/Hearts/Nine.jpg"),
new card('Ten', 'Hearts',10, "./images/Hearts/Ten.jpg"),
new card('Jack', 'Hearts',11, "./images/Hearts/Jack.jpg"),
new card('Queen', 'Hearts',12, "./images/Hearts/Queen.jpg"),
new card('King', 'Hearts',13, "./images/Hearts/King.jpg"),
];
//Diamond Functions
var Diamonds = [
new card('Ace', 'Diamonds',1, "./images/Diamonds/Ace.jpg"),
new card('Two', 'Diamonds',2, "./images/Diamonds/Two.jpg"),
new card('Three', 'Diamonds',3, "./images/Diamonds/Three.jpg"),
new card('Four', 'Diamonds',4, "./images/Diamonds/Four.jpg"),
new card('Five', 'Diamonds',5, "./images/Diamonds/Five.jpg"),
new card('Six', 'Diamonds',6, "./images/Diamonds/Six.jpg"),
new card('Seven', 'Diamonds',7, "./images/Diamonds/Seven.jpg"),
new card('Eight', 'Diamonds',8, "./images/Diamonds/Eight.jpg"),
new card('Nine', 'Diamonds',9, "./images/Diamonds/Nine.jpg"),
new card('Ten', 'Diamonds',10, "./images/Diamonds/Ten.jpg"),
new card('Jack', 'Diamonds',11, "./images/Diamonds/Jack.jpg"),
new card('Queen', 'Diamonds',12, "./images/Diamonds/Queen.jpg"),
new card('King', 'Diamonds',13, "./images/Diamonds/King.jpg"),
];
//Club Functions
var Clubs = [
new card('Ace', 'Clubs',1, "./images/Clubs/Ace.jpg"),
new card('Two', 'Clubs',2, "./images/Clubs/Two.jpg"),
new card('Three', 'Clubs',3, "./images/Clubs/Three.jpg"),
new card('Four', 'Clubs',4, "./images/Clubs/Four.jpg"),
new card('Five', 'Clubs',5, "./images/Clubs/Five.jpg"),
new card('Six', 'Clubs',6, "./images/Clubs/Six.jpg"),
new card('Seven', 'Clubs',7, "./images/Clubs/Seven.jpg"),
new card('Eight', 'Clubs',8, "./images/Clubs/Eight.jpg"),
new card('Nine', 'Clubs',9, "./images/Clubs/Nine.jpg"),
new card('Ten', 'Clubs',10, "./images/Clubs/Ten.jpg"),
new card('Jack', 'Clubs',11, "./images/Clubs/Jack.jpg"),
new card('Queen', 'Clubs',12, "./images/Clubs/Queen.jpg"),
new card('King', 'Clubs',13, "./images/Clubs/King.jpg"),
];
//Spades Functions
var Spades = [
new card('Ace', 'Spades',1, "./images/Spades/One.jpg"),
new card('Two', 'Spades',2, "./images/Spades/Two.jpg"),
new card('Three', 'Spades',3, "./images/Spades/Three.jpg"),
new card('Four', 'Spades',4, "./images/Spades/Four.jpg"),
new card('Five', 'Spades',5, "./images/Spades/Five.jpg"),
new card('Six', 'Spades',6, "./images/Spades/Six.jpg"),
new card('Seven', 'Spades',7, "./images/Spades/Seven.jpg"),
new card('Eight', 'Spades',8, "./images/Spades/Eight.jpg"),
new card('Nine', 'Spades',9, "./images/Spades/Nine.jpg"),
new card('Ten', 'Spades',10, "./images/Spades/Ten.jpg"),
new card('Jack', 'Spades',11, "./images/Spades/Jack.jpg"),
new card('Queen', 'Spades',12, "./images/Spades/Queen.jpg"),
new card('King', 'Spades',13, "./images/Spades/King.jpg"),
//new card('Checkmark', 'Spades',14, "../check_sm.png")
];
});
How Do i add a running total here to the pScore!
function addTotals(){
var totalPlayerScore = 0;
totalPlayerScore = playerScore + totalPlayerScore;
document.getElementById("pScore").innerText = totalPlayerScore;
}