检查当前玩家是否赢得了用JavaScript玩Tic Tac Toe的游戏

时间:2018-08-24 18:01:02

标签: javascript html css web tic-tac-toe

我正在用html CSS和JavaScript制作一个简单的Tic Tac Toe。未实施计算机AL播放器。只有两名选手轮流参赛。我的checkWinner功能无法正常工作,我不知道如何确定中奖者。有人建议我创建一个二维数组来存储获胜组合,这是在名为winCombos的变量中完成的。我正在遍历所有这些对象,并在单元格数组的不同位置与currentPlayer进行比较,但是它不起作用。可能我做错了。有人能帮帮我吗。谢谢

var player1 = "X";
var player2 = "O";
var cells = Array.from(document.querySelectorAll(".cell"));
var currentPlayer = player1;
var winner = "";
var winCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];

function checkWinner(arr, curPlayer){
    arr.forEach(function(subArr){
      var counter = 0;
      subArr.forEach(function(elem){
        if(cells[elem] === curPlayer){
          counter++;
          if(counter == 2 ){
            winner = curPlayer;
            alert(winner);
          }
        }
      });
    });
}

function move(e){
  if(e.target.className === "cell" && e.target.textContent === ""){
    e.target.textContent = currentPlayer;
    checkWinner(winCombos, currentPlayer);
    currentPlayer = (currentPlayer === player1)?player2:player1;
  }
  
}

document.querySelector(".board").addEventListener("click", move);
.container{
  width: 400px;
  height: 480px;
  background: tomato;
  margin: 50px auto;
  text-align: center;
  padding: 10px;
}

.board{
  width: 90%;
  margin: 20px auto;
}

.board .cell{
  width: 118px;
  height: 100px;
  float: left;
  text-align: center;
  line-height: 80px;
  font-size: 80px;
  font-weight: bold;
  border: 1px solid #fff;
  color: #fff;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  <div class="container">
    
   <div class="board">
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
   </div>
  </div>
</body>
</html>

1 个答案:

答案 0 :(得分:4)

两件事:使用innerHTML获取div的内容,而不是包含整个div的字符串。另外,请检查实际上是否连续有3个,而不仅仅是2个。(或者您可以将counter++;移至检查后,然后将其与counter == 2留在一起)

if(cells[elem].innerHTML === curPlayer){
  counter++;
  if(counter == 3 ){
    winner = curPlayer;
    alert(winner);
  }
}

var player1 = "X";
var player2 = "O";
var cells = Array.from(document.querySelectorAll(".cell"));
var currentPlayer = player1;
var winner = "";
var winCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];

function checkWinner(arr, curPlayer){
    arr.forEach(function(subArr){
      var counter = 0;
      subArr.forEach(function(elem){
        if(cells[elem].innerHTML === curPlayer){
          counter++;
          if(counter == 3 ){
            winner = curPlayer;
            alert(winner);
          }
        }
      });
    });
}

function move(e){
  if(e.target.className === "cell" && e.target.textContent === ""){
    e.target.textContent = currentPlayer;
    checkWinner(winCombos, currentPlayer);
    currentPlayer = (currentPlayer === player1)?player2:player1;
  }
  
}

document.querySelector(".board").addEventListener("click", move);
.container{
  width: 400px;
  height: 480px;
  background: tomato;
  margin: 50px auto;
  text-align: center;
  padding: 10px;
}

.board{
  width: 90%;
  margin: 20px auto;
}

.board .cell{
  width: 118px;
  height: 100px;
  float: left;
  text-align: center;
  line-height: 80px;
  font-size: 80px;
  font-weight: bold;
  border: 1px solid #fff;
  color: #fff;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  <div class="container">
    
   <div class="board">
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
     <div class="cell"></div>
   </div>
  </div>
</body>
</html>