Spaces:
Running
Running
<html lang="ko"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>๋ผ์ธ๋ฐฐํ: AI ์ฑ๊ธํ๋ ์ด ์์</title> | |
<style> | |
/* ๊ฐ๋จํ ์คํ์ผ */ | |
body { | |
margin: 20px; | |
font-family: sans-serif; | |
} | |
h1 { | |
margin-bottom: 10px; | |
} | |
#infoBar { | |
margin-bottom: 10px; | |
} | |
#gameBoard { | |
display: grid; | |
grid-template-columns: repeat(10, 40px); | |
grid-template-rows: repeat(6, 40px); | |
gap: 2px; | |
margin-bottom: 10px; | |
} | |
.cell { | |
width: 40px; | |
height: 40px; | |
border: 1px solid #666; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
cursor: pointer; | |
background-color: #ccc; | |
} | |
.red { | |
background-color: #ffaaaa; | |
} | |
.blue { | |
background-color: #aaaaff; | |
} | |
#endTurnBtn { | |
padding: 6px 12px; | |
font-size: 14px; | |
cursor: pointer; | |
margin-right: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>๋ผ์ธ๋ฐฐํ: AI ์ฑ๊ธํ๋ ์ด ์์</h1> | |
<div id="infoBar"> | |
<span id="turnInfo"></span> | | |
<span id="selectedInfo"></span> | |
</div> | |
<div id="gameBoard"></div> | |
<button id="endTurnBtn">ํด ์ข ๋ฃ</button> | |
<div id="message"></div> | |
<script> | |
/******************************************************** | |
* 1. ์ ๋/๊ฒ์ ํ๋ผ๋ฏธํฐ ์ ์ | |
********************************************************/ | |
const UNIT_TYPES = { | |
INFANTRY: { | |
name: "Infantry", | |
hp: 25, | |
attack: 3, | |
defense: 3, | |
move: 2, | |
range: 1, | |
advantage: "ARCHER", // ๋ณด๋ณ > ๊ถ์ | |
}, | |
ARCHER: { | |
name: "Archer", | |
hp: 20, | |
attack: 2, | |
defense: 2, | |
move: 2, | |
range: 3, | |
advantage: "CAVALRY", // ๊ถ์ > ๊ธฐ๋ณ | |
}, | |
CAVALRY: { | |
name: "Cavalry", | |
hp: 22, | |
attack: 4, | |
defense: 2, | |
move: 3, | |
range: 1, | |
advantage: "INFANTRY", // ๊ธฐ๋ณ > ๋ณด๋ณ | |
} | |
}; | |
// ๊ฐ๋จํ ์์ฑ ๋ณด์ (์: 1.5๋ฐฐ) | |
const ADVANTAGE_MULTIPLIER = 1.5; | |
// ๋งต ํฌ๊ธฐ | |
const WIDTH = 10; | |
const HEIGHT = 6; | |
// ํ ์ ์ | |
const RED = "RED"; | |
const BLUE = "BLUE"; | |
// ๊ฒ์ ์ํ | |
let units = []; // ์ ์ฒด ์ ๋ ๊ฐ์ฒด๋ฅผ ๋ณด๊ด | |
let currentTeam = RED; // ํ์ฌ ํด์ ํ | |
let selectedUnit = null; // ์ ์ ๊ฐ ์ ํํ ์ ๋(RED ํด ์ค) | |
/******************************************************** | |
* 2. ์ ๋ ํด๋์ค | |
********************************************************/ | |
class Unit { | |
constructor(typeKey, team, x, y) { | |
const data = UNIT_TYPES[typeKey]; | |
this.type = typeKey; // "INFANTRY", "ARCHER", "CAVALRY" | |
this.team = team; // "RED" ๋๋ "BLUE" | |
this.hp = data.hp; | |
this.attack = data.attack; | |
this.defense = data.defense; | |
this.move = data.move; | |
this.range = data.range; | |
this.advantage = data.advantage; | |
this.x = x; | |
this.y = y; | |
this.hasActed = false; // ์ด ํด ์ค ์ด๋/๊ณต๊ฒฉ ์ฌ๋ถ๋ฅผ ๋จ์ ์ฒดํฌ(ํ์ฅ ๊ฐ๋ฅ) | |
} | |
get isAlive() { | |
return this.hp > 0; | |
} | |
get displayName() { | |
// ์: 'I25' (Infantry, HP 25) | |
return this.type[0] + this.hp; | |
} | |
} | |
/******************************************************** | |
* 3. ์ด๊ธฐํ: ์ ๋ ๋ฐฐ์น | |
********************************************************/ | |
function initUnits() { | |
units = []; | |
// RED - ๋ณด๋ณ(6), ๊ถ์(3), ๊ธฐ๋ณ(3) / ์์๋ก ์ข์ธก์ ๋ฐฐ์น | |
for (let i = 0; i < 6; i++) { | |
units.push(new Unit("INFANTRY", RED, 0, i % HEIGHT)); | |
} | |
for (let i = 0; i < 3; i++) { | |
units.push(new Unit("ARCHER", RED, 1, i)); | |
} | |
for (let i = 0; i < 3; i++) { | |
units.push(new Unit("CAVALRY", RED, 2, i + 3)); | |
} | |
// BLUE - ๋ณด๋ณ(6), ๊ถ์(3), ๊ธฐ๋ณ(3) / ์์๋ก ์ฐ์ธก์ ๋ฐฐ์น | |
for (let i = 0; i < 6; i++) { | |
units.push(new Unit("INFANTRY", BLUE, WIDTH - 1, i % HEIGHT)); | |
} | |
for (let i = 0; i < 3; i++) { | |
units.push(new Unit("ARCHER", BLUE, WIDTH - 2, i)); | |
} | |
for (let i = 0; i < 3; i++) { | |
units.push(new Unit("CAVALRY", BLUE, WIDTH - 3, i + 3)); | |
} | |
} | |
/******************************************************** | |
* 4. ํ๋ฉด ๋ ๋๋ง | |
********************************************************/ | |
function renderBoard() { | |
const board = document.getElementById("gameBoard"); | |
board.innerHTML = ""; | |
// ๋งต์ HEIGHT x WIDTH ์ํํ๋ฉฐ ์ ์์ฑ | |
for (let y = 0; y < HEIGHT; y++) { | |
for (let x = 0; x < WIDTH; x++) { | |
const cellDiv = document.createElement("div"); | |
cellDiv.classList.add("cell"); | |
cellDiv.dataset.x = x; | |
cellDiv.dataset.y = y; | |
// ์ด ์ขํ์ ์ ๋์ด ์๋์ง ํ์ธ | |
const unitHere = units.find(u => u.isAlive && u.x === x && u.y === y); | |
if (unitHere) { | |
if (unitHere.team === RED) cellDiv.classList.add("red"); | |
else cellDiv.classList.add("blue"); | |
cellDiv.textContent = unitHere.displayName; | |
} | |
// ํด๋ฆญ ์ด๋ฒคํธ | |
cellDiv.addEventListener("click", () => onCellClick(x, y)); | |
board.appendChild(cellDiv); | |
} | |
} | |
// ์๋จ ์ ๋ณด | |
document.getElementById("turnInfo").textContent = `ํ์ฌ ํด: ${currentTeam}`; | |
if (selectedUnit) { | |
document.getElementById("selectedInfo").textContent = | |
`์ ํ ์ ๋: ${selectedUnit.type} (HP: ${selectedUnit.hp})`; | |
} else { | |
document.getElementById("selectedInfo").textContent = `์ ํ ์ ๋: ์์`; | |
} | |
} | |
/******************************************************** | |
* 5. ์ ์ ์ ๋ ฅ ์ฒ๋ฆฌ | |
********************************************************/ | |
function onCellClick(x, y) { | |
if (currentTeam !== RED) { | |
// ํ๋ ์ด์ด ํด์ด ์๋ ๋๋ ํด๋ฆญ ๋ฌด์ | |
return; | |
} | |
// ํด๋ฆญ ์ง์ ์ ์ ๋์ด ์๋์ง ํ์ธ | |
const clickedUnit = units.find(u => u.isAlive && u.x === x && u.y === y); | |
if (!selectedUnit) { | |
// ์์ง ์ ๋์ ์ ํํ์ง ์์ ์ํ | |
if (clickedUnit && clickedUnit.team === RED) { | |
// ์๊ตฐ ์ ๋์ด๋ฉด ์ ํ | |
if (!clickedUnit.hasActed) { | |
selectedUnit = clickedUnit; | |
} | |
} | |
} else { | |
// ์ด๋ฏธ ์ ๋์ ์ ํํ ์ํ | |
if (clickedUnit) { | |
// ๊ณต๊ฒฉ ์๋ ์ฌ๋ถ ํ์ธ | |
if (clickedUnit.team !== RED) { | |
// ์ ์ ๋์ด๋ฉด ๊ณต๊ฒฉ ์๋ | |
const dist = getDistance(selectedUnit, clickedUnit); | |
if (dist <= selectedUnit.range) { | |
// ๊ณต๊ฒฉ | |
attack(selectedUnit, clickedUnit); | |
selectedUnit.hasActed = true; | |
selectedUnit = null; | |
} | |
} else { | |
// ๊ฐ์ ํ ์ ๋ ํด๋ฆญ => ์ ํ ์ ๋ ๋ณ๊ฒฝ(๋จ, ์ด๋ฏธ ํ๋ ๋๋ ์ ๋์ ์ ํ ๋ถ๊ฐ) | |
if (!clickedUnit.hasActed) { | |
selectedUnit = clickedUnit; | |
} | |
} | |
} else { | |
// ๋น ์นธ ํด๋ฆญ -> ์ด๋ ์๋ | |
const dist = Math.abs(selectedUnit.x - x) + Math.abs(selectedUnit.y - y); | |
// (๊ฐ๋จํ ๋งจํดํผ ๊ฑฐ๋ฆฌ๋ก๋ง ๊ณ์ฐ. ๋๊ฐ ์ด๋/์ฅ์ ๋ฌผ์ ๊ณ ๋ ค ์ ํจ) | |
if (dist <= selectedUnit.move) { | |
selectedUnit.x = x; | |
selectedUnit.y = y; | |
// ์ด๋๋ง ํ๊ณ ์ถ์ผ๋ฉด ์ฌ๊ธฐ์ hasActed๋ฅผ true๋ก ํ๊ฑฐ๋, | |
// ์ด๋ ํ ๊ณต๊ฒฉ์ ํ์ฉํ ์ง ์ฌ๋ถ๋ ๊ท์น์ ๋ฐ๋ผ ๊ฒฐ์ | |
// ์ฌ๊ธฐ์ ์ด๋ํด๋ ๊ณต๊ฒฉ ๊ฐ๋ฅํ๋๋ก hasActed = false ์ ์ง | |
} | |
} | |
} | |
renderBoard(); | |
} | |
// ๊ณต๊ฒฉ ํจ์ | |
function attack(attacker, defender) { | |
if (!attacker || !defender) return; | |
const baseDamage = Math.max(0, attacker.attack - defender.defense); | |
let finalDamage = baseDamage; | |
// ์์ฑ ์ฒดํฌ | |
if (attacker.advantage === defender.type) { | |
finalDamage = Math.floor(finalDamage * ADVANTAGE_MULTIPLIER); | |
} | |
defender.hp -= finalDamage; | |
// ๋ฉ์ธ์ง ์ถ๋ ฅ | |
const msg = | |
`[${attacker.team} ${attacker.type}]๊ฐ [${defender.team} ${defender.type}]๋ฅผ ๊ณต๊ฒฉ! (๋ฐ๋ฏธ์ง ${finalDamage})`; | |
showMessage(msg); | |
// ์ฌ๋ง ์ฒดํฌ | |
if (defender.hp <= 0) { | |
defender.hp = 0; | |
showMessage(`โ [${defender.team} ${defender.type}] ๊ฒฉํ!`); | |
} | |
} | |
// ๋ฉ์ธ์ง ํ์(๋จ์ํ ๋์ ์ถ๋ ฅ) | |
function showMessage(msg) { | |
const messageDiv = document.getElementById("message"); | |
messageDiv.innerHTML += msg + "<br>"; | |
messageDiv.scrollTop = messageDiv.scrollHeight; | |
} | |
/******************************************************** | |
* 6. ํด ์ข ๋ฃ & AI ๋์ | |
********************************************************/ | |
function endTurn() { | |
// ํ๋ ์ด์ด๊ฐ ํด ์ข ๋ฃ ํด๋ฆญ | |
if (currentTeam === RED) { | |
// ๋ชจ๋ ์ ๋์ hasActed ์ด๊ธฐํ | |
units.forEach(u => { | |
if (u.team === RED) { | |
u.hasActed = false; | |
} | |
}); | |
// ํด ๊ต์ฒด | |
currentTeam = BLUE; | |
selectedUnit = null; | |
renderBoard(); | |
// AI ์ํ | |
setTimeout(() => performAiTurn(), 500); | |
} | |
} | |
// AI ๊ฐ๋จ ๋ก์ง | |
function performAiTurn() { | |
// 1. ํ๋ํ ์ ์๋ BLUE ์ ๋ ๋ชฉ๋ก | |
const blueUnits = units.filter(u => u.team === BLUE && u.isAlive); | |
// ๊ฐ๋จํ ์์ฐจ ์ฒ๋ฆฌ: ๊ฐ ์ ๋๋ณ๋ก ์ ์ผ ๊ฐ๊น์ด ์ (RED)์ ํ์โ์ด๋โ๊ณต๊ฒฉ | |
for (const aiUnit of blueUnits) { | |
// ๊ฐ์ฅ ๊ฐ๊น์ด RED ์ ๋ ์ฐพ๊ธฐ | |
const enemies = units.filter(u => u.team === RED && u.isAlive); | |
if (enemies.length === 0) break; // ์ ์ด ์์ผ๋ฉด ์ข ๋ฃ | |
let closestEnemy = null; | |
let minDist = 9999; | |
for (const e of enemies) { | |
const d = getDistance(aiUnit, e); | |
if (d < minDist) { | |
minDist = d; | |
closestEnemy = e; | |
} | |
} | |
if (!closestEnemy) continue; | |
// ๊ณต๊ฒฉ ๋ฒ์ ์์ ์์ผ๋ฉด ๊ณต๊ฒฉ | |
if (minDist <= aiUnit.range) { | |
attack(aiUnit, closestEnemy); | |
} else { | |
// ๋ฒ์ ๋ฐ์ด๋ฉด ์ด๋(๊ฐ์ฅ ๊ฐ๋จํ, ๊ฐ๊น์์ง๋๋ก x,y๋ฅผ 1์นธ ์ด๋) | |
const moveStep = getSimpleMoveToward(aiUnit, closestEnemy); | |
if (moveStep) { | |
aiUnit.x = clamp(aiUnit.x + moveStep.dx, 0, WIDTH - 1); | |
aiUnit.y = clamp(aiUnit.y + moveStep.dy, 0, HEIGHT - 1); | |
// ์ด๋ ํ, ์ฌ๊ฑฐ๋ฆฌ ์์ด๋ฉด ๋ค์ ๊ณต๊ฒฉ | |
const distAfterMove = getDistance(aiUnit, closestEnemy); | |
if (distAfterMove <= aiUnit.range) { | |
attack(aiUnit, closestEnemy); | |
} | |
} | |
} | |
} | |
// AI ํด ์ข ๋ฃ | |
currentTeam = RED; | |
// AI ์ ๋ hasActed ์ด๊ธฐํ | |
units.forEach(u => { | |
if (u.team === BLUE) { | |
u.hasActed = false; | |
} | |
}); | |
renderBoard(); | |
checkWinCondition(); | |
} | |
/******************************************************** | |
* 7. ๋ณด์กฐ ํจ์๋ค | |
********************************************************/ | |
// ๋ ์ ๋ ๊ฐ ๊ฑฐ๋ฆฌ(๊ฐ๋จํ ๋งจํดํผ ๊ฑฐ๋ฆฌ) | |
function getDistance(u1, u2) { | |
return Math.abs(u1.x - u2.x) + Math.abs(u1.y - u2.y); | |
} | |
// AI ์ด๋์ฉ: ๋ชฉํ ์ ์ ๋์ ์กฐ๊ธ ๋ ๋ค๊ฐ๊ฐ๋ ๋ฐฉํฅ ๊ณ์ฐ | |
function getSimpleMoveToward(aiUnit, target) { | |
// ์ด๋๋ ฅ๋งํผ ์ธ๋ฐํ ๊ณ์ฐํ๊ธฐ๋ณด๋ค๋, 1์นธ๋ง ์ ์ง(๋๋ 2์นธ) ํ๋ ์ | |
const dx = target.x - aiUnit.x; | |
const dy = target.y - aiUnit.y; | |
let stepX = 0; | |
let stepY = 0; | |
if (Math.abs(dx) > Math.abs(dy)) { | |
stepX = dx > 0 ? 1 : -1; | |
} else { | |
stepY = dy > 0 ? 1 : -1; | |
} | |
// aiUnit.move ๋งํผ ์ด๋ํ ์๋ ์์ง๋ง, ์ฌ๊ธฐ์ ๋จ์ํํด์ 1์นธ๋ง ์ด๋ | |
return { dx: stepX, dy: stepY }; | |
} | |
// ๋ฒ์ ์ ํ ํจ์ | |
function clamp(value, min, max) { | |
return Math.max(min, Math.min(max, value)); | |
} | |
// ์น๋ฆฌ ์กฐ๊ฑด ์ฒดํฌ | |
function checkWinCondition() { | |
const redAlive = units.some(u => u.team === RED && u.isAlive); | |
const blueAlive = units.some(u => u.team === BLUE && u.isAlive); | |
if (!redAlive) { | |
showMessage("BLUE ์น๋ฆฌ!"); | |
} else if (!blueAlive) { | |
showMessage("RED ์น๋ฆฌ!"); | |
} | |
} | |
/******************************************************** | |
* 8. ์ด๊ธฐ ์คํ | |
********************************************************/ | |
// ์ด๊ธฐ ์ธํ | |
initUnits(); | |
renderBoard(); | |
// ํด ์ข ๋ฃ ๋ฒํผ | |
document.getElementById("endTurnBtn").addEventListener("click", () => { | |
endTurn(); | |
checkWinCondition(); | |
}); | |
</script> | |
</body> | |
</html> | |