DIV CSS 佈局教程網

用 JavaScript 重現深藍對決卡斯帕羅夫的經典棋局
編輯:JavaScript綜合知識     

1996 年 2 月 10 日,美國費城,IBM 的「深藍」(Deep Blue)在正式比賽規則下擊敗了當時的世界冠軍卡斯帕羅夫。這是電腦下棋史上第一次贏得這項殊榮,因此被稱為世界聞名的一局。

本文分享一個有趣的作法:用 JavaScript 巨集把這場對局「搬進」文字編輯器裡播放。它會畫出棋盤、逐手上色移動棋子,並在棋盤右側顯示棋譜註解,相當有臨場感。

背景資料

深藍是 IBM 為對抗卡斯帕羅夫而開發的系統。它贏下了這第一局,但接下來五局卡斯帕羅夫以 3 勝 2 和回敬,贏得 1996 年的友誼賽。一年後的 1997 年復仇賽,深藍則拿下了整個賽事。

卡斯帕羅夫被視為史上最偉大的人類棋手之一,因此無論是這單局,還是後來的整場勝利,都象徵著電腦象棋正式登上巔峰。

資料結構設計

整個程式由三份資料組成。第一份是棋盤初始狀態,用 Unicode 西洋棋符號(♔♕♖♗♘♙ 等)拼成一張文字棋盤:

var board = " Garry Kasparov
 8║♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜  Move: 0
 7║♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
 6║
 5║
 4║
 3║
 2║♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
 1║♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
  ╚═══════════════
   a b c d e f g h
 Deep Blue";

第二份是對局資訊與歷史註解陣列:

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",
  "This game is world-famous, because it was the first game won by a computer against a reigning world champion under normal chess tournament conditions.",
  "Deep Blue won this game, but Kasparov rebounded over the following 5 games to win 3 and draw 2, soundly beating Deep Blue in the 1996 match.",
  "In the 1997 rematch, Deep Blue managed to win the entire match..."
];

第三份是棋譜字串,格式為「回合. 白棋 黑棋」,並用大括號夾帶註解:

var movelist = " 1. e2e4 c7c5
 2. c2c3
 {It's more common to play 2. Nf3, but Kasparov has deep experience with that line, so white's opening book goes in a different direction.}
 2.... d7d5
 3. e4xd5 Qd8xd5
 4. d2d4 Ng8f6
 5. Ng1f3 Bc8g4
 ...
 37. Rc7xh7+
 {Kasparov resigns - expecting ... Kg6 38. Qg8+ Kf5 and white's strength is overwhelming.}
";

核心邏輯

程式的關鍵在於把「棋盤座標」對應到「文件中的字元位置」。棋盤是純文字,因此 a1 對應第 2 行第 6 欄,b1 是第 2 行第 8 欄,以此類推:

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);
}

移動棋子的動作是:把起點字元換成空格、把終點字元替換成該棋子,再用 indicator 標記起訖位置:

function MakeMove(scimoz, move) {
  move = move.replace("x", "");            // 去掉吃子記號
  if (move.length == 8) {                  // 王車易位:兩子同時移動
    MakeMove(scimoz, move.substr(4));
    move = move.substr(0, 4);
  }
  if (move.length >= 5) {
    move = move.substr(1);                 // 去掉棋子代號,如 Nf3 → f3
  }

  var fromPos = GetBoardPosition(scimoz, move.substr(0, 2));
  scimoz.targetStart = fromPos;
  scimoz.targetEnd = scimoz.positionAfter(fromPos);
  var piece = scimoz.getTextRange(fromPos, scimoz.targetEnd);
  scimoz.replaceTarget(1, " ");
  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);

  DisplayMessage(scimoz, "", false);
}

黑白格上色則靠迴圈掃過棋盤的每個方塊,依「行號與欄號的奇偶組合」決定是黑方格還是白方格,再用不同的 indicator 填色:

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 ? (col % 4) == 0 : (col % 4) == 2;
      scimoz.indicatorCurrent = isBlackSquare ? indicBlackSquare : indicWhiteSquare;
      scimoz.indicatorFillRange(pos, charlength);
    }
  }
}

最後用 setTimeout 依序排入每一步棋與每段註解,就完成了自動播放的效果。棋譜解析時會過濾掉 !、?、+ 等裝飾符號,並用正則 /(\d+)\.?\s*([\w.]+)\s*(\w+)?/ 抓出回合數、白方著法與黑方著法。

這則範例的價值

它示範了幾件常被忽略的技巧:如何把抽象的棋盤狀態映射成線性的文字索引、如何用 indicator 在文字編輯器裡做視覺標記、以及如何用 setTimeout 串起一段時間軸動畫。這些手法在實作「文字型進度視覺化」或「程式碼差異播放」時都用得上。

需注意的是,這段程式是為 Komodo 編輯器的巨集環境撰寫的,依賴 scimoz 與 ko.* 等專屬 API。若要在瀏覽器重現同樣效果,改用 Canvas 或 DOM 元素會直觀得多,棋盤渲染也不必再靠字元排版。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved