The simplest example of game theory Tactictoe

Source: Internet
Author: User

Game theory is a branch of artificial intelligence. As the name implies is the algorithm of chess. Of course, the extended application may be used not only for playing chess, but also for playing games or simulating war strategies.

Game of the basic algorithm is also simulated human thinking, such as when the next step through all possible to seek the most advantageous steps, but a way if not a step to win the other side may take the most unfavorable to their own way, if the other party can not win, and then further consider the other side of the law of their best response is what, So recursively go down and find the odds of each path and take the best strategy.

Pseudo-code can do this:

function Find_best (P)
{
var Bestsolution=worst;
For every possible step S after P:
if (Willwin (s))
return S;
else if (Willend (S))
if (S >= bestsolution) bestsolution = s;
Else
var w= find_worse (S);
if (W >= bestsolution) bestsolution =w;

return bestsolution;
}

The Find_worse function should have similar logic, the only difference being that the Find_worse judgment logic, in turn, finds the most advantageous solution to the other.

The simplest algorithm is described above, in fact, in a slightly complex board game, this algorithm does not work. The reason for this is that the algorithm recursively iterates through all the possibilities, and in many board games, this is time-consuming. If you pre-calculate all possible and too much storage space. This often requires recursion to a specific depth to end, and because often can not search for the final win or negative outcome of the game, you must have an evaluation function in either state to evaluate the score. For further optimization, you might want to crop the traversed path. For example, the Gobang does not consider solutions that are too far from the existing ones, and so on.

Since we only study the simplest case today, the above algorithm can solve the problem. We take the TIC as an example, we can traverse all the cases, and the recursive algorithm directly does not cause stack overflow-a total of only 9 steps in depth:).

Here's the code: http://www.luoxq.com/tic.html

The simplest example of game theory Tactictoe

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.