Script to change player (old Jquery game)

Asked

Viewed 326 times

2

I’m trying to get the tic-tac-toe to change the player who has the turn, I was able to change it from 'X' to 'O', but when I try to get back to 'X' and so on, it won’t.

Follow the code I 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>
<script src="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>

3 answers

3

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

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>
<script src="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>

3


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>
<script src="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, by clicking the button, in case the player be 1, then put X and assign 2 to that variable to indicate that it is the player 2 who will play if it is the turn of player 2 then do the opposite, put O and variable player becomes worth 1.

  • We come to a similar solution :P if you want to remove mine ;]

  • kk Keep yours there, it’s kind of different, it’s good to get the message that there are many ways to do it.

1

I have made some (many) changes. I have implemented the check if the button is pressed again, do not change the set value. Test there and check the code to see if you understand.

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>
<script src="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.