我使用javascript做了一个Tic Tac Toe游戏,我已经制作了一个a.i.,它在轮到它时选择了随机的盒子。但是,它可以选择相同的盒子两次,甚至可以选择玩家(X)选择的盒子。这是代码:
<!DOCTYPE html>
<html>
<body>
<input type="button" id="k1" value=" " onclick="tictactoe(this)">
<input type="button" id="k2" value=" " onclick="tictactoe(this)">
<input type="button" id="k3" value=" " onclick="tictactoe(this)">
<br />
<input type="button" id="k4" value=" " onclick="tictactoe(this)">
<input type="button" id="k5" value=" " onclick="tictactoe(this)">
<input type="button" id="k6" value=" " onclick="tictactoe(this)">
<br />
<input type="button" id="k7" value=" " onclick="tictactoe(this)">
<input type="button" id="k8" value=" " onclick="tictactoe(this)">
<input type="button" id="k9" value=" " onclick="tictactoe(this)">
<br />
<script>
var nummoves=0;
var nummoves1=1;
var nummoves2=2;
var nummoves3=3;
var nummoves4=4;
var nummoves5=5;
var nummoves6=6;
var nummoves7=7;
var nummoves8=8;
var nummoves9=9;
var comp;
function tictactoe(square)
{
var check=["c1","c2","c3","c4","c5","c6","c7","c8","c9"]
check[0]=document.getElementById("k1");
check[1]=document.getElementById("k2");
check[2]=document.getElementById("k3");
check[3]=document.getElementById("k4");
check[4]=document.getElementById("k5");
check[5]=document.getElementById("k6");
check[6]=document.getElementById("k7");
check[7]=document.getElementById("k8");
check[8]=document.getElementById("k9");
comp=check[Math.floor(Math.random()*check.length)];
if(nummoves==0)
{
square.value="X";
}
if(nummoves1==1)
{
comp.value="O";
}
if(nummoves2==2)
{
square.value="X";
}
if(nummoves3==3)
{
comp.value="O";
}
if(nummoves4==4)
{
square.value="X";
}
if(nummoves5==5)
{
comp.value="O";
}
if(nummoves6==6)
{
square.value="X";
}
if(nummoves7==7)
{
comp.value="O";
}
if(nummoves8==8)
{
square.value="X";
}
if(nummoves9==9)
{
comp.value="O";;
}
}
</script>
</body>
</html>
那么我将如何更改此代码(但不完全!),以便a.i.知道只选择一个空白框?
此外,它可以制作一个聪明的A.I.,例如它可以阻止玩家连续三次,甚至尝试连续三次。
答案 0 :(得分:0)
跟踪所做的标记(可能在数组中)并让脚本检查数组,以便在标记之前查看该点是否为空;如果它不是空的,脚本需要选择一个不同的位置。
或者添加此代替comp=check[Math.floor(Math.random()*check.length)]
:
var checking = true; // use this for the loop condition
while (checking)
{
comp = check[Math.floor(Math.random()*check.length)];
if (comp.value == " ") // check to see that the square is empty
{
checking = false; // if so, set the loop condition to false so the loop ends
}
}
答案 1 :(得分:0)
这很快就这样做了。唯一改变的是<script>
中的代码并删除 onclick 属性。阅读代码中的注释,看看会发生什么。
var squares = [], i = 10, move_number = 1;
while(--i) // build array of choices
squares[i-1] = document.getElementById('k'+i), // put square in array
squares[i-1].addEventListener('click', function () {tictactoe(this);}, false); // attach click listener
// `squares` keeps track of available squares
function randomSquare() { // A.I. square chooser
var i = Math.floor( Math.random() * squares.length ),
e = squares[i];
squares.splice(i,1); // remove choice from available
return e;
}
function tictactoe(square) { // passing `this` as first arg
var i;
square || (square = randomSquare()); // if no arg
if (move_number % 2) {
i = squares.indexOf(square);
if (i === -1) return; // square taken, do nothing
else squares.splice(i,1); // remove square from available
square.value="X";
++move_number;
tictactoe(); // A.I. turn
} else {
square.value="O";
++move_number;
}
}