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

Homework giving you a headache? Math gives you a migraine? Can't quite figure out how to do something in photoshop? Never fear, the other members of CAA share their expertise in this forum.

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

Postby Never thirsty! » Mon Apr 21, 2014 12:06 pm

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.
User avatar
Never thirsty!
 
Posts: 245
Joined: Fri Jan 20, 2012 4:15 pm
Location: I don't even know anymore.

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

Postby Davidizer13 » Mon Apr 21, 2014 1:23 pm

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!
User avatar
Davidizer13
 
Posts: 1080
Joined: Fri Jun 26, 2009 9:27 am
Location: VIOLENT CITY

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

Postby MomentOfInertia » Mon Apr 21, 2014 4:17 pm

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
   }
}
MAL - CAA MAL club - Avatar from Hyouka
"DaughterOfZion 06:19 - forget love, fudge conquers all. xD"
"Written assignments are never finished, only due." -me
-Speak not unless you can improve the silence.-
MOES: Members Observing Efficient Sigs
User avatar
MomentOfInertia
 
Posts: 1316
Joined: Tue May 25, 2010 7:21 pm
Location: Around

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

Postby Never thirsty! » Tue Apr 22, 2014 11:43 am

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
User avatar
Never thirsty!
 
Posts: 245
Joined: Fri Jan 20, 2012 4:15 pm
Location: I don't even know anymore.

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

Postby Davidizer13 » Tue Apr 22, 2014 6:40 pm

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.
User avatar
Davidizer13
 
Posts: 1080
Joined: Fri Jun 26, 2009 9:27 am
Location: VIOLENT CITY

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

Postby MomentOfInertia » Wed Apr 23, 2014 2:17 pm

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.
MAL - CAA MAL club - Avatar from Hyouka
"DaughterOfZion 06:19 - forget love, fudge conquers all. xD"
"Written assignments are never finished, only due." -me
-Speak not unless you can improve the silence.-
MOES: Members Observing Efficient Sigs
User avatar
MomentOfInertia
 
Posts: 1316
Joined: Tue May 25, 2010 7:21 pm
Location: Around

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

Postby Xeno » Thu Apr 24, 2014 3:36 pm

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.
Image
User avatar
Xeno
 
Posts: 1895
Joined: Mon Jun 21, 2004 12:13 pm
Location: Oklahoma City

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

Postby Furen » Thu Apr 24, 2014 8:02 pm

Xeno, for just saying Same, that seemed like a crazy gravedig.
And this I pray, that your love would abound still, more and more with real knowledge and all discernment. Be prepared to preach the gospel at a moment's notice. Do you know the gospel well enough to do so yourself? Be ready.
User avatar
Furen
 
Posts: 2695
Joined: Mon Jul 26, 2010 9:39 pm
Location: Mostly at my PC, but meh, I can be wherever.

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

Postby drill » Thu Apr 24, 2014 8:07 pm

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?
Image
User avatar
drill
 
Posts: 572
Joined: Sun Jun 23, 2013 4:57 pm

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

Postby MomentOfInertia » Sat Apr 26, 2014 3:32 pm

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.
MAL - CAA MAL club - Avatar from Hyouka
"DaughterOfZion 06:19 - forget love, fudge conquers all. xD"
"Written assignments are never finished, only due." -me
-Speak not unless you can improve the silence.-
MOES: Members Observing Efficient Sigs
User avatar
MomentOfInertia
 
Posts: 1316
Joined: Tue May 25, 2010 7:21 pm
Location: Around

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

Postby Dante » Sat Jun 07, 2014 12:45 am

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');
  }
User avatar
Dante
 
Posts: 1323
Joined: Thu Mar 04, 2004 8:24 pm
Location: Where-ever it is, it sure is hot!


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 64 guests