Reputation: 1108
So I've been working on this game, in java, I've gotten it working using python and pygame, but when I tried to port it to java using libgdx, I cannot figure out what I am doing wrong, it will compile, however when it runs pieces will overlap, and I'm not completely sure the reasoning for that. I have if (!ICERECTANGLES.contains(CURRENTRECT))
Which should prevent this from happening, however it does not appear to be stopping this incident.
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;
}
}
}
}
In case you are wondering exactly what I want it to do here is the Python code that it is supposed to duplicate.
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
I would appreciate any guidance and advice on how to manipulate the algorithm to allow for no overlapping and not every single coordinate taken.
Upvotes: 1
Views: 754
Reputation: 5095
int[][] PIECE = {};
creates an array of size 0.
Hence these statements
PIECE[i][0] = piece[i][0];
PIECE[i][1] = piece[i][1];
will not work.
You might want to change
int[][] PIECE = {};
to
int[][] PIECE = new int[x][y];//eg. int[][] PIECE = new int[2][4];
Upvotes: 2