Javascript 极大极小问题。卡在调用堆栈溢出中
以下是我认为已损坏且不知道如何修复的代码部分:Javascript 极大极小问题。卡在调用堆栈溢出中,javascript,minimax,Javascript,Minimax,以下是我认为已损坏且不知道如何修复的代码部分: let playerSymbol = ''; let computerSymbol = ''; let computerTurn; let squareValues = ["x","","","","","","","",""]; for (let i=0; i<
let playerSymbol = '';
let computerSymbol = '';
let computerTurn;
let squareValues = ["x","","","","","","","",""];
for (let i=0; i<availableSpots.length; i++) {
let move = {};
move.index = newBoard[availableSpots[i]];
newBoard[availableSpots[i]] = player;
console.table(newBoard[availableSpots[i]])
console.table(availableSpots[i])
// console.log(newBoard);
if (player === "x") {
let result = minimax(newBoard, "o" ,(depth + 1));
console.log(player,"outside",depth);
move.score = result.score;
} else {
let result = minimax(newBoard, "x", (depth + 1));
console.log(player,depth,"else")
move.score = result.score;
}
}
console.log(moves,move)
newBoard[availableSpots[i]] = move.index;
moves.push(move);
console.log(moves,move)
}
function botPicksSpot() {
return minimax(squareValues,computerSymbol).index;
}
function emptySquares() {
return squareValues.filter(el => el === '').map( (el,index) => index)
}
const checkGameStatus = (arr, player) => {
let gameWon = false;
for(let i = 0; i < arr.length; i += 3) {
if((arr[i] === arr[i+1]) && (arr[i] === arr[i+2]) && arr[i]) {
winner = arr[i].toUpperCase();
endRound()
if (player === computerSymbol) {
return true;
}
}
return gameWon;
}
for (let i = 0; i < (arr.length-6); i++) {
if((arr[i] === arr[i+3]) && (arr[i] === arr[i+6]) && arr[i]) {
winner = arr[i].toUpperCase();
endRound()
gameWon = {"index": i, "player": player};
break;
}
return gameWon;
}
if(((arr[0] === arr[4]) && (arr[0] === arr[8])) && arr[4] ||
((arr[2] === arr[4]) && (arr[2] === arr[6]) && arr[4])) {
winner = arr[4].toUpperCase();
endRound()
gameWon = {"index": 4, "player": player};
return gameWon;
}
if(winner === '' && !arr.includes('')) {
winner = 'None';
endRound();
}
if (winner === "" && arr.includes('')) {
return false;
}
return gameWon;
}
function assignSymbol() {
let xOrO = Math.floor(Math.random() * 2);
// console.log(xOrO);
if (xOrO === 0) {
// computer starts first;
computerSymbol = "x";
playerSymbol = "o";
// console.log(computerSymbol,playerSymbol)
computerTurn = true;
localStorage.setItem("isComputerTurn",computerTurn);
localStorage.setItem("playerSymbol",playerSymbol);
localStorage.setItem("computerSymbol",computerSymbol);
computerMove();
} else {
; // player starts first
computerSymbol = "o"
playerSymbol = "x";
// console.log(computerSymbol,playerSymbol)
computerTurn = false;
localStorage.setItem("playerSymbol",playerSymbol);
localStorage.setItem("computerSymbol",computerSymbol);
localStorage.setItem("isComputerTurn",computerTurn)
}
}
assignSymbol();
function computerMove() {
let botIndexPick = botPicksSpot();
// console.log(botPicksSpot());
console.log(squareValues);
computerTurn = !computerTurn
}
window.addEventListener("DOMContentLoaded", event => {
const gameBoard = document.getElementById("tic-tac-toe-board");
gameBoard.addEventListener("click", event => {
if (winner !== '' || computerTurn) {
return;
}
let id = event.target.id; // id of "square-0"
if (id.includes("square-")) {
let gridIndex = parseInt(id[id.length -1])
if (squareValues[gridIndex] === "") {
let selectedSquare = document.getElementById(id);
selectedSquare.innerHTML =
`<img src="https://assets.aaonline.io/Module-DOM-API/formative-project-tic-tac-toe/player-${playerSymbol}.svg">`
squareValues[gridIndex] = playerSymbol;
}
} else return;
localStorage.setItem("savedValues", JSON.stringify(squareValues));
giveUpButton.disabled = false;
// localStorage.setItem("isComputerTurn",computerTurn)
whosTurn();
checkGameStatus(squareValues);
computerTurn = !computerTurn
runBotAfterUser();
})
让playerSymbol='';
让计算机符号=“”;
让计算机转向;
设squareValues=[“x”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”];
对于(设i=0;i el=='').map((el,index)=>index)
}
const checkGameStatus=(arr,玩家)=>{
让gameWon=false;
对于(设i=0;i{
const gameBoard=document.getElementById(“tic-tac-toeboard”);
gameBoard.addEventListener(“单击”,事件=>{
如果(获胜者!=''||计算机回合){
返回;
}
let id=event.target.id;//“square-0”的id
如果(id.includes(“square-”)){
让gridIndex=parseInt(id[id.length-1])
if(平方值[gridIndex]=“”){
让selectedSquare=document.getElementById(id);
selectedSquare.innerHTML=
``
squareValues[gridIndex]=播放器符号;
}
}否则返回;
setItem(“savedValues”,JSON.stringify(squareValues));
giveUpButton.disabled=false;
//setItem(“isComputerTurn”,computerTurn)
whosurn();
检查游戏状态(平方值);
computerTurn=!computerTurn
runBotAfterUser();
})
它似乎每次只更新数组中的第一个元素,而不是递归地遍历可能的结果。我不明白的是,这基本上是从我启动tic tac toe computer vs human项目时读到的一个项目中获取的,只是我的大多数助手函数的结构不同并以不同的方式处理逻辑。minimax函数是我从另一个项目中获取的所有函数,它并不完全相同。我确实尝试将checkGameStatus函数更改为更像教程项目中的示例,但这没有帮助。我相信newBoard不会在每次递归时更新。它只是更改了第一个元素它没有选择要更改的其他元素。这是我的困惑,因为在项目教程中,它的工作方式与我在for循环中的工作方式完全相同
感谢您的帮助和理解请提供一个。为什么不提供您的帖子并提供
可用站点
、新板
、播放器
、极大极小值
、深度的值?请查看如何创建一个。当然控制台.table
和控制台.log
没有损坏;因此ch部分实际已损坏?删除除该部分以外的所有内容;包括重现问题所需的所有内容。“数组中的第一个元素” — 哪个阵列?“基本上取自我读到的一个项目” — 如果这真的是相关的,那么这个项目与你的有什么不同呢?我更新了我的评论,很抱歉我对这一切还是新手。我提供了所有相关的功能,我不担心在这个特定的时刻操纵HTML,我在另一个分支上工作,随机选择计算机。我严格担心的是放置将miniMax函数正确地组合在一起,然后我可以集中精力将各个部分拼接在一起。代码根本不起作用,请使用括号对进行检查。请提供一个。为什么不在您的帖子中提供可用的窗口,新手板,播放器,miniMax,以及深度,也请提供值查看如何创建。当然console.table
和console.log
没有损坏;那么,哪个部分实际上损坏了?删除除该部分之外的所有内容;包括重现问题所需的所有内容。“数组中的第一个元素” — 哪一个数组?“基本上是从我读过的一个项目中提取的