• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Tetris AI?

## Recommended Posts

How would I make a good Tetris AI? Have the AI make certain moves for certain moves the player makes or just random moves?
Wachar''s Eternity <-<-<-<-<- Me own site!

##### Share on other sites
Random moves in Tetris? Well, the AI should do their best to build as many full lines as possible. I don''t think you have to take special care for the player''s moves...

Yesterday we still stood at the verge of the abyss,
today we''re a step onward!

##### Share on other sites
The way I''d do it is for the current piece, try to fit it in
the best way possible, using every orientation of the piece.
How to define best ? That''s up to you. It can be
completing the most lines or leaving the fewest holes .

*** 500 Internal Server Error ***

Kami no Itte ga ore ni zettai naru!

##### Share on other sites
OOps, I meant to say Tic Tac To3!!!!!

Wachar''s Eternity <-<-<-<-<- Me own site!

##### Share on other sites
quote:
Original post by Wachar
OOps, I meant to say Tic Tac To3!!!!!

Wow, how could you get Tic Tac Toe confused with Tetris?

Kami no Itte ga ore ni zettai naru!

##### Share on other sites

And read the reception the game got
• OMG, you''re dumb!!

##### Share on other sites
Wachar,

If you don't want to download and dig through my source, here's how I did that AI....

First, I checked to see if the Computer could win in one move. If he could, Move there.

If not, I checked to see if the Player could win in one move. If he could, Block him.

If not, I checked to see if there was any spot on the board where the computer could make 2 in a row, and be one from winning.

If not, I tried moving to the middle square.

If that didn't work, I moved to a random open position.

This does amount to a TON of code, and there may be a better way, but it works. Here's an example.
----------
The way I checked the board was writing 8 blocks of code for the 8 different rows that you could win on and checked to see what was there. I did this for each of the scenarios above.

For example....

  // This code will check the top Row to see if the computer can win in one move.// XX_// ???// ???if (pBoard[1] == CPU_MOVE && pBoard[2] == CPU_MOVE && pBoard[3] == BLANK){    movespot = 3;}else if (pBoard[1] == CPU_MOVE && pBoard[2] == BLANK && pBoard[3] = CPU_MOVE)// X_X// ???// ???{    movespot = 2;else if (pBoard[1] == BLANK && pBoard[2] == CPU_MOVE && pBoard[3] = CPU_MOVE)// _XX// ???// ???{    movespot = 1;}pBoard[movespot] = CPU_MOVE;

Hope this helps!

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

edit - Fixed the code

[edited by - Radagar on August 12, 2002 12:09:25 PM]

##### Share on other sites
Thankee Grand master!

Wachar''s Eternity (To be changed)<-<-Me own site!
-----------------------------
Thrander<--Formerly know as Wachar

##### Share on other sites
quote:
Original post by Thrander
Thankee Grand master!

Wachar''s Eternity (To be changed)<-<-Me own site!
-----------------------------
Thrander<--Formerly know as Wachar

Grand Master? I''m hardly an apprentice =)

I''m still learning this myself. that Tic-Tac-Toe program took about all I knew. I''m not sure I can do Tetris yet, but I''m trying.

Glad I could be of help.

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

##### Share on other sites
Try doing a search on google for ''Minimax Algorithm''
I believe you will find some examples, many using Tic Tac Toe.

• ### Forum Statistics

• Total Topics
628281
• Total Posts
2981797

• 10
• 11
• 17
• 14
• 9