For the Silverlight version, see [Silverlight development].

Source: Internet
Author: User
ArticleDirectory
    • Simple implementation
    • Why is the first step at the corner the best strategy?
    • Simple Description of the algorithm behind this program

Tic-tac-toe, also spelled tick tack toe, or noughts and crosses/Xs and OS as it is known in the UK, Australia and New Zealand, is a penpencil-and-paper game for two players, X and O, who take turns marking the spaces in a 3 × 3 grid.

It may be the simplest type of chess game. It's as simple as a draw between adults. Therefore, it seems that this game can only cool at most. I was suddenly interested in the game. I saw an image, which explains the best preferred strategy. Then I checked the details on the Internet.(Original article). Player player can be regarded as one of the simplest decision tree games. In history, the similar analysis of the game player inspired many people to understand the combined game and played an important role in game theory. As a result, I want to use Silverlight for simple implementation.

Simple implementation

Install the Silverlight environment.

Why is the first step at the corner the best strategy?

First look at the following figure, click to view: http://xkcd.com/832/ & statement: The following is reproduced, copyright belongs to the original author all: matrix67

Digress: I like this kind of scientific articles very much. There are many articles of this type such as fruit shells and pine tree scientific society, which can stimulate my new views on things.

Preemptible

After the game starts, if you don't say anything, take the first place (for example, in the lower left corner), then the other party has five different strategies: Take the side close to you, occupy the corner close to you, the side away from you, the corner away from you (that is, the diagonal corner), and the center position. What is incredible is that the first four are traps in these five strategies-if the other party accidentally chooses any of the first four strategies, he will inevitably lose.

It shows how you can beat each other in these four situations step by step. If the other side is at the bottom and occupies a side close to you (the leftmost figure), you can occupy the center to handle the problem and force the other side to go to the upper right corner. At this time, you only need to put a child in the upper left corner, and you will be able to win. The figure shows two lines with only one child, and the other party cannot take both of them into consideration. The pictures on the right show the other steps of the opponent's first move. If you select a proper position to deal with him, you can force the other party to have only one way to go. Then you can play the game of "One arrow and two arms", so that the other party cannot block you completely. That is to say, when you occupy the corner of the Board, in the eight optional positions of the opponent's next game, seven of them are mandatory, and the trap is filled with almost the entire board. In the face of the strange start of "first corner", if your friends say no, they will be panic and fail to calmly occupy the middle, which determines the fate of defeat.

If the other party is in the middle, you still have a chance to win. You can occupy the position (for example) in the upper-right corner ). If the other party unfortunately goes through one of the remaining two corners (left), you will be able to retry and win again. Only when the other party chooses the position on the edge (right) can they escape this series of traps and eventually become a draw.

Second-hand preemptible

As a practitioner, you often encounter a classic start of "going first. At this time, do not forget that the first role is still a golden rule. If you accidentally leave the position of a side, the other side will win! The other party can respond to the first picture above in the right corner, forcing you to move the next game to the right. At this time, the convenience can occupy the position in the upper right corner, and two lines with only one child are generated at the same time. The three pictures on the right show that, if you deal with it in the corner, it will always be a game.

Ben Program Behind Algorithm Simple Description

In fact, the algorithm behind it is very simple, just a simple simulation of human thinking, and it is likely not the best implementation method.

First, two matrices are defined, one representing nine positions on the board and the other being the weight matrix. Explain the weight matrix. The weight here refers to the position in which the computer gets the higher weight when thinking about the next step. Therefore, the issue of determining the weight is involved here. Normally, the "victory" weight is the largest, that is, if the computer can win, it will be the next place in the computation process, then, to prevent the opponent from winning, that is, when the player has two pawns and an empty position, the position is blocked. This is two basic points, and then the problem is determined at the basic position. See the followingCodeIs to set the "victory" weight.

If (Smatrix [I, 0 ] + Smatrix [I, 1 ] + Stmatrix [I, 2 ] =   2 )
Weightmatrix [I, j] + =   6 ;
// If a line can be connected, victory

At first, I set a computer with a higher weight at the midpoint. When I saw the analysis of the article, I set a higher weight at the corner. Maybe because the game was simple, I couldn't see the difference between the two. Then, the last weight must be considered to form two connected pawns. The final weight ratio should be: "Win"> "prevent players from winning"> "bottom corner"> "center bottom"> "connecting two pawns ",Finally, if the weights are the same, an empty position is randomly located.

Among them, after each sub-program, a function is called to check whether the game is over. These are all small details. Here is a simple description.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.