aarti789 Posted August 7, 2023 Share Posted August 7, 2023 Well, I am creating a PHP program (code and project reference taking from here) for making chess game but it shows some error at the time of its execution. Here is the source code: <?php // Define chessboard array $chessboard = array( array("R", "N", "B", "Q", "K", "B", "N", "R"), array("P", "P", "P", "P", "P", "P", "P", "P"), array("", "", "", "", "", "", "", ""), array("", "", "", "", "", "", "", ""), array("", "", "", "", "", "", "", ""), array("", "", "", "", "", "", "", ""), array("p", "p", "p", "p", "p", "p", "p", "p"), array("r", "n", "b", "q", "k", "b", "n", "r"), ); // Display chessboard echo "<table>"; for ($row = 0; $row < 8; $row++) { echo "<tr>"; for ($col = 0; $col < 8; $col++) { $piece = $chessboard[$row][$col]; echo "<td>" . $piece . "</td>"; } echo "</tr>"; } echo "</table>"; // Sample move validation (simplified) function isValidMove($startRow, $startCol, $endRow, $endCol, $chessboard) { // Implement move validation logic here return true; // For demonstration purposes } // Sample game loop (simplified) $player = "white"; while (true) { // Display current player's turn echo "<p>{$player}'s turn</p>"; // Get move input from the user $startRow = /* user input */; $startCol = /* user input */; $endRow = /* user input */; $endCol = /* user input */; // Validate and apply the move if (isValidMove($startRow, $startCol, $endRow, $endCol, $chessboard)) { // Update the chessboard and switch players $chessboard[$endRow][$endCol] = $chessboard[$startRow][$startCol]; $chessboard[$startRow][$startCol] = ""; $player = ($player === "white") ? "black" : "white"; } else { // Invalid move, display error message echo "<p>Invalid move!</p>"; } } ?> <?php // Define chessboard array $chessboard = array( array("R", "N", "B", "Q", "K", "B", "N", "R"), array("P", "P", "P", "P", "P", "P", "P", "P"), array("", "", "", "", "", "", "", ""), array("", "", "", "", "", "", "", ""), array("", "", "", "", "", "", "", ""), array("", "", "", "", "", "", "", ""), array("p", "p", "p", "p", "p", "p", "p", "p"), array("r", "n", "b", "q", "k", "b", "n", "r"), ); // Display chessboard echo "<table>"; for ($row = 0; $row < 8; $row++) { echo "<tr>"; for ($col = 0; $col < 8; $col++) { $piece = $chessboard[$row][$col]; echo "<td>" . $piece . "</td>"; } echo "</tr>"; } echo "</table>"; // Sample move validation (simplified) function isValidMove($startRow, $startCol, $endRow, $endCol, $chessboard) { // Implement move validation logic here return true; // For demonstration purposes } // Sample game loop (simplified) $player = "white"; while (true) { // Display current player's turn echo "<p>{$player}'s turn</p>"; // Get move input from the user $startRow = /* user input */; $startCol = /* user input */; $endRow = /* user input */; $endCol = /* user input */; // Validate and apply the move if (isValidMove($startRow, $startCol, $endRow, $endCol, $chessboard)) { // Update the chessboard and switch players $chessboard[$endRow][$endCol] = $chessboard[$startRow][$startCol]; $chessboard[$startRow][$startCol] = ""; $player = ($player === "white") ? "black" : "white"; } else { // Invalid move, display error message echo "<p>Invalid move!</p>"; } } ?> Can anyone give their suggestions on this. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/317162-php-chess-game-coding/ Share on other sites More sharing options...
Strider64 Posted August 7, 2023 Share Posted August 7, 2023 Personally, if I were writing a chess game I would use JavaScript and Canvas not PHP for the main coding part of the game. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317162-php-chess-game-coding/#findComment-1610953 Share on other sites More sharing options...
ginerjm Posted August 7, 2023 Share Posted August 7, 2023 You say 'it shows some error'. Care to pass along the exact error message? 1 Quote Link to comment https://forums.phpfreaks.com/topic/317162-php-chess-game-coding/#findComment-1610956 Share on other sites More sharing options...
aarti789 Posted August 16, 2023 Author Share Posted August 16, 2023 @Strider64 thanks a lot for your suggestions, definetely I will talk about it to my mates. Well, it shows some logical error at the time of its execution. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/317162-php-chess-game-coding/#findComment-1611190 Share on other sites More sharing options...
ginerjm Posted August 16, 2023 Share Posted August 16, 2023 Two weeks later and you still can't show us the error message or describe the problem? 1 Quote Link to comment https://forums.phpfreaks.com/topic/317162-php-chess-game-coding/#findComment-1611194 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.