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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="ko">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>๋ผ์ธ๋ฐฐํ: AI ์ฑ๊ธํ๋ ์ด ์์</title>
|
| 6 |
+
<style>
|
| 7 |
+
/* ๊ฐ๋จํ ์คํ์ผ */
|
| 8 |
+
body {
|
| 9 |
+
margin: 20px;
|
| 10 |
+
font-family: sans-serif;
|
| 11 |
+
}
|
| 12 |
+
h1 {
|
| 13 |
+
margin-bottom: 10px;
|
| 14 |
+
}
|
| 15 |
+
#infoBar {
|
| 16 |
+
margin-bottom: 10px;
|
| 17 |
+
}
|
| 18 |
+
#gameBoard {
|
| 19 |
+
display: grid;
|
| 20 |
+
grid-template-columns: repeat(10, 40px);
|
| 21 |
+
grid-template-rows: repeat(6, 40px);
|
| 22 |
+
gap: 2px;
|
| 23 |
+
margin-bottom: 10px;
|
| 24 |
+
}
|
| 25 |
+
.cell {
|
| 26 |
+
width: 40px;
|
| 27 |
+
height: 40px;
|
| 28 |
+
border: 1px solid #666;
|
| 29 |
+
display: flex;
|
| 30 |
+
align-items: center;
|
| 31 |
+
justify-content: center;
|
| 32 |
+
cursor: pointer;
|
| 33 |
+
background-color: #ccc;
|
| 34 |
+
}
|
| 35 |
+
.red {
|
| 36 |
+
background-color: #ffaaaa;
|
| 37 |
+
}
|
| 38 |
+
.blue {
|
| 39 |
+
background-color: #aaaaff;
|
| 40 |
+
}
|
| 41 |
+
#endTurnBtn {
|
| 42 |
+
padding: 6px 12px;
|
| 43 |
+
font-size: 14px;
|
| 44 |
+
cursor: pointer;
|
| 45 |
+
margin-right: 10px;
|
| 46 |
+
}
|
| 47 |
+
</style>
|
| 48 |
+
</head>
|
| 49 |
+
<body>
|
| 50 |
+
<h1>๋ผ์ธ๋ฐฐํ: AI ์ฑ๊ธํ๋ ์ด ์์</h1>
|
| 51 |
+
<div id="infoBar">
|
| 52 |
+
<span id="turnInfo"></span> |
|
| 53 |
+
<span id="selectedInfo"></span>
|
| 54 |
+
</div>
|
| 55 |
+
<div id="gameBoard"></div>
|
| 56 |
+
<button id="endTurnBtn">ํด ์ข
๋ฃ</button>
|
| 57 |
+
<div id="message"></div>
|
| 58 |
+
|
| 59 |
+
<script>
|
| 60 |
+
/********************************************************
|
| 61 |
+
* 1. ์ ๋/๊ฒ์ ํ๋ผ๋ฏธํฐ ์ ์
|
| 62 |
+
********************************************************/
|
| 63 |
+
const UNIT_TYPES = {
|
| 64 |
+
INFANTRY: {
|
| 65 |
+
name: "Infantry",
|
| 66 |
+
hp: 25,
|
| 67 |
+
attack: 3,
|
| 68 |
+
defense: 3,
|
| 69 |
+
move: 2,
|
| 70 |
+
range: 1,
|
| 71 |
+
advantage: "ARCHER", // ๋ณด๋ณ > ๊ถ์
|
| 72 |
+
},
|
| 73 |
+
ARCHER: {
|
| 74 |
+
name: "Archer",
|
| 75 |
+
hp: 20,
|
| 76 |
+
attack: 2,
|
| 77 |
+
defense: 2,
|
| 78 |
+
move: 2,
|
| 79 |
+
range: 3,
|
| 80 |
+
advantage: "CAVALRY", // ๊ถ์ > ๊ธฐ๋ณ
|
| 81 |
+
},
|
| 82 |
+
CAVALRY: {
|
| 83 |
+
name: "Cavalry",
|
| 84 |
+
hp: 22,
|
| 85 |
+
attack: 4,
|
| 86 |
+
defense: 2,
|
| 87 |
+
move: 3,
|
| 88 |
+
range: 1,
|
| 89 |
+
advantage: "INFANTRY", // ๊ธฐ๋ณ > ๋ณด๋ณ
|
| 90 |
+
}
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
// ๊ฐ๋จํ ์์ฑ ๋ณด์ (์: 1.5๋ฐฐ)
|
| 94 |
+
const ADVANTAGE_MULTIPLIER = 1.5;
|
| 95 |
+
|
| 96 |
+
// ๋งต ํฌ๊ธฐ
|
| 97 |
+
const WIDTH = 10;
|
| 98 |
+
const HEIGHT = 6;
|
| 99 |
+
|
| 100 |
+
// ํ ์ ์
|
| 101 |
+
const RED = "RED";
|
| 102 |
+
const BLUE = "BLUE";
|
| 103 |
+
|
| 104 |
+
// ๊ฒ์ ์ํ
|
| 105 |
+
let units = []; // ์ ์ฒด ์ ๋ ๊ฐ์ฒด๋ฅผ ๋ณด๊ด
|
| 106 |
+
let currentTeam = RED; // ํ์ฌ ํด์ ํ
|
| 107 |
+
let selectedUnit = null; // ์ ์ ๊ฐ ์ ํํ ์ ๋(RED ํด ์ค)
|
| 108 |
+
|
| 109 |
+
/********************************************************
|
| 110 |
+
* 2. ์ ๋ ํด๋์ค
|
| 111 |
+
********************************************************/
|
| 112 |
+
class Unit {
|
| 113 |
+
constructor(typeKey, team, x, y) {
|
| 114 |
+
const data = UNIT_TYPES[typeKey];
|
| 115 |
+
this.type = typeKey; // "INFANTRY", "ARCHER", "CAVALRY"
|
| 116 |
+
this.team = team; // "RED" ๋๋ "BLUE"
|
| 117 |
+
this.hp = data.hp;
|
| 118 |
+
this.attack = data.attack;
|
| 119 |
+
this.defense = data.defense;
|
| 120 |
+
this.move = data.move;
|
| 121 |
+
this.range = data.range;
|
| 122 |
+
this.advantage = data.advantage;
|
| 123 |
+
this.x = x;
|
| 124 |
+
this.y = y;
|
| 125 |
+
this.hasActed = false; // ์ด ํด ์ค ์ด๋/๊ณต๊ฒฉ ์ฌ๋ถ๋ฅผ ๋จ์ ์ฒดํฌ(ํ์ฅ ๊ฐ๋ฅ)
|
| 126 |
+
}
|
| 127 |
+
get isAlive() {
|
| 128 |
+
return this.hp > 0;
|
| 129 |
+
}
|
| 130 |
+
get displayName() {
|
| 131 |
+
// ์: 'I25' (Infantry, HP 25)
|
| 132 |
+
return this.type[0] + this.hp;
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/********************************************************
|
| 137 |
+
* 3. ์ด๊ธฐํ: ์ ๋ ๋ฐฐ์น
|
| 138 |
+
********************************************************/
|
| 139 |
+
function initUnits() {
|
| 140 |
+
units = [];
|
| 141 |
+
// RED - ๋ณด๋ณ(6), ๊ถ์(3), ๊ธฐ๋ณ(3) / ์์๋ก ์ข์ธก์ ๋ฐฐ์น
|
| 142 |
+
for (let i = 0; i < 6; i++) {
|
| 143 |
+
units.push(new Unit("INFANTRY", RED, 0, i % HEIGHT));
|
| 144 |
+
}
|
| 145 |
+
for (let i = 0; i < 3; i++) {
|
| 146 |
+
units.push(new Unit("ARCHER", RED, 1, i));
|
| 147 |
+
}
|
| 148 |
+
for (let i = 0; i < 3; i++) {
|
| 149 |
+
units.push(new Unit("CAVALRY", RED, 2, i + 3));
|
| 150 |
+
}
|
| 151 |
+
// BLUE - ๋ณด๋ณ(6), ๊ถ์(3), ๊ธฐ๋ณ(3) / ์์๋ก ์ฐ์ธก์ ๋ฐฐ์น
|
| 152 |
+
for (let i = 0; i < 6; i++) {
|
| 153 |
+
units.push(new Unit("INFANTRY", BLUE, WIDTH - 1, i % HEIGHT));
|
| 154 |
+
}
|
| 155 |
+
for (let i = 0; i < 3; i++) {
|
| 156 |
+
units.push(new Unit("ARCHER", BLUE, WIDTH - 2, i));
|
| 157 |
+
}
|
| 158 |
+
for (let i = 0; i < 3; i++) {
|
| 159 |
+
units.push(new Unit("CAVALRY", BLUE, WIDTH - 3, i + 3));
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
/********************************************************
|
| 164 |
+
* 4. ํ๋ฉด ๋ ๋๋ง
|
| 165 |
+
********************************************************/
|
| 166 |
+
function renderBoard() {
|
| 167 |
+
const board = document.getElementById("gameBoard");
|
| 168 |
+
board.innerHTML = "";
|
| 169 |
+
|
| 170 |
+
// ๋งต์ HEIGHT x WIDTH ์ํํ๋ฉฐ ์
์์ฑ
|
| 171 |
+
for (let y = 0; y < HEIGHT; y++) {
|
| 172 |
+
for (let x = 0; x < WIDTH; x++) {
|
| 173 |
+
const cellDiv = document.createElement("div");
|
| 174 |
+
cellDiv.classList.add("cell");
|
| 175 |
+
cellDiv.dataset.x = x;
|
| 176 |
+
cellDiv.dataset.y = y;
|
| 177 |
+
|
| 178 |
+
// ์ด ์ขํ์ ์ ๋์ด ์๋์ง ํ์ธ
|
| 179 |
+
const unitHere = units.find(u => u.isAlive && u.x === x && u.y === y);
|
| 180 |
+
if (unitHere) {
|
| 181 |
+
if (unitHere.team === RED) cellDiv.classList.add("red");
|
| 182 |
+
else cellDiv.classList.add("blue");
|
| 183 |
+
cellDiv.textContent = unitHere.displayName;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// ํด๋ฆญ ์ด๋ฒคํธ
|
| 187 |
+
cellDiv.addEventListener("click", () => onCellClick(x, y));
|
| 188 |
+
board.appendChild(cellDiv);
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
// ์๋จ ์ ๋ณด
|
| 193 |
+
document.getElementById("turnInfo").textContent = `ํ์ฌ ํด: ${currentTeam}`;
|
| 194 |
+
if (selectedUnit) {
|
| 195 |
+
document.getElementById("selectedInfo").textContent =
|
| 196 |
+
`์ ํ ์ ๋: ${selectedUnit.type} (HP: ${selectedUnit.hp})`;
|
| 197 |
+
} else {
|
| 198 |
+
document.getElementById("selectedInfo").textContent = `์ ํ ์ ๋: ์์`;
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
/********************************************************
|
| 203 |
+
* 5. ์ ์ ์
๋ ฅ ์ฒ๋ฆฌ
|
| 204 |
+
********************************************************/
|
| 205 |
+
function onCellClick(x, y) {
|
| 206 |
+
if (currentTeam !== RED) {
|
| 207 |
+
// ํ๋ ์ด์ด ํด์ด ์๋ ๋๋ ํด๋ฆญ ๋ฌด์
|
| 208 |
+
return;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
// ํด๋ฆญ ์ง์ ์ ์ ๋์ด ์๋์ง ํ์ธ
|
| 212 |
+
const clickedUnit = units.find(u => u.isAlive && u.x === x && u.y === y);
|
| 213 |
+
|
| 214 |
+
if (!selectedUnit) {
|
| 215 |
+
// ์์ง ์ ๋์ ์ ํํ์ง ์์ ์ํ
|
| 216 |
+
if (clickedUnit && clickedUnit.team === RED) {
|
| 217 |
+
// ์๊ตฐ ์ ๋์ด๋ฉด ์ ํ
|
| 218 |
+
if (!clickedUnit.hasActed) {
|
| 219 |
+
selectedUnit = clickedUnit;
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
} else {
|
| 223 |
+
// ์ด๋ฏธ ์ ๋์ ์ ํํ ์ํ
|
| 224 |
+
if (clickedUnit) {
|
| 225 |
+
// ๊ณต๊ฒฉ ์๋ ์ฌ๋ถ ํ์ธ
|
| 226 |
+
if (clickedUnit.team !== RED) {
|
| 227 |
+
// ์ ์ ๋์ด๋ฉด ๊ณต๊ฒฉ ์๋
|
| 228 |
+
const dist = getDistance(selectedUnit, clickedUnit);
|
| 229 |
+
if (dist <= selectedUnit.range) {
|
| 230 |
+
// ๊ณต๊ฒฉ
|
| 231 |
+
attack(selectedUnit, clickedUnit);
|
| 232 |
+
selectedUnit.hasActed = true;
|
| 233 |
+
selectedUnit = null;
|
| 234 |
+
}
|
| 235 |
+
} else {
|
| 236 |
+
// ๊ฐ์ ํ ์ ๋ ํด๋ฆญ => ์ ํ ์ ๋ ๋ณ๊ฒฝ(๋จ, ์ด๋ฏธ ํ๋ ๋๋ ์ ๋์ ์ ํ ๋ถ๊ฐ)
|
| 237 |
+
if (!clickedUnit.hasActed) {
|
| 238 |
+
selectedUnit = clickedUnit;
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
+
} else {
|
| 242 |
+
// ๋น ์นธ ํด๋ฆญ -> ์ด๋ ์๋
|
| 243 |
+
const dist = Math.abs(selectedUnit.x - x) + Math.abs(selectedUnit.y - y);
|
| 244 |
+
// (๊ฐ๋จํ ๋งจํดํผ ๊ฑฐ๋ฆฌ๋ก๋ง ๊ณ์ฐ. ๋๊ฐ ์ด๋/์ฅ์ ๋ฌผ์ ๊ณ ๋ ค ์ ํจ)
|
| 245 |
+
if (dist <= selectedUnit.move) {
|
| 246 |
+
selectedUnit.x = x;
|
| 247 |
+
selectedUnit.y = y;
|
| 248 |
+
// ์ด๋๋ง ํ๊ณ ์ถ์ผ๋ฉด ์ฌ๊ธฐ์ hasActed๋ฅผ true๋ก ํ๊ฑฐ๋,
|
| 249 |
+
// ์ด๋ ํ ๊ณต๊ฒฉ์ ํ์ฉํ ์ง ์ฌ๋ถ๋ ๊ท์น์ ๋ฐ๋ผ ๊ฒฐ์
|
| 250 |
+
// ์ฌ๊ธฐ์ ์ด๋ํด๋ ๊ณต๊ฒฉ ๊ฐ๋ฅํ๋๋ก hasActed = false ์ ์ง
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
}
|
| 254 |
+
renderBoard();
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
// ๊ณต๊ฒฉ ํจ์
|
| 258 |
+
function attack(attacker, defender) {
|
| 259 |
+
if (!attacker || !defender) return;
|
| 260 |
+
const baseDamage = Math.max(0, attacker.attack - defender.defense);
|
| 261 |
+
let finalDamage = baseDamage;
|
| 262 |
+
// ์์ฑ ์ฒดํฌ
|
| 263 |
+
if (attacker.advantage === defender.type) {
|
| 264 |
+
finalDamage = Math.floor(finalDamage * ADVANTAGE_MULTIPLIER);
|
| 265 |
+
}
|
| 266 |
+
defender.hp -= finalDamage;
|
| 267 |
+
// ๋ฉ์ธ์ง ์ถ๋ ฅ
|
| 268 |
+
const msg =
|
| 269 |
+
`[${attacker.team} ${attacker.type}]๊ฐ [${defender.team} ${defender.type}]๋ฅผ ๊ณต๊ฒฉ! (๋ฐ๋ฏธ์ง ${finalDamage})`;
|
| 270 |
+
showMessage(msg);
|
| 271 |
+
// ์ฌ๋ง ์ฒดํฌ
|
| 272 |
+
if (defender.hp <= 0) {
|
| 273 |
+
defender.hp = 0;
|
| 274 |
+
showMessage(`โ [${defender.team} ${defender.type}] ๊ฒฉํ!`);
|
| 275 |
+
}
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// ๋ฉ์ธ์ง ํ์(๋จ์ํ ๋์ ์ถ๋ ฅ)
|
| 279 |
+
function showMessage(msg) {
|
| 280 |
+
const messageDiv = document.getElementById("message");
|
| 281 |
+
messageDiv.innerHTML += msg + "<br>";
|
| 282 |
+
messageDiv.scrollTop = messageDiv.scrollHeight;
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
/********************************************************
|
| 286 |
+
* 6. ํด ์ข
๋ฃ & AI ๋์
|
| 287 |
+
********************************************************/
|
| 288 |
+
function endTurn() {
|
| 289 |
+
// ํ๋ ์ด์ด๊ฐ ํด ์ข
๋ฃ ํด๋ฆญ
|
| 290 |
+
if (currentTeam === RED) {
|
| 291 |
+
// ๋ชจ๋ ์ ๋์ hasActed ์ด๊ธฐํ
|
| 292 |
+
units.forEach(u => {
|
| 293 |
+
if (u.team === RED) {
|
| 294 |
+
u.hasActed = false;
|
| 295 |
+
}
|
| 296 |
+
});
|
| 297 |
+
// ํด ๊ต์ฒด
|
| 298 |
+
currentTeam = BLUE;
|
| 299 |
+
selectedUnit = null;
|
| 300 |
+
renderBoard();
|
| 301 |
+
// AI ์ํ
|
| 302 |
+
setTimeout(() => performAiTurn(), 500);
|
| 303 |
+
}
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
// AI ๊ฐ๋จ ๋ก์ง
|
| 307 |
+
function performAiTurn() {
|
| 308 |
+
// 1. ํ๋ํ ์ ์๋ BLUE ์ ๋ ๋ชฉ๋ก
|
| 309 |
+
const blueUnits = units.filter(u => u.team === BLUE && u.isAlive);
|
| 310 |
+
|
| 311 |
+
// ๊ฐ๋จํ ์์ฐจ ์ฒ๋ฆฌ: ๊ฐ ์ ๋๋ณ๋ก ์ ์ผ ๊ฐ๊น์ด ์ (RED)์ ํ์โ์ด๋โ๊ณต๊ฒฉ
|
| 312 |
+
for (const aiUnit of blueUnits) {
|
| 313 |
+
// ๊ฐ์ฅ ๊ฐ๊น์ด RED ์ ๋ ์ฐพ๊ธฐ
|
| 314 |
+
const enemies = units.filter(u => u.team === RED && u.isAlive);
|
| 315 |
+
if (enemies.length === 0) break; // ์ ์ด ์์ผ๋ฉด ์ข
๋ฃ
|
| 316 |
+
|
| 317 |
+
let closestEnemy = null;
|
| 318 |
+
let minDist = 9999;
|
| 319 |
+
for (const e of enemies) {
|
| 320 |
+
const d = getDistance(aiUnit, e);
|
| 321 |
+
if (d < minDist) {
|
| 322 |
+
minDist = d;
|
| 323 |
+
closestEnemy = e;
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (!closestEnemy) continue;
|
| 328 |
+
|
| 329 |
+
// ๊ณต๊ฒฉ ๋ฒ์ ์์ ์์ผ๋ฉด ๊ณต๊ฒฉ
|
| 330 |
+
if (minDist <= aiUnit.range) {
|
| 331 |
+
attack(aiUnit, closestEnemy);
|
| 332 |
+
} else {
|
| 333 |
+
// ๋ฒ์ ๋ฐ์ด๋ฉด ์ด๋(๊ฐ์ฅ ๊ฐ๋จํ, ๊ฐ๊น์์ง๋๋ก x,y๋ฅผ 1์นธ ์ด๋)
|
| 334 |
+
const moveStep = getSimpleMoveToward(aiUnit, closestEnemy);
|
| 335 |
+
if (moveStep) {
|
| 336 |
+
aiUnit.x = clamp(aiUnit.x + moveStep.dx, 0, WIDTH - 1);
|
| 337 |
+
aiUnit.y = clamp(aiUnit.y + moveStep.dy, 0, HEIGHT - 1);
|
| 338 |
+
// ์ด๋ ํ, ์ฌ๊ฑฐ๋ฆฌ ์์ด๋ฉด ๋ค์ ๊ณต๊ฒฉ
|
| 339 |
+
const distAfterMove = getDistance(aiUnit, closestEnemy);
|
| 340 |
+
if (distAfterMove <= aiUnit.range) {
|
| 341 |
+
attack(aiUnit, closestEnemy);
|
| 342 |
+
}
|
| 343 |
+
}
|
| 344 |
+
}
|
| 345 |
+
}
|
| 346 |
+
// AI ํด ์ข
๋ฃ
|
| 347 |
+
currentTeam = RED;
|
| 348 |
+
// AI ์ ๋ hasActed ์ด๊ธฐํ
|
| 349 |
+
units.forEach(u => {
|
| 350 |
+
if (u.team === BLUE) {
|
| 351 |
+
u.hasActed = false;
|
| 352 |
+
}
|
| 353 |
+
});
|
| 354 |
+
renderBoard();
|
| 355 |
+
checkWinCondition();
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
/********************************************************
|
| 359 |
+
* 7. ๋ณด์กฐ ํจ์๋ค
|
| 360 |
+
********************************************************/
|
| 361 |
+
// ๋ ์ ๋ ๊ฐ ๊ฑฐ๋ฆฌ(๊ฐ๋จํ ๋งจํดํผ ๊ฑฐ๋ฆฌ)
|
| 362 |
+
function getDistance(u1, u2) {
|
| 363 |
+
return Math.abs(u1.x - u2.x) + Math.abs(u1.y - u2.y);
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
// AI ์ด๋์ฉ: ๋ชฉํ ์ ์ ๋์ ์กฐ๊ธ ๋ ๋ค๊ฐ๊ฐ๋ ๋ฐฉํฅ ๊ณ์ฐ
|
| 367 |
+
function getSimpleMoveToward(aiUnit, target) {
|
| 368 |
+
// ์ด๋๋ ฅ๋งํผ ์ธ๋ฐํ ๊ณ์ฐํ๊ธฐ๋ณด๋ค๋, 1์นธ๋ง ์ ์ง(๋๋ 2์นธ) ํ๋ ์
|
| 369 |
+
const dx = target.x - aiUnit.x;
|
| 370 |
+
const dy = target.y - aiUnit.y;
|
| 371 |
+
let stepX = 0;
|
| 372 |
+
let stepY = 0;
|
| 373 |
+
|
| 374 |
+
if (Math.abs(dx) > Math.abs(dy)) {
|
| 375 |
+
stepX = dx > 0 ? 1 : -1;
|
| 376 |
+
} else {
|
| 377 |
+
stepY = dy > 0 ? 1 : -1;
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
// aiUnit.move ๋งํผ ์ด๋ํ ์๋ ์์ง๋ง, ์ฌ๊ธฐ์ ๋จ์ํํด์ 1์นธ๋ง ์ด๋
|
| 381 |
+
return { dx: stepX, dy: stepY };
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
// ๋ฒ์ ์ ํ ํจ์
|
| 385 |
+
function clamp(value, min, max) {
|
| 386 |
+
return Math.max(min, Math.min(max, value));
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
// ์น๋ฆฌ ์กฐ๊ฑด ์ฒดํฌ
|
| 390 |
+
function checkWinCondition() {
|
| 391 |
+
const redAlive = units.some(u => u.team === RED && u.isAlive);
|
| 392 |
+
const blueAlive = units.some(u => u.team === BLUE && u.isAlive);
|
| 393 |
+
|
| 394 |
+
if (!redAlive) {
|
| 395 |
+
showMessage("BLUE ์น๋ฆฌ!");
|
| 396 |
+
} else if (!blueAlive) {
|
| 397 |
+
showMessage("RED ์น๋ฆฌ!");
|
| 398 |
+
}
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
/********************************************************
|
| 402 |
+
* 8. ์ด๊ธฐ ์คํ
|
| 403 |
+
********************************************************/
|
| 404 |
+
// ์ด๊ธฐ ์ธํ
|
| 405 |
+
initUnits();
|
| 406 |
+
renderBoard();
|
| 407 |
+
|
| 408 |
+
// ํด ์ข
๋ฃ ๋ฒํผ
|
| 409 |
+
document.getElementById("endTurnBtn").addEventListener("click", () => {
|
| 410 |
+
endTurn();
|
| 411 |
+
checkWinCondition();
|
| 412 |
+
});
|
| 413 |
+
</script>
|
| 414 |
+
</body>
|
| 415 |
</html>
|