Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tetris + AI  (Read 3535 times)

0 Members and 1 Guest are viewing this topic.

Sub

  • Full Member
  • ***
  • Posts: 152
    • View Profile
Tetris + AI
« on: February 24, 2013, 11:26:27 pm »
I made this back in October, but I think posting about it now can't hurt. 

There was a 48 hour game jam that I participated in, but I couldn't think of anything that I wanted to make with the theme, so I just decided to make and submit Tetris for a laugh.

After I had a working game of Tetris, I thought it might be fun to expand on it with an AI.

Video of the AI  -- https://www.youtube.com/watch?v=vkUx0RRj4Yc

Brief explanation of the AI -- It analyzes every possible location you can put the current shape and assigns each possible location a score based on aspects like height, how many lines you clear, etc. The highest score is the location chosen. There's also a bit of AI learning in that the value of each aspect of score is determined by a process similar to natural selection. Run a few games with different values, pick the best performing values and change them slightly and repeat the process and it should improve. For example, if the game started off with assigning height a positive score, then after a few generations it should see a downward trend since placing blocks high up is generally not a good strategy to get a good score.

This article explains the whole thing -- http://www.danieljosephpetersen.com/posts/how-my-tetris-ai-works.html

In this version, the AI learning is turned off.  It's always going to be using the same values, which actually weren't the best that I found (it's not as good as the video), but I unfortunately have a bunch of values saved and am not sure which ones were the best, I'd probably be better off just rerunning the thing again overnight.  There's a ton of room for improvement, but I did it over the course of a week and I'm happy enough with it.

Controls:
Enter turns AI on/off
Period (.) key increases time between ai moves
Comma (,) key decreases time between ai moves
« Last Edit: December 08, 2016, 02:58:55 am by Sub »

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Tetris + AI
« Reply #1 on: February 26, 2013, 09:06:00 am »
Very cool. I'm surprised though that he cannot go on forever. I suspect that this is because he doesn't plan ahead and does not know when he actually loses, or should the adaptable AI solve this?

Sub

  • Full Member
  • ***
  • Posts: 152
    • View Profile
Re: Tetris + AI
« Reply #2 on: February 26, 2013, 06:08:20 pm »
The only planning it does is analyzing the best position (as far as it can discern) for the immediate piece it's placing.  In later versions I made it also analyzed the best position (again, as far as it can discern) for the current piece + next piece, but that slowed the program down quite a bit.  I think at its best it was getting around 3,000 lines per game.

There are programs out there that actually plan strategies for the AI to follow.  For example, they'll neatly stack everything except for one line on a side, and then wait for an l piece to get 4 rows at once.  Mine doesn't do anything like that.

Someone correct me if I'm wrong, but I think even the best Tetris AI's can't go on indefinitely.  There are just some piece combinations that will kill you no matter what you do. 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9573
    • View Profile
    • development blog
    • Email
Re: Tetris + AI
« Reply #3 on: February 26, 2013, 06:21:27 pm »
Someone correct me if I'm wrong, but I think even the best Tetris AI's can't go on indefinitely.  There are just some piece combinations that will kill you no matter what you do. 
I'm not an expert on AI nor Tetris, but the tetris pros mostly fail at the speed and not at the pieces combinations, thus I guess it is possible.
I quickly googled and found this article, maybe you want to give it a read? ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Tetris + AI
« Reply #4 on: February 26, 2013, 06:28:04 pm »
Statistically you'll eventually get a series that's impossible to solve, assuming perfect random number generation. Of course since computers struggle with that whole perfectly random thing it's debatable.
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.