1

三目並べゲームを作成していますが、行き詰まっています。私はあなたの後に動くAIを作ったのですが、それはすべて少し混乱しています。自分で試して、何が起こるかを確認してください。誰かがそれを改善して、それをどのように行ったかを説明できるかどうかを見て、見ることができますか?そして、物事を簡単にするために、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

Tic-Tac-Toe の HTML および JS/JQuery 実装のjsFiddleリンク。
現在、対戦相手としてコンピュータを使用しない 2 人用の実装のみです。その上に構築できることを願っています。

<!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 に答える
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;
}

それは効率的ではありませんが、あなたが求めたように機能します。とにかく、埋めるマスがまだ残っているかどうかを確認する必要があります。

また、非常に単純な「the」tic tac toe 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 に答える