gpt4 book ai didi

javascript - 无法读取未定义的属性 'score'

转载 作者:行者123 更新时间:2023-11-28 03:12:48 27 4
gpt4 key购买 nike

无法读取未定义的属性“分数”这是一个使用极小极大算法的井字游戏。这是我整个应用程序的笔。

https://codepen.io/MartinaMF/pen/LjNdNN

    $(document).ready(function(){
var turns=[0,1,2,3,4,5,6,7,8];
var computerTurn;
var playerTurn;
var iter=0;
var count=0;

function winCondition(turnArray,currentTurn){
if((turnArray[0]===currentTurn && turnArray[1]===currentTurn && turnArray[2]===currentTurn)||
(turnArray[3]===currentTurn && turnArray[4]===currentTurn && turnArray[5]===currentTurn)||
(turnArray[6]===currentTurn && turnArray[7]===currentTurn && turnArray[8]===currentTurn)||
(turnArray[0]===currentTurn && turnArray[3]===currentTurn && turnArray[6]===currentTurn)||
(turnArray[1]===currentTurn && turnArray[4]===currentTurn && turnArray[7]===currentTurn)||
(turnArray[2]===currentTurn && turnArray[5]===currentTurn && turnArray[8]===currentTurn)||
(turnArray[0]===currentTurn && turnArray[4]===currentTurn && turnArray[8]===currentTurn)||
(turnArray[2]===currentTurn && turnArray[4]===currentTurn && turnArray[6]===currentTurn)){
return true;
}
else{
return false;}

}
function availableSpots(Arr){
return Arr.filter(function(item){
return item!="X" && item!="O";
});
}
function minimax(reboard,player){
iter++;
let array = availableSpots(reboard);
if (winCondition(reboard, playerTurn))
{
return {score:-10};

}
else if (winCondition(reboard, computerTurn))
{
return
{score:10};

}
else if (array.length === 0)
{
return {score:0};

}
var moves = [];
for(var i=0; i<array.length;i++){
var move = {};
move.index = reboard[array[i]];
reboard[array[i]] = player;
if(player==computerTurn){
var g = minimax(reboard, playerTurn);
move.score = g.score;
}
else{
var g = minimax(reboard, computerTurn);
move.score = g.score;
}
reboard[array[i]] = move.index;
moves.push(move);
}
var bestMove;
if(player === computerTurn){
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];
}

function reset(){
turns = [0,1,2,3,4,5,6,7,8];
count = 0;
$(".box").text("");
}
function movement(turn,id){
var spotTaken = $("#"+id).text();
if(spotTaken!="X" ||spotTaken!="O" ){
count++;
$("#"+id).text(turn);
turns[id]=turn;
if(winCondition(turns,turn)){
alert("you win!!");
reset();
}else if(count>8){
alert("it was a draw");
reset();
}
else{
count++;
var computerChoice = minimax(turns,computerTurn).index;
console.log(computerChoice);
$("#"+computerChoice).text(computerTurn);
turns[computerChoice]=computerTurn;
if(winCondition(turns,computerTurn)){
alert("you lose!!");
reset();
}
else if(count > 8){
alert("it was a draw");
}
}

}
}
$("#onePlayer").on("click",function(){
$("#xOro").show();
$("#chooseGame").hide();
});
$("#chooseX").on("click",function(){
$("#xOro").hide();
$("#gameBoard").show();
computerTurn = "O";
playerTurn = "X";
$(".playerOneTurn").show();
$(".playerOneTurn").html("<p>Computer's turn</p>");

});
$("#chooseO").on("click",function(){
$("#xOro").hide();
$("#gameBoard").show();
computerTurn = "X";
playerTurn = "O";
$(".playerOneTurn").show();
$(".playerOneTurn").html("<p>Computer's turn</p>");
});
$(".box").on("click",function(){
var spot = $(this).attr("id");
movement(playerTurn,spot);
});
});

我在上面的笔中为 tic tac toe 游戏创建了一个笔,但是当我尝试玩它时,它给我这条消息“无法读取未定义的属性分数”我不知道为什么?谁能帮帮我?

最佳答案

你的 minimax() 返回 {score} 然后你这样做:

 var g = minimax(reboard, computerTurn).score;
move.score = g.score;

'g' 的分数值不是对象,所以下面的行将失败

move.score = g.score;

你应该这样做:

var g = minimax(reboard, computerTurn).score;
move.score = g;

关于javascript - 无法读取未定义的属性 'score',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45627191/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com