Page 1 of 1

So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Mon Apr 21, 2014 12:06 pm
by Never thirsty!
it's been running fine until the point at the very end where I typed this code which doesn't run :
{
if(“player's health >=1”)
while(“monster's health is <=0”)
alert(“Congratulations! You win! ^-^");
}
{ else if(“player's health <=0”)
while(“monster's health >=1")
alert(“You lose. :p”);
}
};

I was just wondering if anyone who is at least advanced with javascript could point out where it went wrong the text turns blue each time I press enter and I get no error messages, or unexpected token or anything like that I'm using google chrome's javascript if that's is in any way helpful. the alerts don't show up.

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Mon Apr 21, 2014 1:23 pm
by Davidizer13
It's been forever since I've done any coding (Java or otherwise), so correct me if I'm wrong, but I think your problem is in the "while" statements. If I remember right, in Java, those are used to initialize do-while loops, where a loop keeps running until the condition of the loop is no longer met. What you probably want is an "and" statement.

Another thing you can try is to put the whole battle system into a big do-while loop, something like this (in pseudocode, so don't try to use it as written)

while(playerHP > 0 or enemyHP > 0)
{
(battle code stuff - the battle happens, HP changes, etc.)
}
if playerHP <= 0
{
print ("You win!)
}
else
{
print ("You lose!")
}

Hope that helps!

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Mon Apr 21, 2014 4:17 pm
by MomentOfInertia
A while in java is 'while A do B'
A do-while would be 'do B, repeat while A'

But java is not javascript so YMMV.

More pseudo-code:
Code: Select all
while(playerHP > 0 AND enemyHP > 0)
{
   //round of combat

   if playerHP >= 0 AND enemyHP <=0
   {
      // winning stuff
   }
   else if playerHP <= 0 AND enemyHP >=0
   {
      //losing stuff
   }
   else {
       //DRAW
   }
}

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Tue Apr 22, 2014 11:43 am
by Never thirsty!
I tried both of them they both work I just decided in the future I will use what's easiest thanks for your help.
MOI I like the green code reminds me of a really great movie that takes place in a video game(nonspoiler spoiler alert it's the Matrix) is video game 1 word or 2 that always confused me I don't know why

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Tue Apr 22, 2014 6:40 pm
by Davidizer13
Never thirsty! wrote:is video game 1 word or 2 that always confused me I don't know why


It has come to my attention that in certain circles, simian-minded individuals are refering to vidcons as 'vid cons', ignorantly placing a space between 'vid' and 'con'. Perhaps their brains have dulled by years of Madden and Quake, rather than the mentally invigorating games such as Arc the Lad and Growlanser, becuase even a child could tell that placing a space between the 'vid' and 'con' in vidcon is perhaps more profoundly philistine than a certain American administration that need not be named. Placing a space in vidcon completely belittles the meaning of the word and displays the user's blatantly minuscule intellect and is understanding of the basic precepts of grammar. Vidcon is a perfect marriage of the words console and video game, creating a short and effective portmanteau that quickly and accurately labels mentions objects and anybody who does not immediately recognize 'vid con' as absolutely outrageous clearly lacks the mental faculties to correctly operate a vidcon other than perhaps FIFA Sports. I make this point becuase I have recently been belligerently barrage by imbecilic 'vid con' references that unnerve me to no end and have taken it upon myself to correct the damage that your poor Western education ( though this is a subject to be discussed on a later date) has wrought upon you. You should personally thank me that i did not see it fit to correct your preponderous mistake in Japanese, becuase I am thoroughly positive your neanderthal mind would be incapable of deciphering the Hiragana from the Katakana.

Serious post: How much do you know about functions/methods and object-oriented programming? Can you even do that sort of thing in JavaScript? Depending on how complex you want this to become, you'd probably do well to give those a look. For example, you could make a data class for the Pokemon, with a subclass based on the species that contains the types, moves, base stats, level-up growths, etc. Then, you could make each move its own method with its attack power and type weaknesses/strengths/formulas for how stats modify the damage, pass in the attacker and defender Pokemon objects, and use their data to calculate what happens.

Or maybe I'm just getting ahead of myself here because MoI is proving that my code fu's weakened over the years. Not that I had much to begin with.

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Wed Apr 23, 2014 2:17 pm
by MomentOfInertia
From what I've read Javascript can probably do that stuff, and runs conveniently in your browser. But has the 'flaw' (depends on your perspective) that the compiler doesn't actually do very much to keep you out of trouble.
See here:
http://en.wikipedia.org/wiki/Javascript#Criticisms

I understand that Lua is supposed to be fairly easy to pick up and is popular with people making games because of it.
http://en.wikipedia.org/wiki/Lua_%28programming_language%29

Food for thought.

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Thu Apr 24, 2014 3:36 pm
by Xeno
Davidizer13 wrote:It has come to my attention that in certain circles, simian-minded individuals are refering to vidcons as 'vid cons', ignorantly placing a space between 'vid' and 'con'. Perhaps their brains have dulled by years of Madden and Quake, rather than the mentally invigorating games such as Arc the Lad and Growlanser, becuase even a child could tell that placing a space between the 'vid' and 'con' in vidcon is perhaps more profoundly philistine than a certain American administration that need not be named. Placing a space in vidcon completely belittles the meaning of the word and displays the user's blatantly minuscule intellect and is understanding of the basic precepts of grammar. Vidcon is a perfect marriage of the words console and video game, creating a short and effective portmanteau that quickly and accurately labels mentions objects and anybody who does not immediately recognize 'vid con' as absolutely outrageous clearly lacks the mental faculties to correctly operate a vidcon other than perhaps FIFA Sports. I make this point becuase I have recently been belligerently barrage by imbecilic 'vid con' references that unnerve me to no end and have taken it upon myself to correct the damage that your poor Western education ( though this is a subject to be discussed on a later date) has wrought upon you. You should personally thank me that i did not see it fit to correct your preponderous mistake in Japanese, becuase I am thoroughly positive your neanderthal mind would be incapable of deciphering the Hiragana from the Katakana.


Same.

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Thu Apr 24, 2014 8:02 pm
by Furen
Xeno, for just saying Same, that seemed like a crazy gravedig.

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Thu Apr 24, 2014 8:07 pm
by drill
Furen wrote:Xeno, for just saying Same, that seemed like a crazy gravedig.

You do realize that this thread is only 4 days old, yes? Maybe you thought it said 2004 instead of 2014?

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Sat Apr 26, 2014 3:32 pm
by MomentOfInertia
drill wrote:
Furen wrote:Xeno, for just saying Same, that seemed like a crazy gravedig.

You do realize that this thread is only 4 days old, yes? Maybe you thought it said 2004 instead of 2014?

New users playing with the search function and bumping five year old threads to say "this" is not all that uncommon, though it has been awhile since I've seen one.

Re: So I made a text based version of pokemon red in javascript it's not giving any errors but it's not running right

PostPosted: Sat Jun 07, 2014 12:45 am
by Dante
Code: Select all
{
if(“player's health >=1”)
while(“monster's health is <=0”)
alert(“Congratulations! You win!  ^-^");
}
{ else if(“player's health <=0”)
while(“monster's health >=1")
alert(“You lose. :p”);
}
};


This doesn't look right at all - supposing I have an object player and an object monster, however,

Code: Select all
if( (player.health > 0) && (monster.health > 0) ){
  //TODO: Add code for continuing battle
}
else if( (player.health > 0) && (monster.health <= 0) ){
  alert('Congratulations! You win!  ^-^');
}
else{
  //This conditional fires if the player's health is below zero, independent of what the monster's health is
  alert('You lose. :p');
}


Here's where your code went wrong:

1. “player's health >=1” isn't valid code. It's like asking the computer 'is the phrase "hello world" true, or false? ' It's neither, it's a string, not a Boolean variable. Instead of this, I recommend creating objects for both the monster and the player, which is really easy in javascript. Just say:

Code: Select all
player = {};
player.health = 5;
player.health -= 2;

monster = {};
monster.health = 7;
monster.health -= 3;


Now, of course, it's important to remember. Javascript is an oddball, it's not an object oriented language. Because of this, you won't find your typical class declarations and what not. You can force it, but in truth, it's just too darn easy to tack on a variable name with a dot and set it equal to something (including a function as it turns out). Initialize these in one spot, though or your debugging will be a mess. You can also tack on far more than health. What about atk, or def, or a string variable for a name... or gender (Super effective for helping Professor Oak, who has a horrible time determining the gender of others, even if he lived next to them their whole life).

2. Your logic is off, while in English,

Code: Select all
if(“player's health >=1”)
while(“monster's health is <=0”)


this is understandable, to a computer, it makes no sense at all. It will instead, happily throw a syntax error. Instead, what you're looking for are two conditionals. One that checks that the player's health is above 0 and another to check that the monsters health isn't below zero. That is,

Code: Select all
//If the player's health is greater than zero and the monster's health is less than or equal to zero
//or in code...
if( (player.health > 0) && (monster.health <= 0) ){
  //Do stuff here if the stuff in the parenthesis above returns true
}


Here, I've included two conditionals,

Code: Select all
player.health > 0


and

Code: Select all
monster.health <= 0


with a && between them. The && means that both conditions must be true in order for the conditional to run the code between the brackets { these thingys }. It's the logical equivalent of saying 'and'. || on the other hand means 'or', so that the conditional will run if either condition returns true. The parenthesis are there to simply make sure that each condition is separate from the other one. If you're wondering why we don't just say 'and' and 'or' in the program, well... Python wonders this, too. CoffeeScript, however, fixes that issue - but is only suitable for more advanced users who already understand JavaScript.

3. The other major problem I see is that you're not properly framing your code within your if and while statements. Remember...


Code: Select all
while(true){
  //Stuff you want to complete in the while loop should go here.
}


Code: Select all
if(true){
  //Stuff you want to complete in the if statement go here.
}


And if you want one conditional within another...

Code: Select all
if(test_1){
  if(test_2){
    alert('hooray!');
  }
  else if(test_3){
    alert('Yay!');
  }
}


Finally, while it isn't likely going to hurt anything, don't include a semicolon after your bracket enclosures (e.g. { } these guys). It just looks bad. I got this working for me when I tried out various numbers, see if that does something similiar to what you want.

Code: Select all
  player = {};
  player.health = 0;

  monster = {};
  monster.health = 1;

  if((player.health > 0) && (monster.health > 0)){
    alert('let the battle continue!');
  }
  else if((player.health > 0) && (monster.health <= 0)){
    alert('Congratulations! You win!');
  }
  else{
    alert('You lose :p');
  }