Skip to content

GabrielFMPinheiro/Tic-Tac-Toe-With-Minmax-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic Tac Toe using minmax algorithm (AI)

Table of contents

Overview

The challenge

Users should be able to:

  • Play tic tac toe against computer or with other human player

Screenshot

Links

My process

Built with

  • Semantic HTML5 markup
  • CSS custom properties
  • Flexbox
  • React
  • Testing Library

What I learned

/* minmax algorithm */
  minmax(board, isMaximizing, player) {
    let result = this.winner(board);

    if (result !== null) {
      return scores[result];
    }

    if (isMaximizing) {
      let bestScore = -Infinity;
      board.forEach((cell, i) => {
        if (cell === '') {
          board[i] = 1;
          let score = this.minmax(board, false, player);
          board[i] = '';
          bestScore = Math.max(score, bestScore);
        }
      });
      return bestScore;
    } else {
      let bestScore = Infinity;
      board.forEach((cell, i) => {
        if (cell === '') {
          board[i] = this.changePlayers();
          let score = this.minmax(board, true, this.changePlayers());
          board[i] = '';
          bestScore = Math.min(score, bestScore);
        }
      });
      return bestScore;
    }
  }

Useful resources

Author