huzzak
June 23rd, 2007, 11:34 PM
I'm trying to make a Java applet Tetris clone. I've defined a TetrisPiece class that has arrays holding the various spawn positions and rotation changes for the individuals blocks of each Tetris piece. They are defined by four (x,y) positions.
I had tried doing this with different classes all extending the TetrisPiece class, but since the only thing that differs in each is the data for where the blocks ought to be and consolidating this data makes my functions for checking rotation much simpler, I am trying to just have the one class with all the different data contained within.
My most recent attempt of getting this information consolidated into TetrisPiece was, I thought, pretty clever, and would have been perfect had it worked. I basically set it up so that the int arrays would be filled with the data needed for each tetris piece based on the code it was given by the calling function. Apparently you can't define arrays in the way I tried to do so.
Any suggestions or ideas for different approaches? Hopefully this is clear, but if it isn't, please ask me and I'll do my darndest to clarify. Thanks.
public class TetrisPiece
{
private TetrisBlock[] blocks;
private int rotation_sequence;
private int[] initial_position;
// Changes in position for right rotations
private int[] rot3to0;
private int[] rot0to1;
private int[] rot1to2;
private int[] rot2to3;
// Changes in position for left rotations
private int[] rot0to3;
private int[] rot1to0;
private int[] rot2to1;
private int[] rot3to2;
public TetrisPiece (int tetroid_code)
{
switch (tetroid_code)
{
case 1: // I Tetroid
initial_position = {3,0,4,0,5,0,6,0};
rot3to0 = {-1,-2,0,-1,1,0,2,1};
rot0to1 = {2,-1,1,0,0,1,-1,2};
rot1to2 = {1,2,0,1,-1,0,-2,-1};
rot2to3 = {-2,1,-1,0,0,-1,1,-2};
rot0to3 = {1,2,0,1,-1,0,-2,-1};
rot1to0 = {-2,1,-1,0,0,-1,1,-2};
rot2to1 = {-1,-2,0,-1,1,0,2,1};
rot3to2 = {2,-1,1,0,0,1,-1,2};
break;
case 2: // O Tetroid
initial_position = {4,-1,5,-1,5,0,4,0};
rot3to0 = {0,-1,1,0,0,1,-1,0};
rot0to1 = {1,0,0,1,-1,0,0,-1};
rot1to2 = {0,1,-1,0,0,-1,1,0};
rot2to3 = {-1,0,0,-1,1,0,0,1};
rot0to3 = {0,1,-1,0,0,-1,1,0};
rot1to0 = {-1,0,0,-1,1,0,0,1};
rot2to1 = {0,-1,1,0,0,1,-1,0};
rot3to2 = {1,0,0,1,-1,0,0,-1};
break;
case 3: // T Tetroid
initial_position = {3,0,4,0,5,0,4,-1};
rot3to0 = {-1,-1,0,0,1,1,1,-1};
rot0to1 = {1,-1,0,0,-1,1,1,1};
...
Thanks for your help.
I had tried doing this with different classes all extending the TetrisPiece class, but since the only thing that differs in each is the data for where the blocks ought to be and consolidating this data makes my functions for checking rotation much simpler, I am trying to just have the one class with all the different data contained within.
My most recent attempt of getting this information consolidated into TetrisPiece was, I thought, pretty clever, and would have been perfect had it worked. I basically set it up so that the int arrays would be filled with the data needed for each tetris piece based on the code it was given by the calling function. Apparently you can't define arrays in the way I tried to do so.
Any suggestions or ideas for different approaches? Hopefully this is clear, but if it isn't, please ask me and I'll do my darndest to clarify. Thanks.
public class TetrisPiece
{
private TetrisBlock[] blocks;
private int rotation_sequence;
private int[] initial_position;
// Changes in position for right rotations
private int[] rot3to0;
private int[] rot0to1;
private int[] rot1to2;
private int[] rot2to3;
// Changes in position for left rotations
private int[] rot0to3;
private int[] rot1to0;
private int[] rot2to1;
private int[] rot3to2;
public TetrisPiece (int tetroid_code)
{
switch (tetroid_code)
{
case 1: // I Tetroid
initial_position = {3,0,4,0,5,0,6,0};
rot3to0 = {-1,-2,0,-1,1,0,2,1};
rot0to1 = {2,-1,1,0,0,1,-1,2};
rot1to2 = {1,2,0,1,-1,0,-2,-1};
rot2to3 = {-2,1,-1,0,0,-1,1,-2};
rot0to3 = {1,2,0,1,-1,0,-2,-1};
rot1to0 = {-2,1,-1,0,0,-1,1,-2};
rot2to1 = {-1,-2,0,-1,1,0,2,1};
rot3to2 = {2,-1,1,0,0,1,-1,2};
break;
case 2: // O Tetroid
initial_position = {4,-1,5,-1,5,0,4,0};
rot3to0 = {0,-1,1,0,0,1,-1,0};
rot0to1 = {1,0,0,1,-1,0,0,-1};
rot1to2 = {0,1,-1,0,0,-1,1,0};
rot2to3 = {-1,0,0,-1,1,0,0,1};
rot0to3 = {0,1,-1,0,0,-1,1,0};
rot1to0 = {-1,0,0,-1,1,0,0,1};
rot2to1 = {0,-1,1,0,0,1,-1,0};
rot3to2 = {1,0,0,1,-1,0,0,-1};
break;
case 3: // T Tetroid
initial_position = {3,0,4,0,5,0,4,-1};
rot3to0 = {-1,-1,0,0,1,1,1,-1};
rot0to1 = {1,-1,0,0,-1,1,1,1};
...
Thanks for your help.