1

我正在制作井字游戏,但我被卡住了。我制作了一个跟随你移动的人工智能,但它有点搞砸了。自己尝试一下,看看会发生什么。任何人都可以看看他们是否能够改进它并解释他们是如何做到的?为了简单起见,我怎样才能让 AI 选择尚未选择的任何框。这是代码:

<!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)">
    <script>
        var Xturn = true;
        var nummoves = 0;
        var cat;
        function tictactoe(square) {
            var value = square.value;
            var doc1 = document.getElementById("k1").value;
            var doc2 = document.getElementById("k2").value;
            var doc3 = document.getElementById("k3").value;
            var doc4 = document.getElementById("k4").value;
            var doc5 = document.getElementById("k5").value;
            var doc6 = document.getElementById("k6").value;
            var doc7 = document.getElementById("k7").value;
            var doc8 = document.getElementById("k8").value;
            var doc9 = document.getElementById("k9").value;

            for (nummoves = 0; nummoves < 2; nummoves++) {

                if (doc1 == "X") {
                    cat = document.getElementById("k2").value = "O";
                    Xturn = true;
                }

                if (doc2 = "X") {
                    cat = document.getElementById("k4").value = "O";
                    Xturn = true;
                }

                if (doc3 == "X") {
                    cat = document.getElementById("k5").value = "O";
                    Xturn = true;
                }

                if (doc4 == "X") {
                    car = document.getElementById("k9").value = "O";
                }
            }

            for (nummoves = 2; nummoves < 3; nummoves++) {

                if (doc1 == "X") {
                    cat = document.getElementById("k7").value = "O";
                    Xturn = true;
                }

            }

            if (value != "X" && value != "O") {
                if (Xturn == true) {
                    square.value = "X";
                    return Xturn = false;
                    nummoves++;
                } else if (Xturn == false) {
                    square.value = "O";
                    return Xturn = true;
                    nummoves++;
                }
            } else {
                alert("That square has been clicked.");
            }
        }
    </script>
</body>
</html>

请注意,我承认整个概念不是我的,但我做了有点混乱的 AO 部分。

4

4 回答 4

1

跟踪一个开放方块列表,然后从该列表中随机选择。

这样你就可以消除循环。

于 2012-11-01T15:41:11.363 回答
0

您可以迭代考虑“按钮”并根据随机选择未检查的第一个或另一个。

for(i=1;i<10;i++) {
   if (document.getElementById('k'+i).value = ' ') {
       // not played yet !
   }
}
于 2012-11-01T15:27:38.270 回答
0

考虑以下逻辑:

// function that does an AI move
function doAIMove(xOrO) {
    // randomly gets a number from 1 to 9
    var randomSquare = document.getElementById("k" + getRandomInt(1, 9));
    while (randomSquare.value != " ") {
        randomSquare = document.getElementById("k" + getRandomInt(1, 9));
    }

    randomSquare.value(xOrO);
}


function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

这效率不高,但它可以按照您的要求工作。无论如何,您需要检查是否还有剩余的方块要填写。

您还应该考虑实现非常简单的“井字游戏”AI。你应该遵循这个伪算法:

在制作类似井字游戏的游戏时,AI 应该像这样工作:

1. Check if there is a tile that you can win in 1 move
    if there is no such tile:
2. Check if there is a tile that your opponent can win in 1 move
    if there is no such tile:
3. Check if there is a tile that can make two tiles apply to the rule #1
    if there is no such tile:
4. Check if there is a tile that your opponent can make two tiles apply to the rule #2
    if there is no such tile:
5. implement your own AI form this point
于 2012-11-01T15:36:52.560 回答
0

用于井字游戏的 HTML 和 JS/JQuery 实现的jsFiddle链接。
目前它只有一个没有计算机作为对手的两个玩家实现。希望您能在此基础上进行构建。

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo by bhatkrishnakishor</title>

  <script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>



  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>
    .tictactoe {
    width: 125px;
    height: 125px;
    background: #A2A8A1;
};
  </style>



<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
//this is a demo tic tac toe game
$(document).ready($('input.tictactoe').click(tictactoe));
$(document).ready($('#ff').click(reset));

var whoseMove = 'X';
var xMove = new Array();
var oMove = new Array();
var gameOver = false;
var winningConditions = new Array(     'aa/ab/ac','ba/bb/bc','ca/cb/cc','aa/ba/ca','ab/bb/cb','ac/bc/cc','aa/bb/cc','ac/bb/ca');
var whoWon = '';

function tictactoe() {
    if(gameOver == false && this.value == '  '){
        if(whoseMove == 'X'){
            this.value = whoseMove;
            xMove[xMove.length] = this.id;
            whoseMove = 'O';
        }
        else {
            this.value = whoseMove;
            oMove[oMove.length] = this.id;
            whoseMove = 'X';
        }
    }


    if(xMove.length >2){
        whoWon = endGame();
    }

    if(gameOver && whoWon != '' && whoWon != 'draw') {
        alert(whoWon + ' won!')
    }

    if(!gameOver && whoWon == 'draw') {
        alert('Games been draw!');
    }    
}

function endGame() {
    var winningCombinations = new Array();

    //set this variable value to true incase the game is over
    gameOver = true;    

    for(var index = 0; index < 8; index = index + 1){
        var xMatchCount = 0;
        var oMatchCount = 0;
        winningCombinations = winningConditions[index].split('/');
        for(var i = 0; i < 3; i = i + 1){
        console.log('winningCombinations ' + winningCombinations[i]);
        for(var j = 0; j < xMove.length; j = j + 1){
            console.log('xMove ' + xMove[j]);
            if(winningCombinations[i] == xMove[j]){
                xMatchCount = xMatchCount + 1;
                if(xMatchCount == 3){
                    return 'X';
                }
            }
        }
        for(var k = 0; k < oMove.length; k = k + 1){
            //console.log('oMove ' + oMove[k]);
            if(winningCombinations[i] == oMove[k]){
                oMatchCount = oMatchCount + 1;
                if(oMatchCount == 3){
                    return 'O';
                }
            }                
                }
        }
    }

    console.log('x Move Count ' + xMove.length);
    console.log('o Move Count ' + oMove.length);

    if(xMatchCount < 3 && oMatchCount < 3){
        gameOver = false;
    } 

    if(xMove.length + oMove.length == 9){
        return 'draw';
    }
}

function reset() {

    console.log('Xs Move - ' + xMove.join('/'));
    console.log('Os Move - ' + oMove.join('/'));
    console.log(winningConditions.length);

    whoseMove = 'X';
    xMove = new Array();
    oMove = new Array();
    gameOver = false;
    whoWon = '';

    $('input').filter(function() {
    if(this.id != 'ff') {
        this.value = '  ';
        }
    });
}
});//]]>  

</script>


</head>
<body>
      <input type="button" id="aa" class="tictactoe" value="  ">
    <input type="button" id="ab" class="tictactoe" value="  ">
    <input type="button" id="ac" class="tictactoe" value="  ">
    <br />
    <input type="button" id="ba" class="tictactoe" value="  ">
    <input type="button" id="bb" class="tictactoe" value="  ">
    <input type="button" id="bc" class="tictactoe" value="  ">
    <br />
    <input type="button" id="ca" class="tictactoe" value="  ">
    <input type="button" id="cb" class="tictactoe" value="  ">
    <input type="button" id="cc" class="tictactoe" value="  ">
    <br /><br />
    <input type="button" id="ff" value="Reset">

</body>

于 2013-02-27T14:06:22.373 回答