有人会介意看看我的Tic-tac-toe javascript代码,看看为什么我的compMove()函数(包含我的minimax函数)没有触发?这是一个带有html&的完整代码的链接。 css:https://codepen.io/DRick504/pen/VWpEXv
function compMove() {
function minimax(copyTwo, player) {
var newBoard = copyTwo.slice()
var score = 0
var availSpots = spotsLeft(newBoard) //returns array of available indexes
if (checkWinner(newBoard,human)===true) {
return score = 10
} else if (checkWinner(newBoard,opp)===true) {
return score = -10
} else if (availSpots==='empty') {
return score = 0
}
var moves = []
for (var i = 0; i < availSpots.length; i++){
//takes board and only goes through the available spots on the board. Then puts them in an object.
var move = {};
move.index = newBoard[availSpots[i]];
newBoard[availSpots[i]] = player;
if (player == opp){
var result = minimax(newBoard, human);
move.score = result.score;
} else {
var result = minimax(newBoard, opp);
move.score = result.score;
}
newBoard[availSpots[i]] = move.index;
moves.push(move);
}
var bestMove;
if(player === opp){
var bestScore = -10000;
for(var i = 0; i < moves.length; i++){
if(moves[i].score > bestScore){
bestScore = moves[i].score;
bestMove = i;
}
}
} else{
var bestScore = 10000;
for(var i = 0; i < moves.length; i++){
if(moves[i].score < bestScore){
bestScore = moves[i].score;
bestMove = i;
}
}
}
return moves[bestMove];
}
var boardcopy = gameBoard.slice()
var makeMoves = minimax(boardcopy,opp)
$('.b'+makeMoves).removeClass('block')
$('.b'+makeMoves).addClass(oppClass)
$('.b'+makeMoves).text(opp)
gameBoard[makeMoves]=opp
turnsLeft++
if (checkWinner(gameBoard,opp)===true) {
alert ('You Lose! So Sorry! :\(')
$('.Losses').text('Lost'+losses++)
return reset()
} else if (turnsLeft===9) {
alert('It\'s a Draw!')
return reset()
}
whosTurn=1
} //end of CompMove