繁体   English   中英

如何将对象添加到数组中但仅当数组的当前元素为0时?

[英]How to add an object into an array but only when the current element of the array is 0?

我有下面的代码,当实例化两个类(播放器,游戏)时,在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); 

您应该创建所有可能位置的列表,并为每个新玩家实例随机选择一个,而不是在创建每个玩家时为其分配随机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的属性。

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

这使得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()构造函数之外的坐标并在设置时将它们分配给Player()将更加简洁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM