首页 > 语言 > JavaScript > 正文

JavaScript模拟深蓝vs卡斯帕罗夫的国际象棋对局示例

2024-05-06 16:18:42
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了JavaScript模拟深蓝vs卡斯帕罗夫的国际象棋对局示例,使用javascript较为逼真的模拟出了国际象棋对弈的场景,需要的朋友可以参考下

本文实例讲述了JavaScript模拟深蓝vs卡斯帕罗夫的国际象棋对局示例。分享给大家供大家参考。具体如下:

 

 
  1. /** 
  2. * JavaScript macro to run a chess game, showing board, pieces and moves played. 
  3. * 
  4. * Author: Todd Whiteman 
  5. * Revision: 1.0 
  6. * Date: October 2012 
  7. */ 
  8.  
  9. var board = "/ 
  10. Garry Kasparov /n/ 
  11. 8║♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ Move: 0 /n/ 
  12. 7║♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ /n/ 
  13. 6║ /n/ 
  14. 5║ /n/ 
  15. 4║ /n/ 
  16. 3║ /n/ 
  17. 2║♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ /n/ 
  18. 1║♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ /n/ 
  19. ╚═══════════════ /n/ 
  20. a b c d e f g h /n/ 
  21. Deep Blue /n/ 
  22. "; 
  23.  
  24.  
  25. var gameintro = [ 
  26. "Site: Philadelphia, PA USA /n/ 
  27. Date: 1996.02.10 /n/ 
  28. Round: 1 /n/ 
  29. White: Deep Blue /n/ 
  30. Black: Kasparov, Garry /n/ 
  31. Result: 1-0 /n/ 
  32. Opening: Sicilian Defense 2.c3 /n/ 
  33. Annotator: Wheeler, David A. /n/ 
  34. ",  
  35.  
  36. "This game is world-famous, because it was the first game /n/ 
  37. won by a computer against a reigning world champion under /n/ 
  38. normal chess tournament conditions (in particular, normal time controls). /n/ 
  39. ",  
  40.  
  41. "Deep Blue was a computer developed by IBM to win against Kasparov. /n/ 
  42. Deep Blue won this game, but Kasparov rebounded over the following 5 /n/ 
  43. games to win 3 and draw 2, soundly beating Deep Blue in the 1996 match. /n/ 
  44. ",  
  45.  
  46. "In the 1997 rematch, Deep Blue managed to win the entire match. /n/ 
  47. Garry Kasparov is considered to be one of the greatest human chess players /n/ 
  48. of all time, so both this single game and the later win of a match showed /n/ 
  49. that computer-based chess had truly arrived at the pinnacle of chess play. /n/ 
  50. " 
  51. ]; 
  52.  
  53.  
  54. var movelist = "/ 
  55. 1. e2e4 c7c5 /n/ 
  56. 2. c2c3 /n/ 
  57. {It's more common to play 2. Nf3, but Kasparov has deep experience with /n/ 
  58. that line, so white's opening book goes in a different direction.} /n/ 
  59. /n/ 
  60. 2.... d7d5 /n/ 
  61. 3. e4xd5 Qd8xd5 /n/ 
  62. 4. d2d4 Ng8f6 /n/ 
  63. 5. Ng1f3 Bc8g4 /n/ 
  64. 6. Bf1e2 e7e6 /n/ 
  65. 7. h2h3 Bg4h5 /n/ 
  66. 8. e1g1h1f1 Nb8c6 /n/ 
  67. 9. Bc1e3 c5xd4 /n/ 
  68. 10. c3xd4 Bf8b4 /n/ 
  69. {A more common move here is Be7. This was a new approach by Kasparov, /n/ 
  70. developing the bishop in an unusual way. Whether or not it's a good /n/ 
  71. approach is debated. After this move, the computer left its opening book /n/ 
  72. and began calculating its next move.} /n/ 
  73. /n/ 
  74. 11. a2a3 Bb4a5 /n/ 
  75. 12. Nb1c3 Qd5d6 /n/ 
  76. 13. Nc3b5 Qd6e7?! /n/ 
  77. {This allows white to make its pieces more active. /n/ 
  78. Other moves, which would probably be better, include Qb8 and Qd5.} /n/ 
  79. /n/ 
  80. 14. Nf3e5! Bh5xe2 /n/ 
  81. 15. Qd1xe2 e8g8h8f8 /n/ 
  82. 16. Ra1c1 Ra8c8 /n/ 
  83. 17. Be3g5 /n/ 
  84. {Black now has a problem, especially with the pinned knight on f6.} /n/ 
  85. /n/ 
  86. 17.... Ba5b6 /n/ 
  87. 18. Bg5xf6 g7xf6 /n/ 
  88. {Kasparov avoids ... Qxf6? because white would gain material with 19. Nd7. /n/ 
  89. Note that Kasparov's king is now far more exposed.} /n/ 
  90. /n/ 
  91. 19. Ne5c4! Rf8d8 /n/ 
  92. 20. Nc4xb6! a7xb6 /n/ 
  93. 21. Rf1d1 f6f5 /n/ 
  94. 22. Qe2e3! /n/ 
  95. {This is an excellent place for the white queen.} /n/ 
  96. /n/ 
  97. 22... Qe7f6 /n/ 
  98. 23. d4d5! /n/ 
  99. {Kasparov commented that he might have offered this pawn /n/ 
  100. sacrifice himself in this position, since it hurt black's pawn /n/ 
  101. structure, opened up the board, and black's exposed king suggested /n/ 
  102. that there was probably a way to exploit the result. /n/ 
  103. Kasparov has been attacking the d4 pawn, and the computer wisely /n/ 
  104. decided to advance it for an attack instead of trying to defend it.} /n/ 
  105. /n/ 
  106. 23... Rd8xd5 /n/ 
  107. 24. Rd1xd5 e6xd5 /n/ 
  108. 25. b2b3! Kg8h8? /n/ 
  109. {Kasparov attempts to prepare a counter-attack, by preparing to /n/ 
  110. move his rook to file g, but it won't work. /n/ 
  111. Burgess suggests that 25.... Ne7 Rxc8+ would have better, though /n/ 
  112. white would still have some advantage. /n/ 
  113. Indeed, after this point on it's difficult to identify /n/ 
  114. any move that will dramatically help black.} /n/ 
  115. /n/ 
  116. 26. Qe3xb6 Rc8g8 /n/ 
  117. 27. Qb6c5 d5d4 /n/ 
  118. 28. Nb5d6 f5f4 /n/ 
  119. 29. Nd6xb7 /n/ 
  120. {This is a very 'computerish'/materialistic move; white is grabbing /n/ 
  121. an undeveloped pawn for a small gain in material. /n/ 
  122. However, the computer has not identified any threat of checkmate or /n/ 
  123. other risks from black, so it simply acquires the material.} /n/ 
  124. /n/ 
  125. 29.... Nc6e5 /n/ 
  126. 30. Qc5d5 /n/ 
  127. {The move 30. Qxd4?? would be terrible, because Nf3+ /n/ 
  128. would win the white queen.} /n/ 
  129. /n/ 
  130. 30.... f4f3 /n/ 
  131. 31. g2g3 Ne5d3 /n/ 
  132. {The move 31... Qf4 won't work, because of 32. Rc8! Qg5 33. Rc5!} /n/ 
  133. /n/ 
  134. 32. Rc1c7 Rg8e8 /n/ 
  135. {Kasparov is attacking, but the computer has correctly determined that /n/ 
  136. the attack is not a real threat.} /n/ 
  137. /n/ 
  138. 33. Nb7d6 Re8e1+ /n/ 
  139. 34. Kg1h2 Nd3xf2 /n/ 
  140. 35. Nd6xf7+ Kh8g7 /n/ 
  141. 36. Nf7g5 Kg7h6 /n/ 
  142. 37. Rc7xh7+ /n/ 
  143. {Kasparov resigns - expecting ... Kg6 38. Qg8+ Kf5 Nxf3 and white's /n/ 
  144. strength is overwhelming. White will have lots of ways to defeat black, /n/ 
  145. while black has no real way to attack white.} /n/ 
  146. "; 
  147.  
  148.  
  149.  
  150. /****************************** 
  151. * Komodo macro contents begin. 
  152. ******************************/ 
  153.  
  154. var moveDisplayTime = 2000; // milliseconds 
  155. var messageDisplayTime = 6000; // milliseconds 
  156.  
  157. // Indicator values, range from 8..30 - though Komodo uses a lot of these 
  158. // numbers for special purposes. 
  159. var indicWhiteSquare = 10; 
  160. var indicBlackSquare = 11; 
  161. var indicMoveFrom = 12; 
  162. var indicMoveTo = 13; 
  163.  
  164. /** 
  165. * Highlight the black/white chess squares. 
  166. * 
  167. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  168. */ 
  169. function HighlightSquares(scimoz) { 
  170. for (var line=1; line < 9; line++) { 
  171. for (var col=6; col < 21; col+=2) { 
  172. var pos = scimoz.findColumn(line, col); 
  173. var charlength = scimoz.positionAfter(pos) - pos; 
  174. var isBlackSquare = (line % 2) == 0 ? (col % 4) == 0 : (col % 4) == 2; 
  175. if (isBlackSquare) { 
  176. scimoz.indicatorCurrent = indicBlackSquare; 
  177. } else { 
  178. scimoz.indicatorCurrent = indicWhiteSquare; 
  179. } 
  180. scimoz.indicatorFillRange(pos, charlength); 
  181. } 
  182. } 
  183. } 
  184.  
  185. /** 
  186. * Draw the starting board layout. 
  187. * 
  188. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  189. */ 
  190. function DrawInitialBoard(scimoz) { 
  191. // Set board styling. 
  192. scimoz.setMarginWidthN(0, 0); // Remove the line number margin. 
  193. scimoz.caretStyle = scimoz.CARETSTYLE_INVISIBLE; // Hide the caret 
  194. scimoz.indicSetStyle(indicWhiteSquare, scimoz.INDIC_STRAIGHTBOX); // See Scintilla docs for others 
  195. scimoz.indicSetAlpha(indicWhiteSquare, 40); 
  196. scimoz.indicSetOutlineAlpha(indicWhiteSquare, 30); 
  197. scimoz.indicSetFore(indicWhiteSquare, 0xFFFFFF); // Colour is BGR format!! 
  198. scimoz.indicSetStyle(indicBlackSquare, scimoz.INDIC_STRAIGHTBOX); // See Scintilla docs for others 
  199. scimoz.indicSetAlpha(indicBlackSquare, 40); 
  200. scimoz.indicSetOutlineAlpha(indicBlackSquare, 30); 
  201. scimoz.indicSetFore(indicBlackSquare, 0x000000); // Colour black - it's BGR format!! 
  202. scimoz.indicSetStyle(indicMoveFrom, scimoz.INDIC_ROUNDBOX); // See Scintilla docs for others 
  203. scimoz.indicSetAlpha(indicMoveFrom, 40); 
  204. scimoz.indicSetOutlineAlpha(indicMoveFrom, 90); 
  205. scimoz.indicSetFore(indicMoveFrom, 0x00EEEE); // Colour is BGR format!! 
  206. scimoz.indicSetStyle(indicMoveTo, scimoz.INDIC_ROUNDBOX); // See Scintilla docs for others 
  207. scimoz.indicSetAlpha(indicMoveTo, 40); 
  208. scimoz.indicSetOutlineAlpha(indicMoveTo, 90); 
  209. scimoz.indicSetFore(indicMoveTo, 0x00EEEE); // Colour is BGR format!! 
  210. // Add the board text. 
  211. scimoz.addText(ko.stringutils.bytelength(board), board); 
  212. // Make it a large board - valid range is +-20. 
  213. scimoz.zoom = 15; 
  214. // Highlight the black/white squares. 
  215. HighlightSquares(scimoz); 
  216. } 
  217.  
  218. /** 
  219. * Display the given message beside the board. Clears any previous message. 
  220. * 
  221. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  222. * @param {String} message - The message to display. 
  223. */ 
  224. function DisplayMessage(scimoz, message, nosplit) { 
  225. try { 
  226. // Clear existing message lines. 
  227. for (var line=1; line < scimoz.lineCount; line++) { 
  228. var pos = scimoz.findColumn(line, 26); 
  229. var eolpos = scimoz.getLineEndPosition(line); 
  230. if (eolpos > pos) { 
  231. scimoz.targetStart = pos; 
  232. scimoz.targetEnd = eolpos; 
  233. scimoz.replaceTarget(0, ""); 
  234. } 
  235. } 
  236. // Format the message. 
  237. var textUtils = Components.classes["@activestate.com/koTextUtils;1"] 
  238. .getService(Components.interfaces.koITextUtils); 
  239. var lines = message.split("/n"); 
  240. for (var i=0; i < lines.length; i++) { 
  241. lines[i] = ko.stringutils.strip(lines[i]); 
  242. } 
  243. if (!nosplit) { 
  244. message = lines.join(" "); 
  245. message = textUtils.break_up_lines(message, 26); 
  246. lines = message.split("/n"); 
  247. } 
  248. // Display new message - limit lines to  
  249. for (var i=0; i < lines.length; i++) { 
  250. var line = lines[i]; 
  251. if (i+1 >= scimoz.lineCount) { 
  252. scimoz.currentPos = scimoz.length; 
  253. scimoz.newLine(); 
  254. } 
  255. var pos = scimoz.findColumn(i+1, 26); 
  256. var lineStart = scimoz.positionFromLine(i+1); 
  257. var lineDiff = pos - lineStart; 
  258. while (lineDiff < 26) { 
  259. // Add space padding to the start of the line. 
  260. line = " " + line; 
  261. lineDiff += 1; 
  262. } 
  263. scimoz.currentPos = pos; 
  264. scimoz.addText(ko.stringutils.bytelength(line), line); 
  265. } 
  266. } catch(ex) { 
  267. // Exception handling - show problems to the user. 
  268. alert("Error: " + ex + "/n/n" + ex.stack.toString()); 
  269. } 
  270. } 
  271.  
  272. /** 
  273. * Play the introduction strings. 
  274. * 
  275. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  276. */ 
  277. function PlayIntro(scimoz, callback) { 
  278. for (var i=0; i < gameintro.length; i++) { 
  279. setTimeout(DisplayMessage, messageDisplayTime * i, scimoz, gameintro[i], i == 0); 
  280. } 
  281. setTimeout(callback, (messageDisplayTime * gameintro.length), scimoz); 
  282. } 
  283.  
  284. /** 
  285. * Highlight the chess move. 
  286. * 
  287. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  288. * @param {Integer} indicator - The indicator to use for highlighting. 
  289. * @param {Integer} pos - The position to highlight. 
  290. */ 
  291. function HighlightMove(scimoz, indicator, pos) { 
  292. scimoz.indicatorCurrent = indicator; 
  293. scimoz.indicatorClearRange(0, scimoz.length); 
  294. var charlength = scimoz.positionAfter(pos) - pos; 
  295. scimoz.indicatorFillRange(pos, charlength); 
  296. } 
  297.  
  298. /** 
  299. * Determine the position in the document for the co-ordinates. 
  300. * 
  301. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  302. * @param {String} move - The coded chess move to make. 
  303. */ 
  304. function GetBoardPosition(scimoz, chesscode) { 
  305. var col = chesscode.charCodeAt(0) - 'a'.charCodeAt(0); 
  306. var row = '8'.charCodeAt(0) - chesscode.charCodeAt(1); 
  307. return scimoz.findColumn(row+1, (col*2)+6); 
  308. }  
  309.  
  310. /** 
  311. * Make the given chess move. 
  312. * 
  313. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  314. * @param {String} move - The coded chess move to make. 
  315. */ 
  316. function MakeMove(scimoz, move) { 
  317. var isTake = (move.indexOf("x") >= 0); 
  318. move = move.replace("x", ""); 
  319. if (move.length == 8) { 
  320. // Special double move for castling. 
  321. MakeMove(scimoz, move.substr(4)); 
  322. move = move.substr(0, 4); 
  323. } 
  324. if (move.length >= 5) { 
  325. move = move.substr(1); 
  326. } 
  327. var fromPos = GetBoardPosition(scimoz, move.substr(0, 2)); 
  328. scimoz.targetStart = fromPos; 
  329. scimoz.targetEnd = scimoz.positionAfter(fromPos); 
  330. piece = scimoz.getTextRange(fromPos, scimoz.targetEnd); 
  331. scimoz.replaceTarget(" ".length, " "); 
  332. HighlightMove(scimoz, indicMoveFrom, fromPos); 
  333. var toPos = GetBoardPosition(scimoz, move.substr(2)); 
  334. scimoz.targetStart = toPos; 
  335. scimoz.targetEnd = scimoz.positionAfter(toPos); 
  336. scimoz.replaceTarget(piece.length, piece); 
  337. HighlightSquares(scimoz); 
  338. HighlightMove(scimoz, indicMoveTo, toPos); 
  339. // Clear old messages. 
  340. DisplayMessage(scimoz, "", false); 
  341. }  
  342.  
  343. /** 
  344. * Make the given chess move. 
  345. * 
  346. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  347. * @param {String} move - The coded chess move to make. 
  348. */ 
  349. function ProcessMove(scimoz, move) { 
  350. move = move.replace("!", ""); 
  351. move = move.replace("?", ""); 
  352. move = move.replace("+", ""); 
  353. var match = move.match(/(/d+)/./s*([/w/.]+)/s*(/w+)?/); 
  354. if (!match.length) { 
  355. dump("Unrecognized move: " + move + "/n"); 
  356. } 
  357. var moveWhite = match[2]; 
  358. var moveBlack = match[3]; 
  359. if (moveWhite[0] != ".") { 
  360. MakeMove(scimoz, moveWhite); 
  361. } else { 
  362. MakeMove(scimoz, moveBlack); 
  363. return; 
  364. } 
  365. setTimeout(MakeMove, moveDisplayTime, scimoz, moveBlack); 
  366. } 
  367.  
  368. /** 
  369. * Play all of the chess moves and display the move commentary. 
  370. * 
  371. * @param {Components.interfaces.ISciMoz} scimoz - The editor control. 
  372. */ 
  373. function PlayMoves(scimoz) { 
  374. var moves = movelist.split("/n"); 
  375. var state = "move"; 
  376. var message = ""; 
  377. var nexttimeout = 0; 
  378. for (var i=0; i < moves.length; i++) { 
  379. var move = ko.stringutils.strip(moves[i]); 
  380. if (!move) { 
  381. continue; 
  382. } 
  383. switch (state) { 
  384. case "move": 
  385. if (move.match(/^[0-9]+/./)) { 
  386. // Piece to move. 
  387. setTimeout(ProcessMove, nexttimeout, scimoz, move); 
  388. nexttimeout += moveDisplayTime; 
  389. nexttimeout += moveDisplayTime; 
  390. break; 
  391. } else if (move[0] == "{") { 
  392. state = "message"; 
  393. message = ""; 
  394. move = move.substr(1); 
  395. // Fallthrough. 
  396. } else { 
  397. continue; 
  398. } 
  399. case "message": 
  400. if (move.indexOf("}") >= 0) { 
  401. move = move.substring(0, move.indexOf("}")); 
  402. state = "move"; 
  403. } 
  404. if (message) message += " "; 
  405. message += move; 
  406. if (state == "move") { 
  407. setTimeout(DisplayMessage, nexttimeout, scimoz, message, false); 
  408. message = ""; 
  409. nexttimeout += messageDisplayTime; 
  410. } 
  411. break; 
  412. } 
  413. } 
  414. } 
  415.  
  416. /** 
  417. * Play the chess game in the given editor. 
  418. * 
  419. * @param {Components.interfaces.koIScintillaView} view - The editor view. 
  420. */ 
  421. function PlayChess(view) { 
  422. try { 
  423. /** 
  424. * @type {Components.interfaces.ISciMoz} - The editor control. 
  425. */ 
  426. var scimoz = view.scimoz; 
  427. DrawInitialBoard(scimoz); 
  428. PlayIntro(scimoz, PlayMoves); 
  429. } catch(ex) { 
  430. // Exception handling - show problems to the user. 
  431. alert("Error: " + ex + "/n/n" + ex.stack.toString()); 
  432. } 
  433. } 
  434.  
  435. // Create a new text file asynchronously and start playing chess. 
  436. ko.views.manager.doNewViewAsync("Text", "editor", PlayChess); 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选