Spaces:
Running
Running
Update index.html
Browse files- index.html +105 -199
index.html
CHANGED
|
@@ -459,46 +459,21 @@
|
|
| 459 |
}
|
| 460 |
}
|
| 461 |
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
if(this.isValidMove(i, j)) {
|
| 467 |
-
validMoves.push([i, j]);
|
| 468 |
-
}
|
| 469 |
-
}
|
| 470 |
-
}
|
| 471 |
-
|
| 472 |
-
if(validMoves.length > 0) {
|
| 473 |
-
const [row, col] = validMoves[Math.floor(Math.random() * validMoves.length)];
|
| 474 |
-
this.placeStone(row, col);
|
| 475 |
-
} else {
|
| 476 |
-
this.pass();
|
| 477 |
-
}
|
| 478 |
-
}
|
| 479 |
-
|
| 480 |
-
makeStrategicMove() {
|
| 481 |
-
const depth = this.difficulty === 'hard' ? 3 : 1;
|
| 482 |
-
let bestMove = this.findBestMove(depth);
|
| 483 |
|
| 484 |
-
|
| 485 |
-
this.placeStone(bestMove[0], bestMove[1]);
|
| 486 |
-
} else {
|
| 487 |
-
this.pass();
|
| 488 |
-
}
|
| 489 |
-
}
|
| 490 |
-
|
| 491 |
-
findBestMove(depth) {
|
| 492 |
let bestScore = -Infinity;
|
| 493 |
let bestMove = null;
|
| 494 |
|
| 495 |
-
// μ 체 보λλ₯Ό νκ°
|
| 496 |
for(let i = 0; i < this.size; i++) {
|
| 497 |
for(let j = 0; j < this.size; j++) {
|
| 498 |
if(this.isValidMove(i, j)) {
|
| 499 |
-
//
|
| 500 |
this.board[i][j] = 'white';
|
| 501 |
-
const score = this.
|
| 502 |
this.board[i][j] = null;
|
| 503 |
|
| 504 |
if(score > bestScore) {
|
|
@@ -509,169 +484,116 @@ findBestMove(depth) {
|
|
| 509 |
}
|
| 510 |
}
|
| 511 |
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
minimax(depth, isMaximizing) {
|
| 516 |
-
if(depth === 0) {
|
| 517 |
-
return this.evaluateBoard();
|
| 518 |
-
}
|
| 519 |
-
|
| 520 |
-
if(isMaximizing) {
|
| 521 |
-
let maxScore = -Infinity;
|
| 522 |
-
for(let i = 0; i < this.size; i++) {
|
| 523 |
-
for(let j = 0; j < this.size; j++) {
|
| 524 |
-
if(this.isValidMove(i, j)) {
|
| 525 |
-
this.board[i][j] = 'white';
|
| 526 |
-
const score = this.minimax(depth - 1, false);
|
| 527 |
-
this.board[i][j] = null;
|
| 528 |
-
maxScore = Math.max(maxScore, score);
|
| 529 |
-
}
|
| 530 |
-
}
|
| 531 |
-
}
|
| 532 |
-
return maxScore;
|
| 533 |
} else {
|
| 534 |
-
|
| 535 |
-
for(let i = 0; i < this.size; i++) {
|
| 536 |
-
for(let j = 0; j < this.size; j++) {
|
| 537 |
-
if(this.isValidMove(i, j)) {
|
| 538 |
-
this.board[i][j] = 'black';
|
| 539 |
-
const score = this.minimax(depth - 1, true);
|
| 540 |
-
this.board[i][j] = null;
|
| 541 |
-
minScore = Math.min(minScore, score);
|
| 542 |
-
}
|
| 543 |
-
}
|
| 544 |
-
}
|
| 545 |
-
return minScore;
|
| 546 |
}
|
| 547 |
}
|
| 548 |
|
| 549 |
evaluateBoard() {
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
|
|
|
|
|
|
|
|
|
| 554 |
}
|
| 555 |
|
| 556 |
-
|
| 557 |
let score = 0;
|
| 558 |
-
const visited = new Set();
|
| 559 |
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
|
| 571 |
return score;
|
| 572 |
}
|
| 573 |
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
const
|
| 577 |
-
const borders = new Set();
|
| 578 |
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
territory.add(key);
|
| 587 |
-
|
| 588 |
-
const neighbors = this.getNeighbors(r, c);
|
| 589 |
-
for(const [nr, nc] of neighbors) {
|
| 590 |
-
if(this.board[nr][nc] === null) {
|
| 591 |
-
stack.push([nr, nc]);
|
| 592 |
-
} else {
|
| 593 |
-
borders.add(`${nr},${nc}`);
|
| 594 |
-
}
|
| 595 |
-
}
|
| 596 |
}
|
| 597 |
}
|
| 598 |
}
|
| 599 |
-
|
| 600 |
-
territory.borders = borders;
|
| 601 |
-
return territory;
|
| 602 |
}
|
| 603 |
|
| 604 |
-
|
| 605 |
-
let
|
| 606 |
-
|
| 607 |
|
| 608 |
-
for(const
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
|
|
|
|
|
|
| 612 |
}
|
| 613 |
-
|
| 614 |
-
if(blackCount > whiteCount) return 'black';
|
| 615 |
-
if(whiteCount > blackCount) return 'white';
|
| 616 |
-
return null;
|
| 617 |
}
|
| 618 |
|
| 619 |
-
|
| 620 |
let score = 0;
|
|
|
|
| 621 |
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
}
|
| 629 |
}
|
| 630 |
}
|
| 631 |
|
| 632 |
return score;
|
| 633 |
}
|
| 634 |
|
| 635 |
-
|
| 636 |
-
let
|
|
|
|
|
|
|
| 637 |
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
const newRow = row + i;
|
| 643 |
-
const newCol = col + j;
|
| 644 |
-
|
| 645 |
-
if(newRow >= 0 && newRow < this.size &&
|
| 646 |
-
newCol >= 0 && newCol < this.size) {
|
| 647 |
-
influence += (radius - distance + 1) / radius;
|
| 648 |
-
}
|
| 649 |
-
}
|
| 650 |
-
}
|
| 651 |
-
}
|
| 652 |
|
| 653 |
-
return
|
| 654 |
}
|
| 655 |
|
| 656 |
-
|
| 657 |
let score = 0;
|
| 658 |
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
} else {
|
| 669 |
-
score -= liberties * 2;
|
| 670 |
-
if(liberties === 1) score += 10;
|
| 671 |
-
}
|
| 672 |
-
}
|
| 673 |
-
}
|
| 674 |
-
}
|
| 675 |
|
| 676 |
return score;
|
| 677 |
}
|
|
@@ -691,57 +613,41 @@ countLiberties(group) {
|
|
| 691 |
return liberties.size;
|
| 692 |
}
|
| 693 |
|
| 694 |
-
|
| 695 |
-
let score = 0;
|
| 696 |
-
|
| 697 |
-
for(let i = 0; i < this.size; i++) {
|
| 698 |
-
for(let j = 0; j < this.size; j++) {
|
| 699 |
-
if(this.board[i][j] === 'white') {
|
| 700 |
-
score += this.evaluateStoneConnectivity(i, j);
|
| 701 |
-
} else if(this.board[i][j] === 'black') {
|
| 702 |
-
score -= this.evaluateStoneConnectivity(i, j);
|
| 703 |
-
}
|
| 704 |
-
}
|
| 705 |
-
}
|
| 706 |
-
|
| 707 |
-
return score;
|
| 708 |
-
}
|
| 709 |
-
|
| 710 |
-
evaluateStoneConnectivity(row, col) {
|
| 711 |
-
let connectivity = 0;
|
| 712 |
const neighbors = this.getNeighbors(row, col);
|
| 713 |
-
|
|
|
|
| 714 |
|
| 715 |
for(const [nRow, nCol] of neighbors) {
|
| 716 |
-
if(this.board[nRow][nCol] ===
|
| 717 |
-
|
| 718 |
-
}
|
| 719 |
}
|
| 720 |
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
|
| 727 |
-
return
|
|
|
|
|
|
|
|
|
|
| 728 |
}
|
| 729 |
|
| 730 |
-
|
| 731 |
-
const
|
| 732 |
-
|
| 733 |
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
if(newRow >= 0 && newRow < this.size &&
|
| 739 |
-
newCol >= 0 && newCol < this.size) {
|
| 740 |
-
neighbors.push([newRow, newCol]);
|
| 741 |
-
}
|
| 742 |
-
}
|
| 743 |
|
| 744 |
-
return
|
| 745 |
}
|
| 746 |
}
|
| 747 |
|
|
|
|
| 459 |
}
|
| 460 |
}
|
| 461 |
|
| 462 |
+
// GoGame ν΄λμ€ λ΄μ makeStrategicMove λ©μλλ₯Ό λ€μκ³Ό κ°μ΄ μμ
|
| 463 |
+
makeStrategicMove() {
|
| 464 |
+
// νμ¬ λ³΄λ μν νκ°
|
| 465 |
+
const boardScore = this.evaluateBoard();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
+
// κ°λ₯ν λͺ¨λ μμ μ μλ₯Ό κ³μ°
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
let bestScore = -Infinity;
|
| 469 |
let bestMove = null;
|
| 470 |
|
|
|
|
| 471 |
for(let i = 0; i < this.size; i++) {
|
| 472 |
for(let j = 0; j < this.size; j++) {
|
| 473 |
if(this.isValidMove(i, j)) {
|
| 474 |
+
// κ°μμΌλ‘ λμ λμλ³΄κ³ μ μ κ³μ°
|
| 475 |
this.board[i][j] = 'white';
|
| 476 |
+
const score = this.calculateMoveScore(i, j, boardScore);
|
| 477 |
this.board[i][j] = null;
|
| 478 |
|
| 479 |
if(score > bestScore) {
|
|
|
|
| 484 |
}
|
| 485 |
}
|
| 486 |
|
| 487 |
+
if(bestMove) {
|
| 488 |
+
this.placeStone(bestMove[0], bestMove[1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
} else {
|
| 490 |
+
this.pass();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
}
|
| 492 |
}
|
| 493 |
|
| 494 |
evaluateBoard() {
|
| 495 |
+
const evaluation = {
|
| 496 |
+
territory: this.evaluateTerritory(),
|
| 497 |
+
influence: this.evaluateInfluence(),
|
| 498 |
+
connections: this.evaluateConnections(),
|
| 499 |
+
captures: this.captures
|
| 500 |
+
};
|
| 501 |
+
return evaluation;
|
| 502 |
}
|
| 503 |
|
| 504 |
+
calculateMoveScore(row, col, boardScore) {
|
| 505 |
let score = 0;
|
|
|
|
| 506 |
|
| 507 |
+
// 1. μν μ μ
|
| 508 |
+
score += this.calculateTerritoryScore(row, col) * 2;
|
| 509 |
+
|
| 510 |
+
// 2. μ°κ²°μ± μ μ
|
| 511 |
+
score += this.calculateConnectionScore(row, col) * 1.5;
|
| 512 |
+
|
| 513 |
+
// 3. 곡격/λ°©μ΄ μ μ
|
| 514 |
+
score += this.calculateTacticalScore(row, col);
|
| 515 |
+
|
| 516 |
+
// 4. μν ννΌ μ μ
|
| 517 |
+
score += this.calculateSafetyScore(row, col);
|
| 518 |
+
|
| 519 |
+
// 5. μ λ΅μ μμΉ μ μ
|
| 520 |
+
score += this.calculateStrategicScore(row, col);
|
| 521 |
|
| 522 |
return score;
|
| 523 |
}
|
| 524 |
|
| 525 |
+
calculateTerritoryScore(row, col) {
|
| 526 |
+
let score = 0;
|
| 527 |
+
const radius = 3;
|
|
|
|
| 528 |
|
| 529 |
+
for(let i = Math.max(0, row-radius); i <= Math.min(this.size-1, row+radius); i++) {
|
| 530 |
+
for(let j = Math.max(0, col-radius); j <= Math.min(this.size-1, col+radius); j++) {
|
| 531 |
+
const distance = Math.abs(row-i) + Math.abs(col-j);
|
| 532 |
+
if(distance <= radius) {
|
| 533 |
+
if(this.board[i][j] === null) score += (radius - distance + 1);
|
| 534 |
+
else if(this.board[i][j] === 'white') score += 2;
|
| 535 |
+
else score -= 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
}
|
| 537 |
}
|
| 538 |
}
|
| 539 |
+
return score;
|
|
|
|
|
|
|
| 540 |
}
|
| 541 |
|
| 542 |
+
calculateConnectionScore(row, col) {
|
| 543 |
+
let score = 0;
|
| 544 |
+
const neighbors = this.getNeighbors(row, col);
|
| 545 |
|
| 546 |
+
for(const [nRow, nCol] of neighbors) {
|
| 547 |
+
if(this.board[nRow][nCol] === 'white') {
|
| 548 |
+
score += 10;
|
| 549 |
+
// λ λμ΄ μ°κ²°λλ©΄ λ λμ μ μ
|
| 550 |
+
if(this.areConnected(row, col, nRow, nCol)) score += 5;
|
| 551 |
+
}
|
| 552 |
}
|
| 553 |
+
return score;
|
|
|
|
|
|
|
|
|
|
| 554 |
}
|
| 555 |
|
| 556 |
+
calculateTacticalScore(row, col) {
|
| 557 |
let score = 0;
|
| 558 |
+
const neighbors = this.getNeighbors(row, col);
|
| 559 |
|
| 560 |
+
// μλ λ 곡격 κΈ°ν νμΈ
|
| 561 |
+
for(const [nRow, nCol] of neighbors) {
|
| 562 |
+
if(this.board[nRow][nCol] === 'black') {
|
| 563 |
+
const group = this.getGroup(nRow, nCol);
|
| 564 |
+
const liberties = this.countLiberties(group);
|
| 565 |
+
if(liberties <= 2) score += (20 - liberties * 5);
|
|
|
|
| 566 |
}
|
| 567 |
}
|
| 568 |
|
| 569 |
return score;
|
| 570 |
}
|
| 571 |
|
| 572 |
+
calculateSafetyScore(row, col) {
|
| 573 |
+
let score = 0;
|
| 574 |
+
const group = [[row, col]];
|
| 575 |
+
const liberties = this.countLiberties(group);
|
| 576 |
|
| 577 |
+
score += liberties * 5;
|
| 578 |
+
|
| 579 |
+
// λ νμ± κ°λ₯μ± μ²΄ν¬
|
| 580 |
+
if(this.canFormEye(row, col)) score += 15;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
|
| 582 |
+
return score;
|
| 583 |
}
|
| 584 |
|
| 585 |
+
calculateStrategicScore(row, col) {
|
| 586 |
let score = 0;
|
| 587 |
|
| 588 |
+
// μ€μ μ§μ μ νΈ
|
| 589 |
+
const centerDistance = Math.abs(row - 9) + Math.abs(col - 9);
|
| 590 |
+
score += Math.max(0, 10 - centerDistance);
|
| 591 |
+
|
| 592 |
+
// λ³μ λΆμ§ μλλ‘
|
| 593 |
+
if(row === 0 || row === 18 || col === 0 || col === 18) score -= 5;
|
| 594 |
+
|
| 595 |
+
// μ€νν¬μΈνΈ κ·Όμ² μ νΈ
|
| 596 |
+
if(this.isNearStarPoint(row, col)) score += 3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
|
| 598 |
return score;
|
| 599 |
}
|
|
|
|
| 613 |
return liberties.size;
|
| 614 |
}
|
| 615 |
|
| 616 |
+
canFormEye(row, col) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
const neighbors = this.getNeighbors(row, col);
|
| 618 |
+
let whiteCount = 0;
|
| 619 |
+
let emptyCount = 0;
|
| 620 |
|
| 621 |
for(const [nRow, nCol] of neighbors) {
|
| 622 |
+
if(this.board[nRow][nCol] === 'white') whiteCount++;
|
| 623 |
+
else if(this.board[nRow][nCol] === null) emptyCount++;
|
|
|
|
| 624 |
}
|
| 625 |
|
| 626 |
+
return whiteCount >= 2 && emptyCount >= 1;
|
| 627 |
+
}
|
| 628 |
+
|
| 629 |
+
isNearStarPoint(row, col) {
|
| 630 |
+
const starPoints = [
|
| 631 |
+
[3,3], [3,9], [3,15],
|
| 632 |
+
[9,3], [9,9], [9,15],
|
| 633 |
+
[15,3], [15,9], [15,15]
|
| 634 |
+
];
|
| 635 |
|
| 636 |
+
return starPoints.some(point =>
|
| 637 |
+
Math.abs(point[0] - row) <= 1 &&
|
| 638 |
+
Math.abs(point[1] - col) <= 1
|
| 639 |
+
);
|
| 640 |
}
|
| 641 |
|
| 642 |
+
areConnected(row1, col1, row2, col2) {
|
| 643 |
+
const diagonal = Math.abs(row1 - row2) === 1 && Math.abs(col1 - col2) === 1;
|
| 644 |
+
if(!diagonal) return true;
|
| 645 |
|
| 646 |
+
// λκ°μ λ°©ν₯ μ°κ²° νμΈ
|
| 647 |
+
const mid1 = this.board[row1][col2];
|
| 648 |
+
const mid2 = this.board[row2][col1];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
+
return mid1 === null || mid2 === null;
|
| 651 |
}
|
| 652 |
}
|
| 653 |
|