Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.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 我可以使用2D数组在JS中创建Tic-Tac-Toe AI吗?_Javascript_Arrays_Multidimensional Array_Artificial Intelligence - Fatal编程技术网

Javascript 我可以使用2D数组在JS中创建Tic-Tac-Toe AI吗?

Javascript 我可以使用2D数组在JS中创建Tic-Tac-Toe AI吗?,javascript,arrays,multidimensional-array,artificial-intelligence,Javascript,Arrays,Multidimensional Array,Artificial Intelligence,我正在使用HTML和JS创建一个tic-tac-toe游戏。我将任意大小的电路板(宽度和高度相同)存储在2d阵列中。又名存储数据行的数组。这些行中的数据是列,因此可以使用array[row][column]访问数组中的位置。我正在尝试创建一个机器人,它将尝试与玩家对抗。我在考虑用尽可能少的人类棋子对半空的行/列/对角线进行暴力强迫。我还了解到minmax机器人可以更好地利用资源。但我在JS中找不到任何东西,也找不到任何可以转换为JS的东西,因为我见过的大多数其他机器人都使用带有9个元素的1d数组

我正在使用HTML和JS创建一个tic-tac-toe游戏。我将任意大小的电路板(宽度和高度相同)存储在2d阵列中。又名存储数据行的数组。这些行中的数据是列,因此可以使用
array[row][column]
访问数组中的位置。我正在尝试创建一个机器人,它将尝试与玩家对抗。我在考虑用尽可能少的人类棋子对半空的行/列/对角线进行暴力强迫。我还了解到minmax机器人可以更好地利用资源。但我在JS中找不到任何东西,也找不到任何可以转换为JS的东西,因为我见过的大多数其他机器人都使用带有9个元素的1d数组。再说一次,我的电路板可以是任何大小,宽度和高度相等,所以我需要制作一个理解这一点的机器人。这是我到目前为止已经破解的代码

function startArtificialIdiot() {
    // General analytical skills.
    // First the bot will look for open rows, once it finds one it will check adjacent columns, and diagonals for the best play...
    let optimalPlay = [];
    let safeRows = [];

    // Generate row css.
    for (let row = 1; row <= boardSize; row++) {
        safeRows.push(row);
    }

    console.log(safeRows)

    // Loop through and remove rows that have player one's tile in them, or are full.
    rows: 
    for (let checkedRow = 1; checkedRow <= boardSize; checkedRow++) {
        // Go through each row, and check if all columns are empty.
        let columnsFull = 0;
        for (let column = 1; column <= boardSize; column++) {
            // If a row is occupied, we te add to the columnsFull counter.
            if (board[checkedRow][column] != "") {
                columnsFull ++;
            }
        }

        // If this row is full, then we remove it from the list.
        if (columnsFull == boardSize) {
            console.warn(`AI: Row ${checkedRow} is full. Deleting it from the list.`);
            safeRows.splice(safeRows.indexOf(checkedRow), 1);
        }
    }

    // If we can't find a good play we safely abort with a random play.
    if (safeRows.length == 0) {
        console.warn("AI: Playing randomly because I can't find a good place to play...");
        changePiece(Math.ceil(Math.random()*boardSize), Math.ceil(Math.random()*boardSize));
    }

    // Temporarily randomly play in one of the safe rows.
    let rowPick = safeRows[Math.floor(Math.random() * safeRows.length)];
    console.warn(`AI: Moving to row ${rowPick} from ${safeRows}`);
    changePiece(rowPick, Math.ceil(Math.random()*boardSize));

    console.log(safeRows);
}
函数startitificialidiot(){
//一般分析技能。
//首先,机器人将查找打开的行,一旦找到一行,它将检查相邻的列和对角线以获得最佳播放效果。。。
让我们玩=[];
设safeRows=[];
//生成行css。

对于(让row=1;row你可以查看他是否取得了任何进展。如果你想单独完成,你可以创建一个包含所有终点胜利的数组,然后通过你的AI检查该数组是否有匹配的“洞”。终点胜利将是所有水平线、所有垂直线和两条对角线。