dimanche 27 octobre 2019

Why I keep on get the same result on this randomized game?

I am on my first week learning CS and coding. I tried to make a simple randomized fight betweet A and B. It is supposed to run until either one reach life=0 and the one who dies will be announced through console.log

I have tried to play with all of the variables, but still get the same result.

var lifeA = 500    
var lifeB = 250
var powerA = 2
var powerB = 4
var increasepowerA = Math.floor(Math.random() * 5)
var increasepowerB = Math.floor(Math.random() * 10)
var chanceattack = 0.5
var x = 0


function getx(){
  const z = Math.random()
  if (z <= chanceattack){
     x = 1
  }else{x = 0}
}

getx()

function execute(){
  if(x = 1){attackA()
  }else{attackB()}
}


function attackB(){
  lifeA -= powerB;
  powerB += increasepowerB;
  chanceattack += 0.075;
  if (lifeA <= 0){
    console.log("A has dead")
  }else{getx(); execute()}
}

function attackA(){
  lifeB -= powerA;
  powerA += increasepowerA;
  chanceattack -= 0.075;
  if (lifeB <= 0){
    console.log("B has dead")

  }else{getx(); execute()}
}

execute()

Since this is a randomized game, I expect that everytime I refresh my live server I would get different result either "A has died" or "B has died". However, I constantly get "B has died"




Aucun commentaire:

Enregistrer un commentaire