1

I'm trying to write a tetris clone. My board is a 10x20 array of integers on which 0 means no tile, 1-7 means a tile of specific color. It is constantly translated to graphic interpretation. My shapes are 4x4 arrays of integers. I've just come to a realisation that while making all of the shapes 4x4 makes some things easier, it also causes a problem when moving a piece left and right. Let's say we've got the I shape:

0010
0010
0010
0010

Now, if I move it to left wall there will always be a two units long gap, since the 0s cant move outside of the main board array. What would be the easiest way to allow the 1s to move to the left wall without causing an out of bounds exception?

sdonchor
  • 82
  • 7

2 Answers2

4

Using your described method, one way to simply avoid getting the IndexOutOfBoundsException would be to expand your board to be 18 x 24 instead of 10 x 24, and then write in additional code that doesn't let you move a block left/right if there would be any 1's in the object array that leave the middle 10 squares of the grid. By adding this 'padding' to your grid, you avoid the exception and should still be able to implement.

I hope this approach makes sense to you. If not I can provide a more pseudo-code driven answer, but I hope you get the idea. (Just comment if you have any questions.)

BTW, @assylias makes a very good point. It is important to have a good design/plan before you start implementing things to avoid road-bumps like these. It comes with experience, so keep practicing and you will get the hang of it.

NOTE: As Nick pointed out in the comment, another way of doing this is to simply check if any 1's leave the grid before moving any of the arrays. This is certainly possible (and arguably a more elegant/simple solution), although it may be a bit harder to get right.

Alerra
  • 1,479
  • 11
  • 25
  • Thanks, i'll try that :) – sdonchor Aug 30 '18 at 12:45
  • Let me know if you run into any more problems, glad I could help! – Alerra Aug 30 '18 at 12:47
  • @Alerra, the advice you gave to write code to ensure that none of the 1's leaves the grid is the only valid part of your answer. The same routine can work with the original sized grid. Adding padding around the edges is unnecessary. – Old Nick Aug 30 '18 at 18:37
  • Indeed, a check could be done beforehand to check if the 1's leave, I will add a note in my answer. However, I answered the question assuming that OP wanted to keep the part of the program where he uses a 4x4 grid for the pieces. Doing it without padding would be do-able, but also make it more difficult (in my opinion). – Alerra Aug 30 '18 at 18:52
  • Yes, I wanted to keep 4x4 pieces design. A 3 wide padding on left, right and bottom worked great – sdonchor Aug 31 '18 at 08:32
0

You need a way of detecting collisions with borders and existing pieces.

You would probably have a fixed handle on each piece, you'll also have an X and Y offest for the piece which indicates it's position as it moves down the grid.

To stop a piece moving out of bounds, loop through the 4*4 matrix of the moving piece and for the bits which are set to 1 simply check to make sure that the X position + X offset is >= 0 and <=9 and the Y Position is >=0 and <=19 if either of these checks fail then your piece would be moving outside the limits of the board array so stop the change to x or y offest as appropriate.

Translating the co-ordinates of the set bits in your piece matrix with the board array also allows you to check and see whether your piece has collided with a tile already in the board.

You should be doing these collision checks when a piece rotates as well I would have thought.

Old Nick
  • 995
  • 9
  • 19