Hi all. I'm writing my first javascript game. It's a role playing game where the user fights an Ogre, and not elaborate at all. I'd like the game to end when the Ogre's life is at zero OR the users life is at zero. I can't figure out how to have the program keep track of the amount of damage the user and the Ogre inflict on each other. Any ideas? Also, if this has been asked before, please point me in the direction of the completed discussion and I'll look it up there. Thanks.
<!DOCTYPE HTML>
<head>
<script>
var life=100;
var ogrelife=2000;
function ogreattack(){
while (ogrelife>0){
var pmove=prompt("Do you want to stab, punch or run?");
switch (pmove){
case "stab":
stabbing();
ogremove();
break;
case "punch":
punching();
ogremove();
break;
case "run":
running();
break;}
}
}
function stabbing(){
var stab=Math.floor(Math.random()*2500);
var deflect=Math.floor(Math.random()*10);
if (deflect>7){
document.write('You have stabbed the Ogre and dealt ' + stab + ' damage<br>');
if (stab>ogrelife){
document.write('You have killed the Ogre<br>');}}
else{
document.write('Your attack was deflected<br>');}}
function punching(){
var punch=Math.floor(Math.random()*2000);
var deflect=Math.floor(Math.random()*10);
if (deflect>4){
document.write('You have punched the Ogre and dealt ' + punch + ' damage<br>');
if (punch>ogrelife){
document.write('You have knocked out the Ogre<br>');}}
else{
document.write('Your punch was deflected<br>');}}
function running(){
var run=Math.floor(Math.random()*2000);
if (run>1000){
document.write('You have ran away and lost the fight<br>');
return;}
else{
document.write('Your escape was blocked<br>');
ogremove();}}
function ogremove(){
var hit=Math.floor(Math.random()*1000);
if (hit>300){
var damage=Math.floor(Math.random()*1000);
document.write('You have been hit<br>');}
else{
document.write('The Ogre missed you<br>');}}
</script>
</head>
<body onload="ogreattack()">
</body>
</html>