Guess number game in javascript with two players

1.2k views Asked by At

I have created my turn guess number game in javascript with three levels of difficulty. At the beginning, players choose the difficulty. Next, it's the beginning of the game: each player try to guess the number.

Here my code:

var number = 0;
var playerTurn = 1


var levelChoice = prompt( "choisissez un niveau en tapant 1, 2 ou 3" );
if ( levelChoice == "1" ) {
  number = Math.round(Math.random()*10) ;
} else if ( levelChoice == "2" ) {
  number = Math.round(Math.random()*100) ;
} else if ( levelChoice == "3" ) {
  number = Math.round(Math.random()*1000) ;
} else {
  alert( "fais d'abord un des trois niveaux, impatient!" );
}


function displayTurn() {
  var NumberPlayer = prompt( "choisissez votre joueur entre 1 et 2" )
  if (playerTurn%2==0){
    alert("Joueur 1 c'est à vous");
  } else{
    alert("Joueur 2 c'est à vous");
  }
}

function incPlayerTurn() {
  playerTurn++;
}

function play() {
  displayTurn();

  incPlayerTurn();
}

for(var i = 1; i <= 8; i++) {
  var guess = prompt( "Devine à quel nombre je pense entre 1 et 100" );
  if( guess < number ) { 
    alert( "Plus haut! Quel est le nombre?" + ( 8-i ) + " essais restants" );
  } else if ( guess > number ) {
    alert( "Plus bas! Quel est le nombre?" + ( 8-i ) + " essais restants" );
  } else {
   alert( " Bravo! Vous avez trouvé le nombre, c'était bien " + number );
  }
}

Everything works very fine except the turn. Can someone explain how can I create this turn ?

1

There are 1 answers

1
halbgut On BEST ANSWER

You have a function play which your aren't calling anywhere. Just do this:

for(var i = 1; i <= 8; i++) {
  play();

You'll probably also want to remove this line:

  var NumberPlayer = prompt( "choisissez votre joueur entre 1 et 2" )