【问题标题】:How to add an object into an array but only when the current element of the array is 0?如何将对象添加到数组中,但仅当数组的当前元素为 0 时?
【发布时间】:2018-09-29 15:42:02
【问题描述】:

我有下面的代码,当两个类 (Player,Game) 被实例化时,特定数量的玩家被插入到 Player.gameBoard 数组中。仅当数组元素为 0 时,我才尝试将 Player 类添加到数组中。因此,如果将 Player 对象插入到 gameboard[0][0] 位置,则没有其他玩家可以覆盖他。目前,如果选择大量玩家(例如 20 个),其中一些会被覆盖,并且不会全部出现。所以我认为while循环有问题。

var question = prompt('how many players');
var numOfPlayers = parseInt(question);

class Game {
  constructor(){
    this.health = 100;
    this.hammer = false
    this.knife = false;
    this.sword = false;
    this.baseballbat = false;
    this.damage = 0;
    this.gameBoard = [
      [0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0],
    ];
    }
  }

class Player {
  constructor(id){
    this.id=id;
    this.location = {
      x:Math.floor(Math.random()*8),
      y:Math.floor(Math.random()*8)
    };
  }
}

var play = new Game();
let player =[];
for (i=0; i <numOfPlayers; i++ ){
    player.push(new Player(i));
    while (play.gameBoard[player[i].location.y][player[i].location.x]===0){
    play.gameBoard[player[i].location.y][player[i].location.x] = player[i];
    }
}

console.log(play);

【问题讨论】:

  • 您的 while 循环仅在空间为空时执行,但如果它不为空,则不会为玩家生成新坐标,因此它会被跳过/不会出现在网格。
  • 玩家会得到一个随机的x/y - 如果该空间被其他玩家占据,你想发生什么?
  • 重新运行 Math.floor(Math.random()*8) 以获取坐标
  • 那么它不应该在Player的构造函数中,而是在初始化循环中完成(并分配给播放器)

标签: javascript arrays object while-loop


【解决方案1】:

如果玩家的位置已经在棋盘上填满,您需要确保为玩家生成了新坐标。

这使得for-loop 代码如下所示:

for (i=0; i <numOfPlayers; i++ ){
   var tempPlayer = new Player(i);
   while (play.gameBoard[tempPlayer.location.y][tempPlayer.location.x]!=0){
      tempPlayer = new Player(i)
    }
   player.push(tempPlayer);
   play.gameBoard[player[i].location.y][player[i].location.x] = player[i];
}

首先,您将新播放器存储在本地。然后在生成的坐标被占用时,您将重新创建具有相同 id 的 Player。一旦坐标给了一个空白空间,您将把玩家推送到player 数组并将其分配给游戏板。

这是基于您当前代码的答案,但在 Player() 构造函数之外生成坐标并在设置时将它们分配给玩家会更整洁。

【讨论】:

    【解决方案2】:

    不要在创建每个玩家时为每个玩家分配一个随机的x/y,而是应该创建一个所有可能位置的列表,并为每个新玩家实例随机选择一个。你有一个 8x8 的网格。所以有 64 个可能的位置

    如果你对每个玩家:

    • 随机选择一个位置
    • 删除该位置

    你永远不会有重叠。

    var positions = [];
    for(var x=0;x<8;x++){
       for(var y=0;y<8;y++){
          positions.push({x,y});
       }
    }
    
    // pick 5 random positions for demo
    
    for(var i=0;i<5;i++){
       var rnd = Math.floor(Math.random()*positions.length);
       console.log("random position chosen:", positions[rnd]);
       //remove this so its not picked again
       positions.splice(rnd,1);
    }

    应用于您的代码如下所示:

    var question = prompt('how many players');
    var numOfPlayers = parseInt(question);
    
    class Game {
      constructor(){
        this.health = 100;
        this.hammer = false
        this.knife = false;
        this.sword = false;
        this.baseballbat = false;
        this.damage = 0;
        this.gameBoard = [
          [0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0],
        ];
        }
      }
    
    class Player {
      constructor(id, location){
        this.id=id;
        this.location = location;
      }
    }
    
    var positions = [];
    for(var x=0;x<8;x++){
       for(var y=0;y<8;y++){
          positions.push({x,y});
       }
    }
    
    var play = new Game();
    let player =[];
    for (i=0; i <numOfPlayers; i++ ){
        var rndPos = Math.floor(Math.random()*positions.length);    
        player.push(new Player(i, positions[rndPos]));
        positions.splice(rndPos,1);
        play.gameBoard[player[i].location.y][player[i].location.x] = player[i];
    }
    
    console.log(play);

    如果有意义的话,您甚至可以考虑将 availablePositions 数组作为您的 Game 的属性。

    【讨论】:

    • 从未想过这种方法。非常感谢!
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多