gpt4 book ai didi

javascript - 使用 for 循环语句的结果作为 else if 语句中的条件

转载 作者:行者123 更新时间:2023-12-01 03:09:59 25 4
gpt4 key购买 nike

我的第三个else if语句条件应该是for循环的结果。如何使用条件循环?

        //If player clicks centre on first move go in corner square
if (current[4] === playerToken && this.state.stepNumber === 1) {
let move = cornerSquares[Math.floor(Math.random() * cornerSquares.length)];
drawSquare(move);
}
//If player clicks corner square on first move go in centre square
else if (this.state.stepNumber === 1) {
for (let i = 0; i < cornerSquares.length; i++){
if (current[cornerSquares[i]] === playerToken) {
drawSquare(4);
}
}
}
//If player or computer has 2 in a row, place in 3rd square to win or block
else if (/*CONDITION OF THE BELOW FOR LOOP*/) {
for (let i = 0; i < twoInRow.length; i++) {
const [a, b, c] = twoInRow[i];
if (current[a] && current[a] === current[b]) {
drawSquare(c);
}
}
}
//Place in random empty square
else {
//code to randomly place x/o in random square
}
}

最佳答案

您可以使用 move 变量来指示您在何处找到了移动或尚未找到移动,并在下一个 if 中使用该变量(没有 else):

let move = null;
//If player clicks centre on first move go in corner square
if (current[4] === playerToken && this.state.stepNumber === 1) {
let move = cornerSquares[Math.floor(Math.random() * cornerSquares.length)];
}
//If player clicks corner square on first move go in centre square
else if (this.state.stepNumber === 1) {
for (let i = 0; i < cornerSquares.length; i++){
if (current[cornerSquares[i]] === playerToken) {
move = 4;
break; // Don't lose time on looping further
}
}
}
//If player or computer has 2 in a row, place in 3rd square to win or block
if (move === null) {
for (let i = 0; i < twoInRow.length; i++) {
const [a, b, c] = twoInRow[i];
if (current[a] && current[a] === current[b]) {
move = c;
break; // Don't lose time on looping further
}
}
}
//Place in random empty square
if (move === null {
//code to randomly place x/o in random square
move = // your logic here
}
// Perform move
drawSquare(move);

关于javascript - 使用 for 循环语句的结果作为 else if 语句中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45914535/

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