我目前有一幅畫有 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 > 是我們的控制臺顯示的內容,它可以作業,但我不確定它是否不是太復雜。
uj5u.com熱心網友回復:
你已經有了代表棋盤游戲的矩陣,所以你只需要檢查 -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
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388193.html
標籤:javascript 数组 循环 图形
上一篇:輸出指標回傳(空)而不是字符值
