Chess PHP


SUBMITTED BY: Guest

DATE: July 23, 2014, 1:11 p.m.

FORMAT: Text only

SIZE: 7.1 kB

HITS: 1318

  1. <?
  2. session_start();
  3. /* load settings */
  4. if (!isset($_CONFIG))
  5. require 'config.php';
  6. /* define constants */
  7. require 'chessconstants.php';
  8. /* include outside functions */
  9. if (!isset($_CHESSUTILS))
  10. require 'chessutils.php';
  11. require 'gui.php';
  12. require 'chessdb.php';
  13. require 'move.php';
  14. require 'undo.php';
  15. /* allow WebChess to be run on PHP systems < 4.1.0, using old http vars */
  16. fixOldPHPVersions();
  17. /* check session status */
  18. require 'sessioncheck.php';
  19. /* check if loading game */
  20. if (isset($_POST['gameID']))
  21. $_SESSION['gameID'] = $_POST['gameID'];
  22. /* debug flag */
  23. define ("DEBUG", 0);
  24. /* connect to database */
  25. require 'connectdb.php';
  26. /* load game */
  27. $isInCheck = ($_POST['isInCheck'] == 'true');
  28. $isCheckMate = false;
  29. $isPromoting = false;
  30. $isUndoing = false;
  31. loadHistory();
  32. loadGame();
  33. processMessages();
  34. if ($isUndoing)
  35. {
  36. doUndo();
  37. saveGame();
  38. }
  39. elseif (($_POST['promotion'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != ""))
  40. {
  41. savePromotion();
  42. $board[$_POST['toRow']][$_POST['toCol']] = $_POST['promotion'] | ($board[$_POST['toRow']][$_POST['toCol']] & BLACK);
  43. saveGame();
  44. }
  45. elseif (($_POST['fromRow'] != "") && ($_POST['fromCol'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != ""))
  46. {
  47. /* ensure it's the current player moving */
  48. /* NOTE: if not, this will currently ignore the command... */
  49. /* perhaps the status should be instead? */
  50. /* (Could be confusing to player if they double-click or something */
  51. $tmpIsValid = true;
  52. if (($numMoves == -1) || ($numMoves % 2 == 1))
  53. {
  54. /* White's move... ensure that piece being moved is white */
  55. if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != 0) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0))
  56. /* invalid move */
  57. $tmpIsValid = false;
  58. }
  59. else
  60. {
  61. /* Black's move... ensure that piece being moved is black */
  62. if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != BLACK) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0))
  63. /* invalid move */
  64. $tmpIsValid = false;
  65. }
  66. if ($tmpIsValid)
  67. {
  68. saveHistory();
  69. doMove();
  70. saveGame();
  71. }
  72. }
  73. mysql_close();
  74. ?>
  75. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  76. <html>
  77. <head>
  78. <?
  79. /* find out if it's the current player's turn */
  80. if (( (($numMoves == -1) || (($numMoves % 2) == 1)) && ($playersColor == "white"))
  81. || ((($numMoves % 2) == 0) && ($playersColor == "black")))
  82. $isPlayersTurn = true;
  83. else
  84. $isPlayersTurn = false;
  85. if ($_SESSION['isSharedPC'])
  86. echo("<title>WebChess</title>\n");
  87. else if ($isPlayersTurn)
  88. echo("<title>WebChess - Your Move</title>\n");
  89. else
  90. echo("<title>WebChess - Opponent's Move</title>\n");
  91. echo("<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'>\n");
  92. /* if it's not the player's turn, enable auto-refresh */
  93. if (!$isPlayersTurn && !isBoardDisabled() && !$_SESSION['isSharedPC'])
  94. {
  95. echo ("<META HTTP-EQUIV=Refresh CONTENT='");
  96. if ($_SESSION['pref_autoreload'] >= $CFG_MINAUTORELOAD)
  97. echo ($_SESSION['pref_autoreload']);
  98. else
  99. echo ($CFG_MINAUTORELOAD);
  100. echo ("; URL=chess.php?autoreload=yes'>\n");
  101. }
  102. ?>
  103. <script type="text/javascript">
  104. /* transfer board data to javacripts */
  105. <? writeJSboard(); ?>
  106. <? writeJShistory(); ?>
  107. if (DEBUG)
  108. alert("Game initilization complete!");
  109. </script>
  110. <script type="text/javascript" src="javascript/chessutils.js">
  111. /* these are utility functions used by other functions */
  112. </script>
  113. <script type="text/javascript" src="javascript/commands.js">
  114. // these functions interact with the server
  115. </script>
  116. <script type="text/javascript" src="javascript/validation.js">
  117. // these functions are used to test the validity of moves
  118. </script>
  119. <script type="text/javascript" src="javascript/isCheckMate.js">
  120. // these functions are used to test the validity of moves
  121. </script>
  122. <script type="text/javascript" src="javascript/squareclicked.js">
  123. // this is the main function that interacts with the user everytime they click on a square
  124. </script>
  125. </head>
  126. <body>
  127. <table border="0">
  128. <tr valign="top" align="center"><td>
  129. <form name="gamedata" method="post" action="chess.php">
  130. <?
  131. if ($isPromoting)
  132. writePromotion();
  133. ?>
  134. <?
  135. if ($isUndoRequested)
  136. writeUndoRequest();
  137. ?>
  138. <?
  139. if ($isDrawRequested)
  140. writeDrawRequest();
  141. ?>
  142. <? drawboard(); ?>
  143. <!-- table border="0">
  144. <tr><td -->
  145. <nobr>
  146. <input type="button" name="btnReload" value="Reload" onClick="window.open('chess.php', '_self')">
  147. <input type="button" name="btnUndo" value="Request Undo" <? if (isBoardDisabled()) echo("disabled='yes'"); else echo ("onClick='undo()'"); ?>>
  148. <input type="button" name="btnDraw" value="Request Draw" <? if (isBoardDisabled()) echo("disabled='yes'"); else echo ("onClick='draw()'"); ?>>
  149. <input type="button" name="btnResign" value="Resign" <? if (isBoardDisabled()) echo("disabled='yes'"); else echo ("onClick='resigngame()'"); ?>>
  150. <input type="button" name="btnMainMenu" value="Main Menu" onClick="window.open('mainmenu.php', '_self')">
  151. <input type="button" name="btnLogout" value="Logout" onClick="logout()">
  152. <input type="hidden" name="ToDo" value="Logout"> <!-- NOTE: this field is only used to Logout -->
  153. </nobr>
  154. <!-- /td></tr>
  155. </table -->
  156. <input type="hidden" name="requestUndo" value="no">
  157. <input type="hidden" name="requestDraw" value="no">
  158. <input type="hidden" name="resign" value="no">
  159. <input type="hidden" name="fromRow" value="<? if (isPromoting) echo ($_POST['fromRow']); ?>">
  160. <input type="hidden" name="fromCol" value="<? if (isPromoting) echo ($_POST['fromCol']); ?>">
  161. <input type="hidden" name="toRow" value="<? if (isPromoting) echo ($_POST['toRow']); ?>">
  162. <input type="hidden" name="toCol" value="<? if (isPromoting) echo ($_POST['toCol']); ?>">
  163. <input type="hidden" name="isInCheck" value="false">
  164. <input type="hidden" name="isCheckMate" value="false">
  165. </form>
  166. <p>Note: when castling, just move the king (the rook will move automatically).</p>
  167. </td>
  168. <td width="50">&nbsp;</td>
  169. <td>
  170. <? writeStatus(); ?>
  171. <br>
  172. <? writeHistory(); ?>
  173. </td></tr>
  174. </table>
  175. </body>
  176. </html>

comments powered by Disqus