Tutorialspoint
Problem
Solution
Submissions

Min-Max Algorithm for Game Playing

Certification: Advanced Level Accuracy: 50% Submissions: 2 Points: 15

Implement the Min-Max algorithm with Alpha-Beta pruning for a Tic-Tac-Toe game. It should choose the best move by simulating all future outcomes.

Example 1
  • Input:
    board = [['X', 'O', ' '], [' ', 'X', ' '], [' ', ' ', ' ']]
    player = 'O'
  • Output:
    (2, 2)
  • Explanation:
    • 'O' blocks winning diagonal for 'X' by choosing (2, 2).
Example 2
  • Input:
    board = [['X', ' ', ' '], [' ', 'O', ' '], [' ', ' ', 'X']]
  • Output:
    (0, 1)
  • Explanation:
    • 'O' creates two winning paths by selecting (0, 1).
Constraints
  • Board: 3x3
  • Player is 'X' or 'O'
  • Empty cell = ' '
  • Time Complexity: O(b^d)
  • Space Complexity: O(d)
ArraysFunctions / MethodsHCL TechnologiesZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Implement a function to evaluate the board from a player's perspective
  • Use recursion to explore all possible moves to a certain depth
  • Apply alpha-beta pruning to optimize the search
  • Maximize score for the player, minimize for the opponent
  • Handle base cases like win, loss, and draws

Steps to solve by this approach:

 Step 1: Create a function to evaluate the current state of the board (win, loss, draw, or ongoing).
 Step 2: Implement the minimax algorithm with alpha-beta pruning to efficiently search the game tree.
 Step 3: For each empty cell, simulate placing the player's mark and evaluate the resulting position.
 Step 4: Recursively alternate between maximizing player's score and minimizing opponent's score.
 Step 5: Return the move with the highest evaluated score for the current player.

Submitted Code :