My Tetris Clone

Thread in 'Discussion' started by bughunter2, 18 May 2007.

  1. bughunter2

    bughunter2 Unregistered

    Hi, I'm creating a Tetris clone and I wanted to be compliant on the Tetris Guidelines.


    Now I'm not sure how to implement the "randomizer" algorithm in C/C++. If I've read the topics correctly, the generator should give the user each 7 blocks, but in a random order.


    What I now use is rand()%7 but I figured it's crap because it sometimes gives the user 5 of the same Tetriminoes or like 30 without any I Tetrimino amongst them.


    So how to fill up the random bag with 7 random Tetriminoes which are not already in the bag, how to generate another one every loop iteration?


    If more information is needed or somebody wants a particular part of the source codes or something else, feel free to ask. [​IMG]
     
  2. tepples

    tepples Lockjaw developer

    If you've ever implemented blackjack or poker or solitaire, you've probably written code similar to this:

    Code:
    // #define N_CARDS 52
    #define N_CARDS 7
    
    int deck[N_CARDS];
    int cardsLeft = 0;
    
    void shuffleDeck(void) {
    
     /* First generate all cards in the deck */
     for (int i = 0; i < N_CARDS; ++i) {
      deck[i] = i;
     }
    
     /* Then swap each card with a random card on or before it */
     for (int i = N_CARDS; i > 0; --i) {
      int swapIndex = rand() % i;
      int temp = deck[i - 1];
      deck[i - 1] = deck[swapIndex];
      deck[swapIndex] = temp;
     }
    
     cardsLeft = N_CARDS;
    }
    
    int dealCard(void) {
     if (cardsLeft <= 0) {
      shuffleDeck();
     }
     --cardsLeft;
     return deck[cardsLeft];
    }
    
     
  3. I never got that far, but I did once have a small program that shuffled a deck. It was the traditional way of doing it, though, where you have an array of flags for the card having been dealt, and re-roll if you find a dealt card.


    So lets see how this works...


    For the swapping, all cards in positions that are <= i are unshuffled, and you pick a random one of those (possibly including the one you are trying to replace) and put it into position.


    That's a very clever way of doing it...
     
  4. tepples

    tepples Lockjaw developer

    There's a way to make my code even more clever (from the current O(1) amortized and O(n) worst case to O(1) worst case), but it might confuse a beginner.


    Another thing: There's no such language as C/C++. Though C++ is nearly a superset of C, the idiomatic solution to a problem such as shuffling will often look very different in C vs. C++. For instance, C handles information hiding with pointers to incomplete struct types, while C++ uses classes with private fields. C handles polymorphism with explicit function pointers and unions (as in Allegro's built-in GUI library), while C++ uses virtual method tables created by the compiler.
     
  5. bughunter2

    bughunter2 Unregistered

    With C/C++ I was referring to these languages as "C or C++" as I didn't care in which of these languages an example would be provided. I know that C/C++ are different languages, no offence, thanks for your explanation though.


    Thanks for your example, I will have a good look at it and maybe post a question later on (if I have any, that is).


    EDIT: Just took a look at the code. But: WOW! [​IMG] It's almost too easy, I was thinking way too hard. Guess I had to think just like in the real world, pick all cards (read: Tetriminos), and shuffle them, nothing easier than that! Thanks man, way to go [​IMG]

    If anyone is interested in seeing the program and/or source code when it's fully compliant (I don't really wanna release it earlier [​IMG]) then feel free to post it here or PM me.
     
  6. bughunter2

    bughunter2 Unregistered

    What Python code? Btw I've never written a single line in Python. You see, I'm an 18 year old chap, I started in assembly, exploring kernels and stuff and later on moved to C and C++.


    Oh and, this Tetris clone I'm writing is the first game I wrote in my whole life.


    I have a few things left to do on my clone, like a ghost piece, showing the pieces that are in the random bag (which I already got working now by the way! [​IMG]) and some small other things.
     
  7. On the talk page, someone named Lambda provided us with some code that fully implememts the randomizer language that is listed on that page. But it's in Python, and I don't know any either. [​IMG]

    That's a "hardcore" start you've had. I look forward to seeing what you produce...
     
  8. bughunter2

    bughunter2 Unregistered

    Well as because I started in assembly, I sometimes notice it affects my C coding style (I did C for just about one year). It actually happened that I began with assembly because I was interested in boot sectors/kernels so I gave it a go, I also managed to create a small experimental OS and I might rewrite one later but then in C. In my spare time I try to read good C books about structured programming, data structures, algorithms etc.


    A question to one of you guys out here by the way, about naming conventions. Do you use "m_", "l_" prefixes and friends for variable names? What is actually the standard in this [​IMG]

    I am now working on the ghost piece, it's actually nothing more than collision detection and a little drawing, and then the game is almost compliant to the Tetris guidelines [​IMG]. The SRS system and the infinity feature are completed now.
     
  9. tepples

    tepples Lockjaw developer

    The code for hard drop and ghost piece is exactly the same. Just make a pretend hard drop every time the tetromino enters, moves sideways, or rotates. Then when the player actually performs a hard drop, set the current Y coordinate of the tetromino to that of the ghost.
     
  10. bughunter2

    bughunter2 Unregistered


    Perhaps I should first implement hard drop then [​IMG]
     
  11. bughunter2

    bughunter2 Unregistered

    I won't have much time for a few weeks (have to do things for school). I'll continue later on or maybe on a free evening.


    This is a list of things that are left to do:


    - Hard drop

    - Ghost piece

    - Hold piece

    - Locked soft drop speed (not depending on the operating system's keyboard typematic rate setting)

    - Usage of Roger Dean's Logo (is this a must-have actually?)


    Not really a heck of a lot to do left [​IMG]
     
  12. tepples

    tepples Lockjaw developer

    Of course, if you're outside of Mr. Rogers' Neighborhood, you skip Roger Dean's logo [​IMG] Make something else in a similar spirit: a word (which shouldn't start with "tet" nor end in "ris") in front of one or two big tetrominoes.


    Eventually, you will want to use software typematic for all keys, so that for example the player can shift the falling piece sideways while rotating it. You will have to use software typematic if you want to make your game compatible with a joystick.
     
  13. bughunter2

    bughunter2 Unregistered

    Good idea about making it compatible with a gamepad/joystick. I myself have a gamepad so I can actually try that later on (I've put it on the todo list for later investigations [​IMG]

    How about managing a state for each key that can be set to TRUE or FALSE when the key is pressed and released? Then I check with a little timer when it's time to emulate the "software typematic rate" and move a block. [​IMG]

    I actually wanted to implement this before because it was very annoying that blocks couldn't be moved while rotating but I figured other items had more priority.
     
  14. tepples

    tepples Lockjaw developer

    Just about every game has this true/false state for each gamepad button. And for the expected behavior of the sideways timer, see DAS on the wiki.


    Which framework are you working with? Allegro? SDL? ClanLib? Straight DirectX?
     
  15. bughunter2

    bughunter2 Unregistered

    Well since it's my first game, I started in Win32 GDI. Would it be easy to convert from GDI to SDL?
     
  16. bughunter2

    bughunter2 Unregistered

    Good news; hard drop is working.


    EDIT: Even more good news; ghost piece is working.


    And another EDIT: Sonic drop is working too [​IMG]
     
  17. bughunter2

    bughunter2 Unregistered

    Almost finished I guess, how is this for a first C++ game?


    Highscores are even encrypted with a simple symmetric algorithm [​IMG]

    http://www.megaupload.com/?d=4Z0CQ58O


    I'll upload the source codes too if anyone wants [​IMG]

    EDIT: the name "Blockjes" is a mix between the English and Dutch words for "Blocks", not very creative though [​IMG]
     
  18. bughunter2

    bughunter2 Unregistered

    Bump, anyone yet looked at it?
     
  19. DIGITAL

    DIGITAL Unregistered

    Nice work so far. Here are my comments and suggestions (not from a programmer's point of view though).


    - Default keys should be more standard. Customizable keys would be nice.

    - Wallkicks need work.

    - There should be a timer to go with the scores.

    - There should be a pause before the game starts to allow the player to adjust.
     

Share This Page