• Welcome to The Cave of Dragonflies forums, where the smallest bugs live alongside the strongest dragons.

    Guests are not able to post messages or even read certain areas of the forums. Now, that's boring, don't you think? Registration, on the other hand, is simple, completely free of charge, and does not require you to give out any personal information at all. As soon as you register, you can take part in some of the happy fun things at the forums such as posting messages, voting in polls, sending private messages to people and being told that this is where we drink tea and eat cod.

    Of course I'm not forcing you to do anything if you don't want to, but seriously, what have you got to lose? Five seconds of your life?

JavaScript Help.... please?

Chewy the Crispy Crunch

Crunch, not Crotch
Hey, I'm Crunch.

So a few weeks ago I started writing a game with JavaScript. The basic idea is, you and a CPU player fight against each other. You and the enemy can attack or heal each turn, and sometimes, you'll get a critical hit. If the CPU's HP is below 100, it can go into Berserk Mode and do more damage.

Now the problem is this: for some reason, the code for when the enemy tries to heal itself doesn't work. Here's the HTML code:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>
The Game
</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
body {
font-family:"Calibri";
background:#9a92ff;
text-align:center;
font-size:30px;
}

#left {
width:370px;
height:200px;
margin-left:300px;
margin-top:250px;
background:#9a92ee;
border:5px inset #c0c0c0;
float:left;
overflow:auto;
text-align:left;
}

#right {
width:370px;
height:200px;
margin-right:300px;
margin-top:250px;
background:#9a92ee;
border:5px inset #c0c0c0;
float:right;
overflow:auto;
text-align:right;
}
</style>
</head>
<body>
<div id="left">
<span id="playerHealth"></span>
<br />
<span id="playerName"></span>
<hr />
<span id="playerItems"></span>
</div>
<div id="right">
<span id="enemyHealth"></span>
<br />
<span id="enemyName"></span>
<hr />
<span id="enemyItems"></span>
</div>
<p id="choices">
<a onclick="playerAttack();">Attack</a>   <a onclick="playerHeal();">Heal</a>   <a onclick="location.reload(1);">Restart</a>
</p>
<script src="theGame.js">
</script>
</body>
</html>
and here's the JavaScript:
playerName = prompt("Please choose a name for yourself.","Your Name Here");
enemyName = prompt("Please choose a name for the enemy.","Enemy Name Here");
playerItems = parseInt(prompt("Please insert number of potions for you."));
enemyItems = parseInt(prompt("Please insert number of potions for the enemy."));
playerHealth = 500;
enemyHealth = 500;
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
document.getElementById("playerName").innerHTML = playerName;
document.getElementById("enemyName").innerHTML = enemyName;
document.getElementById("playerItems").innerHTML = "   Potions (" + playerItems + ")";
document.getElementById("enemyItems").innerHTML = "Potions (" + enemyItems + ")   ";

function playerAttack() {
alert("It is your turn!");
playerDamage = Math.ceil(100 * Math.random());
playerCrit = Math.ceil(8 * Math.random());
if (playerCrit == 8)
{
playerDamage = 2 * playerDamage;
alert("Critical hit!");
}
alert(playerName + " did " + playerDamage + " damage!");
enemyHealth = enemyHealth - playerDamage;
if (enemyHealth <= 0)
{
playerWin ();
}
else
{
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
enemyAttack();
}
}

function playerWin() {
document.getElementById("enemyHealth").innerHTML = "0/500";
alert("You win!");
document.getElementById("choices").innerHTML = "You win!<br \/><a onclick=\"location.reload(1);\">Play again?<\/a>";
}

function enemyWin() {
document.getElementById("playerHealth").innerHTML = "0/500";
alert("You lose!");
document.getElementById("choices").innerHTML = "You lose!<br \/><a onclick=\"location.reload(1);\">Play again?<\/a>";
}

function playerHeal() {
alert("It is your turn!");
if (playerItems == 0)
{
alert(playerName + " is out of potions!");
enemyAttack();
}
else
{
playerPotion = Math.ceil(100 * Math.random());
alert(playerName + " healed " + playerPotion + " health!");
playerItems = playerItems - 1;
document.getElementById("playerItems").innerHTML = "   Potions (" + playerItems + ")";
playerHealth = playerHealth + playerPotion;
if (playerHealth >= 500)
{
playerHealth = 500;
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
enemyAttack();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
enemyAttack();
}
}
}

function enemyAttack() {
alert("It is the enemy's turn!");
enemyHeal = Math.ceil(2 * Math.random());
enemyDamage = Math.ceil(100 * Math.random());
enemyCrit = Math.ceil(8 * Math.random());
enemyBerserk = Math.ceil(2 * Math.random());
if (enemyHeal == 2)
{
enemyHeal();
}
else
{
if (enemyHealth <= 100)
{
if (enemyBerserk == 2)
{
enemyDamage = 5 * enemyDamage;
alert(enemyName + " went berserk!");
if (enemyCrit == 8)
{
enemyDamage = 2 * enemyDamage;
alert("Critical hit!");
}
alert(enemyName + " did " + enemyDamage + " damage!");
playerHealth = playerHealth - enemyDamage;
if (playerHealth <= 0)
{
enemyWin();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
}
}
else
{
if (enemyCrit == 8)
{
enemyDamage = 2 * enemyDamage;
alert("Critical hit!");
}
alert(enemyName + " did " + enemyDamage + " damage!");
playerHealth = playerHealth - enemyDamage;
if (playerHealth <= 0)
{
enemyWin();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
}
}
}
else
{
if (enemyCrit == 8)
{
enemyDamage = 2 * enemyDamage;
alert("Critical hit!");
}
alert(enemyName + " did " + enemyDamage + " damage!");
playerHealth = playerHealth - enemyDamage;
if (playerHealth <= 0)
{
enemyWin();
}
else
{
document.getElementById("playerHealth").innerHTML = playerHealth + "/500";
}
}
}
}

function enemyHeal() {
if (enemyItems == 0)
{
alert(enemyItems + " is out of potions!");
}
else
{
enemyPotion = Math.ceil(100 * Math.random());
alert(enemyName + "healed " + enemyPotion + " health!");
enemyItems = enemyItems - 1;
document.getElementById("enemyItems").innerHTML = "Potions (" + enemyItems + ")   ";
enemyHealth = enemyHealth + enemyPotion;
if (enemyHealth >= 500)
{
enemyHealth = 500;
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
}
else
{
document.getElementById("enemyHealth").innerHTML = enemyHealth + "/500";
}
}
}
The HTML file for the game is here.
 
Your healing is completely useless anyway - your average return values for both attack and heal are the same. In your current implementation, the enemy would be better off just bashing the player all the time.

One can have NaN potions - not good; at least default to something.

Berserk seems only to prevent the player from winning - 5* is a bit much, no?

You should put statements all over the place to see where the program is executing.

Alerts are annoying. You don't need them.

Indent, choose names appropriately (enemy does not always attack), and
Code:
if (a) {
  ...;
  q();
}
else {
  ...;
  q();
}
should be written
Code:
if (a) {
  ...;
}
else {
  ...;
}
q()

In short, I'm complaining about your complaint about something not working that wouldn't work anyway. I might try again later when I get firebug.

'vee...

EDIT: enemyHeal never seems to be called. 7 tries, still no. Possibly a the 1/2^7 chance, but still.
 
Last edited:
Back
Top Bottom