在功能“ 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>
当我单击机器人时,必须选择我没有单击的其他单元并将其涂成红色。当所有单元都绘制完毕后,发出警告“它起作用了!”。应该出现,并且consol中的随机数必须停止生成。
答案 0 :(得分:0)
没什么,评论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>