How would u implement Tetris? [Pseudo code only]

Status
Not open for further replies.

megadave

Ars Scholae Palatinae
1,042
There are multiple ways to implement the game Tetris,
from a game engine stand point.

If u were going to program it, how would i implement it?
Using objects or procedurally, etc.

Think of yours FIRST, post it, then read the rest of them

Its more fun that way



I'll go first :D

Each collection of blocks would be an object with a few
different properties, which could be altered to create
the different shapes, etc, meaning all objects would be from
the exact same class.

Each object would propagate thru a 2d array, from top to bottom going 'downwards'

Each object would respond to the keyboard input, moving,
changing shape, etc.

Once it comes to rest, a quick parse of the 2d array will
reveal if a line is filled. If so, clear the line, drop
any necessary blocks, repeat. (probably recursive)

Loop back to the top.


PS: if this is a stupid threat, my apologies, but its 4:30 am
and I can't sleep
 
That was the first thing I programmed in QBasic on my 386 over a decade ago.

Code:
   choose random piece
      Can piece be added to game?
         No = Game Over
      place piece

      while (piece can move down)
         user manipulation (left/right/rotate/slam) for [x milliseconds]
         move piece down
      end

      for each line from bottom to top
          is line full?
             Yes = destroy and move above lines down one
          New level count reached?
             adjust [x milliseconds]
      end
   goto choose random piece

That's the basic skeleton. There's not much more to it.
 

morpheus1981

Wise, Aged Ars Veteran
104
"goto" can be used, but you really have to be careful when you use it. I wouldn't mess with it unless you're working with structural programming models. Back in high school, my classmates and I used to use GOTO just because our teacher said we shouldn't use it. Our programs never crashed or did the wrong thing...but we had to make sure the labels were in the right place, and the results were correct. It's kind of old fashioned, so most people avoid it now, especially since instructors tend to tell everyone not to use it. But if you've ever programmed assmebly, GOTO is very similar to the "JMP" instruction. You just need to be careful with it. Use it if you want, but be careful.
 
Originally posted by :skylark
I thought it was firmly established that "goto" shouldn't be used in a program ?

By teachers. Remeber when teachers told you that you can't divide by zero, or take the square root of a negative number.

Teachers lie.

Try reading code that is meant to run _fast_ 1 out of 70 instructions in the linux core kernel is a goto. There are also cases where I have seen people jump through hoops to avoid a goto, their code would have been much more readable with a goto.

Goto is a powerful tool, used wrong it is very very bad <shudder> however used well it is no worse than an if statement.
 
Remeber when teachers told you that you can't divide by zero
You can't. You can take the limit of k/x as x approaches zero, however.

or take the square root of a negative number
In the set of real numbers, you can't. Considering that was what they were talking about (as imaginary components hadn't yet been introduced yet), I wouldn't call it lying.

Likewise, CS professors teach how to write programs that are easy to follow and maintain. Optimizations come later, and you can hardly blam the professor for not telling you about something that didn't apply to what they were teaching...
 
Originally posted by Thanatos:
You can't. You _can_ take the limit of k/x as x approaches zero, however.

opps the lie has stuck in your head.

Here let me help you unlearn the lie: 1/0= infinity

Note the complete lack of limits or any other qualifiers.
Originally posted by Thanatos:
In the set of real numbers, you can't. Considering that was what they were talking about (as imaginary components hadn't yet been introduced yet), I wouldn't call it lying.

It is a lie by omission. If they had said anything close to what you said they would not be lieing, however they left it all out.

Call it the easy lie, or a lie by omission, it can still stick in your head, and mess you up. Note how many people in this thread have had the lies stick.

Originally posted by Thanatos:
Likewise, CS professors teach how to write programs that are easy to follow and maintain. Optimizations come later, and you can hardly blam the professor for not telling you about something that didn't apply to what they were teaching...

Except for the fact that avoiding using a goto makes code harder to read in many cases. Note that you suggested using a break to replace a goto, however they are the samething.

break = goto loop end.

Since they are the samething, many CS teachers say breaks are forbidden just like gotos.
 
Status
Not open for further replies.