当前位置: 代码迷 >> JavaScript >> 如何在其他函数中使用函数?
  详细解决方案

如何在其他函数中使用函数?

热度:102   发布时间:2023-06-03 18:13:48.0

我不知道如何在函数 'player_turn' 之后使函数 'bot_turn' 工作

也许两个'while'函数存在一些问题

 var asasa = 0; var lal = -1; var end_game = 0; var asass = 0 asasa = 0; function player_turn() { if (document.getElementById('tables1').className == 'one' & asasa == 1) { document.getElementById('tables1').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables2').className == 'two' && asasa == 2) { document.getElementById('tables2').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables3').className == 'three' && asasa == 3) { document.getElementById('tables3').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables4').className == 'four' && asasa == 4) { document.getElementById('tables4').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables5').className == 'five' && asasa == 5) { document.getElementById('tables5').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables6').className == 'six' && asasa == 6) { document.getElementById('tables6').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables7').className == 'seven' && asasa == 7) { document.getElementById('tables7').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables8').className == 'eight' && asasa == 8) { document.getElementById('tables8').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables9').className == 'nine' && asasa == 9) { document.getElementById('tables9').className = 'none' alert("Bot turn") end_game = end_game + 1; bot_turn() } console.log(asasa) } var randomik = 0; function bot_turn() { while (asass = 0) { lal = 0 while (lal = 0) { randomik = Math.floor((Math.random() * 9) + 1); console.log(randomik) if (randomik == 1) { if (document.getElementById('tables1').className == 'one') { document.getElementById('tables1').className = 'none' document.getElementById('tables1').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1 end_game += 1 } } else if (randomik == 2) { if (document.getElementById('tables2').className == 'two') { document.getElementById('tables2').className = 'none' document.getElementById('tables2').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (randomik == 3) { if (document.getElementById('tables3').className == 'three') { document.getElementById('tables3').className = 'none' document.getElementById('tables3').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (randomik == 4) { if (document.getElementById('tables4').className == 'four') { document.getElementById('tables4').className = 'none' document.getElementById('tables4').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (randomik == 5) { if (document.getElementById('tables5').className == 'five') { document.getElementById('tables5').className = 'none' document.getElementById('tables5').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (randomik == 6) { if (document.getElementById('tables6').className == 'six') { document.getElementById('tables6').className = 'none' document.getElementById('tables6').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (randomik == 7) { if (document.getElementById('tables7').className == 'seven') { document.getElementById('tables7').className = 'none' document.getElementById('tables7').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (randomik == 8) { if (document.getElementById('tables8').className == 'eight') { document.getElementById('tables8').className = 'none' document.getElementById('tables8').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (randomik == 9) { if (document.getElementById('tables9').className == 'nine') { document.getElementById('tables9').className = 'none' document.getElementById('tables9').style.backgroundColor = '#FF2400' alert("Player turn") lal = 1; end_game += 1 } } else if (end_game == 9) { asass = 1 alert('It works!') } } } }
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tic Tac Toe</title> </head> <body> <table border='1px' width='500px' height='500px' align="center"> <tr> <td id='tables1' class="one" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=1; player_turn(); "></td> <td id='tables2' class="two" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=2; player_turn();"></td> <td id='tables3' class="three" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=3; player_turn();"></td> </tr> <tr style='border-color: red'> <td id='tables4' class="four" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=4; player_turn(); "></td> <td id='tables5' class="five" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=5; player_turn();"></td> <td id='tables6' class="six" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=6; player_turn();"></td> </tr> <tr style='border-color: red'> <td id='tables7' class="seven" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=7; player_turn();"></td> <td id='tables8' class="eight" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=8; player_turn();"></td> <td id='tables9' class="nine" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=9; player_turn();"></td> </tr> </table> </body>

当我单击 bot 时,必须选择另一个我没有单击的单元格并将其涂成红色。 当所有单元格都被绘制时发出警报“它有效!” 应该出现并且 consol 中的随机数必须停止生成。

很少有事情,首先在评论中提到asass=0是赋值操作而不是比较操作,它应该是asass==0

通过bot_turn()函数中的外部 while 循环并将内部 while 循环更改为 if 语句,我得到了一个工作版本。 唯一的问题是当机器人选择一个已经被选择的数字时,在这种情况下机器人永远不会执行他的回合,所以你需要添加一个数组来存储选择的数字并确保机器人选择一个不在该数组中的数字.

下面是一个工作片段(删除了警报)。

 var asasa = 0; var lal = -1; var end_game = 0; var asass = 0 asasa = 0; guessArr = [] function player_turn() { if (end_game > 8) { asass = 0 alert('It works!') return; } if (guessArr.includes(asasa)) { player_turn() console.log("you already guessed this") } else { guessArr.push(asasa) if (document.getElementById('tables1').className == 'one' & asasa == 1) { document.getElementById('tables1').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables2').className == 'two' && asasa == 2) { document.getElementById('tables2').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables3').className == 'three' && asasa == 3) { document.getElementById('tables3').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables4').className == 'four' && asasa == 4) { document.getElementById('tables4').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables5').className == 'five' && asasa == 5) { document.getElementById('tables5').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables6').className == 'six' && asasa == 6) { document.getElementById('tables6').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables7').className == 'seven' && asasa == 7) { document.getElementById('tables7').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables8').className == 'eight' && asasa == 8) { document.getElementById('tables8').className = 'none' end_game = end_game + 1; bot_turn() } else if (document.getElementById('tables9').className == 'nine' && asasa == 9) { document.getElementById('tables9').className = 'none' end_game = end_game + 1; bot_turn() } console.log(asasa) } } var randomik = 0; function bot_turn() { if (end_game > 8) { asass = 0 alert('It works!') return; } lal = 0 if (lal == 0) { randomik = Math.floor((Math.random() * 9) + 1); if (guessArr.includes(randomik)) { bot_turn() console.log("you already guessed this") } else { guessArr.push(randomik) console.log(randomik) if (randomik == 1) { if (document.getElementById('tables1').className == 'one') { document.getElementById('tables1').className = 'none' document.getElementById('tables1').style.backgroundColor = '#FF2400' lal = 1 end_game += 1 } } else if (randomik == 2) { if (document.getElementById('tables2').className == 'two') { document.getElementById('tables2').className = 'none' document.getElementById('tables2').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } else if (randomik == 3) { if (document.getElementById('tables3').className == 'three') { document.getElementById('tables3').className = 'none' document.getElementById('tables3').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } else if (randomik == 4) { if (document.getElementById('tables4').className == 'four') { document.getElementById('tables4').className = 'none' document.getElementById('tables4').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } else if (randomik == 5) { if (document.getElementById('tables5').className == 'five') { document.getElementById('tables5').className = 'none' document.getElementById('tables5').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } else if (randomik == 6) { if (document.getElementById('tables6').className == 'six') { document.getElementById('tables6').className = 'none' document.getElementById('tables6').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } else if (randomik == 7) { if (document.getElementById('tables7').className == 'seven') { document.getElementById('tables7').className = 'none' document.getElementById('tables7').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } else if (randomik == 8) { if (document.getElementById('tables8').className == 'eight') { document.getElementById('tables8').className = 'none' document.getElementById('tables8').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } else if (randomik == 9) { if (document.getElementById('tables9').className == 'nine') { document.getElementById('tables9').className = 'none' document.getElementById('tables9').style.backgroundColor = '#FF2400' lal = 1; end_game += 1 } } } } }
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tic Tac Toe</title> </head> <body> <table border='1px' width='500px' height='500px' align="center"> <tr> <td id='tables1' class="one" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=1; player_turn(); "></td> <td id='tables2' class="two" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=2; player_turn();"></td> <td id='tables3' class="three" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=3; player_turn();"></td> </tr> <tr style='border-color: red'> <td id='tables4' class="four" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=4; player_turn(); "></td> <td id='tables5' class="five" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=5; player_turn();"></td> <td id='tables6' class="six" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=6; player_turn();"></td> </tr> <tr style='border-color: red'> <td id='tables7' class="seven" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=7; player_turn();"></td> <td id='tables8' class="eight" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=8; player_turn();"></td> <td id='tables9' class="nine" style="font-size: 50px; text-align:center;" onclick="this.style.backgroundColor = '#045d99';asasa=9; player_turn();"></td> </tr> </table> </body>

  相关解决方案