1

At first I think my question should have been asked before, but I didn't find what I want.

One element of this iOS app I'm developing is break a 8x8 grid into Tetris pieces (every piece is made of 4 blocks). Two particular question I have are:

  1. what is the best way to represent a Tetris piece in objective-C?

  2. what algorithm to present the grid into random Tetris pieces (and later on how to check if two pieces fits together).

Edition on 01/28 @livingtech, I think I implemented pretty much what you say, except the point of "having a hole". My code works with no hole at simple stage when Tetris block is two blocks only (yes, two squares, connected either horizontally or vertically), but at 3-square Tetris block, I would get holes. I just tested and out of 1000 running, I would get one without a hole. So definitely I need some mechanism to check if next square will be a singleton.

Tony Xu
  • 3,031
  • 4
  • 32
  • 43
  • what I have now is the grid with 8x8=64 blocks, each one can be moved around. however, what I really need is 4-block Tetris piece. any suggestion? Thanks. – Tony Xu Jan 26 '13 at 04:20
  • Think this is a pretty subjective question. There are so many ways to represent a tetris piece! I have implemented Tetris a few times myself, and for the most part, haven't bothered making a "Tetromino" class. I did a talk in 2010 on Cocos2D, and the demo was tetris. Don't know if the Cocos stuff will make it irrelevant, but it might be fun for you to look at: https://github.com/mgrider/iPhone-Cocos2D-Gamedev-Demo I represent the pieces with a struct in `TetrisDemoGameModel.h` – livingtech Jan 27 '13 at 05:45

5 Answers5

1

I been trying to do the same thing for my game. Though I am a total beginner, and I'm using XNA and C#.

But the way I'm trying to go about it is: 4x6 grid array

--y123456
X1-000000
X2-000000
X3-000000
X4-000000

Here,

  • 0 signifies no block
  • 1 defines a block

Algorithm

  1. Start by taking the very first 0 in the array ( top left corner ) and randomly pick a 0 or a 1.
  2. Randomly choose the coordinates based on x1/x2-y1/y2, decide 1 or 0.
  3. If it is 1, then decide coordinated based on where that 1 was put.
  4. If it was 1 on x2 y1, then decide if a 1 should go on next touching coordinate.
  5. If you just have to code in what coordinates touch and which don't, and the logic will roll through.

I have mine set up bit different. But this is the basic foundation of my random Tetris engine.

I also found that making it like that really helps to have a whiteboard and make a drawing of the grid and label with your coordinates.

Praveen Vinny
  • 2,372
  • 6
  • 32
  • 40
jawn
  • 11
  • 1
0

since ur board is 8*8, i think u can use a int64 to represent the board. each bit of the int64 represents whether the specific grid is filled or not.

songlj
  • 927
  • 1
  • 6
  • 10
  • 8x8 is just for example, it may be 12x12 or 16x16, even larger. I'm thinking to have the shapes handy, and randomly pick one shape to fill the available most left-upper block one by one. Not sure if this is the right approach. Thanks for your input. – Tony Xu Jan 26 '13 at 05:27
0

Implementing Tetris is a hobby of mine. First implemented it in Windows/C. Then in Perl/Tk! Last implementation I did in Obj-C/Cocoa (Mac). In all cases, the game logic is the same. Only the UI stuff changes. I treat every little box separately and have a two-dimensional array which contains the presence (and color) of every "set" box on the board. Standard board size I use is 10 boxes wide by 20 boxes high.

Separately I keep track of the "dropping" piece: it's location and what kind of piece it is. Based on a timer, try to make the piece drop. If any of the boxes where the "dropping" piece would drop is already set, then stop dropping the piece and add the piece boxes to the "set" part of the board. Create a new piece, and start over.

It may not be the best way to implement it, but it makes sense in my head. From a pure OO perspective, each shape of a dropping piece could be a subclass of a generic shape class. Override functions that check whether the shape can drop, the offsets of the individual boxes in the shape, etc.

user1509623
  • 192
  • 1
0

I don't think anybody has taken a stab at your question #2 yet here, so I'm going to outline what I would do.

Setup:

  1. You'll need to represent your grid as an array of some kind. At the very least, you'll want some kind of boolean values, to denote whether each coordinate in the grid is "occupied".
  2. You'll need to keep track of the pieces on your grid. This could be another array, this time holding references to the four coordinates for each piece.
  3. You'll need a variable or variables to keep track of a coordinate in your grid where you'll start filling in pieces, (I would probably populate these with a corner to start).
  4. Set up a "pool" of all possible Tetris pieces and rotations. (You'll want to keep track of which ones you've already checked on every iteration outlined below.)

Iterate:

  1. Get a random piece from your pool that will fit into your starting coordinate. (If you want to get fancy, you could be smart about which ones you choose, or you could just go totally random. As pieces don't fit, mark them checked, so you don't keep checking randomly forever. If you get to a point where you've checked all the pieces, you have a solution that doesn't work, either back up an iteration, or start over.)
  2. Make sure the Tetris piece you selected didn't leave a "hole", or empty space with less than 4 squares. (I don't know your requirements for solving this problem, so I can't say whether you should focus on speed or ease of coding, but you may be able to skip this step if you want, and "brute force" the solution.)
  3. "Place" the piece, by writing it to your piece array and marking the coordinates filled.
  4. Check for "finished" condition, in which all your spaces are filled.
  5. Pick a new coordinate in your grid and repeat #1. (I would pick an empty one next to the previous coordinate.)
livingtech
  • 3,570
  • 29
  • 42
  • Thanks a lot for your input. This is what I want. Initially, I was hoping there was some trick to search possibilities how to dissect the grid into Tetris pieces instead of brute force. I appreciate your logic, sounds very reasonable and clear enough to implement. – Tony Xu Jan 28 '13 at 01:40
  • I think I implemented pretty much what you say, except the point of "having a hole". My code works with no hole at simple stage when Tetris block is two blocks only (yes, two squares, connected either horizontally or vertically), but at 3-square Tetris block, I would get holes. I just tested and out of 1000 running, I would get one without a hole. So definitely I need some mechanism to check if next square will be a singleton. Hope someone can come up a smart way to check if next will be a singleton. Thanks. – Tony Xu Jan 29 '13 at 04:57
  • Tetris blocks are 4 squares. If you are creating blocks of other sizes, I'd recommend you just randomly fill, and then at the end create some (presumably smaller) blocks to fill all the holes in. – livingtech Jan 30 '13 at 23:03
  • what you said does simplify my life, but that's not how it should be done technically. – Tony Xu Jan 31 '13 at 04:03
  • OK, I don't know your requirements. – livingtech Jan 31 '13 at 17:55
0

If this actual yet, I wrote test tetris app on Objective-C few months ago https://github.com/SonnyBlack/Test-Demo-Tetris . I think my algorithm not very well, but it working. =)

frankWhite
  • 1,523
  • 15
  • 21