display.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  6. <title>Empty Board Example</title>
  7. <link rel="stylesheet" href="css/chessboard.css" />
  8. <style>
  9. #display {
  10. float: left;
  11. margin: 50px;
  12. padding: 0px 50px;
  13. }
  14. #board {
  15. width: 600px;
  16. display: inline-block;
  17. vertical-align: top;
  18. }
  19. #match {
  20. margin-left: 20px;
  21. width: 275px;
  22. display: inline-block;
  23. vertical-align: top;
  24. }
  25. </style>
  26. </head>
  27. <body style="font-family: monospace;">
  28. <div>
  29. <form id="game_form">
  30. Player1:<br>
  31. <input type="text" name="player1" value="Player1">
  32. <br>
  33. Player2:<br>
  34. <input type="text" name="player2" value="Player2">
  35. <br>
  36. Move History:<br>
  37. <textarea rows="3" cols="120" name="moves" placeholder="[[0, 0], [3, 2], ...]"></textarea>
  38. <br>
  39. <input type="submit" id="runGame" value="Run Game">
  40. </form>
  41. </div>
  42. <div id="display">
  43. <div id="match">
  44. <table id="moves" style="text-align: center;"></table>
  45. </div>
  46. <div id="board"></div>
  47. </div>
  48. <script src="js/json3.min.js"></script>
  49. <script src="js/jquery-1.10.1.min.js"></script>
  50. <script src="js/chessboard.js"></script>
  51. <script>
  52. var init = function() {
  53. var ind2alpha = function(xy) {
  54. var alpha = "abcdefg";
  55. var num = "1234567";
  56. return alpha[xy[1]] + num[6 - xy[0]];
  57. };
  58. var runGame = function() {
  59. event.preventDefault();
  60. form = document.getElementById("game_form");
  61. if ( !form.player1.value || !form.player2.value || !form.moves.value)
  62. return;
  63. document.getElementById("runGame").disabled = true;
  64. game = {player1: form.player1.value,
  65. player2: form.player2.value,
  66. moves: JSON.parse(form.moves.value)};
  67. var interval = 500; // Length of the pause between moves (in milliseconds)
  68. // Build the moves table by adding a header
  69. var table = document.getElementById("moves");
  70. var header = table.createTHead();
  71. var row = header.insertRow();
  72. var cell = row.insertCell();
  73. cell.setAttribute("colspan", 2);
  74. cell.innerHTML = "<h3>" + game["player1"] + " vs " + game["player2"] + "</h3>";
  75. // Add the pieces in their starting positions directly to the board
  76. p0 = ind2alpha(game["moves"][0]);
  77. p1 = ind2alpha(game["moves"][1]);
  78. pos = {}
  79. pos[p0] = "wN";
  80. pos[p1] = "bN";
  81. board.position(pos);
  82. row = table.insertRow();
  83. cell = row.insertCell();
  84. cell.innerHTML = "(" + game["moves"][0] + ")";
  85. cell = row.insertCell();
  86. cell.innerHTML = "(" + game["moves"][1] + ")";
  87. idx = 0;
  88. // Perform moves until the list is exhausted
  89. timer = window.setInterval(function() { doMove() }, interval);
  90. function doMove() {
  91. var start = game["moves"][idx];
  92. var end = game["moves"][idx + 2];
  93. var move = ind2alpha(start) + "-" + ind2alpha(end);
  94. board.move(move);
  95. // Write the player location in the moves table
  96. if (idx % 2 == 0) {
  97. row = table.insertRow();
  98. cell = row.insertCell();
  99. cell.innerHTML = "(" + game["moves"][idx + 2] + ")";
  100. } else {
  101. cell = row.insertCell();
  102. cell.innerHTML = "(" + game["moves"][idx + 2] + ")";
  103. }
  104. idx++;
  105. // quit when the game is resolved
  106. if (idx >= game["moves"].length - 2) {
  107. var winCell = ind2alpha(game["moves"][idx + 1]);
  108. var loseCell = ind2alpha(game["moves"][idx]);
  109. board.finalize(winCell, loseCell);
  110. window.clearInterval(timer);
  111. return;
  112. }
  113. };
  114. };
  115. var board = ChessBoard('board');
  116. document.getElementById("game_form").addEventListener('submit', runGame);
  117. };
  118. $(document).ready(init);
  119. </script>
  120. </body>
  121. </html>