Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript计算机AI_Javascript_Jquery_Artificial Intelligence - Fatal编程技术网

JavaScript计算机AI

JavaScript计算机AI,javascript,jquery,artificial-intelligence,Javascript,Jquery,Artificial Intelligence,我有一个用JavaScript构建的tic-tac-toe游戏。我已经为函数中的计算机AI部分编写了逻辑。我想调用代码上部的函数,但它不起作用。有人对如何实现代码的Ai功能有什么建议吗?我想成功,所以很难赢。有什么问题需要帮助吗 多谢各位 这是我的JSFIDLE链接: HTML: <body> <div id="container"> <header> <h1>Tic Tac Toe</h1> </

我有一个用JavaScript构建的tic-tac-toe游戏。我已经为函数中的计算机AI部分编写了逻辑。我想调用代码上部的函数,但它不起作用。有人对如何实现代码的Ai功能有什么建议吗?我想成功,所以很难赢。有什么问题需要帮助吗

多谢各位

这是我的JSFIDLE链接:

HTML:

<body>
<div id="container">
    <header>
        <h1>Tic Tac Toe</h1>
    </header>
    <ul id="board">
        <li id="spot1"></li>
        <li id="spot2"></li>
        <li id="spot3"></li>
        <li id="spot4"></li>
        <li id="spot5"></li>
        <li id="spot6"></li>
        <li id="spot7"></li>
        <li id="spot8"></li>
        <li id="spot9"></li>
    </ul>
    <div class="clearfix"></div>
    <footer id="reset">Reset</footer>
</div>
</body>

这里有一个固定版本:

我根据我的评论修改了轮次,还修复了
*。text
调用

AI仍然有一些错误(有时不玩,没有正确防守(玩1,3,2总是赢),没有做赢的动作,等等),但我相信你会发现的



如果你真的想让玩家难受,我建议你放弃人工智能,写一个递归检查,在任何情况下都能找出最好的动作。正如我在第一条评论中所说,游戏很简单,所以即使是今天最慢的机器,如果写得好,也会以纳秒计算。这样的话,人类就只能画一张了。

Tic-tac-toe非常简单,因此很容易使用暴力。有很多关于通用AI权重的文章,将其实现到tic-tac-toe中应该很容易。一旦你能对这台机器做出的最好成绩是平局,你的AI就很好了。@Shomz谢谢你。我已经创建了AI部件。它在一个函数中。但是,该函数没有运行。@mellamokb,这就是我试图解释的。我不知道你为什么要叫我投反对票,因为这是一个合法的问题。。。需要更好的解释,但没关系。在
xWins()
之后执行一个简单的
turn++
将导致计算机每次都紧跟在播放器之后移动-或者干脆跳过检查,因为它始终是播放器移动+单击计算机移动。
O
移动未显示,因为您需要检查
spot#.text()
,而不是
spot#.text
<代码>文本是一个函数。@FNunez,不客气。哦,我明白了,问题是你的代码正在等待人类玩家和计算机玩家的点击,而它应该只等待人类玩家的点击,然后检查人类胜利,如果不玩计算机移动并检查计算机胜利。谢谢,这很有帮助。我发现我的Ai函数有很多语法错误。我将尝试循环进行递归检查。我知道有很多关于这个话题的文章。我还在学习,所以将算法翻译成JavaScript代码对我来说仍然很棘手。是的,想一个方法来估计某个“位置”有多好-例如,如果我能在这一步中获胜,它是100%好;如果AI在我的移动后能赢,它是100%;如果我有可能在两步中获胜,那么,比如说,25%的优势,等等。。。从那里AI只需要对每一个可能的移动的值求和,然后玩最好的(最高值的)一个。
$(document).ready(function() {
    var x = "x";
    var o = "o";
    var turns = 0;

// spots stored in variables 
    var spot1 = $("#spot1");
    var spot2 = $("#spot2");
    var spot3 = $("#spot3");
    var spot4 = $("#spot4");
    var spot5 = $("#spot5");
    var spot6 = $("#spot6");
    var spot7 = $("#spot7");
    var spot8 = $("#spot8");
    var spot9 = $("#spot9");

// function for resetting the board
    function remove() {
            $("#board li").text("");
            $("#board li").removeClass("disable");
            $("#board li").removeClass("o");
            $("#board li").removeClass("x");
            turns = 0;
    }

// Board click 
    $("#board li").on("click", function() {
        console.log(turns);

        if(turns % 2 === 0) {
            $(this).text(x);
            $(this).addClass('disable x');
        } 
        xWins();

        if(turns % 2 !== 0) {
            compCheck();
        } 
        oWins();
        turns ++;
        draw();
    });


    //Checking for Wins

    function xWins() {
        if(spot1.hasClass(x) && spot2.hasClass(x) && spot3.hasClass(x) || 
            spot1.hasClass(x) && spot4.hasClass(x) && spot7.hasClass(x) ||
            spot1.hasClass(x) && spot5.hasClass(x) && spot9.hasClass(x) ||
            spot2.hasClass(x) && spot5.hasClass(x) && spot8.hasClass(x) ||
            spot3.hasClass(x) && spot5.hasClass(x) && spot7.hasClass(x) ||
            spot3.hasClass(x) && spot6.hasClass(x) && spot9.hasClass(x) ||
            spot4.hasClass(x) && spot5.hasClass(x) && spot6.hasClass(x) ||
            spot7.hasClass(x) && spot8.hasClass(x) && spot9.hasClass(x)
            )   {
                alert("Winner is X");
                remove();           
                }
    }

        function oWins() {
        if(spot1.hasClass(o) && spot2.hasClass(o) && spot3.hasClass(o) || 
            spot1.hasClass(o) && spot4.hasClass(o) && spot7.hasClass(o) ||
            spot1.hasClass(o) && spot5.hasClass(o) && spot9.hasClass(o) ||
            spot2.hasClass(o) && spot5.hasClass(o) && spot8.hasClass(o) ||
            spot3.hasClass(o) && spot5.hasClass(o) && spot7.hasClass(o) ||
            spot3.hasClass(o) && spot6.hasClass(o) && spot9.hasClass(o) ||
            spot4.hasClass(o) && spot5.hasClass(o) && spot6.hasClass(o) ||
            spot7.hasClass(o) && spot8.hasClass(o) && spot9.hasClass(o)
            )   {
                alert("Winner is O");
                remove();           
                }
    }

    //Checks for tie 
    function draw() {
        while(turns == 9) {
            alert("It's a tie !");
            remove();
        }
    };

    //Reset button function
        $("#reset").on("click", function() {
            remove();
        });


// Computer AI MOVES 

function compCheck() {
    if (spot1.text == "" && ((spot3.text == "x" && spot2 == "x") || (spot9 == "x" && spot5 == "x") || (spot7 == "x" && spot4 == "x"))) {
        spot1.text(o);
        spot1.addClass('disable o');
        turns++;
    } else {
      if (spot1.text == "" && ((spot1.text == "x" && spot3.text == "x") || (spot8.text == "x" && spot5 == "x"))) {
        spot2.text(o);
        spot2.addClass('disable o');
        turns++; 
        }
        else{
        if (spot3.text == "" && ((spot1.text == "x" && spot2.text == "x") || (spot7.text == "x" && spot5.text == "x") || (spot9.text == "x" && spot6.text == "x"))) {
            spot3.text(o);
            spot3.addClass('disable o');
            turns++;
        }
            else{
            if (spot9.text == "" && ((spot7.text == "x" && spot8.text == "x") || (spot1.text == "x" && spot5.text == "x") || (spot3.text == "x" && spot6.text == "x"))) {
                spot9.text(o);
                spot9.addClass('disable o');
                turns++;
        }
                else{
                if (spot7.text == "" && ((spot9.text == "x" && spot8.text == "x") || (spot3.text == "x" && spot2.text == "x") || (spot1.text == "x" && spot4.text == "x"))) {
                    spot7.text(o);
                    spot7.addClass('disable o');
                    turns++;
        }
                    else{
                    if (spot8.text == "" && ((spot9.text == "x" && spot7 == "x") || (spot2.text == "x" && spot5.text == "x"))) {
                        spot8.text(o);
                        spot8.addClass('disable o');
                        turns++;
        }
                        else{
                        if (spot4.text == "" && ((spot6.text == "x" && spot5.text == "x") || (spot1.text == "x" && spot7.text == "x"))) {
                            spot4.text(o);
                            spot4.addClass('disable o');
                            turns++;
        }
                            else{
                            if (spot6.text == "" && ((spot3.text == "x" && spot9.text == "x") || (spot5.text == "x" && spot4.text == "x"))) {
                                spot4.text(o);
                                spot4.addClass('disable o');
                                turns++;
        }
                                else{
                                if (spot5.text == "" && ((spot3.text == "x" && spot7.text == "x") || (spot9.text == "x" && spot1.text == "x") || (spot6.text == "x" && spot4.text == "x") || (spot8.text == "x" && spot2.text == "x"))) {
                                    spot4.text(o);
                                    spot4.addClass('disable o');
                                    turns++;
        }
                                   else{ // if no spot to block then play these spots....
                                    if (spot5.text == "") {
                                        spot5.text(o);
                                        spot5.addClass('disable o');
                                        turns++;

                                    }
                                        else{
                                        if (spot1.text == "") {
                                            spot1.text(o);
                                            spot1.addClass('disable o');
                                            turns++;

                                    }
                                            else{
                                            if (spot9.text == "") {
                                                spot9.text(o);
                                                spot9.addClass('disable o');
                                                turns++;

                                    } 
                                                else {
                                                if (spot8.text == "") {
                                                    spot8.text(o);
                                                    spot8.addClass('disable o');
                                                    turns++;

                                    }
                                                    else{
                                                    if (spot4.text == "") {
                                                        spot4.text(o);
                                                        spot4.addClass('disable o');
                                                        turns++;

                                    }
                                                    }
                                                }
                                            }


                                        }
                                   }
                                }
                            }
                        }
                    }
                }
            }
        }
    }   
    }; 
    });