Spaces:
Running
Running
Update index.html
Browse files- index.html +50 -166
index.html
CHANGED
|
@@ -301,28 +301,29 @@
|
|
| 301 |
}
|
| 302 |
|
| 303 |
handleMove(e) {
|
| 304 |
-
|
| 305 |
-
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
|
|
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
}
|
| 322 |
-
}
|
| 323 |
}
|
| 324 |
}
|
| 325 |
-
|
| 326 |
|
| 327 |
isValidMove(row, col) {
|
| 328 |
if(this.board[row][col] !== null) return false;
|
|
@@ -492,171 +493,65 @@
|
|
| 492 |
|
| 493 |
// GoGame ν΄λμ€ λ΄μ makeStrategicMove λ©μλλ₯Ό λ€μκ³Ό κ°μ΄ μμ
|
| 494 |
makeStrategicMove() {
|
| 495 |
-
// νμ¬ λ³΄λ μν νκ°
|
| 496 |
-
const boardScore = this.evaluateBoard();
|
| 497 |
-
|
| 498 |
-
// κ°λ₯ν λͺ¨λ μμ μ μλ₯Ό κ³μ°
|
| 499 |
let bestScore = -Infinity;
|
| 500 |
let bestMove = null;
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
this.
|
| 507 |
-
|
| 508 |
-
this.board[i][j] = null;
|
| 509 |
-
|
| 510 |
-
if(score > bestScore) {
|
| 511 |
bestScore = score;
|
| 512 |
bestMove = [i, j];
|
| 513 |
}
|
| 514 |
}
|
| 515 |
}
|
| 516 |
}
|
| 517 |
-
|
| 518 |
-
|
|
|
|
| 519 |
this.placeStone(bestMove[0], bestMove[1]);
|
| 520 |
} else {
|
| 521 |
this.pass();
|
| 522 |
}
|
| 523 |
}
|
| 524 |
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
territory: this.evaluateTerritory(),
|
| 528 |
-
influence: this.evaluateInfluence(),
|
| 529 |
-
connections: this.evaluateConnections(),
|
| 530 |
-
captures: this.captures
|
| 531 |
-
};
|
| 532 |
-
return evaluation;
|
| 533 |
-
}
|
| 534 |
-
|
| 535 |
-
calculateMoveScore(row, col, boardScore) {
|
| 536 |
-
let score = 0;
|
| 537 |
-
|
| 538 |
-
// 1. μν μ μ
|
| 539 |
-
score += this.calculateTerritoryScore(row, col) * 2;
|
| 540 |
-
|
| 541 |
-
// 2. μ°κ²°μ± μ μ
|
| 542 |
-
score += this.calculateConnectionScore(row, col) * 1.5;
|
| 543 |
-
|
| 544 |
-
// 3. 곡격/λ°©μ΄ μ μ
|
| 545 |
-
score += this.calculateTacticalScore(row, col);
|
| 546 |
-
|
| 547 |
-
// 4. μν ννΌ μ μ
|
| 548 |
-
score += this.calculateSafetyScore(row, col);
|
| 549 |
-
|
| 550 |
-
// 5. μ λ΅μ μμΉ μ μ
|
| 551 |
-
score += this.calculateStrategicScore(row, col);
|
| 552 |
-
|
| 553 |
-
return score;
|
| 554 |
-
}
|
| 555 |
-
|
| 556 |
-
calculateTerritoryScore(row, col) {
|
| 557 |
let score = 0;
|
| 558 |
-
const radius = 3;
|
| 559 |
-
|
| 560 |
-
for(let i = Math.max(0, row-radius); i <= Math.min(this.size-1, row+radius); i++) {
|
| 561 |
-
for(let j = Math.max(0, col-radius); j <= Math.min(this.size-1, col+radius); j++) {
|
| 562 |
-
const distance = Math.abs(row-i) + Math.abs(col-j);
|
| 563 |
-
if(distance <= radius) {
|
| 564 |
-
if(this.board[i][j] === null) score += (radius - distance + 1);
|
| 565 |
-
else if(this.board[i][j] === 'white') score += 2;
|
| 566 |
-
else score -= 1;
|
| 567 |
-
}
|
| 568 |
-
}
|
| 569 |
-
}
|
| 570 |
-
return score;
|
| 571 |
-
}
|
| 572 |
|
| 573 |
-
|
| 574 |
-
let score = 0;
|
| 575 |
const neighbors = this.getNeighbors(row, col);
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
if(this.board[nRow][nCol] === '
|
| 579 |
-
score += 10;
|
| 580 |
-
// λ λμ΄ μ°κ²°λλ©΄ λ λμ μ μ
|
| 581 |
-
if(this.areConnected(row, col, nRow, nCol)) score += 5;
|
| 582 |
-
}
|
| 583 |
-
}
|
| 584 |
-
return score;
|
| 585 |
-
}
|
| 586 |
-
|
| 587 |
-
calculateTacticalScore(row, col) {
|
| 588 |
-
let score = 0;
|
| 589 |
-
const neighbors = this.getNeighbors(row, col);
|
| 590 |
-
|
| 591 |
-
// μλ λ 곡격 κΈ°ν νμΈ
|
| 592 |
-
for(const [nRow, nCol] of neighbors) {
|
| 593 |
-
if(this.board[nRow][nCol] === 'black') {
|
| 594 |
-
const group = this.getGroup(nRow, nCol);
|
| 595 |
-
const liberties = this.countLiberties(group);
|
| 596 |
-
if(liberties <= 2) score += (20 - liberties * 5);
|
| 597 |
-
}
|
| 598 |
}
|
| 599 |
-
|
| 600 |
-
return score;
|
| 601 |
-
}
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
const
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
score += liberties * 5;
|
| 609 |
-
|
| 610 |
-
// λ νμ± κ°λ₯μ± μ²΄ν¬
|
| 611 |
-
if(this.canFormEye(row, col)) score += 15;
|
| 612 |
-
|
| 613 |
-
return score;
|
| 614 |
-
}
|
| 615 |
|
| 616 |
-
|
| 617 |
-
let score = 0;
|
| 618 |
-
|
| 619 |
-
// μ€μ μ§μ μ νΈ
|
| 620 |
const centerDistance = Math.abs(row - 9) + Math.abs(col - 9);
|
| 621 |
score += Math.max(0, 10 - centerDistance);
|
| 622 |
-
|
| 623 |
-
// λ³μ λΆμ§ μλλ‘
|
| 624 |
-
if(row === 0 || row === 18 || col === 0 || col === 18) score -= 5;
|
| 625 |
-
|
| 626 |
-
// μ€νν¬μΈνΈ κ·Όμ² μ νΈ
|
| 627 |
-
if(this.isNearStarPoint(row, col)) score += 3;
|
| 628 |
-
|
| 629 |
-
return score;
|
| 630 |
-
}
|
| 631 |
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
for(const [row, col] of group) {
|
| 636 |
-
const neighbors = this.getNeighbors(row, col);
|
| 637 |
-
for(const [nRow, nCol] of neighbors) {
|
| 638 |
-
if(this.board[nRow][nCol] === null) {
|
| 639 |
-
liberties.add(`${nRow},${nCol}`);
|
| 640 |
-
}
|
| 641 |
-
}
|
| 642 |
}
|
| 643 |
-
|
| 644 |
-
return liberties.size;
|
| 645 |
-
}
|
| 646 |
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
let emptyCount = 0;
|
| 651 |
-
|
| 652 |
-
for(const [nRow, nCol] of neighbors) {
|
| 653 |
-
if(this.board[nRow][nCol] === 'white') whiteCount++;
|
| 654 |
-
else if(this.board[nRow][nCol] === null) emptyCount++;
|
| 655 |
}
|
| 656 |
-
|
| 657 |
-
return
|
| 658 |
}
|
| 659 |
|
|
|
|
| 660 |
isNearStarPoint(row, col) {
|
| 661 |
const starPoints = [
|
| 662 |
[3,3], [3,9], [3,15],
|
|
@@ -669,17 +564,6 @@ isNearStarPoint(row, col) {
|
|
| 669 |
Math.abs(point[1] - col) <= 1
|
| 670 |
);
|
| 671 |
}
|
| 672 |
-
|
| 673 |
-
areConnected(row1, col1, row2, col2) {
|
| 674 |
-
const diagonal = Math.abs(row1 - row2) === 1 && Math.abs(col1 - col2) === 1;
|
| 675 |
-
if(!diagonal) return true;
|
| 676 |
-
|
| 677 |
-
// λκ°μ λ°©ν₯ μ°κ²° νμΈ
|
| 678 |
-
const mid1 = this.board[row1][col2];
|
| 679 |
-
const mid2 = this.board[row2][col1];
|
| 680 |
-
|
| 681 |
-
return mid1 === null || mid2 === null;
|
| 682 |
-
}
|
| 683 |
}
|
| 684 |
|
| 685 |
const game = new GoGame();
|
|
|
|
| 301 |
}
|
| 302 |
|
| 303 |
handleMove(e) {
|
| 304 |
+
const row = parseInt(e.target.dataset.row);
|
| 305 |
+
const col = parseInt(e.target.dataset.col);
|
| 306 |
|
| 307 |
+
// AI μ°¨λ‘μΌ λλ νλ μ΄μ΄κ° λμ λμ μ μμ
|
| 308 |
+
if (this.gameMode === 'ai' && this.currentPlayer === 'white') {
|
| 309 |
+
return;
|
| 310 |
+
}
|
| 311 |
|
| 312 |
+
if (this.isValidMove(row, col)) {
|
| 313 |
+
this.placeStone(row, col);
|
| 314 |
+
|
| 315 |
+
// AI μ°¨λ‘μΌ λ μλμΌλ‘ λ λκΈ°
|
| 316 |
+
if (this.gameMode === 'ai' && this.currentPlayer === 'white') {
|
| 317 |
+
setTimeout(() => {
|
| 318 |
+
if (this.difficulty === 'easy') {
|
| 319 |
+
this.makeRandomMove();
|
| 320 |
+
} else {
|
| 321 |
+
this.makeStrategicMove();
|
| 322 |
+
}
|
| 323 |
+
}, 500);
|
| 324 |
}
|
| 325 |
}
|
| 326 |
+
}
|
| 327 |
|
| 328 |
isValidMove(row, col) {
|
| 329 |
if(this.board[row][col] !== null) return false;
|
|
|
|
| 493 |
|
| 494 |
// GoGame ν΄λμ€ λ΄μ makeStrategicMove λ©μλλ₯Ό λ€μκ³Ό κ°μ΄ μμ
|
| 495 |
makeStrategicMove() {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
let bestScore = -Infinity;
|
| 497 |
let bestMove = null;
|
| 498 |
+
|
| 499 |
+
// λͺ¨λ κ°λ₯ν μμΉ νκ°
|
| 500 |
+
for (let i = 0; i < this.size; i++) {
|
| 501 |
+
for (let j = 0; j < this.size; j++) {
|
| 502 |
+
if (this.isValidMove(i, j)) {
|
| 503 |
+
const score = this.evaluateMove(i, j);
|
| 504 |
+
if (score > bestScore) {
|
|
|
|
|
|
|
|
|
|
| 505 |
bestScore = score;
|
| 506 |
bestMove = [i, j];
|
| 507 |
}
|
| 508 |
}
|
| 509 |
}
|
| 510 |
}
|
| 511 |
+
|
| 512 |
+
// μ΅μ μ μμΉμ λ λκΈ°
|
| 513 |
+
if (bestMove) {
|
| 514 |
this.placeStone(bestMove[0], bestMove[1]);
|
| 515 |
} else {
|
| 516 |
this.pass();
|
| 517 |
}
|
| 518 |
}
|
| 519 |
|
| 520 |
+
// μμΉ νκ°λ₯Ό μν λ©μλ μΆκ°
|
| 521 |
+
evaluateMove(row, col) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
let score = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
|
| 524 |
+
// 1. μ£Όλ³ λ νκ°
|
|
|
|
| 525 |
const neighbors = this.getNeighbors(row, col);
|
| 526 |
+
for (const [nRow, nCol] of neighbors) {
|
| 527 |
+
if (this.board[nRow][nCol] === 'white') score += 10;
|
| 528 |
+
else if (this.board[nRow][nCol] === 'black') score += 5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
}
|
|
|
|
|
|
|
|
|
|
| 530 |
|
| 531 |
+
// 2. ν¬μ κ°λ₯μ± νκ°
|
| 532 |
+
this.board[row][col] = 'white';
|
| 533 |
+
const captures = this.checkCaptures(row, col);
|
| 534 |
+
this.board[row][col] = null;
|
| 535 |
+
score += captures.length * 30;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
|
| 537 |
+
// 3. μ€μ μ νΈλ
|
|
|
|
|
|
|
|
|
|
| 538 |
const centerDistance = Math.abs(row - 9) + Math.abs(col - 9);
|
| 539 |
score += Math.max(0, 10 - centerDistance);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
| 541 |
+
// 4. μ€νν¬μΈνΈ κ·Όμ² μ νΈ
|
| 542 |
+
if (this.isNearStarPoint(row, col)) {
|
| 543 |
+
score += 5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
}
|
|
|
|
|
|
|
|
|
|
| 545 |
|
| 546 |
+
// 5. κ°μ₯μ리 ννΌ
|
| 547 |
+
if (row === 0 || row === 18 || col === 0 || col === 18) {
|
| 548 |
+
score -= 5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
}
|
| 550 |
+
|
| 551 |
+
return score;
|
| 552 |
}
|
| 553 |
|
| 554 |
+
// μ€νν¬μΈνΈ κ·Όμ²μΈμ§ νμΈνλ λ©μλ μΆκ°
|
| 555 |
isNearStarPoint(row, col) {
|
| 556 |
const starPoints = [
|
| 557 |
[3,3], [3,9], [3,15],
|
|
|
|
| 564 |
Math.abs(point[1] - col) <= 1
|
| 565 |
);
|
| 566 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
}
|
| 568 |
|
| 569 |
const game = new GoGame();
|