Web Analytics Made Easy -
StatCounter Just trying to make a game with friends but my code keeps faiiling... - CodingForum

Announcement

Collapse
No announcement yet.

Just trying to make a game with friends but my code keeps faiiling...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Just trying to make a game with friends but my code keeps faiiling...

    I am just trying to make a game with some mates from school, and since I am the only one who knows programming, I have to be the one to code it. The only issue is that
    1. My randomizer does not work, it always chooses "Laarvite".
    2. My "Attack" algorithm at the bottom does not even run after asking what move you want to use.

    This is so infuriating and I need to get it finished by the end of the week or I might get kicked out.
    And yes, It is probably something super simple that I just dont know yet, if so, pardon me. Just please help me.


    Code:
    alert("Welcome to our currently unnamed online card game!")
    var keepPlay = true
    var dmg11 = 0
    var dmg12 = 0
    var dmg21 = 0
    var dmg22 = 0
    var hp1 = 1
    var hp2 = 1
    var def1 = 1
    var def2 = 1
    var playerElement1 = 5
    var playerElement2 = 5
    var move1p1 = "none"
    var move2p1 = "none"
    var move1p2 = "none"
    var move2p2 = "none"
    var elementUse11 = 0
    var elementUse21 = 0
    var elementUse12 = 0
    var elementUse22 = 0
    var dmgmult11 = 1
    var dmgmult21 = 1
    var dmgmult12 = 1
    var dmgmult22 = 1
    var cooldownLate1 = 0
    var cooldownLate2 = 0
    var cooldownp1 = 0
    var cooldownp2 = 0
    var recoil11 = 0
    var recoil21 = 0
    var recoil12 = 0
    var recoil22 = 0
    	while (keepPlay = true) {
    		var monsterArray = ["Laarvite", "Flortite"]
    		var playerMonster1 = Math.floor(Math.random()*monsterArray.length);
    		var playerMonster2 = Math.floor(Math.random()*monsterArray.length);
    				if (playerMonster1 = "Laarvite") {	
    					hp2 = 25
    					def2 = 40
    					move1p1 = "Gnaw"
    					move2p1 = "Repopulate"
    					alert("Player One has drawn Laarvite!")
    				} else if (playermonster1 = "Flortite") {
    					def2 = 35
    					move1p1 = "Ramming Force"
    					move2p1 = "Wrath"
    					alert("Player Two has drawn Flortite!")
    				}
    						if (playerMonster2 = "Laarvite") {
    							hp2 = 25
    							def2 = 40
    							move1p2 = "Gnaw"
    							move2p2 = "Repopulate"
    							alert("Player Two has drawn Laarvite!")
    						} else if (playermonster2 = "Flortite") {
    							hp2 = 35
    							def2 = 35
    							move1p2 = "Ramming Force"
    							move2p2 = "Wrath"
    							alert("Player Two has drawn Flortite!")
    						}
    			if (move1p1 = "Gnaw") {
    				dmg11 = 10;
    				elementUse11 = 3;
    			} else if (move2p1 = "Repopulate") {
    				dmgmult12 = 3;
    				elementUse21 = 6;
    			} else if (move1p2 = "Ramming Force") {
    				cooldownLate1 = 3;
    				dmg12 = 40;
    				recoil21 = 2;
    				elementUse12 = 4;
    			} else if (move2p2 = "Wrath") {
    				dmgmult = 2;
    				elementUse22 = 3;
    			}
    			var attackPrompt = prompt("PLAYER 1: Attack with " + move1p1 + " or " + move2p1 + " ? You have " + playerElement1 + "Element points.")
    	if (attackPrompt === move1p1) { // Move 1 Player 1 check
    		if (cooldown1 === 0) {
    			if (elementUse11 <= playerElement1) {
    				playerElement1 - elementUse11 === playerElement1;
    				if (def2 > dmg11) {
    					dmg11 === def2;
    					alert("The opponent's defense caused you attack to be weakened!")
    					dmg11 * dmgmult11 === dmg11;
    					cooldownLate1 + cooldown1 === cooldown1;
    					hp1 - recoil === hp1;
    						if (hp2 < 1) {
    							alert("You have defeated your opponent!")
    						} else if (hp1 < 0) {
    							alert("You have defeated yourself!")
    						} else {
    							alert("Your opponent is still attacking, and so are you!")
    						}
    				} else {
    					dmg11 * dmgmult11 === dmg11;
    					cooldownLate1 + cooldown1 === cooldown1;
    					hp1 - recoil === hp1;
    				}
    			} else {
    				alert("You do not have enough Element for this attack! You need " + playerElement1 + " element in order to use this move.")
    			}
    		} else {
    			alert("You need to wait " + cooldown1 + " turns in order to attack!")
    		}
    	} else if (attackPrompt === move2p1); { // Move 2 Player 1 check
    		
    	}
    	}

  • #2
    You have alot of errors in there. You should inspect the values of your variables using the browserconsole so you understand what is going on.
    Code:
    while (keepPlay = true) {	// setting keepPlay to true, instead of comparing it to true
    	var monsterArray = ["Laarvite", "Flortite"]
    	var playerMonster1 = Math.floor(Math.random()*monsterArray.length);	// 0 or 1
    	var playerMonster2 = Math.floor(Math.random()*monsterArray.length);	// 0 or 1
    		if (playerMonster1 = "Laarvite") {				// again setting instead of comparing, playerMonster1 would be 0 or 1 not a string.

    Comment


    • #3
      if (playerMonster1 = "Laarvite") {
      A single equal sign is an assignment; a double == is the comparator.

      var monsterArray = ["Laarvite", "Flortite"];
      var playerMonster1 = Math.floor(Math.random()*monsterArray.length); // 0 or 1
      var playerMonster2 = Math.floor(Math.random()*monsterArray.length); // 0 or 1
      if (playerMonster1 =="Laarvite") {
      Evolution - The non-random survival of random variants.
      Physics is actually atoms trying to understand themselves.

      Comment

      Working...
      X