Tony Xu
Tony Xu

Reputation: 3091

how to generate Tetris piece from a given grid

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.

Upvotes: 1

Views: 2858

Answers (5)

frankWhite
frankWhite

Reputation: 1542

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. =)

Upvotes: 0

jawn
jawn

Reputation: 11

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.

Upvotes: 1

livingtech
livingtech

Reputation: 3660

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.)

Upvotes: 0

user1509623
user1509623

Reputation: 192

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.

Upvotes: 0

songlj
songlj

Reputation: 927

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.

Upvotes: 0

Related Questions