Page 1 of 2

HALtris - A.I. Tetris Offshoot

Posted: Tue Dec 31, 2013 11:01 am
by numandina
Hello.

I had to learn löve in the space of a weekend for a job application, and I ended up programming this tetris game where you are pitted against an AI who "chooses" which pieces are being handed to you in each turn. I liked the end result so much I've decided to post it here.

Turning the AI off will choose pieces randomly (regular tetris), while setting it to "1" (evil) will give you the worst possible piece for the current board configuration, and setting it to "-1" will give you the best possible piece.

The code is fully commented and although it can be made better, consider this just an alpha build (if I get feedback I may update it) and the code can be tidied up plenty. Included are playing music and having a rudimentary menu, and I've also displayed an "AI thinking process" board on the right that gives insight on how each piece was chosen. You can turn this off if you want to play without waiting.

The algorithm takes in several factors which are weighted and added up. You can easily edit these in the code. So far, there is only ONE case which enables you to "beat" the evil AI (try to figure it out). After fixing that I think it would really be impossible to get but one single line off of it. I've seen some similar games online but they aren't very robust and their AI can be easily beaten.

To make things entertaining, there is the option of switching difficulty in real time, which means you can pit the good AI versus the evil AI (by alternating between the two), or use the good AI to help you when you're in a tight spot.

Please check it out and give me feedback. Thanks! :)

Google Drive Link: https://docs.google.com/file/d/0B0etIhI ... R3Mkk/edit

EDIT:
Link for löve 0.9.0: https://docs.google.com/file/d/0B0etIhI ... =drive_web

Re: HALtris - A.I. Tetris Offshoot

Posted: Tue Dec 31, 2013 4:33 pm
by davisdude
Sounds like it's fun! I can't play it, though. Here's the error:
HALtris wrote:Error

maingame.lua: 78: attempt to call method 'setLifeTime' (a nil value)

Traceback

maingame.lua 78: in function 'load'
main.lua 20: in function 'load'
[C]: in function 'xpcall'

Re: HALtris - A.I. Tetris Offshoot

Posted: Wed Jan 01, 2014 10:46 am
by shatterblast
I get the same error.

Re: HALtris - A.I. Tetris Offshoot

Posted: Wed Jan 01, 2014 7:33 pm
by XCaptain
This works fine in love 0.8.0.

This AI is very impressive :awesome:! It will never let you finish a line in mode one. never, not one, not even one line :|.
This AI is pure evil.

Re: HALtris - A.I. Tetris Offshoot

Posted: Thu Jan 02, 2014 1:26 am
by riidom
Hi, I would like to try this too.
However, you could improve a few things maybe:
a) Don't go fullscreen right from start.
b) Take care for other people computers resolutions. It is sadly impossible for me to play, since I see the screen just partially.
c) It uses >600MB of RAM after 10 seconds of calculations. I know it is HAL, but still! :)

Re: HALtris - A.I. Tetris Offshoot

Posted: Thu Jan 09, 2014 9:20 am
by numandina
Here is a version that works in löve 0.9. I removed the particle system.

Please note that the game starts paused for some reason in 0.9, so press the pause button twice to unpause.

Looking forward to more feedback!

Re: HALtris - A.I. Tetris Offshoot

Posted: Sat Jan 11, 2014 2:47 pm
by Germanunkol
This is really great.

Maybe you should make an AI-mode that's somewhere between 0 and 1, though, because, as XCaptain said, it won't ever let you finish a line in mode 1... so the game's impossible to win.

Great idea, though... and it works really well.

Also liked playing in mode -1 :D ... probably my best game score ever :)

Re: HALtris - A.I. Tetris Offshoot

Posted: Tue Jan 14, 2014 8:15 am
by numandina
That's a good idea. Added to the list.

P.S. It is not impossible to win a line in difficulty 1. There is one case, try to find out what it is.

Also if you find the game boring, try switching between difficulties in real time while playing. For example choose difficulty 1 at start, then after half the board is filled choose difficulty -1 to see what happens next.

Re: HALtris - A.I. Tetris Offshoot

Posted: Tue Jan 14, 2014 8:38 pm
by YellowAfterlife
Interesting idea. I did experiment with Tetris "doing player a favour" some time ago, but never thought of turning that around to increase difficulty.

If I'm not mistaken, this is the case when AI gives in - it does not counter "manoeuvring" around blocks:
Image

Also there's a bug being that if you flip a tetriminoe next to a wall, it can get stuck in that in a variety of ways, erasing a piece of wall in process. This can be exploited to score a decade of lines or two, provided that game does not just drop you 20 square blocks in a row. That said, block variety is a problem of both modes -1 and +1. Game will often drop the same block over and over because factors are not changing. Could have a bit of randomness to that.

Without much exploitation, here's the farthest I got at mode+1
Image

Re: HALtris - A.I. Tetris Offshoot

Posted: Thu Jan 16, 2014 11:55 am
by numandina
Thanks for the bug report. I could've sworn I tested that intensively. Added to the list.

I (and the AI) did not take into account maneuvering around blocks, which would really complicate the engine. Maybe I should take into account...

The one case I was talking about was a mathematical case, namely this:

Image

Reaching this particular block structure means that any of the blocks will enable you to finish a line, a result of the AI only looking forward one step.