javascript - 在精确位置数组中搜索

标签 javascript arrays loops graph

我目前有一幅画有 64 个图 block ,每种颜色都已定义。灰色是有效位置,黑色是无效位置(墙),绿色是玩家 pawn 1,红色是玩家 pawn 2。当玩家 1 单击他的绿色 pawn 时,他可以选择在靠近他的有效图 block 上复制自己(灰色)或跳到靠近他的第二 block 瓷砖上。如果绿色棋子是靠近红色棋子的棋子,它就会变成绿色现在我正在寻找的是。

如何搜索所有有效位置,无论是准备好的图 block 还是跳转到第二个图 block ,并正确检查之后有什么。

enter image description here

class Game{
        constructor(){
            super();
            this.default_grid = null;
            this.curr_grid_playing = null;
            this.player = 1;
            this.curr_player_turn = 1;
            this.game_is_ready = false;
            this.rows = [];

            this.do_new_game();
        }

        get_random_grid(){
            const array_grid = [
                "3100000010000000000000000003300000033000000000000000000200000023",
                "1000000200300300033003300000000000000000033003300030030010000002",
                "0000000000000000033300300313203003013230030033300000000000000000",
                "0000000000000000003033000313003003230030003033000000000000000000"
            ];
            return array_grid[Math.floor(Math.random()*array_grid.length)];
        }

        do_new_game(){
            this.default_grid = this.get_random_grid();
            this.curr_grid_playing = this.default_grid;
            
            for(let i = 0; i < this.default_grid.length; i++){   
                if(i % 8 == 0)
                    this.rows.push([]);
                this.rows[this.rows.length - 1].push([i, this.default_grid.charAt(i)]);

                let new_game_node = this.create_game_button(this.default_grid.charAt(i), i);
                this.append_child_node(new_game_node);
                
            }     
        }

        get_grid_possibilities(from_index){
            if(this.curr_player_turn == 1 && (this.curr_player_turn == this.player)){
               console.log(this.rows);
               
            } else if(this.curr_player_turn == 2 && (this.curr_player_turn == this.player)){

            }
        }
    }

我正在考虑在数组中制作一个图形来准确表示网格 < this.rows > 是我们的控制台显示的内容,它可以工作,但我不确定它是否不太复杂。

最佳答案

您已经有了代表棋盘游戏的矩阵,因此您只需检查 -1 和 +1 方格即可。

let characterPosition = {x:5, y:5};

for (let row-1; row<=1; row++) {
    for (let col-1; col<=1; col++) {
        
        let testPosX = characterPosition.x + col;
        let testPosY = characterPosition.y + row;
        
        if (row===0 && col===0) {
            // player pos -> skip
            break;
        }
        
        if (testPosX<0 || testPosY<0 || testPosY>matrix.length-1 || testPosX>matrix[0].length-1) {
            // outside board -> skip
            break;
        }
        
        if (matrix[testPosY][testPosX]===0) {
            // this is a empty square
        } else {
            // this is not an empty square
        }
        
    }
}

关于javascript - 在精确位置数组中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70418680/

相关文章:

javascript - 循环函数和数组: javascript

javascript - 将 promise 链中的值传递给处理程序

javascript - 在哪里可以下载独立的 Microsoft 脚本编辑器?

PHP - 单个数组项作为方法的参数

c++ - 数组匹配时无输出

javascript - 在 JS 对象中搜索值

mysql - 使用 'While' 回显数组结果不显示第 1 行

javascript - 如何用 "-"替换数组中所有未定义的值?

javascript - 旋转后获取元素的默认左侧和顶部位置

javascript - 使用 Array.reduce() 将数组转换为对象