swing - Java Tetris - weird row clearing issue -
i’m encountering couple weird things clearing rows in tetris...
if set board’s width , height same (10 , 10):
board = new board(10, 10, 35);
which determined by:
public board(int w, int h, int ts) { width = w; height = h; tilesize = ts; grid = new tile[width][height]; ... } public int getwidth() { return width * tilesize; } public int getheight() { return height * tilesize; }
row clearing seems work fine (although, i’m not sure why there hidden row beneath it… shouldn’t fit jframe?):
but if set width , height differently (here it’s 10 , 12), rows won’t clear.
board = new board(10, 12, 35);
row clearing:
public void checkbottomfull() { system.out.println(grid.length); int lines = 0; for(int row = grid.length-1; row >= 0; row--) { while (isfull(row)) { lines++; clearrow(row); } } } public boolean isfull(int row) { (int col = 0; col <= grid[row].length-1; col++) { while(grid[col][row] == null) { return false; } } return true; } public void clearrow(int rowtoclear) { for(int row = rowtoclear; row > 0; row--) { for(int col = 0; col < grid[row].length; col++) { grid[col][row] = grid[col][row-1]; } } }
any thoughts why having equal dimensions allows rows clear? why should matter?
thanks!
i think found issue, when tried resolve it, throws error.
before, had checkbottomfull()
looping through grid.length-1, equal 10 ( for(int row = grid.length-1; row >= 0; row--) {
) put height (rows) 12 gameboard = new board(10, 12, 35);
, should looping through 12 rows, not 10. , should looping through 10 columns per row...
so hardcoded rows @ 12 , columns @ 10 below...
//loop through rows (12 rows) public void checkbottomfull() { system.out.println(grid.length); for(int row = 12; row > 0; row--) { while (isfull(row)) { clearrow(row); } } } //loop through columns in row (10 columns) public boolean isfull(int row) { system.out.println(grid[row].length); (int col = 0; col <= 10; col++) { if(grid[col][row] == null) { return false; } } return true; }
but it's throwing indexoutofbounds exception...
no idea why
one thing notice this:
for (int col = 0; col <= grid[row].length-1; col++) { while(grid[col][row] == null) {
you've got grid[row]
grid[col][row]
. leads me think should either grid[col]
or grid[row][col]`
Comments
Post a Comment