fork download
  1. // กำหนดระบบดาวและโครงสร้างบอร์ด
  2. const systems = {
  3. "Nebula Rex": ["-", "-", "-", "-"],
  4. "Vortex Prime": ["-", "-", "-", "-"],
  5. "Drakon’s Veil": ["-", "-", "-", "-"],
  6. "Solaris Abyss": ["-", "-", "-", "-"],
  7. "Kryon Verge": ["-", "-", "-", "-"],
  8. "Aetherion Rift": ["-", "-", "-", "-"],
  9. "Synthara Nexus": ["-", "-", "-", "-"],
  10. "Zorath Crucible": ["-", "-", "-", "-"]
  11. };
  12.  
  13. // ความสัมพันธ์ "ติดกัน" ระหว่างระบบ
  14. const adjacentSystems = {
  15. "Nebula Rex": ["Vortex Prime", "Zorath Crucible"],
  16. "Vortex Prime": ["Nebula Rex", "Drakon’s Veil"],
  17. "Drakon’s Veil": ["Vortex Prime", "Solaris Abyss"],
  18. "Solaris Abyss": ["Drakon’s Veil", "Kryon Verge"],
  19. "Kryon Verge": ["Solaris Abyss", "Aetherion Rift"],
  20. "Aetherion Rift": ["Kryon Verge", "Synthara Nexus"],
  21. "Synthara Nexus": ["Aetherion Rift", "Zorath Crucible"],
  22. "Zorath Crucible": ["Synthara Nexus", "Nebula Rex"]
  23. };
  24.  
  25. // ผู้เล่น (ตัวอย่าง 2 ผู้เล่น)
  26. const players = {
  27. "Player A": { energy: 4, symbol: "A", bases: 0 },
  28. "Player B": { energy: 4, symbol: "B", bases: 0 }
  29. };
  30.  
  31. // แสดงบอร์ด
  32. function printBoard() {
  33. console.log("\n=== Galactic Betrayal Board ===");
  34. for (const system in systems) {
  35. console.log(`${system}: [${systems[system].join(", ")}]`);
  36. }
  37. console.log("\nPlayer Stats:");
  38. for (const player in players) {
  39. console.log(`${player}: Energy = ${players[player].energy}, Bases = ${players[player].bases}`);
  40. }
  41. }
  42.  
  43. // ตรวจสอบการครองระบบดาว
  44. function checkSystemControl() {
  45. for (const system in systems) {
  46. const planets = systems[system];
  47. if (planets.every(p => p === planets[0] && p !== "-")) {
  48. console.log(`${system} is controlled by ${planets[0]}!`);
  49. for (const player in players) {
  50. if (players[player].symbol === planets[0]) {
  51. players[player].energy += 3;
  52. console.log(`${player} gains 3 energy for controlling ${system}!`);
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59. // สร้างฐานทัพ
  60. function buildBase(player, system, position) {
  61. if (players[player].energy >= 2 && systems[system][position] === "-") {
  62. players[player].energy -= 2;
  63. players[player].bases += 1;
  64. systems[system][position] = players[player].symbol;
  65. console.log(`${player} builds a base in ${system} at position ${position}`);
  66. } else {
  67. console.log(`${player} cannot build: not enough energy or position occupied`);
  68. }
  69. }
  70.  
  71. // ยึดฐานทัพ
  72. function captureBase(player, system, position) {
  73. const targetSymbol = systems[system][position];
  74. if (targetSymbol !== "-" && targetSymbol !== players[player].symbol) {
  75. if (players[player].energy >= 2) {
  76. let canCapture = false;
  77. const allAdjacent = [...adjacentSystems[system], system];
  78. for (const adjSystem of allAdjacent) {
  79. if (systems[adjSystem].includes(players[player].symbol)) {
  80. canCapture = true;
  81. break;
  82. }
  83. }
  84. if (canCapture) {
  85. players[player].energy -= 2;
  86. systems[system][position] = players[player].symbol;
  87. console.log(`${player} captures a base in ${system} at position ${position}`);
  88. for (const p in players) {
  89. if (players[p].symbol === targetSymbol) {
  90. players[p].bases -= 1;
  91. }
  92. }
  93. players[player].bases += 1;
  94. } else {
  95. console.log(`${player} cannot capture: no adjacent base`);
  96. }
  97. } else {
  98. console.log(`${player} cannot capture: not enough energy`);
  99. }
  100. } else {
  101. console.log(`${player} cannot capture: empty or own base`);
  102. }
  103. }
  104.  
  105. // จำลองเกม
  106. function playGame() {
  107. let turn = 1;
  108. function nextTurn() {
  109. if (turn > 10) {
  110. console.log("\nGame Over: 10 turns completed");
  111. return;
  112. }
  113. console.log(`\n=== Turn ${turn} ===`);
  114. for (const player in players) {
  115. players[player].energy += 1; // ได้พลังงาน 1 ต่อตา
  116. printBoard();
  117. const action = prompt(`${player}, choose action (build/capture/skip): `);
  118. if (action === "build") {
  119. const system = prompt("Enter system (e.g., Nebula Rex): ");
  120. const pos = parseInt(prompt("Enter position (0-3): "));
  121. buildBase(player, system, pos);
  122. } else if (action === "capture") {
  123. const system = prompt("Enter system (e.g., Nebula Rex): ");
  124. const pos = parseInt(prompt("Enter position (0-3): "));
  125. captureBase(player, system, pos);
  126. }
  127. checkSystemControl();
  128. if (players[player].bases >= 15) {
  129. console.log(`${player} wins by conquering 15 bases!`);
  130. return;
  131. }
  132. }
  133. turn++;
  134. setTimeout(nextTurn, 100); // หน่วงเวลาเล็กน้อยเพื่อให้คอนโซลไม่รวน
  135. }
  136. nextTurn();
  137. }
  138.  
  139. // รันเกม (ต้องรันในเบราว์เซอร์)
  140. playGame();
Success #stdin #stdout 0.03s 25712KB
stdin
Standard input is empty
stdout
// กำหนดระบบดาวและโครงสร้างบอร์ด
const systems = {
    "Nebula Rex": ["-", "-", "-", "-"],
    "Vortex Prime": ["-", "-", "-", "-"],
    "Drakon’s Veil": ["-", "-", "-", "-"],
    "Solaris Abyss": ["-", "-", "-", "-"],
    "Kryon Verge": ["-", "-", "-", "-"],
    "Aetherion Rift": ["-", "-", "-", "-"],
    "Synthara Nexus": ["-", "-", "-", "-"],
    "Zorath Crucible": ["-", "-", "-", "-"]
};

// ความสัมพันธ์ "ติดกัน" ระหว่างระบบ
const adjacentSystems = {
    "Nebula Rex": ["Vortex Prime", "Zorath Crucible"],
    "Vortex Prime": ["Nebula Rex", "Drakon’s Veil"],
    "Drakon’s Veil": ["Vortex Prime", "Solaris Abyss"],
    "Solaris Abyss": ["Drakon’s Veil", "Kryon Verge"],
    "Kryon Verge": ["Solaris Abyss", "Aetherion Rift"],
    "Aetherion Rift": ["Kryon Verge", "Synthara Nexus"],
    "Synthara Nexus": ["Aetherion Rift", "Zorath Crucible"],
    "Zorath Crucible": ["Synthara Nexus", "Nebula Rex"]
};

// ผู้เล่น (ตัวอย่าง 2 ผู้เล่น)
const players = {
    "Player A": { energy: 4, symbol: "A", bases: 0 },
    "Player B": { energy: 4, symbol: "B", bases: 0 }
};

// แสดงบอร์ด
function printBoard() {
    console.log("\n=== Galactic Betrayal Board ===");
    for (const system in systems) {
        console.log(`${system}: [${systems[system].join(", ")}]`);
    }
    console.log("\nPlayer Stats:");
    for (const player in players) {
        console.log(`${player}: Energy = ${players[player].energy}, Bases = ${players[player].bases}`);
    }
}

// ตรวจสอบการครองระบบดาว
function checkSystemControl() {
    for (const system in systems) {
        const planets = systems[system];
        if (planets.every(p => p === planets[0] && p !== "-")) {
            console.log(`${system} is controlled by ${planets[0]}!`);
            for (const player in players) {
                if (players[player].symbol === planets[0]) {
                    players[player].energy += 3;
                    console.log(`${player} gains 3 energy for controlling ${system}!`);
                }
            }
        }
    }
}

// สร้างฐานทัพ
function buildBase(player, system, position) {
    if (players[player].energy >= 2 && systems[system][position] === "-") {
        players[player].energy -= 2;
        players[player].bases += 1;
        systems[system][position] = players[player].symbol;
        console.log(`${player} builds a base in ${system} at position ${position}`);
    } else {
        console.log(`${player} cannot build: not enough energy or position occupied`);
    }
}

// ยึดฐานทัพ
function captureBase(player, system, position) {
    const targetSymbol = systems[system][position];
    if (targetSymbol !== "-" && targetSymbol !== players[player].symbol) {
        if (players[player].energy >= 2) {
            let canCapture = false;
            const allAdjacent = [...adjacentSystems[system], system];
            for (const adjSystem of allAdjacent) {
                if (systems[adjSystem].includes(players[player].symbol)) {
                    canCapture = true;
                    break;
                }
            }
            if (canCapture) {
                players[player].energy -= 2;
                systems[system][position] = players[player].symbol;
                console.log(`${player} captures a base in ${system} at position ${position}`);
                for (const p in players) {
                    if (players[p].symbol === targetSymbol) {
                        players[p].bases -= 1;
                    }
                }
                players[player].bases += 1;
            } else {
                console.log(`${player} cannot capture: no adjacent base`);
            }
        } else {
            console.log(`${player} cannot capture: not enough energy`);
        }
    } else {
        console.log(`${player} cannot capture: empty or own base`);
    }
}

// จำลองเกม
function playGame() {
    let turn = 1;
    function nextTurn() {
        if (turn > 10) {
            console.log("\nGame Over: 10 turns completed");
            return;
        }
        console.log(`\n=== Turn ${turn} ===`);
        for (const player in players) {
            players[player].energy += 1; // ได้พลังงาน 1 ต่อตา
            printBoard();
            const action = prompt(`${player}, choose action (build/capture/skip): `);
            if (action === "build") {
                const system = prompt("Enter system (e.g., Nebula Rex): ");
                const pos = parseInt(prompt("Enter position (0-3): "));
                buildBase(player, system, pos);
            } else if (action === "capture") {
                const system = prompt("Enter system (e.g., Nebula Rex): ");
                const pos = parseInt(prompt("Enter position (0-3): "));
                captureBase(player, system, pos);
            }
            checkSystemControl();
            if (players[player].bases >= 15) {
                console.log(`${player} wins by conquering 15 bases!`);
                return;
            }
        }
        turn++;
        setTimeout(nextTurn, 100); // หน่วงเวลาเล็กน้อยเพื่อให้คอนโซลไม่รวน
    }
    nextTurn();
}

// รันเกม (ต้องรันในเบราว์เซอร์)
playGame();