无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了JavaScript模拟深蓝vs卡斯帕罗夫的国际象棋对局示例。分享给大家供大家参考。具体如下:
/** * JavaScript macro to run a chess game, showing board, pieces and moves played. * * Author: Todd Whiteman * Revision: 1.0 * Date: October 2012 */ var board = " Garry Kasparov \n 8║"; var gameintro = [ "Site: Philadelphia, PA USA \n Date: 1996.02.10 \n Round: 1 \n White: Deep Blue \n Black: Kasparov, Garry \n Result: 1-0 \n Opening: Sicilian Defense 2.c3 \n Annotator: Wheeler, David A. \n ", "This game is world-famous, because it was the first game \n won by a computer against a reigning world champion under \n normal chess tournament conditions (in particular, normal time controls). \n ", "Deep Blue was a computer developed by IBM to win against Kasparov. \n Deep Blue won this game, but Kasparov rebounded over the following 5 \n games to win 3 and draw 2, soundly beating Deep Blue in the 1996 match. \n ", "In the 1997 rematch, Deep Blue managed to win the entire match. \n Garry Kasparov is considered to be one of the greatest human chess players \n of all time, so both this single game and the later win of a match showed \n that computer-based chess had truly arrived at the pinnacle of chess play. \n " ]; var movelist = "1. e2e4 c7c5 \n2. c2c3 \n{It's more common to play 2. Nf3, but Kasparov has deep experience with \nthat line, so white's opening book goes in a different direction.} \n \n2.... d7d5 \n3. e4xd5 Qd8xd5 \n4. d2d4 Ng8f6 \n5. Ng1f3 Bc8g4 \n6. Bf1e2 e7e6 \n7. h2h3 Bg4h5 \n8. e1g1h1f1 Nb8c6 \n9. Bc1e3 c5xd4 \n10. c3xd4 Bf8b4 \n{A more common move here is Be7. This was a new approach by Kasparov, \ndeveloping the bishop in an unusual way. Whether or not it's a good \napproach is debated. After this move, the computer left its opening book \nand began calculating its next move.} \n \n11. a2a3 Bb4a5 \n12. Nb1c3 Qd5d6 \n13. Nc3b5 Qd6e7"; /****************************** * Komodo macro contents begin. ******************************/ var moveDisplayTime = 2000; // milliseconds var messageDisplayTime = 6000; // milliseconds // Indicator values, range from 8..30 - though Komodo uses a lot of these // numbers for special purposes. var indicWhiteSquare = 10; var indicBlackSquare = 11; var indicMoveFrom = 12; var indicMoveTo = 13; /** * Highlight the black/white chess squares. * * @param {Components.interfaces.ISciMoz} scimoz - The editor control. */ function HighlightSquares(scimoz) { for (var line=1; line < 9; line++) { for (var col=6; col < 21; col+=2) { var pos = scimoz.findColumn(line, col); var charlength = scimoz.positionAfter(pos) - pos; var isBlackSquare = (line % 2) == 0 ""); } } // Format the message. var textUtils = Components.classes["@activestate.com/koTextUtils;1"] .getService(Components.interfaces.koITextUtils); var lines = message.split("\n"); for (var i=0; i < lines.length; i++) { lines[i] = ko.stringutils.strip(lines[i]); } if (!nosplit) { message = lines.join(" "); message = textUtils.break_up_lines(message, 26); lines = message.split("\n"); } // Display new message - limit lines to for (var i=0; i < lines.length; i++) { var line = lines[i]; if (i+1 >= scimoz.lineCount) { scimoz.currentPos = scimoz.length; scimoz.newLine(); } var pos = scimoz.findColumn(i+1, 26); var lineStart = scimoz.positionFromLine(i+1); var lineDiff = pos - lineStart; while (lineDiff < 26) { // Add space padding to the start of the line. line = " " + line; lineDiff += 1; } scimoz.currentPos = pos; scimoz.addText(ko.stringutils.bytelength(line), line); } } catch(ex) { // Exception handling - show problems to the user. alert("Error: " + ex + "\n\n" + ex.stack.toString()); } } /** * Play the introduction strings. * * @param {Components.interfaces.ISciMoz} scimoz - The editor control. */ function PlayIntro(scimoz, callback) { for (var i=0; i < gameintro.length; i++) { setTimeout(DisplayMessage, messageDisplayTime * i, scimoz, gameintro[i], i == 0); } setTimeout(callback, (messageDisplayTime * gameintro.length), scimoz); } /** * Highlight the chess move. * * @param {Components.interfaces.ISciMoz} scimoz - The editor control. * @param {Integer} indicator - The indicator to use for highlighting. * @param {Integer} pos - The position to highlight. */ function HighlightMove(scimoz, indicator, pos) { scimoz.indicatorCurrent = indicator; scimoz.indicatorClearRange(0, scimoz.length); var charlength = scimoz.positionAfter(pos) - pos; scimoz.indicatorFillRange(pos, charlength); } /** * Determine the position in the document for the co-ordinates. * * @param {Components.interfaces.ISciMoz} scimoz - The editor control. * @param {String} move - The coded chess move to make. */ function GetBoardPosition(scimoz, chesscode) { var col = chesscode.charCodeAt(0) - 'a'.charCodeAt(0); var row = '8'.charCodeAt(0) - chesscode.charCodeAt(1); return scimoz.findColumn(row+1, (col*2)+6); } /** * Make the given chess move. * * @param {Components.interfaces.ISciMoz} scimoz - The editor control. * @param {String} move - The coded chess move to make. */ function MakeMove(scimoz, move) { var isTake = (move.indexOf("x") >= 0); move = move.replace("x", ""); if (move.length == 8) { // Special double move for castling. MakeMove(scimoz, move.substr(4)); move = move.substr(0, 4); } if (move.length >= 5) { move = move.substr(1); } var fromPos = GetBoardPosition(scimoz, move.substr(0, 2)); scimoz.targetStart = fromPos; scimoz.targetEnd = scimoz.positionAfter(fromPos); piece = scimoz.getTextRange(fromPos, scimoz.targetEnd); scimoz.replaceTarget(" ".length, " "); HighlightMove(scimoz, indicMoveFrom, fromPos); var toPos = GetBoardPosition(scimoz, move.substr(2)); scimoz.targetStart = toPos; scimoz.targetEnd = scimoz.positionAfter(toPos); scimoz.replaceTarget(piece.length, piece); HighlightSquares(scimoz); HighlightMove(scimoz, indicMoveTo, toPos); // Clear old messages. DisplayMessage(scimoz, "", false); } /** * Make the given chess move. * * @param {Components.interfaces.ISciMoz} scimoz - The editor control. * @param {String} move - The coded chess move to make. */ function ProcessMove(scimoz, move) { move = move.replace("!", ""); move = move.replace("", ""); move = move.replace("+", ""); var match = move.match(/(\d+)\.\s*([\w\.]+)\s*(\w+)"Unrecognized move: " + move + "\n"); } var moveWhite = match[2]; var moveBlack = match[3]; if (moveWhite[0] != ".") { MakeMove(scimoz, moveWhite); } else { MakeMove(scimoz, moveBlack); return; } setTimeout(MakeMove, moveDisplayTime, scimoz, moveBlack); } /** * Play all of the chess moves and display the move commentary. * * @param {Components.interfaces.ISciMoz} scimoz - The editor control. */ function PlayMoves(scimoz) { var moves = movelist.split("\n"); var state = "move"; var message = ""; var nexttimeout = 0; for (var i=0; i < moves.length; i++) { var move = ko.stringutils.strip(moves[i]); if (!move) { continue; } switch (state) { case "move": if (move.match(/^[0-9]+\./)) { // Piece to move. setTimeout(ProcessMove, nexttimeout, scimoz, move); nexttimeout += moveDisplayTime; nexttimeout += moveDisplayTime; break; } else if (move[0] == "{") { state = "message"; message = ""; move = move.substr(1); // Fallthrough. } else { continue; } case "message": if (move.indexOf("}") >= 0) { move = move.substring(0, move.indexOf("}")); state = "move"; } if (message) message += " "; message += move; if (state == "move") { setTimeout(DisplayMessage, nexttimeout, scimoz, message, false); message = ""; nexttimeout += messageDisplayTime; } break; } } } /** * Play the chess game in the given editor. * * @param {Components.interfaces.koIScintillaView} view - The editor view. */ function PlayChess(view) { try { /** * @type {Components.interfaces.ISciMoz} - The editor control. */ var scimoz = view.scimoz; DrawInitialBoard(scimoz); PlayIntro(scimoz, PlayMoves); } catch(ex) { // Exception handling - show problems to the user. alert("Error: " + ex + "\n\n" + ex.stack.toString()); } } // Create a new text file asynchronously and start playing chess. ko.views.manager.doNewViewAsync("Text", "editor", PlayChess);
希望本文所述对大家的javascript程序设计有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月19日
2024年11月19日
- 证声音乐图书馆《海风摇曳·盛夏爵士曲》[320K/MP3][63.06MB]
- 龚玥《金装龚玥HQCD》头版限量[WAV分轨]
- 李小春《吻别》萨克斯演奏经典[原抓WAV+CUE]
- 齐秦《辉煌30年24K珍藏版》2CD[WAV+CUE]
- 证声音乐图书馆《海风摇曳·盛夏爵士曲》[FLAC/分轨][321.47MB]
- 群星 《世界经典汽车音乐》 [WAV分轨][1G]
- 冷漠.2011 《冷漠的爱DSD》[WAV+CUE][1.2G]
- 陈明《流金岁月精逊【中唱】【WAV+CUE】
- 群星《Jazz-Ladies1-2爵士女伶1-2》HQCD/2CD[原抓WAV+CUE]
- 群星《美女私房歌》(黑胶)[WAV分轨]
- 郑源.2009《试音天碟》24BIT-96KHZ[WAV+CUE][1.2G]
- 飞利浦试音碟 《环球群星监听录》SACD香港版[WAV+CUE][1.1G]
- 车载音乐最强享受《车载极致女声精选CD》[WAV分轨][1G]
- 童宇.2024-爱情万年历【TOUCH音乐】【FLAC分轨】
- 黄晓君.2010-丽风金典系列VOL.1.2CD【丽风】【WAV+CUE】