Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Minimax Tic Tac-Toe Javascript游戏无法智能地玩_Javascript_Tic Tac Toe_Minimax - Fatal编程技术网

Minimax Tic Tac-Toe Javascript游戏无法智能地玩

Minimax Tic Tac-Toe Javascript游戏无法智能地玩,javascript,tic-tac-toe,minimax,Javascript,Tic Tac Toe,Minimax,下午好 有人能帮我弄清楚为什么我的Tic-Tac-Toe javascript游戏不能智能地玩……这意味着它不应该让我赢。我已经花了好几个小时在这上面了,但还是弄不明白。以下是指向我的项目的链接: 非常感谢你 "use-strict" $(document).ready(function() { var gameBoard = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], humanPlayer = 'X', EMPTY = '

下午好

有人能帮我弄清楚为什么我的Tic-Tac-Toe javascript游戏不能智能地玩……这意味着它不应该让我赢。我已经花了好几个小时在这上面了,但还是弄不明白。以下是指向我的项目的链接: 非常感谢你

"use-strict"
$(document).ready(function() {
  var gameBoard = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    humanPlayer = 'X',
    EMPTY = ' ',
    computerPlayer = 'O',
    currentPlayer = 'computer',
    choice;

  function buildBoard(board) {
    $('td').each(function(index) {
      $(this).text(board[index]);
    });
  }

  function makeMove(board, symbol, move) {
    board[move] = symbol;
  }

  function checkWin(board, player) {
    return ((board[0] == player && board[1] == player && board[2] == player) || (board[3] == player && board[4] == player && board[5] == player) || (board[6] == player && board[7] == player && board[8] == player) || (board[0] == player && board[3] == player && board[6] == player) || (board[1] == player && board[4] == player && board[7] == player) || (board[0] == player && board[4] == player && board[8] == player) || (board[2] == player && board[4] == player && board[6] == player) || (board[2] == player && board[5] == player && board[8] == player));
  }

  function boardSpaceOpen(board, move) {
    return board[move] == EMPTY;
  }

  function boardIsFull(board) {
    for (var i = 0; i < board.length; i++) {
      if (boardSpaceOpen(board, i)) {
        return false;
      }
    }
    return true;
  }

  function resetBoard() {
    gameBoard = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
    buildBoard(gameBoard);
    currentPlayer = 'computer';
    computerPlay();
  }



  function computerPlay() {
    var moves = getOpenSpaces(gameBoard);
    var move = moves[0];
    var score = -10000;
    for (var i = 0; i < moves.length; i++) {
      var newBoard = gameBoard.slice();
      newBoard[moves[i]] = computerPlayer;

      if (checkWin(computerPlayer, newBoard)) {
        move = moves[i];
        break;
      }
      var newScore = minimax(humanPlayer, newBoard);
      if (newScore > score) {
        score = newScore;
        choice = moves[i];
      }
    }
    makeMove(gameBoard, computerPlayer, move);
    buildBoard(gameBoard);

    if (checkWin(gameBoard, computerPlayer)) {
      alert('computer Wins!!');
      resetBoard();
    } else if (boardIsFull(gameBoard)) {
      alert('DRAW!!');
      resetBoard();
    } else {
      currentPlayer = 'human';
    }
  }

  function getOpenSpaces(board) {
    var emptySpaces = [];
    for (var i = 0; i < board.length; i++) {
      if (boardSpaceOpen(board, i)) {
        emptySpaces.push(i);
      }
    }
    return emptySpaces;
  }

  function getOpponentOf(player) {
    return currentPlayer == 'computer' ? 'human' : 'computer';
  }

  function minimax(board, player) {
    if (checkWin(board, humanPlayer)) {
      return -100
    }
    if (checkWin(board, computerPlayer)) {
      return 100
    }
    if (boardIsFull(board)) {
      return 0;
    }

    var availableMoves = getOpenSpaces(board);
    var scores = availableMoves.map(function(move){
      var newBoard = board.slice();
      newBoard[move] = player; 
      return minimax(getOpponentOf(currentPlayer), newBoard);
    });

    if(currentPlayer == 'computer'){
      return Math.max.apply(null,scores);
    }
    else{
      return Math.min.apply(null,scores)
    }
  }



  $('#reset').on('click', function() {
    resetBoard();
  });

  $('.piece').on('click', function() {
   humanPlayer = this.id;
   computerPlayer = humanPlayer == 'X' ? 'O' : 'X';
   computerPlay();
  });

  $('td').on('click', function() {
    if (currentPlayer == 'human') {
      var move = this.id;
      if (boardSpaceOpen(gameBoard, move)) {
        makeMove(gameBoard, humanPlayer, move);
        buildBoard(gameBoard);

        if (checkWin(gameBoard, humanPlayer)) {
          alert('You Won!!');
          resetBoard();
        } else if (boardIsFull(gameBoard)) {
          alert('DRAW!!');
          resetBoard();
        } else {
          currentPlayer = 'computer';
          computerPlay();
        }
      }
    }
  });

});
“使用严格的”
$(文档).ready(函数(){
var gameBoard=['','','','','','','','',
humanPlayer='X',
空=“”,
computerPlayer='O',
currentPlayer='计算机',
选择;
功能构建板(board){
$('td')。每个(函数(索引){
$(本).text(董事会[索引]);
});
}
函数makeMove(板、符号、移动){
板[移动]=符号;
}
功能checkWin(棋盘、玩家){
返回(棋盘[0]==玩家和棋盘[1]==玩家和棋盘[2]==玩家)| |(棋盘[3]==玩家和棋盘[4]==玩家和棋盘[5]==玩家)| |(棋盘[6]==玩家和棋盘[7]==玩家和棋盘[8]==玩家)| | |(棋盘[0]==玩家和棋盘[3]==玩家和棋盘[6]==玩家)| |(棋盘[1]==玩家和棋盘[4]==玩家)==(棋盘[0]==玩家和棋盘[4]==玩家和棋盘[8]==玩家)| |(棋盘[2]==玩家和棋盘[4]==玩家和棋盘[6]==玩家)| |(棋盘[2]==玩家和棋盘[5]==玩家和棋盘[8]==玩家));
}
功能板空格打开(板,移动){
返回板[移动]==空;
}
功能板已满(板){
对于(变量i=0;i分数){
分数=新闻核心;
选择=移动[i];
}
}
makeMove(游戏板、电脑播放器、移动);
建筑板(游戏板);
if(checkWin(游戏板、电脑播放器)){
警报(‘计算机赢了!!’);
重置板();
}else if(棋盘已满(游戏棋盘)){
警惕('DRAW!!');
重置板();
}否则{
currentPlayer='人类';
}
}
函数getOpenSpaces(板){
var-emptySpaces=[];
对于(变量i=0;i
这个问题太离题了,但我无能为力:你是不是在告诉我们你的javascript tic tac toe不够聪明,它确实让你赢了?真的吗?我不认为这样的算法通常比编码它的人更聪明……也可能读一读这里:首先让计算机检查它是否应该在ro中阻止2这是“请做我的家庭作业”吗?这是我在拼命工作,但我还是被卡住了。我是说我不应该打败电脑。我应该打成平局或者输。