将代码放入另一个函数的重构是否会影响我的Javascript Tic-Tac-Toe中的计时?

将代码放入另一个函数的重构是否会影响我的Javascript Tic-Tac-Toe中的计时?,javascript,scope,return,refactoring,tic-tac-toe,Javascript,Scope,Return,Refactoring,Tic Tac Toe,对于下面的Tictaoe游戏,一切都很好,但在同一代码块中,我使用了以下两行代码: if (playerhasWon()) { declareWinner() return } if (emptySpaceRemains() == false) { declareTie() return } 我想重构并调用这两个if语句,函数hasGameEnded 然而,如果使用H

对于下面的Tictaoe游戏,一切都很好,但在同一代码块中,我使用了以下两行代码:

      if (playerhasWon()) {
        declareWinner()
        return
      } 
      
      if (emptySpaceRemains() == false) {
        declareTie()
        return
      }
我想重构并调用这两个if语句,函数hasGameEnded

然而,如果使用HasGameEnd,我的代码会发生一件奇怪的事情

玩家赢得游戏后,计算机不尊重应该停止游戏的回报

相反,它在declareWinner中发送警报,然后还运行下一个函数swapTurns并让AI播放其移动

如果我在2IF语句中离开,它工作正常。为什么会这样

将PlayerHaswan功能放在另一个区块是否影响了我的回击?它是否延迟了发射,以至于计算机没有意识到游戏赢了,仍然在玩它的游戏

var ONE_类 二类变量 const btn=document.querySelector'playerSymbol'; btn.onclick=函数{ const XOs=document.queryselectoral'input[name=choice]; 对于XOs的常数XO{ 如果XO.checked{ 一类=XO.value 二类=XO.value='X'?'O':'X' 打破 } } 第一步属于+1职业+。选择玩家2。; }; 变量playerTwoIdentity const btn2=document.querySelector'PlayerTwoChoice'; btn2.onclick=函数{ const=document.queryselectoral'input[name=choice2]; 为对手的康斯特对手{ 如果对手被选中{ playerTwoIdentity=对手.value 打破 } } 提醒你的对手是+玩家WOIdentity+。开始新游戏。 }; 让我们转身 函数交换{ playeronturn=!playeronturn }; 常数winningTrios=[ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [6, 4, 2] ] restartbn.addEventListener'click',startName; 函数起始名{ 如果一个_类==未定义| | playerTwoIdentity==未定义{ 返回警报确保定义了玩家 } console.logplayer 1=+ONE_CLASS+,player 2=+playerTwoIdentity 绞车; 反过来=真; } const arrayfromBoxes=Array.fromdocument.getElementsByClassName'box'; const stylengofboxes=document.queryselectoral.box'; 多功能绞车{ console.LogStylengOfBox 对于let i=0;i{ gridBox.classList.removeONE\u类 gridBox.classList.removeTWO_类 gridBox.classList.删除“winner” gridBox.innerHTML= } } 函数boxmarkede{ 常量索引=arrayfromBoxes.indexOfe.target //如何巩固?也许我只是让一个职业标记,然后如果AI或玩家 //或者更早地将其与playerTurn链接? 如果你转身{ ArrayFrombox[index].classList.addONE\u类 e、 target.innerHTML=一个类 }否则{ arrayfromBoxes[index].classList.addTWO\u类 e、 target.innerHTML=TWO\u类 } //如果玩家赢了{ //庄家 //返回 // } //如果EmptySpaceResistence==false{ //声明 //返回 // } 哈斯加梅德 天鹅座 //消除重复- 如果playerTwoIdentity==哑AI{ var dumbAIArray=arrayfromBoxes.reducedumbAIArray,box,idx=>{ 如果box.innerHTML=={ dumbAIArray.pushidx; } 返回Dumbairray; }, []; 让dumbAIpicked=dumbAIArray[Math.floordumbairray.length*Math.random] arrayfromBoxes[dumbaipick].classList.addTWO\u类 ArrayFrombox[dumbaipick].innerHTML=TWO\u类 //为什么Timeoutfunction会阻止对手序列? //setTimeout=>{arrayfromBoxes[Dumbaipick].classList.addTWO_CLASS},500; //setTimeout=>{arrayfromBoxes[dumbaipick].innerHTML=TWO_CLASS},500; //如果玩家赢了{ //庄家 //返回 // } //如果EmptySpaceResistence==false{ //声明 //返回 // } 哈斯加梅德 天鹅座 }否则{ 控制台:logHuman } } 函数已结束{ //fix declareWinner在添加的类bc警报比重画更快发生之前出现 //我也不能拔出这些,因为这样对手就会移动火力和表演 //可能与块代码中的时间有关 如果玩家赢了{ 庄家 回来 } 如果EmptySpaceResistence==false{ 声明 回来 } } 函数检查类{ 如果你转身{ 返回一个_类 }否则{ 返回二级 }; } 功能空白{ var innerHTMLempty=insidebox=>insidebox.innerHTML== console.logarrayfromBoxes.someinnerHTMLempty 返回ArrayFrombox.someinnerHTMLempty } 功能声明{ setTimeoutalertTIE游戏,1000 } 功能播放器哈斯旺{ var indexOfSelected=arrayfromBoxes.reduceindexOfSelected,box,idx=>{ if box.classList[1]==checkClass{ indexOfSelected.pushidx; } 返回指数选择; }, []; 常数winningThreeIndexes=winningTrios .maptrio=>trio.filteri=>indexOfSelected.includesi .filteri=>i.length==3; console.LogWinningThreeIndex console.logwinningthreeindex.length 如果winningThreeIndexes.length==1{ WinningThreeIndex[0]。mapindex=>{ arrayfromBoxes[索引].className+=“获胜者” } } var isThereAWinner= winningTrios.sometrio=>{ return trio.everyi=>indexOfSelected.includesi }; console.log{ 这是内在的吗 }; 回程在这里 } 函数声明器{ setTimeoutalertcheckClass+赢,1000; 对于let i=0;i函数中的return表示结束该函数,但不是在调用者中结束。您需要返回一些值作为状态或抛出错误以停止

例如:

函数已结束{ //fix declareWinner在添加的类bc警报比重画更快发生之前出现 //我也不能拔出这些,因为这样对手就会移动火力和表演 //可能与块代码中的时间有关 如果玩家赢了{ 庄家 返回真值 } 如果EmptySpaceResistence==false{ 声明 返回真值 } 返回错误 } 函数boxmarkede{ ... 如果游戏结束了{ //比赛以某人获胜或平局结束 //…做点什么来阻止 }否则{ 天鹅座 } }
函数中的return表示函数的结束,但不表示调用者的结束。您需要返回一些值作为状态来检查或抛出错误来停止它。啊,谢谢,我理解其中的一部分,但不是全部!如果函数中的return意味着结束该函数,那么这可以解释为什么当我取出返回并将其放入新函数function hasGameEnded时,函数boxmarkede没有结束。我需要了解返回值作为状态的含义。