Writing a Tetris clone -- revisited with C++11

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
2,378
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Apr 24, 2011

In this tool-assisted education video I demonstrate how to create a Tetris clone in C++, with the GCC toolchain ported for MS-DOS, i.e. DJGPP. You see me type every line. I use C++11 features (previously known as C++0x) such as lambda functions and automatic typing, rather extensively. Also standard C++ features like operator overloading and both function and class templates. I rather wanted to showcase an example of how to utilize them in practical programming challenges.

In http://www.youtube.com/watch?v=JDnypVoQcPw I created a Tetris clone using GW-BASIC. Here, I ported that program into C++, and added new features.

The AI (computer player) that I created uses Pierre Dellacherie's real time tetris algorithm, which I extended to account for multi-ply lookahead and realtime scanning for possible slides.
You can find another example of Pierre Dellacherie's algorithm, written in C#, here: http://www.colinfahey.com/tetris/tetris_en.html

Background music:
-- Betrayal at Krondor bgm 31, played through ADLMIDI
-- B-Wing (NES) mixed together with the same song converted into MIDI and played through ADLMIDI (with OPL3 & reverb)
-- Troika (composer unknown, sequencer unknown), played through ADLMIDI.

I created the editor myself.

P.S. I had to re-encode and upload this video a total of 12 times before Youtube finally could decode it properly. Maybe my x264 is broken.
P.P.S. If you're wondering about my stating the loss at that game, wondering whether I should have just played slower, do note that right before my gameover the computer player also beat my score. I had 13636 points (30 lines), COM had 14282 points (29 lines). If I played slower, I would have lasted longer but the computer would have been far ahead in points. Also, I deliberately chose this style of play, aiming for the best balance of performance (I got to even show the "skill" bonus, while the computer got to show a long "combo" by chance), while still not exceeding the 15 minute Youtube length limit. This is a tool-assisted education video after all.

Source code and resources: http://bisqwit.iki.fi/jutut/kuvat/programming_examples/tetre/

The whole video series:
-- http://youtu.be/JDnypVoQcPw -- Creating a rudimentary TETRIS clone in GW-BASIC
-- http://youtu.be/V65mtR08fH0 -- Improving the game by rewriting from scratch C++, added AI
-- http://youtu.be/-j9AG2I0f3Y -- Illustrating the AI's power with tetrominos and pentominos
-- http://youtu.be/ukXDCBewnsg -- Illustrating the AI's power with only tetrominos
-- http://youtu.be/zQZLK8r1NFs -- Illustrating the AI's power with tetrominos, pentominos and hexominos
-- http://youtu.be/7kHnnkVlBKI -- Testing the AI at tackling octominos. Not good.
-- http://youtu.be/Yw02EkQE3Cw -- Illustrating the AI's power with tetrominos on a half-width field.

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (Bisqwit)

  • Hey , Friend , Can you tell me what is named the program you use to make these videos?

    Like , what is the re-recording program?... Thx , waiting for a reply

  • @ahron123456 There is no re-recording program, nor savestates involved. I use DOSBox for making these videos. When you increase the cycle count in DOSBox, DOSBox appears to be running slower, but in fact only the emulation becomes slower. Video captures still run at normal speed. This allows the user to appear to do stuff quickly. I utilized that feature extensively in my first videos. Nowadays I use scripted input, with exact means varying from video to video.

  • I was wondering why Pierre Dellacherie ai algorithm ?

    What are those co-routine macros for ?

    Now that i think about it, videos about ai programming would be nice.

    Long time since i poked that area..

  • @labobo Oh, and the general principle of the Tetris AI is this: The robot must decide where to put the piece. So it evaluates all rotations and all placements for the piece, choosing the one that looks "best", and maneuvers the piece there. The choice is made by scoring each location. The score is calculated by a formula which considers things such the number of transitions from block to hole and back, both vertically and horizontally, number of pieces eroded if block is placed there, and so on.

see all

All Comments (22)

Sign In or Sign Up now to post a comment!
  • The "cycles commands" speed up the avi file , i was trying to see how DOSBOX capture with it , and then i got a too fast video , :P , can't fix this , "UNFIXABLE"

  • @Bisqwit thank you for your effort , i start to study the codes , complie it sucessus under xp but the full version crash in my dosbox(under win7) , show case work fine there (just a few offset problem tetre Area is on the Left ).

    after playing i find it's evil that the game pick difficult piece for me ..XD

  • @labobo It is a programming paradigm in which two or more parts of a program can intercommunicate, or be interleaved while being written in a manner that normally is used for non-interleaved code.

  • @Bisqwit Ok, I'll read Tatham's coroutine page after this comment. So co-routine macros are multi-threading for dos ?

    I imagine using normal threads are better than co-routines.

  • @labobo I used Pierre Dellacherie's algorithm because it was the best Tetris AI that I could find example source code for. The co-routine macros are used to implement co-routines. With co-routines, it is possible for a timesharing algorithm to be written in a non-timesharing style (infinite loops etc.); a return can be issued from the middle of a function (in order to timeshare with other functions) without having to take care how to resume the same spot. See Tatham's coroutine page for details.

  • @alexliang0627 Link to source code added in the video description.

Loading...
Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more