Java 放置俄罗斯方 block 像 block 算法

标签 java arrays algorithm libgdx porting

所以我一直在研究这个游戏,在 java 中,我已经使用 python 和 pygame 让它工作,但是当我尝试使用 libgdx 将它移植到 java 时,我无法弄清楚我做错了什么,它会编译,但是当它运行时,片段会重叠,我不完全确定其中的原因。我有 if (!ICERECTANGLES.contains(CURRENTRECT)) 这应该可以防止这种情况发生,但它似乎并没有阻止这一事件。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;`

Map<Integer, int[][]> ICEBLOCKS = new HashMap<Integer, int[][]>();
Map<Integer, int[][]> PIECES = new HashMap<Integer, int[][]>();

private void iceBegin(){
    int PIECESADDED = 0;
    Random RANDOM = new Random();
    for (int x = 0; x < BOARDWIDTH; x++){
        for (int y = 0; y < BOARDHEIGHT; y++){
            System.out.println( x + ", " + y);
            int LOOPNUM = 0;
            boolean FIT = false;
            while (!FIT){
                LOOPNUM += 1;
                if (LOOPNUM == 5) break;
                //Arrange the dictionary/array containing all of the pieces
                Map<Integer, int[][]> PIECESDICT = new HashMap<Integer, int[][]>();
                int[][] first = new int[][]{ {0,0}, {0,1}, {0,2}, {1,1} };
                PIECESDICT.put(1, first);
                int[][] second = new int[][]{ {0,0}, {0,1}, {1, 1} };
                PIECESDICT.put(2, second);
                int[][] third = new int[][]{ {0,0}, {0,1}, {1,0} };
                PIECESDICT.put(3, third);
                int[][] fourth = new int[][]{ {0,0}, {0,1}, {1,0}, {1,1} };
                PIECESDICT.put(4, fourth);
                int[][] fifth = new int[][]{ {0,0}, {1,0}, {2,0} };
                PIECESDICT.put(5, fifth);
                int[][] piece = new int[1][4];
                piece = PIECESDICT.get(RANDOM.nextInt(PIECESDICT.size()) + 1);
                int[][] PIECE = new int[piece.length][2];
                // fails in this for loop!!!!
                for (int i = 0; i < piece.length; i++){
                    PIECE[i][0] = piece[i][0];
                    PIECE[i][1] = piece[i][1];
                }
                //forward_backward determines whether the piece will be facing forward or backwards
                //also known as left or right
                boolean for_back = RANDOM.nextBoolean();
                if (for_back){
                    for (int[] P: PIECE){
                        if (P[0] > 0) P[0] = P[0] * -1; 
                    }
                }
                boolean PIECETRUE = true;
                // Makes sure that the block will fit without overlapping any other pieces
                for (int[] item: PIECE){
                    item[0] = item[0] + x;
                    item[1] = item[1] + y;
                    if (item[0] <= BOARDWIDTH && item[0] >=0){
                        if (item[1] <= BOARDHEIGHT && item[1] >= 0){
                            Rectangle CURRENTRECT = new Rectangle();
                            CURRENTRECT.x = item[0] * 50;
                            CURRENTRECT.y = item[1] * 50;
                            CURRENTRECT.width = 50;
                            CURRENTRECT.height = 50;
                            if (!ICERECTANGLES.contains(CURRENTRECT)){
                                PIECETRUE = PIECETRUE;
                            }
                            else PIECETRUE = false;
                        }
                        else PIECETRUE = false;
                    }
                    else PIECETRUE = false;
                }
                //then if piece fits add the rectangles to the rectangle array, and also add the PIECE to the PIECES array
                if (PIECETRUE == true){
                    PIECES.put(PIECES.size(), PIECE);
                    for (int[] item: PIECE){
                        Rectangle CURRENTRECT = new Rectangle();
                        CURRENTRECT.x = item[0] * 50;
                        CURRENTRECT.y = item[1] * 50;
                        CURRENTRECT.width = 50;
                        CURRENTRECT.height = 50;
                        ICERECTANGLES.add(CURRENTRECT);
                        ICEBLOCKS.put(ICEBLOCKS.size(), item);
                        PIECESADDED += 1;
                        System.out.println("New Piece Added: " + CURRENTRECT);
                    }
                    PIECES.put(PIECES.size(), PIECE);
                    break;
                }
            }
        }
    }

如果您想知道我到底想要它做什么,这里是它应该复制的 Python 代码。

def randomPieces():
global PIECESPOS
POINTSTAKEN = []
PIECESPOS = {}
MovesAv = True
for x in range(BOARDWIDTH):
    for y in range(BOARDHEIGHT):
        PIECESDICT = {1: [[0,0], [0,1], [0,2], [1,1]], 2: [[0,0], [1,0], [0,1]], 3: [[0,0], [0,1], [1,1]], 4: [[0,0], [0,1], [1,0], [1,1]], 5: [[0,0], [1,0], [2,0]]}   
        NUMTIMES = 0
        FIT = False
        while FIT != True:
            NUMTIMES += 1
            if NUMTIMES == 50:
                FIT = True
            piece = PIECESDICT[random.randint(1, len(PIECESDICT))]
            PIECE = []
            for item in piece: PIECE.append(item);
            for_back = bool(random.randint(0,1))
            if for_back:
                for item in PIECE:
                    if item[0] > 0: item[0] = item[0]*(-1);
            PIECETRUE = True
            for item in PIECE:
                item[0], item[1] = (item[0] + x), (item[1] + y)
                if item[0] <= BOARDWIDTH and item[0] >= 0:
                    if item[1] <= BOARDHEIGHT and item[1] >= 0:
                        if item not in POINTSTAKEN:
                            if PIECETRUE:
                                PIECETRUE = PIECETRUE
                        else:
                            PIECETRUE = False
                    else:
                        PIECETRUE = False
                else:
                    PIECETRUE = False
            if PIECETRUE:
                PIECESPOS[len(PIECESPOS)] = PIECE
                for item in PIECE: POINTSTAKEN.append(item)
                FIT = True

我将不胜感激任何关于如何操纵算法以允许不重叠且不采用每个坐标的指导和建议。

最佳答案

int[][] PIECE = {};

创建一个大小为 0 的数组。

因此这些声明

PIECE[i][0] = piece[i][0];
PIECE[i][1] = piece[i][1];

将不起作用。

你可能想改变

int[][] PIECE = {};

int[][] PIECE = new int[x][y];//eg. int[][] PIECE = new int[2][4];

关于Java 放置俄罗斯方 block 像 block 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21243545/

相关文章:

java - 从 json 数组中删除引号

algorithm - 如何从写成单词的数字中读取值?

javascript - THREE.js 从 raycaster 交点检测相邻面

java - 如何将数学符号(例如 R^n)放入 Java 字符串?

java - 无状态 session Bean 与无状态单例

image - 无法使用 JComponent 绘制简单图像

Java 递归 - 传递引用的替代方法 :

Python OLA 类型错误 : can only assign array

javascript - 使用 Meteor 按数组对象中的日期字段排序

java - 查找没有溢出的整数数组的总和