Jump to content

Help a newbie out


Recommended Posts

How do I add a window.alert popup where if the warrior’s health drops dangerously low, the user is told she must win the next battle to stay alive.
I tried inputting function and window.alert(); every where but the program doesn't work. What must I do to make it work? If you can, can you give me some tips how to improve my skills. I would greatly appreciate it.
Thanks!


---------------------------------------------------------------------------------------------
<!DOCTYPE>
<html>
<head>
<title>Requisite Warrior</title>
</head>
<body>
<h1>Requisite Warrior</h1>
<script>
var requisiteWarrior = {
health: 20,
doBattle: function () {
var isVictorious = Math.random() > 0.5;
if (isVictorious) {
this.health += 8; // 'this' is very important!
}
else {
this.health -= 10; // 'this' is very important!
}
return isVictorious;
},
almostDead: function () {
if (this.health >= 10) {
window.alert(Your health is low!);}
}
isAlive: function () {
if (this.health >= 0) {
return true;
}
else {
return false;
}
}
};
var isWon = null,
msg = '';
// Begin game.
window.alert('Do battle!');
while (requisiteWarrior.isAlive()) {
isWon = requisiteWarrior.doBattle();
if (isWon) {
msg = 'Victory! Health is up to ' + requisiteWarrior.health + '.\nClick OK to do battle again.';
}
else if (requisiteWarrior.isAlive()) {
msg = 'Defeat. Health is down to ' + requisiteWarrior.health + '.\nClick OK to do battle again.';
}
else {
msg = 'Alas, you have fought your last battle. :-(';
}
window.alert(msg);
}
// Ending message
window.alert('You provided great service and were valued for you bravery.');
</script>
</body>
</html>
Edited by K31V
Link to post
Share on other sites

You have an almostDead() method, but at no point is your program using it. It's just there, not being used.

 

You should use a code box to put code on the forum, and try to properly indent your code and keep closing braces on their own line for readability.

  • Like 1
Link to post
Share on other sites

So how do I make it so almostDead can run with the window.alert(); if the player's health is less than 10?

And where should I put it to make it functional?

 

almostDead: function () {

if (this.health >= 10) {
window.alert(Your health is low!);}
Edited by K31V
Link to post
Share on other sites

It's a 50/50 game. You start with 20 HP and you lose 10 HP when you lose and gain 8 HP when you win.

I want the window.alert(); to show up to warn the player when the character's HP is equal or less than 10 HP, and that's where I'm stuck at.

Edited by K31V
Link to post
Share on other sites

So how would you find out when that happened?

 

Before writing code, have you written down in words exactly what your program does? Try to describe it to somebody who doesn't know code.

Link to post
Share on other sites
Hopefully this will help clarify things up:

<!DOCTYPE>
<html>
<head>
<title>Requisite Warrior </title>
</head>
<body>
<h1>Requisite Warrior </h1>
<script>
// Define entities.
/* requisiteWarrior starts out with a health of 20.
* When she does battle, there is a 50/50 chance that she will emerge
* victorious. Victory ramps up her health by 8, defeat down by 10.
* The requisiteWarrior dies when her health drops below 0. */
var requisiteWarrior = {
health: 20,
/* doBattle makes requisiteWarrior do battle. If she wins, her health
* is increased; if she looses it is reduced.
* Returns whether the battle resulted in victory or not. */
doBattle: function () {
var isVictorious = Math.random() > 0.5;
if (isVictorious) {
this.health += 8; // 'this' is very important!
}
else {
this.health -= 10; // 'this' is very important!
}
return isVictorious;
}
isAlive: function () {
if (this.health >= 0) {
return true;
}
else {
return false;
}
}
};
var isWon = null, // stores result of last battle.
msg = ''; // a temporary variable for composing messages.
// Begin game. (Game ends when requisiteWarrior is no longer alive.)
window.alert('Do battle!');
while (requisiteWarrior.isAlive()) {
isWon = requisiteWarrior.doBattle(); // battle!
if (isWon) {
msg = 'Victory! Health is up to ' + requisiteWarrior.health + '.\nClick OK to do battle again.';
}
else if (requisiteWarrior.isAlive()) {
msg = 'Defeat. Health is down to ' + requisiteWarrior.health + '.\nClick OK to do battle again.';
}
else {
msg = 'Alas, you have fought your last battle. :-(';
}
window.alert(msg);
}
// Ending message
window.alert('You provided great service and were valued for you bravery.');
</script>
</body>
</html>
Edited by K31V
Link to post
Share on other sites

I know what your current code does, what I actually need to know is what you really want it to do, from beginning to end. Just write a paragraph with no code explaining how the program is supposed to work.

 

This is part of software design, if you can't describe what your program is meant to do, I don't think you'll be able to tell the computer to do it.

Link to post
Share on other sites

The program is a 50/50 chance game of winning or losing. The player must reach 100 HP in the 50/50 game in order to win. If the player reaches 100 HP, I want add a message saying ("Congratulations, you have won!") and the game finishes.

 

I already got down the code where if you lose it'll say ("you have fought your last battle and you've lost..").

 

The other thing is where do I input a message saying ("You must win the next battle if you want to survive.") when the player's HP is equal or less than 10 HP before hitting 0 HP within the code.

Edited by K31V
Link to post
Share on other sites

I'm going to give a step by step description of what your program should do:

  1. Set warrior's health to 20
  2. Do a battle (This is a sub-procedure that should be defined)
  3. If the previous battle was won show a victory message
  4. If the previous battle was not won and the warrior is still alive, show a defeat message
  5. If the warrior's HP is between 0 and 10, show "Your health is low"
  6. If the warrior is still alive and his HP is less than 100 then return to step 2.
  7. If the warrior's HP is 100 or more, show the final victory message
  8. If the warrior is not alive, show the failure message.

No code whatsoever, just a description of what I interpreted as what you really want. This is the most important aspect of programming: Finding out exactly what you want the program to do.

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...