Kółko i krzyżyk krok 13: komputer nie do pokonania (minimax)

Kółko i krzyżyk krok po kroku - JavaScript na stronach WWW (HTML/CSS/JS)

Reguły da się ograć sprytnym rozwidleniem. Minimax sprawdza wszystkie możliwe dalsze partie: funkcja rekurencyjna próbuje każdy wolny ruch, gra dalej za obu graczy i ocenia koniec (+1 wygrana komputera, -1 przegrana, 0 remis). Komputer wybiera ruch o najlepszej ocenie, zakładając, że gracz zawsze wybierze dla niego najgorszy. Partii jest najwyżej kilkaset tysięcy, więc to trwa ułamek sekundy. Spróbuj wygrać: da się najwyżej zremisować.

<!DOCTYPE html>
<html lang="pl">
<head>
  <meta charset="utf-8">
  <title>Kółko i krzyżyk</title>
  <style>
    body { font-family: sans-serif; max-width: 36em; line-height: 1.4; }
    input, select, button, textarea { font: inherit; padding: 0.2em 0.4em; }
    table { border-collapse: collapse; }
    th, td { border: 1px solid #94a3b8; padding: 0.2em 0.6em; }

    #board { display: grid; grid-template-columns: repeat(3, 60px); gap: 4px; margin: 0.5em 0; }
    #board button { width: 60px; height: 60px; font-size: 1.8em; padding: 0; }
    #board .win { background: #bbf7d0; }
  </style>
</head>
<body>
  <p id="info">Ruch: X</p>
  <div id="board"></div>
  <p><button id="reset">Nowa gra</button></p>
  <p id="score"></p>
  <script>
    const board = document.getElementById('board');
    const info = document.getElementById('info');
    const buttons = [];
    for (let i = 0; i < 9; i++) {
      const button = document.createElement('button');
      button.dataset.index = i;
      board.append(button);
      buttons.push(button);
    }
    // osiem linii: trzy wiersze, trzy kolumny, dwie przekątne
    const LINES = [
      [0, 1, 2], [3, 4, 5], [6, 7, 8],
      [0, 3, 6], [1, 4, 7], [2, 5, 8],
      [0, 4, 8], [2, 4, 6],
    ];

    function winningLine(cells) {
      return LINES.find(([a, b, c]) => cells[a] !== '' && cells[a] === cells[b] && cells[a] === cells[c]) ?? null;
    }
    // model: cały stan gry i jej zasady, bez dostępu do strony
    class Game {
      constructor() {
        this.score = { X: 0, O: 0, draw: 0 };
        this.reset();
      }

      reset() {
        this.cells = Array(9).fill('');
        this.player = 'X';
        this.result = null; // 'X', 'O', 'draw' albo null, gdy gra trwa
      }

      get line() {
        return winningLine(this.cells);
      }

      get status() {
        if (this.result === 'draw') return 'Remis';
        if (this.result) return `Wygrywa ${this.result}`;
        return `Ruch: ${this.player}`;
      }

      play(index) {
        if (this.result || this.cells[index] !== '') return false;
        this.cells[index] = this.player;
        if (this.line) this.result = this.player;
        else if (this.cells.every(c => c !== '')) this.result = 'draw';
        else this.player = this.player === 'X' ? 'O' : 'X';
        if (this.result) this.score[this.result]++;
        return true;
      }
    }
    const game = new Game();

    // widok: rysuje model, nic nie liczy
    function render() {
      const line = game.line;
      buttons.forEach((button, i) => {
        button.textContent = game.cells[i];
        button.classList.toggle('win', line !== null && line.includes(i));
      });
      info.textContent = game.status;
      document.getElementById('score').textContent = `X: ${game.score.X}, O: ${game.score.O}, remisy: ${game.score.draw}`;
    }
    // ocena pozycji z punktu widzenia komputera (O): +1 wygrana, -1 przegrana, 0 remis
    function minimax(cells, turn) {
      const line = winningLine(cells);
      if (line) return cells[line[0]] === 'O' ? 1 : -1;
      if (cells.every(c => c !== '')) return 0;
      const scores = [];
      for (let i = 0; i < 9; i++) {
        if (cells[i] !== '') continue;
        cells[i] = turn;
        scores.push(minimax(cells, turn === 'O' ? 'X' : 'O'));
        cells[i] = '';
      }
      return turn === 'O' ? Math.max(...scores) : Math.min(...scores);
    }

    function computerMove(cells) {
      let best = -2;
      let move = -1;
      for (let i = 0; i < 9; i++) {
        if (cells[i] !== '') continue;
        cells[i] = 'O';
        const value = minimax(cells, 'X');
        cells[i] = '';
        if (value > best) {
          best = value;
          move = i;
        }
      }
      return move;
    }

    board.addEventListener('click', e => {
      const button = e.target.closest('button');
      if (!button || !game.play(Number(button.dataset.index))) return;
      if (!game.result) game.play(computerMove(game.cells));
      render();
    });

    document.getElementById('reset').addEventListener('click', () => {
      game.reset();
      render();
    });
    render();
  </script>
</body>
</html>

Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.

Wszystkie lekcje kursu JavaScript na stronach WWW·Zaloguj się, aby zapisać postęp na koncie.