<?php
session_start();

// 게임 초기화
if (!isset($_SESSION['board']) || isset($_GET['reset'])) {
    $_SESSION['board'] = array_fill(0, 15, array_fill(0, 15, 0));
    $_SESSION['current_player'] = 1; // 1: 흑돌, 2: 백돌
    $_SESSION['game_over'] = false;
    $_SESSION['winner'] = 0;
}

// 돌 놓기
if (isset($_GET['row']) && isset($_GET['col']) && !$_SESSION['game_over']) {
    $row = intval($_GET['row']);
    $col = intval($_GET['col']);

    // 유효한 위치이고 비어있는 경우
    if ($row >= 0 && $row < 15 && $col >= 0 && $col < 15 && $_SESSION['board'][$row][$col] == 0) {
        $_SESSION['board'][$row][$col] = $_SESSION['current_player'];
        
        // 승리 확인
        if (checkWin($row, $col, $_SESSION['current_player'], $_SESSION['board'])) {
            $_SESSION['game_over'] = true;
            $_SESSION['winner'] = $_SESSION['current_player'];
        } else {
            // 플레이어 전환
            $_SESSION['current_player'] = $_SESSION['current_player'] == 1 ? 2 : 1;
        }
    }
}

// 승리 확인 함수
function checkWin($row, $col, $player, $board) {
    $directions = [
        [0, 1], // 가로
        [1, 0], // 세로
        [1, 1], // 대각선 ↘
        [1, -1] // 대각선 ↗
    ];

    foreach ($directions as $dir) {
        $count = 1;
        
        // 정방향 확인
        for ($i = 1; $i <= 4; $i++) {
            $new_row = $row + $dir[0] * $i;
            $new_col = $col + $dir[1] * $i;
            
            if ($new_row < 0 || $new_row >= 15 || $new_col < 0 || $new_col >= 15 || $board[$new_row][$new_col] != $player) {
                break;
            }
            $count++;
        }
        
        // 역방향 확인
        for ($i = 1; $i <= 4; $i++) {
            $new_row = $row - $dir[0] * $i;
            $new_col = $col - $dir[1] * $i;
            
            if ($new_row < 0 || $new_row >= 15 || $new_col < 0 || $new_col >= 15 || $board[$new_row][$new_col] != $player) {
                break;
            }
            $count++;
        }
        
        if ($count >= 5) {
            return true;
        }
    }
    
    return false;
}
?>

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP 오목 게임</title>
    <style>
        body {
            font-family: 'Malgun Gothic', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #f5f5f5;
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        .board {
            background-color: #e9bb5e;
            border: 2px solid #000;
            padding: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
        }
        .row {
            display: flex;
        }
        .cell {
            width: 30px;
            height: 30px;
            border: 1px solid #000;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;
            background-color: transparent;
        }
        .cell::before {
            content: "";
            position: absolute;
            width: 100%;
            height: 1px;
            background-color: #000;
            z-index: 1;
        }
        .cell::after {
            content: "";
            position: absolute;
            width: 1px;
            height: 100%;
            background-color: #000;
            z-index: 1;
        }
        .stone {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            z-index: 2;
        }
        .black {
            background-color: #000;
            box-shadow: 0 0 2px 1px rgba(255, 255, 255, 0.5);
        }
        .white {
            background-color: #fff;
            box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.5);
        }
        .status {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
        }
        .controls {
            margin-top: 15px;
        }
        .controls a {
            display: inline-block;
            padding: 8px 15px;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 4px;
        }
        .controls a:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <h1>PHP 오목 게임</h1>
    
    <div class="status">
        <?php if ($_SESSION['game_over']): ?>
            <?php echo $_SESSION['winner'] == 1 ? '흑돌' : '백돌'; ?> 승리!
        <?php else: ?>
            현재 차례: <?php echo $_SESSION['current_player'] == 1 ? '흑돌' : '백돌'; ?>
        <?php endif; ?>
    </div>
    
    <div class="board">
        <?php for ($i = 0; $i < 15; $i++): ?>
            <div class="row">
                <?php for ($j = 0; $j < 15; $j++): ?>
                    <div class="cell">
                        <?php if ($_SESSION['board'][$i][$j] != 0): ?>
                            <div class="stone <?php echo $_SESSION['board'][$i][$j] == 1 ? 'black' : 'white'; ?>"></div>
                        <?php else: ?>
                            <a href="?row=<?php echo $i; ?>&col=<?php echo $j; ?>" style="display: block; width: 100%; height: 100%;"></a>
                        <?php endif; ?>
                    </div>
                <?php endfor; ?>
            </div>
        <?php endfor; ?>
    </div>
    
    <div class="controls">
        <a href="?reset=1">게임 재시작</a>
    </div>
</body>
</html>
