JavaScript simulation of Navy vs Kasparov chess game example

Source: Internet
Author: User
Tags exception handling split tostring

JavaScript simulation of Navy vs Kasparov chess game example

This article mainly introduces the JavaScript simulation of dark blue vs Kasparov chess Game example, using JavaScript more realistic simulation of the chess game scene, the need for friends can refer to the

The examples in this article are examples of the chess games that JavaScript simulates dark blue vs Kasparov. Share to everyone for your reference. Specifically as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 30 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359-360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390-3 91 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420-421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 /** * JavaScript macro to run a chess game, showing board, pieces and moves played. * * author:todd Whiteman * revision:1.0 * date:october   var board = "Garry Kasparov n 8║♜♞♝♛♚♝♞♜ move:0 n 7║♟♟♟♟♟♟♟♟n 6║n 5║n 4║n 3║n 2║♙♙♙♙♙♙♙♙n 1║♖♘♗♕♔♗♘♖n╚═══════════════n a b c d E F g H n Deep Blue n ";     var gameintro = ["Site:philadelphia, PA USA n date:1996.02.10 n round:1 n white:deep Blue N black:kasp Arov, 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 I game N won by a computer against a reigning world champion under N normal chess tournamen T conditions (in particular, normal time controls). N ",  " Deep Blue was a computer developed from IBM to win against Kasparov. N Deep Blue won this game, but Kasparov rebounded on the following 5 N games to win 3 and draw 2, soundly beating Deep B Lue in the 1996 match. N ", &NBSp In the 1997 rematch, Deep Blue managed to win the entire match. N Garry Kasparov is considered to being one of the greatest human chess players N of all time, so both this single game and T He later win of a match showed n this computer-based chess had truly arrived at the pinnacle of chess. n "];     var movelist = "1. E2e4 c7c5 N 2. c2c3 N {It ' s more common to play 2. NF3, but Kasparov has deep experience with n which line, so White's opening book goes in a different direction.} n N 2 .... D7d5 N 3. E4xd5 qd8xd5 N 4. D2D4 ng8f6 N 5. NG1F3 bc8g4 N 6. BF1E2 e7e6 N 7. H2H3 bg4h5 N 8. E1G1H1F1 nb8c6 N 9. BC1E3 c5xd4 N 10. C3xd4 bf8b4 N {A more common to Be7. This is a new approach by Kasparov, N developing the bishop in a unusual way. Whether or not it's a good n approach is debated. After this move, the computer left it opening book N and began calculating it next move.} n N 11. A2A3 bb4a5 N 12. nb1c3 qd5d6 N 13. NC3B5 qd6e7?! n {This allowsTS pieces more active. n other moves, which would probably to be better, include Qb8 and Qd5.} n N 14. nf3e5! Bh5xe2 N 15. Qd1xe2 e8g8h8f8 N 16. RA1C1 ra8c8 N 17. Be3g5 N has a problem, especially with the pinned Knight on F6.} n N 17 .... Ba5b6 N 18. Bg5xf6 g7xf6 N {Kasparov avoids ... Qxf6? Because White would gain material with 19. Nd7. n Note that Kasparov ' s King are now far more exposed.} n N 19. ne5c4! Rf8d8 N 20. nc4xb6! a7xb6 N 21. RF1D1 f6f5 N 22. qe2e3! n {This is a excellent place for the White Queen.} N N 22 ... Qe7f6 N 23. d4d5! n {Kasparov commented that him might have offered this pawn n sacrifice himself to this position since it hurt n n structure, opened up the board, and Black's exposed king suggested n that there is probably a way to exploit the ResU Lt. N Kasparov has been attacking the D4 pawn, and the computer wisely N decided to advance it for a attack instead of TR Ying to defend it.} N N 23 ... Rd8xd5 N 24. Rd1xd5 e6xd5 N 25. b2b3! Kg8h8? n {KaSparov attempts to prepare a counter-attack with preparing to N-Move he rook to file G, but it won ' t work. N Burgess suggests that 25 .... Ne7 rxc8+ would have better, though N White would still, have some. N Indeed, after this point on it's difficult to identify n any move that would dramatically help black.} n N 26. Qe3xb6 Rc8g8 N 27. QB6C5 d5d4 N 28. Nb5d6 f5f4 N 29. Nd6xb7 N {This are a very ' computerish '/materialistic move; White is grabbing n an undeveloped pawn for a small gain in mat Erial. n However, the computer has not identified any threat of Checkmate or N, and risks from black, so it simply acquires the Material.} n N 29 .... Nc6e5 N 30. Qc5d5 N {The move 30. Qxd4?? Would is terrible, because nf3+ N would win the White Queen.} n N 30 .... F4f3 N 31. G2G3 ne5d3 N {The move 31 ... Qf4 won ' t work, because of 32. rc8! Qg5 33. rc5!} n N 32. Rc1c7 rg8e8 N {Kasparov is attacking, but the computer has correctly ' determined that n the ' attack is ' not ' real threat.} n N 33. Nb7d6re8e1+ N 34. KG1H2 Nd3xf2 N 35. nd6xf7+ Kh8g7 N 36. Nf7g5 Kg7h6 N 37. rc7xh7+ N {Kasparov resigns-expecting ... Kg6 38. qg8+ Kf5 Nxf3 and white ' s n strength is overwhelming. White would have lots of ways to defeat Black, n while Black has no real way to attack white.} n ";      /****************************** * Komodo macro contents begin. /  var movedisplaytime = 2000; milliseconds var messagedisplaytime = 6000; milliseconds  //indicator values, range from 8..30-though Komodo uses a lot of all//numbers for special PU Rposes. 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 <; 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; if (isblacksquare) {scimoz.indicatorcurrent = Indicblacksquare;} else {scimoz.indicatorcurrent = indicWhiteSquare;} SC Imoz.indicatorfillrange (POS, charlength); }}  /** * Draw the starting board layout. * * @param {Components.interfaces.ISciMoz} scimoz-the editor control. */function Drawinitialboard (SCIMOZ) {//Set board styling. SCIMOZ.SETMARGINWIDTHN (0, 0);//Remove the line number Margi N. Scimoz.caretstyle = Scimoz. caretstyle_invisible; Hide the caret Scimoz.indicsetstyle (Indicwhitesquare, Scimoz. Indic_straightbox); Scintilla Docs for others Scimoz.indicsetalpha (Indicwhitesquare, 40); Scimoz.indicsetoutlinealpha (Indicwhitesquare, 30); Scimoz.indicsetfore (Indicwhitesquare, 0xFFFFFF); Colour is BGR format!! Scimoz.indicsetstyle (Indicblacksquare, Scimoz. Indic_straightbox); Scintilla Docs for others Scimoz.indicsetalpha (Indicblacksquare, 40); SCimoz.indicsetoutlinealpha (Indicblacksquare, 30); Scimoz.indicsetfore (Indicblacksquare, 0x000000); Colour black-it ' s BGR format!! Scimoz.indicsetstyle (Indicmovefrom, Scimoz. Indic_roundbox); Scintilla Docs for others Scimoz.indicsetalpha (Indicmovefrom, 40); Scimoz.indicsetoutlinealpha (Indicmovefrom, 90); Scimoz.indicsetfore (Indicmovefrom, 0x00eeee); Colour is BGR format!! Scimoz.indicsetstyle (Indicmoveto, Scimoz. Indic_roundbox); Scintilla Docs for others Scimoz.indicsetalpha (Indicmoveto, 40); Scimoz.indicsetoutlinealpha (Indicmoveto, 90); Scimoz.indicsetfore (Indicmoveto, 0x00eeee); Colour is BGR format!! ADD the board text. Scimoz.addtext (ko.stringutils.bytelength (board), board); Make it a large board-valid the range is +-20. Scimoz.zoom = 15; Highlight the Black/white squares. Highlightsquares (SCIMOZ); }  /** * Display The given message beside the board. Clears any previous message. * * @param {Components.interfaces.ISciMoz} scimoz-the Editor contRol. * @param {String} message-the message to display. */function DisplayMessage (SCIMOZ, message, Nosplit) {try {//Clear existing message lines. for (var line=1; line < s Cimoz.linecount; line++) {var pos = scimoz.findcolumn (line, num); var eolpos = scimoz.getlineendposition (line); if (Eolpos > pos) {SCIM Oz.targetstart = pos; Scimoz.targetend = Eolpos; Scimoz.replacetarget (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.joi N (""); 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 >= SCI Moz.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 <) {//Add spaces padding to the ' start of ' line = ' "+ line; Linediff + 1; } Scimoz.currentpos = pos; Scimoz.addtext (line), line (ko.stringutils.bytelength); The catch (ex) {//Exception handling-show problems to the user. Alert ("Error:" + ex + "NN" + 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, message DisplayTime * 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. */function Getboardposition (Scimoz, Chesscode) {var col = chesscode.charcodeat (0)-' a '. charCodeAt (0); var row = ' 8 '. Cha Rcodeat (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. */function Makemove (Scimoz, move) {var istake = (Move.indexof ("x") >= 0); move = Move.replace ("x", ""); if (move.length = = 8) {//Special double 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. */function Processmove (Scimoz, move) {move = Move.replace ("!", ""); move = Move.replace ("?", ""); move = Move.replace ("+", ""); var match = Move.match (/(d+). s* ([w.] +) s* (w+)?/); if (!match.length) {dump ("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); }  /** * All of the 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 (stat e) {case ' move ': if (Move.match (/^[0-9]+./)) {//Piece to move. settimeout (Processmove, Nexttimeout, Scimoz, move); Timeout + = 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); 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); The catch (ex) {//Exception handling-show problems to the user. Alert ("Error:" + ex + "NN" + ex.stack.toString ());}  //Create A new text file asynchronously and start playing chess. Ko.views.manager.doNewViewAsync ("Text", "editor", Playchess);

The

wants this article to help you with your JavaScript programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.