#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Error with my basic javascript game - Please Help :D


    Hey

    I'm new both to these forums and to programming in general (I'm up to Javascript on both Codecademy and TTH), and I was just wondering if you guys/gals could help me out with a little side project I'm working on.

    It's basically a really simple game. It's you (User) vs the computer (Comp), and you both start with 20 life. Each round the User can choose to Attack or Run, and if the User Attack's, both the User and Comp do a number of damage between 1 - 10.

    It works fine for the first round, but then when it loops back up to the second round, it displays the 2nd userAttack, but then crashes before displaying the 2nd compAttack.

    Here is what I've come up with so far:

    javascript Code:
     
    var userHealth = 20;
    var compHealth = 20;
    var userAttack = 0;
    var compAttack = 0;
    var userRun = 0;
    var compRun = 0;
    var userChoice = "";
     
    for (i=2; i<3; i++){
    	console.log("Your Health:"+" "+userHealth);
    	console.log("Comp Health:"+" "+compHealth);
    	var userChoice = prompt("Would you like to ATTACK or RUN?").toLowerCase();
    	switch(userChoice) {
    		case "attack":
    			var userAttack = Math.floor(Math.random() * 10);
    			var compAttack = Math.floor(Math.random() * 10);
    			alert("You did"+" "+userAttack+" "+"damage!");
    			alert("You took"+" "+compAttack+" "+"damage!");
    			var userHealth = userHealth - compAttack;
    			var compHealth = compHealth - userAttack;
    			alert("Your life:"+" "+userHealth+". Comp life:"+" "+compHealth+".");
    			break;
    		case "run":
    			var userRun = Math.random() * 5;
    			var compRun = Math.random() * 4;
    			if (userRun > compRun) {
    				alert("You got away!");
    				alert("Game over!");
    			} else {
    				alert("You tried to run but couldn't get away!");
    				compAttack = Math.floor(Math.random() * 10);
    				userHealth = userHealth - compAttack;
    				alert("You were shot while running and took"+" "+compAttack+" "+"damage!");
    			}
    			break;
    		default:
    			alert("You didn't pick 'attack' or 'run'. Please try again!");
    	}
    				if (userHealth <= 0 && compHealth > 0) {
    				alert("Game over dude! Game over!");
    			} else if (userHealth > 0 && compHealth <=0) {
    				alert("You won!");
    				alert("You didn't win ****! But you won!");
    			} else if (userHealth < 1 && compHealth < 1) {
    				alert("You killed each other!");
    				alert("So technically since you're dead, you lost!");
    				alert("Play better next time n00b!");
    			} else {
    				i = 1;
    			}
    }


    and here is the error that Codecademy's scratchpad gives me (but only after doing a full loop once, it only crashes on the second time going through the loop):

    Code:
    [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMWindow.alert]"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: http://www.codecademy.com/assets/repl.html :: <TOP_LEVEL> :: line 18"  data: no]
    The only thing I can grasp from that is that it messes up on line 18, which is where it is supposed to display an alert using the "compAttack" variable. Normally I like to do trial and error, but since it displays userAttack but not compAttack, and it displays both on the first loop but crashes on the second, I just don't even have the slightest idea as to where to begin.

    If anyone has any advice or recommendations, either on how to fix this or regarding a better/easier/more-efficient way to code what I have so far, that'd be awesome! I'm doing this as a side project to kind of nail everything down that I've been learning so far, and once I get this working I'm planning on adding more options one at a time. My side-project goal is to end up with a game similar to that of "Drug Wars".

    Anyways, thanks a lot everyone. I'm looking forward to hearing your advice.

    Update:
    One more thing

    It took me a little while to figure out how to even make it loop back up, and I'm not sure I have the best way. Like, I couldn't figure out how to tell the computer to loop back up if the game wasn't over, but to end if it WAS over. I finally came up with the (i=2;i<3;i++) with the variable "i" being set to 1 (instead of 2) at the end of the loop, but only if the game wasn't over. I figured this would restart the loop and make "i" 2 again, and since it's still less than 3, the game continues. If either User or Comp dies, "i" stays valued at 2 at the end of the loop, and restarts the loop to become 3 which closes the loop.

    If there is a simpler/better way to do this (I'm sure there is no less than 20 simpler/better ways lol), I'd love to hear about it. Thanks

    Update 2:
    If I'm using the wrong terms, words, phrases, etc., please let me know. I'd rather hear about it now then continue sounding ignorant later Thanks
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    a simpler line 18:

    alert("you did " + userAttack + " damage!");

    You don't need the + " " because you can just build it into your strings using the space as a character. That might help.

    But you say it works on the first loop through? hmm... let me take another look.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Math.floor((Math.random()*10)+1);

    the above is the code you need to get a number between 1 and 10. What you are generating is 0 - 9. Not that big a deal, but just sayin'.

    btw, I just ran your code at code academy labs and it works great! went all the way through the game, and we killed each other. Which is actually an excellent lesson in the futility of war... you're a born philosopher my friend.

IMN logo majestic logo threadwatch logo seochat tools logo