-3

我使用 javascript 制作了一个井字游戏,并且我制作了一个 ai,它轮流选择随机框。但是,它可能会选择同一个框两次,甚至是玩家(即 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> 

那么我将如何更改此代码(但不是全部!),以便 ai 只知道选择一个空白框?

此外,可以制作一个聪明的AI,例如它可以阻止玩家连续获得三个,或者甚至尝试自己获得三个连续。

4

2 回答 2

0

很快就做到了。唯一改变的是onclick属性中的代码<script>和删除。阅读代码中的注释,看看会发生什么。

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;
    }
}

示例小提琴

于 2013-01-02T17:23:39.753 回答
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
    }
}
于 2013-01-02T17:11:07.550 回答