Java 搜索二维数组的算法
我正在尝试重新创建一个类似俄罗斯方块的游戏。我有一个2D数组,“_tiles”,它存储名为“ColorShape”的对象 当我点击向下箭头键找到阵列中的下一个可用插槽以快速放置形状时,我正在尝试创建一个quickDrop()方法。我很难想出一个算法,它将搜索该块所在列中当前块下方的行 到目前为止,我一直试图这样做,但我认为我的做法完全错了:(碎片是30x30像素,这就是为什么它们被30除,因此阵列位置对应于形状的x和y位置) 公共void quickDrop(){Java 搜索二维数组的算法,java,Java,我正在尝试重新创建一个类似俄罗斯方块的游戏。我有一个2D数组,“_tiles”,它存储名为“ColorShape”的对象 当我点击向下箭头键找到阵列中的下一个可用插槽以快速放置形状时,我正在尝试创建一个quickDrop()方法。我很难想出一个算法,它将搜索该块所在列中当前块下方的行 到目前为止,我一直试图这样做,但我认为我的做法完全错了:(碎片是30x30像素,这就是为什么它们被30除,因此阵列位置对应于形状的x和y位置) 公共void quickDrop(){ //j是工件当前所在的列 in
//j是工件当前所在的列
int j=_proxypece.getXLocation()/30;
对于(int i=0;i一个算法来解决这个问题:
取下当前下落件,将其从当前位置向下移动到每个Y值上
如果它与先前放置的工件碰撞,或超出栅格底部,则上次选中的Y位置是有效的解决方案
如果您想要所有有效的解决方案,而不仅仅是一个,请对当前选定工件的所有可能旋转重复此算法
下面是一个算法的例子。它不能按原样处理您的代码,但它会让您快速开始
ColorShape[][] grid = new ColorShape[width][height];
TetrisShape shape = new TetrisShape(Shape.L_SHAPE);
//position will be bottom left coordinate of bounding rectangle of dropping shape.
Point position = new Point(shape.getPosition());
int resultY = -1;
for(int dy=0;dy<height;dy++){
for(int y=0;y<height;y++){
int shapeY = position.y+y;
if(shapeY>=height || shape.intersectsGrid(grid)){
resultY = shapeY -1;
break;
}
}
}
ColorShape[][]网格=新的ColorShape[宽度][高度];
四字形=新的四字形(shape.L_形);
//位置将是放置形状的边框的左下角坐标。
点位置=新点(shape.getPosition());
int resultY=-1;
对于(int dy=0;dyWhat做什么getXLocation()
return?如果不是整数,简单地将其除以30可能会产生意外的结果。很高兴再次见到您,看到您正在开发类似俄罗斯方块的游戏也很有趣。这将教会您很多好的选择。我已经为您的问题提供了一个可能的解决方案,请告诉我是否可以提供进一步的帮助。如果典型俄罗斯方块游戏的相对较小的网格(我假设OP的尺寸为8x17),暴力强迫它的典型低效可能不会引起注意。我完全同意。我编辑了我的帖子,不包括这一点。我想你没有接受我的答案。如果是,请解释原因。我很高兴进一步帮助你。@Williammerrison谢谢你的帮助,但我尝试使用你的建议,但无法使它发挥作用。我更新了用我目前正在测试的方法编辑了我的帖子。它似乎可行,但在绘制片段时存在一些问题。您正在调用getXLocation,将其分配到J,然后使用J作为y坐标。。。
// j is the column that the piece is currently in
int j = _proxyPiece.getXLocation()/30;
for (int i=0; i<17;i++){
if(_tiles[j][i] == null)
continue;
else if (_tiles[j][i] != null){
_tiles[j][i-2] = _proxyPiece.getFirstPiece();
_tiles[j][i-1] = _proxyPiece.getSecondPiece();
repaint();
_proxyPiece.setPiece(this.newPiece());
repaint();
break;
}
}
}
public void paintComponent(Graphics g) {
if (_pauseState == false){
_pauseText.setVisible(false);
super.paintComponent(g);
// simplify the positioning of things.
g.translate(0, 0);
//Draws the board outline and fills it white
g.setColor(Color.WHITE);
g.drawRect(0, 0, 240, 480);
g.fillRect(0, 0, 240, 480);
//Draws a dark gray grid
g.setColor(Color.DARK_GRAY);
for(int x = 0; x < COL_COUNT + 1; x++) {
for(int y = 0; y < VISIBLE_ROW_COUNT+1; y++) {
g.drawLine(0, y * TILE_SIZE, COL_COUNT * TILE_SIZE, y * TILE_SIZE);
g.drawLine(x * TILE_SIZE, 0, x * TILE_SIZE, VISIBLE_ROW_COUNT * TILE_SIZE);
}
}
Graphics2D aBetterPen = (Graphics2D)g;
_proxyPiece.fill(aBetterPen);
for (int i = 0; i<16; i++){
for(int j=0; j<8;j++){
if(_tiles[j][i] != null)
_tiles[j][i].fill(aBetterPen);
}
}
}
else if (_pauseState == true){
_pauseText.setVisible(true);
super.paintComponent(g);
// simplify the positioning of things.
g.translate(0, 0);
g.setColor(Color.WHITE);
g.drawRect(0, 0, 240, 480);
g.fillRect(0, 0, 240, 480);
}
}
ColorShape[][] grid = new ColorShape[width][height];
TetrisShape shape = new TetrisShape(Shape.L_SHAPE);
//position will be bottom left coordinate of bounding rectangle of dropping shape.
Point position = new Point(shape.getPosition());
int resultY = -1;
for(int dy=0;dy<height;dy++){
for(int y=0;y<height;y++){
int shapeY = position.y+y;
if(shapeY>=height || shape.intersectsGrid(grid)){
resultY = shapeY -1;
break;
}
}
}