File Exchange

image thumbnail

Tetris (vs AI)

version 1.12.0.0 (9.15 KB) by Jan
An extended tetris game, using the plot function. You can either play by yourself or against an AI.

5 Downloads

Updated 15 Dec 2011

View License

----------------------------------------
Tetris:
----------------------------------------

Gameplay:
- move: arrows
- rotate: z and x
- pause: p

My highscore for the given standard configuration is now 2680 ;)

----------------------------------------
Tetris AI:
----------------------------------------

The game tetris is played by the computer, which chooses the move that has a maximum blockvalue after landing, where:

blockvalue = number of edges that touch a wall or other block - average height of the block.

Of course other factors can and maybe will be added to blockvalue: we could punish gaps, reward full rows, etc.

After 500 runs, the results of the standard configuration are:

mean: 1733 (160 lines)
worst: 260 (26 lines)
best: 8080 (748 lines)

----------------------------------------
Tetris versus the AI:
----------------------------------------

The AI and the original Tetris game are combined in one game. First you make a move in your own field and then the AI makes a move with the exact same block in the other field.
Try to beat the AI!

Cite As

Jan (2020). Tetris (vs AI) (https://www.mathworks.com/matlabcentral/fileexchange/33701-tetris-vs-ai), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Hello!!! there I currently taking this subject which is called COMPUTER VISION AND IMAGE PROCESSING and this subject has linking to implement a game. So i required to implement a tetris game and i also new to matlab. Is there anyone there can teach me how to implement tetris game step by step because i want to what happen on each of function is being created instead of just copy and paste people code to the matlab. I hope to get reply as soon as possible. Thank You

Jan

Thanks for your extensive explanation Mingjing Zhang! As soon as I have some time left I will incorporate this in the next update.

"Has anyone an idea about setting a fixed time a block has to wait before it moves down one line?"

There are many ways to do it. You can either make use of a "timer" function, which runs in a given interval. Or use a structure such as the following:

fall_interval = 1.2

start_time = tic;
last_time = start_time;

while 1
current_time = toc(start_time);
if current_time-last_time>=fall_interval
block_fall();
last_time = current_time;
end
process_keyboard();
...
end

start_time is the time when a block starts to fall; last_time is the time when a block moved down; current_time is...current time.

This structure can be further improved. For example, it is not perfect to set last_time = current_time. Because if the fall_interval = 1.2
Then "last_time" (the expected time when the moving occurs) should be one of the following numbers: 1.2, 2.4, 3.6, 4.8,... Maybe you can add a counter to record how many lines have a block moved down since it appeared, and calculate the time the last fall "should have occurred".

If you want, you may check my game "Stellaria" to see how timing is handled.

Good luck

Jan

I don't know anything about the GUI, so I am afraid a can't help you with that.

i want to make the live video using gui ..when i press the push button video is display in the axes and when i push the other push button then video is stop

Jan

Thanks for your suggestion George, but I don't see how that could work: pause() pauses the execution, but (multiple) keyboard input should be processed immediately.
(Maybe it is possible with some comparison in time, maybe even using tic and toc - I dont know yet.)

Rado

Extremely cool!

George

you can use pause() function

Alex Bakken

Nevermind, I was using space not z/x. X & space make it rotate CCW, but z is CW, the way I'm used to.

Alex Bakken

How can you make it so that the objects rotate in the opposite direction (clockwise)?

Jan

Has anyone an idea about setting a fixed time a block has to wait before it moves down one line?

In the current implementation the waiting time is obtained by counting up to a fixed number and hence performance varies per computer.

Updates

1.12.0.0

The Tetris_vs_AI game is added.

1.9.0.0

AI added

1.6.0.0

Found and corrected an error in createblock()

1.5.0.0

Added 6 levels (level = certain set of blocks)

1.4.0.0

Found and removed a small error in the line removing loop.

1.3.0.0

Scorekeeping added

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Tags Add Tags
Acknowledgements

Inspired: MATLABTETRIS