Java 在二维阵列中创建随机生成的对象
全部。我正在开发一个小游戏,其中创建了一个2D阵列来放置游戏“地图”的每个瓷砖。游戏产生了一个玩家和一堆各式各样的物品。这些单元格如下所示:Java 在二维阵列中创建随机生成的对象,java,multidimensional-array,Java,Multidimensional Array,全部。我正在开发一个小游戏,其中创建了一个2D阵列来放置游戏“地图”的每个瓷砖。游戏产生了一个玩家和一堆各式各样的物品。这些单元格如下所示: private String [][] cells; ... public void set(int cellX, int cellY, String str) { cells[cellX][cellY] = str; } public void addItem(Item item, int limit) { ArrayList&
private String [][] cells;
...
public void set(int cellX, int cellY, String str)
{
cells[cellX][cellY] = str;
}
public void addItem(Item item, int limit)
{
ArrayList<Cell> passages = new ArrayList<Cell>();
for(int x = 0; x < cells.length; x++)
{
for (int y = 0; y < cells[w].length; y++)
{
if (cells[x][y] == "P") //if it's a passage
passages.add(new Cell(x,y));
}
}
Random rand = new Random();
while(spawnedItems < limit){
if(passages.size() == 0)
throw LimitImpossibleError();
int randomNum = rand.nextInt(passages.size());
items.add(new ItemPosition(item, passages.get(randomNum))); //assuming that you have a global ArrayList of Item and respective Cell (ItemPosition) to draw them later
}
}
每个字符串表示每个位置的单元格的外观。例如,有时会生成墙,有时会创建通道(这些都是从文件中读取的)。所以问题是:
如何在某些单元格上随机生成对象?例如,如果我总共有36个单元格(6x6),但玩家只能“移动”其中的23个,那么我如何随机生成每个单元格上产卵机会相等的项目
到目前为止,我的代码是这样的
public void draw()
{
for (int x = 0; x < cells.length; x++)
{
for (int y = 0; y < cells[w].length; y++)
{
if(cells[x][y] == "W"){
Cell wall = new Cell(config.wallImage());
wall.draw(config, x, y);
if(cells[x][y] == "P"){
Cell passage = new CellPassage(config.passageImage());
// This is where I need to check to see if the item is okay
// to be placed. If it is unable, nothing will be added to
// the Cell passage.
//passage.attemptToAdd(item);
passage.draw(config, x, y);
}
}
}
hero.draw();
}
public void draw()
{
对于(int x=0;x
您可以创建符合条件的点的阵列列表,然后使用[您的阵列列表的名称].get(Math.random()*[您的阵列列表的名称].size())随机选择它们。
您可以创建符合条件的点的阵列列表,然后使用[your ArrayList's name].get(Math.random()*[your ArrayList's name].size())
因此,在与OP的聊天中进行了讨论,这就是问题所在,从问题帖中看起来有点混乱:
项
,例如键
和宝石
,可能在每一轮中产生Item
有一个每轮必须生成的限制数。例如,每轮可能生成5个Gem
Cell
的ArrayList
。存储通道的所有单元格private String [][] cells;
...
public void set(int cellX, int cellY, String str)
{
cells[cellX][cellY] = str;
}
public void addItem(Item item, int limit)
{
ArrayList<Cell> passages = new ArrayList<Cell>();
for(int x = 0; x < cells.length; x++)
{
for (int y = 0; y < cells[w].length; y++)
{
if (cells[x][y] == "P") //if it's a passage
passages.add(new Cell(x,y));
}
}
Random rand = new Random();
while(spawnedItems < limit){
if(passages.size() == 0)
throw LimitImpossibleError();
int randomNum = rand.nextInt(passages.size());
items.add(new ItemPosition(item, passages.get(randomNum))); //assuming that you have a global ArrayList of Item and respective Cell (ItemPosition) to draw them later
}
}
public void addItem(项目,整数限制)
{
ArrayList通道=新的ArrayList();
对于(int x=0;x
讨论中OP的另一个问题是如何在以后绘制项目。因此,我给了他两个建议:
将迷宫的表示形式更改为表示迷宫中内容的对象。例如,CellInfo
,它可能是Wall
、Passage
、Item
,等等的父对象。但是,这需要在实际工作中进行大量更改。例如,他实际上是从文件中读取迷宫的
有一个带有项的阵列列表
和相应的单元格
。在绘制
方法中,在绘制所有墙壁和通道(迷宫中唯一用字符串
表示的东西)后,遍历该阵列列表
,并在相应位置绘制它们
因此,在与OP的聊天中进行了讨论,这就是问题所在,从问题帖子上看,这似乎有点令人困惑:
有项
,例如键
和宝石
,可能在每一轮中产生
那些Item
有一个每轮必须生成的限制数。例如,每轮可能生成5个Gem
它们必须以相同的概率在通道中产卵
因此,为了解决这个问题,我的建议是:
创建Cell
的ArrayList
。存储通道的所有单元格
生成一个介于0和数组长度-1之间的随机数
重复此操作,直到达到所需项目的限制
它应该是这样的:
private String [][] cells;
...
public void set(int cellX, int cellY, String str)
{
cells[cellX][cellY] = str;
}
public void addItem(Item item, int limit)
{
ArrayList<Cell> passages = new ArrayList<Cell>();
for(int x = 0; x < cells.length; x++)
{
for (int y = 0; y < cells[w].length; y++)
{
if (cells[x][y] == "P") //if it's a passage
passages.add(new Cell(x,y));
}
}
Random rand = new Random();
while(spawnedItems < limit){
if(passages.size() == 0)
throw LimitImpossibleError();
int randomNum = rand.nextInt(passages.size());
items.add(new ItemPosition(item, passages.get(randomNum))); //assuming that you have a global ArrayList of Item and respective Cell (ItemPosition) to draw them later
}
}
public void addItem(项目,整数限制)
{
ArrayList通道=新的ArrayList();
对于(int x=0;x
讨论中OP的另一个问题是如何在以后绘制项目。因此,我给了他两个建议:
将迷宫的表示形式更改为表示迷宫中的对象。例如,CellInfo
,它可能是Wall
,Passage
的父对象,