Script to change player (old Jquery game)

1

I'm trying to make the game of the old one change the player who has the turn, I managed to make it change from 'X' to 'O', but when I try to return to 'X' and so on, it will not.

Follow the code I've made so far:

$(document).ready(function() {
   $(".botao").click(function() {
     $(this).text("X");
     $("#jogador").text("É a vez do jogador 2");

     mudarSimbolo();
   });

   function mudarSimbolo() {
     if ($("#jogador").text() == "É a vez do jogador 2") {
       $(".botao").click(function() {
         $(this).text("O");
         $("#jogador").text("É a vez do jogador 1");
       });
     } else if ($("#jogador").text() == "É a vez do jogador 1") {
       $(".botao").click(function() {
         $(this).text("X");
         $("#jogador").text("É a vez do jogador 2");
       });
     }
   }

 });
.btn-default {
  padding: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" style="border:1px solid red; width:320px; height:320px;">
  <button class="btn btn-default botao">1</button>
  <button class="btn btn-default botao">2</button>
  <button class="btn btn-default botao">1</button>
  <button class="btn btn-default botao">2</button>
  <button class="btn btn-default botao">3</button>
  <button class="btn btn-default botao">4</button>
  <button class="btn btn-default botao">5</button>
  <button class="btn btn-default botao">6</button>
  <button class="btn btn-default botao">7</button>
</div>
<div class="container">
  <label id="jogador">É a vez do jogador 1</label>
</div>
    
asked by anonymous 16.01.2017 / 01:20

3 answers

2

Follow the code with correction:

$(document).ready(function() {
  
  var player = 1;
  
   $(".botao").click(function() {
     if(player == 1) {
       $(this).text("X");
       player = 2;
     } else {
        $(this).text("O");
        player = 1;
     }
       
     
     $("#jogador").text("É a vez do jogador " + player);
   });

 });
.btn-default {
  padding: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" style="border:1px solid red; width:320px; height:320px;">
  
  <p>
    <button class="btn btn-default botao">1</button>
    <button class="btn btn-default botao">2</button>
    <button class="btn btn-default botao">3</button>
  </p>
  
  <p>
    <button class="btn btn-default botao">4</button>
    <button class="btn btn-default botao">5</button>
    <button class="btn btn-default botao">6</button>
  </p>
  
  <p>
    <button class="btn btn-default botao">7</button>
    <button class="btn btn-default botao">8</button>
    <button class="btn btn-default botao">9</button>
  </p>
</div>
<div class="container">
  <label id="jogador">É a vez do jogador 1</label>
</div>

What I did was create a global variable in the document called player , when I click the button, if player is 1, then I put X and attribute 2 to that variable to indicate that it is player 2 who will play, if it is the turn of player 2 then does the opposite, put O and the variable player becomes worth 1.

    
16.01.2017 / 01:40
2

I would simplify the logic, just create a global variable and check it:

var elem = "O";
$(document).ready(function() {
   $(".botao").click(function() {
     $(this).text(elem);
     if (elem == "X") {
       elem = "O";
       $("#jogador").text("É a vez do jogador 1");
     } else if (elem == "O") {
       elem = "X";
       $("#jogador").text("É a vez do jogador 2");
     }
   });
 });
.btn-default {
  padding: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" style="border:1px solid red; width:320px; height:320px;">
  <button class="btn btn-default botao">1</button>
  <button class="btn btn-default botao">2</button>
  <button class="btn btn-default botao">1</button>
  <button class="btn btn-default botao">2</button>
  <button class="btn btn-default botao">3</button>
  <button class="btn btn-default botao">4</button>
  <button class="btn btn-default botao">5</button>
  <button class="btn btn-default botao">6</button>
  <button class="btn btn-default botao">7</button>
</div>
<div class="container">
  <label id="jogador">É a vez do jogador 1</label>
</div>
    
16.01.2017 / 01:38
1

I made a few (many) changes. I checked the verification if the button is pressed again, do not change the set value. Test there and check the code to see if it fits.

var vez = false;


$(document).ready(function() {
   $(".botao").click(function(event) {     
     //verifica se o botão já foi precionado e aborta.
     if ($(event.target).data("key") =="1"){
       return;
     }
     //verifica de quem é a vez
     if (vez) {   
         //indica a vez do jogador em questão
         $("#jogador").text("É a vez do jogador 1");
          $(event.target).html("X");
          //marca como botão já precionado.
          $(event.target).data("key","1");
          vez = !vez;
     } else {
         $("#jogador").text("É a vez do jogador 2");
         $(event.target).html("O");
          $(event.target).data("key","1");
        vez = !vez;
     }
   });
 });
.btn-default {
  padding: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" style="border:1px solid red; width:320px; height:320px;">
  <button class="btn btn-default botao">1</button>
  <button class="btn btn-default botao">2</button>
  <button class="btn btn-default botao">1</button><br>
  <button class="btn btn-default botao">2</button>
  <button class="btn btn-default botao">3</button>
  <button class="btn btn-default botao">4</button><br>
  <button class="btn btn-default botao">5</button>
  <button class="btn btn-default botao">6</button>
  <button class="btn btn-default botao">7</button>
</div>
<div class="container">
  <label id="jogador">É a vez do jogador 1</label>
</div>
    
16.01.2017 / 02:01