How can I make a bot to play Tetris?

View previous topic View next topic Go down

How can I make a bot to play Tetris?

Post by Daxaal on 1/12/2012, 11:19 am

I am interesting in creating a bot to play tetris these christmas holidays Smile

The tetris game I am talking about is this
http://www.tetrisfriends.com/games/Marathon/game.php

So how can I create it like what programming language should I use and how do bots know what to do in a game? Like do they read the image? code? Or what and what do you recommend I do when trying to create this bot to play tetris.

if you can give any example codes for creating this that would be great Very Happy If your post helps me with creating this tetris bot I'll give you a rep+ Very Happy

avatar
Daxaal
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by Broeder on 1/12/2012, 11:34 am

From what i understand you first write the code, and then implement the pictures and link them together so it shows as a game.

If i were to make a tetris game, it would probably be something mathematical, like with matrices and some basic multiplication. Only language i know is python and while i could probably code it, idk how i would make the graphics work
avatar
Broeder
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by bs merchants on 1/12/2012, 1:57 pm

Not really allowed to discuss in game bots, so I'll be vague.

There is a form of Pascal that allows colour detection. Using this you can 'visualise' the grid, and the pieces. Once you've done that, it's easy.

Construct the grid as a 2 dimensional array of booleans, false for tile empty, true for tile filled. If an entire row becomes true, overwrite with all rows 'above' it (and reset the top row to all false).

For identifying where the piece should go, you could use a trial and error method for slotting it in a position that would achieve the highest score, where you score each row individually and add them up. You can score a row individually by counting the number of trues and falses, and make it so that complete rows/nearly complete rows are worth lots more than near empty rows. You can also make each row score be multiplied by an increasing factor, depending on how far down it is (so the bottom row is worth more).

Keyboard input isn't difficult either, using the same program that can detect colours.

Once you've done that, you can consider much more advanced strategies which you can probably find by googling for algorithms for botting tetris, but make sure the program works first.
avatar
bs merchants
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by Ben348 on 1/12/2012, 2:06 pm

Ok so there are two options here for you, one harder than the other.


1) This is probably the easiest option for you. Like many of the old RS bots, they rely on detecting events on the screen by color and bitmap searching. This means for your tetris game you'd have a function/-s to detect when a new tetris object appeared and the orientation of it.

The http://villavu.com/ SRL resource library is pretty good for this - has some good functions that will save you time making yourself and they have been optimized to be very fast. A few others and myself built a similar library a few years ago, although very outdated I'll try and find it for you if I can - might be helpful.


2) Many of the modern RS bots these days work this way. They read the code of the client to determine what is what and perform certain actions based on that. This is probably out of scope for you, it would require an extensive knowledge of programming to complete from scratch and for something like this I wouldn't say its worth the effort.


My advice is to do it based on image and color detection. I'd make the "bot" build a multi-dimesional array of information that would replicate what appears in the tetris client. From this you would be able to detect what object would best fit where.

1) Create a data structure to replicate the testris grid
2) Detect when a new object appers - including the object type + orientation.
3) Using a custom algorith you would determine where on the grid is the best place for the object.
4) Send the key combinations to change and move the object to the correct position.

* Possible include a timer which would monitor how long you have till the object will reach a certain position.

As for a language, it really depends on your knowledge of programming and how advanced you are. A project like this is fairly big and not something for a beginner. Just a guess, I could be wrong, but I'd say you're fairly new to this type of stuff so I would probably go for Python or C# - there will be lots of examples in those languages around the internet for stuff you need to do, however a lot of the libraries you'd end up using can be utilised in many more languages.

If you can I'd say try and stay away from Java, although it has its uses and there are occasions where it is the best language for the job (Such as RS bots) for anything else its not really that great, but I'm not going to go into my reasons for this - that's a different topic all together and everyone has their preference.

Best of luck with the project and if you need any more help be sure to post back Very Happy
avatar
Ben348
Tier 1 (Registered)
Tier 1 (Registered)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by bs merchants on 1/12/2012, 3:52 pm

Villavu is the Pascal client I was suggesting. It makes sense to use pascal for the main body of the program too, but of course there is nothing to stop you.

Ben's second suggested method is using and abusing the fact that OpenGL stores some information in the memory of your computer when running programs. Whether or not this can be utilised for a flash game I do not know, but its a very difficult thing to perform so I would not recommend considering this at all.
avatar
bs merchants
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by Broeder on 1/12/2012, 3:56 pm

Oh i read it as making a tetris game, my bad. disregard my post :p
avatar
Broeder
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by Swadq on 1/12/2012, 10:59 pm

Start out by solving arbitrary tetris problems in your code. Then expand that so that it solves tetris problems inputted from some other source (screenshot/colour picker/whatever). Then, translate "solutions" to mouse/key events
avatar
Swadq
Grandmaster (2000 posts)
Grandmaster (2000 posts)


http://swadq.com

Back to top Go down

Re: How can I make a bot to play Tetris?

Post by Daxaal on 4/12/2012, 6:03 am

Ben348 wrote:
Spoiler:

Ok so there are two options here for you, one harder than the other.


1) This is probably the easiest option for you. Like many of the old RS bots, they rely on detecting events on the screen by color and bitmap searching. This means for your tetris game you'd have a function/-s to detect when a new tetris object appeared and the orientation of it.

The http://villavu.com/ SRL resource library is pretty good for this - has some good functions that will save you time making yourself and they have been optimized to be very fast. A few others and myself built a similar library a few years ago, although very outdated I'll try and find it for you if I can - might be helpful.


2) Many of the modern RS bots these days work this way. They read the code of the client to determine what is what and perform certain actions based on that. This is probably out of scope for you, it would require an extensive knowledge of programming to complete from scratch and for something like this I wouldn't say its worth the effort.


My advice is to do it based on image and color detection. I'd make the "bot" build a multi-dimesional array of information that would replicate what appears in the tetris client. From this you would be able to detect what object would best fit where.

1) Create a data structure to replicate the testris grid
2) Detect when a new object appers - including the object type + orientation.
3) Using a custom algorith you would determine where on the grid is the best place for the object.
4) Send the key combinations to change and move the object to the correct position.

* Possible include a timer which would monitor how long you have till the object will reach a certain position.

As for a language, it really depends on your knowledge of programming and how advanced you are. A project like this is fairly big and not something for a beginner. Just a guess, I could be wrong, but I'd say you're fairly new to this type of stuff so I would probably go for Python or C# - there will be lots of examples in those languages around the internet for stuff you need to do, however a lot of the libraries you'd end up using can be utilised in many more languages.

If you can I'd say try and stay away from Java, although it has its uses and there are occasions where it is the best language for the job (Such as RS bots) for anything else its not really that great, but I'm not going to go into my reasons for this - that's a different topic all together and everyone has their preference.

Best of luck with the project and if you need any more help be sure to post back Very Happy

What would be a starting code to recognise a Red block? I have no idea how to write pythaon or C# Razz I know some java and that's it Razz

@BS Merchants - thanks for the info Smile gave you a +Rep I got an idea now on how to create it just have to learn the language Razz. I knew runescape bots discussion wasn't allowed I didn't know it applied for all bots.

I have no intentions on harming the game just want to learn about programming languages more and how to create simple artifical intelligence programs, I chose tetris because it is a really simple game and doesn't affect others that much because it is one player
avatar
Daxaal
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by Ben348 on 7/12/2012, 2:20 pm

You'd want to look in cross-correlation for detection, quite a few good articles and journals on the internet which go into detail. But anyway, as you know Java, C# wont be that different or hard for you to pickup.

One simple way which would only be a few lines would be to:
1) Take a screenshot of the screen
2) Find an image in an image

However that isn't a practical solution for something like this.

What I suggest is looking into http://www.aforgenet.com/ it's a brilliant .NET framework for AI and image processing and if you want to see some code this is a good project too which will help you http://www.codeproject.com/Articles/19323/Image-Recognition-with-Neural-Networks

Detection isn't the hard part, it would be writing the logic to determine the best fit for the blocks.
avatar
Ben348
Tier 1 (Registered)
Tier 1 (Registered)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by bs merchants on 7/12/2012, 5:02 pm

As pointed out, Villavu offers easy-to-access and use colour detection systems along with mouse and keyboard inputs (and outputs). The language is Pascal which is trivial to learn (like Visual Basic), and is often used as a generic pseudo-code within groups/companies.

I can't help too much on the Java front, but C# is very much like it and probably more useful in this case.
avatar
bs merchants
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

Re: How can I make a bot to play Tetris?

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum