1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java实现井字棋 人工智能 在井字棋人工智能使用C#

java实现井字棋 人工智能 在井字棋人工智能使用C#

时间:2019-01-14 10:49:56

相关推荐

java实现井字棋 人工智能 在井字棋人工智能使用C#

I have made a Tic-Tac-Toe game for 2 players. Now, I want to give the game Artificial Intelligence.

So that game can be played between 1 player and computer.

Please, help How do I start?

解决方案

With Tic Tac Toe it's not so much an AI but a lookup table: For each possible board layout, find the best spot.

XKCD has such a lookup table. Basically each Board Layout gets a unique ID and the address of the field where to set the next mark. Wikipedia has that table in another format.

The table works like this: X goes first, then O.

X puts his X into one of the 9 cells. When O goes, there are now 9 possible Board Layouts, depending on which Cell has the X:

X | |

----+----+----

| |

----+----+----

| |

If you look at the map of O, there are 9 big grids in it, and the one in the top left has X in the top left spot, so that is the one to use. Place O in the Middle.

Now when X goes again, it needs to find this board layout:

X | |

----+----+----

| O |

----+----+----

| |

You will find this in the middle. Red is where to put the X in the XKCD image, and that shows you put it in the lower right:

X | |

----+----+----

| O |

----+----+----

| | X

Now, O goes again and looks for the above board layout, which is in the bottom right small grid in the top left big grid. O needs to be placed into the middle bottom:

X | |

----+----+----

| O |

----+----+----

| O | X

And so forth. The diagram is a bit hard to read at first (click on it to enlarge it) as it's nested, but as said: You create a Lookup table that has each unique board layout and information where to put the next mark.

This creates a perfect opponent though: The computer will never ever lose. How to make him more human is then fine-tuning (e.g., randomly discard the choice and place the mark in a random cell)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。