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

preview_player
Показать описание
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.

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.

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.

The whole video series:
Рекомендации по теме
Комментарии
Автор

You are probably the most interesting guy I ever met on YouTube.
Thanks for your videos, they are always motivating for me.

globoxmusic
Автор

You are without a doubt THE smartest YouTuber of all.

cennessio
Автор

Well sir, you still deserve a medal. You made a basic 3D game in 15 minutes. Thats seems pretty rare too me.

skolanbrinner
Автор

My sister: "What is he doing?"
Me: "He is speaking the language of Gods"

catorials
Автор

i've been coding for a few years now and I still feel like i should learn a lot after watching this video. I feel intimidated.

maksymiliank
Автор

Generally that question is addressed in video 7IeO7CYFd9E . For this particular video however, in the beginning I used the song 31 from Betrayal at Krondor MIDI soundtrack verbatim (google "betrayal at krondor midi" to find). In the latter part, I used the B-Wing NSF verbatim, mixed together simultaneously with a MIDI conversion thereof that I generated using my own tools. In the end, I used an unknown Troika MIDI downloaded from the Internet. All MIDIs played in ADLMIDI that is my own creation.

Bisqwit
Автор

OMFG this guy isn't a robot nor a human he's a cyborg, have u looked at him programming
he has the power to imagine and process new code like a human and has the reasoning of a robot, he does it instantly, my friend one day we have to deeply study your brain, so we can teach the future generation to think like you, thanks for existing ! :D

darkdevil
Автор

I always thought these videos were timelapsed, then i looked at the clock at the top of your screen. You must type at like 300 wpm!

baileyharrison
Автор

awesome Video.
I am not the best at programming (Just wrote couple little things etc for myself in C#).
This Video gives me alot of motivation. As a "noob", C++ really looks scary somehow :P

salatwurzel-
Автор

Man I recently tried to make my own Tetris clone in C++, but I just couldn't. I been programming for just over a year now, and apparently according to some Tetris is considered a beginner project which I just don't understand at all.

I tried creating it without reference. I defined my own Tetris Area class and that had a std::vector of size 20 * 10, and so I could access specific "tiles" with y * width + height. This was an std::vector of "squares", another class I made that had just two members: a Boolean to say if it was empty and a colour value.

So I filled out the vector with squares etc and it worked fine.

But then the shapes... How on earth are you meant to represent shapes? Watching your video, you have a method called MakeNext, which chooses a random piece array, (just curious, why do you use hexadecimal for that?) which I guess is a good way to do that.

But adding it to the the original playing area vector, and then having it move while having the other pieces that...

Hopsonn
Автор

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

Bisqwit
Автор

Such beautiful code... and then you add a goto as a finishing touch.

JackTheSpades
Автор

Thanks for writing. I admit the uses of lambdas in this Tetris clone are rather gimmicky re:benefit. But lambdas are actually useful in countless real situations. Not that there isn't another way to do the same, but often lambdas end up being most concise. It quickly tells the intention and the reader can move on. Examples include functors (e.g. for std::sort) and inline functions (avoid code repetition inside a function). See my epromread video for examples on the latter. Also: Matter of taste.

Bisqwit
Автор

@BadPotat0 Thanks. I created the editor for the other video I was supposed to publish a month ago, because I was unsatisfied with the features of Borland C++ IDE (mainly, BC IDE only supports 80 characters wide display modes). And having made it, I decided to use it here as well.

Bisqwit
Автор

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

Bisqwit
Автор

@alexliang0627 This video is a choreographed programming performance. The source code was designed and debugged beforehand to the smallest detail. Only the playing was impromptu. I will put the source code available for downloading some day soon. I am surprised to find that I forgot about it. Thanks for reporting!

Bisqwit
Автор

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

Bisqwit
Автор

B-Wing Very Very awesome hardest game ever!!! Nice work Nice video

velozoefurioso
Автор

Maybe you can get a deal with the owners of Tetris ;)
Love watching your educational videos. XD
This is my favorite youtube channel. I wonder if schools and or books teach Tetris Pacman Asteroids and Frogger Clone demos where you live aswell? It seems that when students want to make their favorite new games sometimes they can be convinced to make a clone of an old game instead. I made a Pacman like game because I had a completely different idea of eating dots. I guess namco might have seen it, and I got lucky that they didnt sue me to my knowlage, It started off as a programming lesson, and I went a totally different direction. You should too I think, I have been learning that when I try and make something old new, people advise me that even tho I am making it as a demo for the company, you should in my advice get their permission first. I would not now make a demo and show it to them, I would request that I may make a demo for an old videogame licence holder, and get permission. Rather than trying to surprise them with a great unfinished demo. Also do not assume that these very old games are public domain.
That it turns out is a common misconception at least where I live. If I could do it all again, Instead of making a great demo and assuming that very old videogames owners would appreciate a free upgrade, as I said, I would in learning, request permission first. Nintendo have been very kind to me I have not heard so much as a peep from them about my tinkerings. I hope they have seen that people would like more than Super Mario Maker, and would enjoy a console system or app where they can skin premade games with new graphics of their making. Like simple moving graphics with no images preassigned.
Best wishes imaginable,
- Myles Johnston

uMiLO
Автор

Thanks for the report and feedback. Problem fixed!

Bisqwit