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

时间:2014-01-20 20:30:09

标签: 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

我很感激有关如何操纵算法以避免重叠而不是每个单独坐标的任何指导和建议。

1 个答案:

答案 0 :(得分:2)

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];