php实现五子棋人机对战游戏

涛哥 PHP代码

以下是使用PHP编写的五子棋人机对战游戏的简单示例代码:

<?php
// 定义棋盘大小和胜利条件
define('BOARD_SIZE', 15);
define('WIN_COUNT', 5);
// 初始化棋盘和当前下棋方
$board = array();
for ($i = 0; $i < BOARD_SIZE; $i++) {
    $board[$i] = array_fill(0, BOARD_SIZE, 0);
}
$current_player = 1;
// 判断是否胜利
function checkWin($board, $x, $y, $player) {
    // 横向判断
    $count = 1;
    for ($i = $x - 1; $i >= 0; $i--) {
        if ($board[$i][$y] == $player) {
            $count++;
        } else {
            break;
        }
    }
    for ($i = $x + 1; $i < BOARD_SIZE; $i++) {
        if ($board[$i][$y] == $player) {
            $count++;
        } else {
            break;
        }
    }
    if ($count >= WIN_COUNT) {
        return true;
    }
    // 纵向判断
    $count = 1;
    for ($i = $y - 1; $i >= 0; $i--) {
        if ($board[$x][$i] == $player) {
            $count++;
        } else {
            break;
        }
    }
    for ($i = $y + 1; $i < BOARD_SIZE; $i++) {
        if ($board[$x][$i] == $player) {
            $count++;
        } else {
            break;
        }
    }
    if ($count >= WIN_COUNT) {
        return true;
    }
    // 左上到右下斜向判断
    $count = 1;
    for ($i = $x - 1, $j = $y - 1; $i >= 0 && $j >= 0; $i--, $j--) {
        if ($board[$i][$j] == $player) {
            $count++;
        } else {
            break;
        }
    }
    for ($i = $x + 1, $j = $y + 1; $i < BOARD_SIZE && $j < BOARD_SIZE; $i++, $j++) {
        if ($board[$i][$j] == $player) {
            $count++;
        } else {
            break;
        }
    }
    if ($count >= WIN_COUNT) {
        return true;
    }
    // 右上到左下斜向判断
    $count = 1;
    for ($i = $x - 1, $j = $y + 1; $i >= 0 && $j < BOARD_SIZE; $i--, $j++) {
        if ($board[$i][$j] == $player) {
            $count++;
        } else {
            break;
        }
    }
    for ($i = $x + 1, $j = $y - 1; $i < BOARD_SIZE && $j >= 0; $i++, $j--) {
        if ($board[$i][$j] == $player) {
            $count++;
        } else {
            break;
        }
    }
    if ($count >= WIN_COUNT) {
        return true;
    }
    return false;
}
// 机器下棋
function aiMove($board, &$x, &$y) {
    // 随机找一个空位下棋
    do {
        $x = rand(0, BOARD_SIZE - 1);
        $y = rand(0, BOARD_SIZE - 1);
    } while ($board[$x][$y] != 0);
}
// 处理用户点击事件
if (isset($_GET['x']) && isset($_GET['y'])) {
    $x = intval($_GET['x']);
    $y = intval($_GET['y']);
    if ($board[$x][$y] == 0) {
        $board[$x][$y] = $current_player;
        if (checkWin($board, $x, $y, $current_player)) {
            echo "Player $current_player Win!";
            exit;
        }
        $current_player = 3 - $current_player;
        aiMove($board, $x, $y);
        $board[$x][$y] = $current_player;
        if (checkWin($board, $x, $y, $current_player)) {
            echo "Player $current_player Win!";
            exit;
        }
        $current_player = 3 - $current_player;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>五子棋人机对战</title>
    <style>
        table {
            border-collapse: collapse;
        }
        td {
            width: 30px;
            height: 30px;
            border: 1px solid #ccc;
            text-align: center;
            vertical-align: middle;
            font-size: 20px;
            cursor: pointer;
        }
        td.player1 {
            color: red;
        }
        td.player2 {
            color: blue;
        }
    </style>
</head>
<body>
    <h1>五子棋人机对战</h1>
    <table>
        <?php for ($i = 0; $i < BOARD_SIZE; $i++): ?>
            <tr>
                <?php for ($j = 0; $j < BOARD_SIZE; $j++): ?>
                    <td class="player<?php echo $board[$i][$j]; ?>" onclick="move(<?php echo $i; ?>, <?php echo $j; ?>)"></td>
                <?php endfor; ?>
            </tr>
        <?php endfor; ?>
    </table>
    <script>
        function move(x, y) {
            location.href = '?x=' + x + '&y=' + y;
        }
    </script>
</body>
</html>

该示例代码实现了一个简单的五子棋人机对战游戏,玩家和机器轮流下棋,先连成5个棋子的一方获胜。在HTML文件中,通过使用JavaScript来处理用户点击事件,将点击的坐标作为参数传递给PHP代码,PHP代码根据当前下棋方和机器的随机下棋来判断胜负,并更新棋盘状态。