A black and white game A written in JavaScript

Source: Internet
Author: User

Lai Yonghao (http://laiyonghao.com)

First, I did not write this code, but I added the comments. Author is shaofei cheng, his website: http://shaofei.name

Second, currently, this code only uses alpha-beta pruning, which has a weak playing force and has a lot of room for optimization. However, the Code is clearly written. If a friend is interested in the topic of man-machine chess and has not been started yet, this code is a great example.

Third, at present, computers can only search for three layers. I think it is not a problem to search for five layers after iterative deepening and historical inspiration algorithms are added. The performance of modern JavaScript is good.

Fourth, the author shows a lot of skills in the code, which is worth learning and learning from. Even if you don't understand JavaScript, you can easily understand the code (I don't understand it ).

Fifth, try this AI chess: html "> http://shaofei.name/OthelloAI/othello.html

The following code is used:

View plaincopy to clipboardprint?
Var AI = {};
New function (){
AI. Pattern = pattern;
// Defines 8 Offsets
// You can simply use addition to obtain the coordinates of any eight points around a point.
//-11-10-9
//-1x1
// 9 10 11
// For example, the coordinates in the upper left corner are x + (-11)
Var directions = [-11,-10,-9,-, 9, 10, 11];
Function pattern ()
{
// Fill the entire board with 0
For (var I = 0; I <100; I ++) this [I] = 0;
// Four grids in the middle. Put two black and two white pawns first
This [54] = this [45] = 1; this [55] = this [44] = 2;
// The number of Black wins (black minus white), which is used for valuation.
This. divergence = 0;
// Currently, you can play the game in black.
This. color = 1;
// A few moves have been taken
This. moves = 0;
// Stable prototype
// 0 is blank, 1 is black, 2 is white, 3 is border
// It is a skill to extend the 8*8 chessboard to 10*10.
// Simplifies the determination of coordinate Validity
Var stableProto = [
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3
]
// Load the status from an 8*8 chessboard
This. load = function (arr)
{
For (var y = 1; y <= 8; y ++)
{
For (var x = 1; x <= 8; x ++)
{
This [y * 10 + x] = arr [Y-1] [x-1];
}
}
}
// Determine whether pass is allowed
// If yes, the player can be changed.
This. pass = function ()
{
For (var y = 1; y <= 8; y ++)
{
For (var x = 1; x <= 8; x ++)
{
If (this [y * 10 + x] = 0)
{
// You cannot Pass any step of the game.
If (this. move (x, y, this. color ))
{
Return false;
}
}
}
}
// Alert ("pass ");
// This is a trick, because the value of this. color is {1, 2}
// When the color is 1, the next statement is 2.
// When the color is 2, it is 1 after the next statement is executed.
This. color = 3-this. color;
Return true;
}
This. clone = function ()
{
Function pattern (){}
Pattern. prototype = this;
Return new pattern ();
}
This. toString = function ()
{
Var icon = ["", "*", "o"]
Var r = "";
For (var y = 1; y <= 8; y ++)
{
For (var x = 1; x <= 8; x ++)
{
R + = icon [this [y * 10 + x] + "";
// R + = stableDiscs [y * 10 + x] + "";
}
R + = "";
} & Nb

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.