Jump to content

PHP Chess Game Coding


aarti789

Recommended Posts

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

 

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.