Home  >  Article  >  Web Front-end  >  An Othello Ace written in JavaScript

An Othello Ace written in JavaScript

高洛峰
高洛峰Original
2016-11-25 10:26:281900browse

First of all, this code was not written by me, but the comments were added by me.

Second, this code currently only uses alpha-beta pruning, which is still weak and has a lot of room for optimization. But the code is written very clearly. If there are friends who are interested in the subject of human-machine chess but haven’t gotten started yet, this code is great as an example.

Third, currently computers can only search 3 layers. I think with the addition of iterative deepening and history-inspired algorithms, it will not be a problem to search 5 layers. Modern JavaScript performs well.

Fourthly, the author has demonstrated many techniques in the code, which are worth learning and learning from. It is easy to understand the code even if you don’t understand JavaScript (I don’t understand either).

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

The following is the code:

view plaincopy to clipboardprint?
var AI = {};
new function(){
AI.Pattern= pattern;
// 8 offsets are defined
// You can simply add 8 points around any point to get The coordinates of
// -11 -10 -9
// -1 -9,-1,1,9,10,11];
function pattern()
{
// Fill the entire board with 0s
for(var i=0;i<100;i++)this[i]= 0;
// In the middle 4 squares, first place two black and two white chess pieces
this[54]=this[45]=1;this[55]=this[44]=2;
// Black net The number of winning pieces (black minus white), used in valuation.
this.divergence=0;
// The current moveable player is black
this.color=1;
// A few moves have been made
this.moves=0;
// Stable prototype
// 0 is a blank, 1 is a black chess, 2 is a white chess, and 3 is a border
// Expanding the 8 * 8 chessboard into 10 * 10 is a technique
// It can simplify the judgment 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, 3
] ]
// Loading state from an 8 * 8 board
this.load=function(arr)
{
for(var y=1;y<=8;y++) ​​​
                                                                                                           Determine whether it can pass
// If it can, the current player can change
this.pass=function()
{ for(var y=1;y<=8;y++)
{ = 1;x<=8;x++)                                                                                          You can move, but you can’t even pass if(this.move(x ,y,this.color))
{U Return false;
}}}}}}
// Alert ("PASS");
// This is a technique, because the value domain of this.Color is {1, 2}
​​// so when you should be a color as color When it is 1, it will be 2 after executing the next statement. // When color is 2, it will be 1 after executing the next statement. This.color = 3 - this.color;
return true;
this.clone = Function () {
Function Pattern () {}
Pattern.prototype = this;
Return New Pattern (); "*" ,"o"]
var r="";
for(var y=1;y<=8;y++)
{ for(var x=1;x<=8;x++)
{                                                                                                                          [this[y*10+x]]+” “;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn