10 Replies - 407 Views - Last Post: 25 October 2012 - 05:49 PM Rate Topic: -----

#1 ppkhoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 12

Writing toString method for Tetris Pieces

Posted 24 October 2012 - 12:45 AM

I'm writing the toString() method for Tetris Pieces (T, L, J, I etc...) and can't figure out how to re-represent them when they are rotated. For an I ([]\n[]\n[]\n[]) when rotated ([][][][]) and same for other pieces. Each pieces have their own class (7 pieces have 7 classes). I can either write toString for each of them or toString in Pieces class (the parent class of the pieces, preferred...). In parent class, there will be an array of type Points, each block have 4 points, user will gave the 1st block coordinate and specify block type, it will be added to parent class's Point array and so on. Here's my rotation method:

public void rotatePiece()
  {
    for (int j = 0; j <= 2; j++)
      //rotate counterclockwise = 3 times clockwise
    {
      for (int i = 0; i < my_point_list.length; i++)
        //rotate clockwise
      {
        final double dx = my_point_list[i].getX() - my_point_list[1].getX();
        final double dy = my_point_list[i].getY() - my_point_list[1].getY();
        my_point_list[i].setLocation(my_point_list[1].getX() + dy, 
                                     my_point_list[1].getY() - dx);
      }
    }
  }


Yes, this is my homework, I did everything except I can't figure out the general toString method and rotated pieces String representation. I do have one idea but it's a very very very long one, a lot of redundant code. I really want to upload the class but this homework isn't due yet, have to keep it to myself.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing toString method for Tetris Pieces

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 03:54 AM

Seems to me that the design to have 7 classes for the 7 possible Tetris pieces was not a good idea to start with/ Or at least each of these class should extend a common/abstract class piece that contains a piece descripyion in a 2D array like

int[][] pos = {
                {1, 0, 0},
                {1, 0, 0},
                {1, 0, 0}
               };

or

int[][] pos = {
                {1, 1, 0},
                {1, 1, 0},
                {0, 0, 0}
               };

or

int[][] pos = {
                {1, 0, 0},
                {1, 0, 0},
                {1, 1, 1}
               };


tghen this common class can host the toString() method common to all using the pos[][] array
Was This Post Helpful? 0
  • +
  • -

#3 ppkhoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 12

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 07:20 AM

View Postpbl, on 24 October 2012 - 03:54 AM, said:

Seems to me that the design to have 7 classes for the 7 possible Tetris pieces was not a good idea to start with/ Or at least each of these class should extend a common/abstract class piece that contains a piece descripyion in a 2D array like

int[][] pos = {
                {1, 0, 0},
                {1, 0, 0},
                {1, 0, 0}
               };

or

int[][] pos = {
                {1, 1, 0},
                {1, 1, 0},
                {0, 0, 0}
               };

or

int[][] pos = {
                {1, 0, 0},
                {1, 0, 0},
                {1, 1, 1}
               };


tghen this common class can host the toString() method common to all using the pos[][] array


Can you explain more... I'm lost
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 11:07 AM

We haven't seen your code so we can only guess but if I had to write a Tetris game I would have:
- a main class (may be abstract) to hold the pieces, rotation, movements, ...
- subclasses for the different pieces shapes

The subclasses are just for the use of their constructor all the methods are in the Piece class

public class Piece {
      char[][] shape;                   // will hold the 5X5 shape
      int positionX, positionY;
      
      // constructor
      Piece(char[][] init) {
         // clone the initial shape
         shape = new char[init.length];
         for(int i = 0; i < init.length; ++i) {
             shape[i] = new char[init[i].length];
             for(int j = 0; j < init[i].length; ++j) {
                shape[i][j] = init[i][j];
             }
         }
      }
      
      public String toString() {
         ... build String based on shape[][]
         ... return that string
      }
      
      public void rotateLeft() {
         ... rotate anticlockwise shape[][] based on its dimensions
      }
      
      public void rotateRight() {
         ... rotate clockwise shape[][] based on its dimensions
      }
      
      public moveDown() {
      }
} 

public class SingleBar extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X', 'X', 'X', 'X'},
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class SingleBar nothing else will go to it

public class Block extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X'},
                         {'X', 'X'}
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class Block nothing else will go to it

public class TBar extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X', 'X', 'X', 'X'},
                         {' ', ' ', 'X', ' ', ' '}
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class TBar nothing else will go to it


Happy coding
Was This Post Helpful? 0
  • +
  • -

#5 ppkhoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 12

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 12:00 PM

View Postpbl, on 24 October 2012 - 11:07 AM, said:

We haven't seen your code so we can only guess but if I had to write a Tetris game I would have:
- a main class (may be abstract) to hold the pieces, rotation, movements, ...
- subclasses for the different pieces shapes

The subclasses are just for the use of their constructor all the methods are in the Piece class

public class Piece {
      char[][] shape;                   // will hold the 5X5 shape
      int positionX, positionY;
      
      // constructor
      Piece(char[][] init) {
         // clone the initial shape
         shape = new char[init.length];
         for(int i = 0; i < init.length; ++i) {
             shape[i] = new char[init[i].length];
             for(int j = 0; j < init[i].length; ++j) {
                shape[i][j] = init[i][j];
             }
         }
      }
      
      public String toString() {
         ... build String based on shape[][]
         ... return that string
      }
      
      public void rotateLeft() {
         ... rotate anticlockwise shape[][] based on its dimensions
      }
      
      public void rotateRight() {
         ... rotate clockwise shape[][] based on its dimensions
      }
      
      public moveDown() {
      }
} 

public class SingleBar extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X', 'X', 'X', 'X'},
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class SingleBar nothing else will go to it

public class Block extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X'},
                         {'X', 'X'}
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class Block nothing else will go to it

public class TBar extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X', 'X', 'X', 'X'},
                         {' ', ' ', 'X', ' ', ' '}
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class TBar nothing else will go to it


Happy coding



Thanks, I have written the code exactly like what you described. I have an abstract parent class for all the pieces and each piece just need to call super(x, y). the abstract class will hold movement, rotate information for the pieces.

View Postppkhoa, on 24 October 2012 - 11:58 AM, said:

View Postpbl, on 24 October 2012 - 11:07 AM, said:

We haven't seen your code so we can only guess but if I had to write a Tetris game I would have:
- a main class (may be abstract) to hold the pieces, rotation, movements, ...
- subclasses for the different pieces shapes

The subclasses are just for the use of their constructor all the methods are in the Piece class

public class Piece {
      char[][] shape;                   // will hold the 5X5 shape
      int positionX, positionY;
      
      // constructor
      Piece(char[][] init) {
         // clone the initial shape
         shape = new char[init.length];
         for(int i = 0; i < init.length; ++i) {
             shape[i] = new char[init[i].length];
             for(int j = 0; j < init[i].length; ++j) {
                shape[i][j] = init[i][j];
             }
         }
      }
      
      public String toString() {
         ... build String based on shape[][]
         ... return that string
      }
      
      public void rotateLeft() {
         ... rotate anticlockwise shape[][] based on its dimensions
      }
      
      public void rotateRight() {
         ... rotate clockwise shape[][] based on its dimensions
      }
      
      public moveDown() {
      }
} 

public class SingleBar extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X', 'X', 'X', 'X'},
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class SingleBar nothing else will go to it

public class Block extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X'},
                         {'X', 'X'}
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class Block nothing else will go to it

public class TBar extends Piece {
      private static final char[][] init = 
                      {
                         {'X', 'X', 'X', 'X', 'X'},
                         {' ', ' ', 'X', ' ', ' '}
                      };
               
      // constructor
      SingleBar() {
         super(init);
      }
}   // finish for class TBar nothing else will go to it


Happy coding



Thanks, I have written the code exactly like what you described. I have an abstract parent class for all the pieces and each piece just need to call super(x, y). the abstract class will hold movement, rotate information for the pieces.


I wrote the code before I posted here, not that I fixed my code to what you described, maybe you did not get what I said in the first post ...
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 12:02 PM

and thus have the toString() method to return an ASCII representation of the array.

Wasn't that your initial question ? :)
Was This Post Helpful? 0
  • +
  • -

#7 ppkhoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 12

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 12:07 PM

View Postpbl, on 24 October 2012 - 12:02 PM, said:

and thus have the toString() method to return an ASCII representation of the array.

Wasn't that your initial question ? :)


Yeah, thanks! I need to review 2d array ... Oh, and by the way, it's only 4 block for every piece, not 5 or 6, it's a simple tetris game.

Another question, when the bar is rotated, should the code you wrote above give me something like:

X
X
X
X

Instead of XXXX?

Do I have to write the code for it? Like:

{"X"}
{"X"}
{"X"}
{"X"}

for the init?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 12:11 PM

Does not matter 4, 5 or 10
the Piece class will accept the size received by the subclass. If you change the size of your pieces in the subclasses you shouldn't have to change a single line of code in class Piece :)

public String toString() {
   StringBuilder sb = new StringBuilder();
   for(int i = 0; i < shape.length; i++) {
      for(int j = 0; j < shape[i].length; j++) {
          sb.append(shape[i][j]);
      }
      sb.append('\n');
   }
   return sb.toString();
}


Was This Post Helpful? 0
  • +
  • -

#9 ppkhoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 12

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 12:14 PM

View Postpbl, on 24 October 2012 - 12:11 PM, said:

Does not matter 4, 5 or 10
the Piece class will accept the size received by the subclass. If you change the size of your pieces in the subclasses you shouldn't have to change a single line of code in class Piece :)

public String toString() {
   StringBuilder sb = new StringBuilder();
   for(int i = 0; i < shape.length; i++) {
      for(int j = 0; j < shape[i].length; j++) {
          sb.append(shape[i][j]);
      }
      sb.append('\n');
   }
   return sb.toString();
}



Can I send you my code through pm? Maybe you can take a look and tell me what you think?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Writing toString method for Tetris Pieces

Posted 24 October 2012 - 12:25 PM

Your init should be

static final char[][] init {
                              {'X', 'X', 'X', 'X'}
                           };
or
static final char[][] init {
                              {'X'},
                              {'X'},
                              {'X'},
                              {'X'}
                           };


shouldn't matter. Depends on how you want the piece to show up.
After that it is the job of the rotate() methods to do their job
Was This Post Helpful? 0
  • +
  • -

#11 ppkhoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-October 12

Re: Writing toString method for Tetris Pieces

Posted 25 October 2012 - 05:49 PM

View Postpbl, on 24 October 2012 - 12:25 PM, said:

Your init should be

static final char[][] init {
                              {'X', 'X', 'X', 'X'}
                           };
or
static final char[][] init {
                              {'X'},
                              {'X'},
                              {'X'},
                              {'X'}
                           };


shouldn't matter. Depends on how you want the piece to show up.
After that it is the job of the rotate() methods to do their job


I have sent a PM to you, hope you can take a look at the code and tell me what you think. What should I do if I wrote it like that? Do I have to change everything?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1