在精确位置数组中搜索

Search in array of precise positions

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

如何搜索所有有效位置,无论是准备好的瓷砖还是在 2 日跳跃,并正确检查之后的位置。

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
        }
        
    }
}