A* Search

## 🔹 What is A* Search?

**A*** (āωāĻšā§āϚāĻžāϰāĻŖāσ “A star”) āĻšāϞ⧋ āĻāĻ•āϟāĻž **informed search algorithm**,

āϝāĻž āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĻĻ⧁āχāϟāĻž important function:

[

f(n) = g(n) + h(n)

]

👉 āĻāĻ–āĻžāύ⧇,

* `g(n)` = cost so far (start āĻĨ⧇āϕ⧇ current state āĻĒāĻ°ā§āϝāĻ¨ā§āϤ cost)

* `h(n)` = heuristic estimate (goal āĻ āĻĒ⧌āρāĻ›āĻžāϤ⧇ āĻŦāĻžāĻ•āĻŋ distance āĻŦāĻž effort āĻāϰ āĻ…āύ⧁āĻŽāĻžāύ)

A* always selects the node āϝāĻžāϰ **f(n)** value āϏāĻŦāĻšā§‡āϝāĻŧ⧇ āĻ•āĻŽ —

āĻŽāĻžāύ⧇ āϝ⧇āϟāĻž āϏāĻŦāĻšā§‡āϝāĻŧ⧇ promising path āĻŽāύ⧇ āĻšāϝāĻŧāĨ¤

## đŸŽ¯ Applying A* Search in Tic Tac Toe

Tic Tac Toe-āϤ⧇ āφāĻŽāϰāĻž use āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ A* logic to find **best move** for player X.

āĻĒā§āϰāϤāĻŋāϟāĻž move āĻāĻ•āϟāĻž **state** āĻāĻŦāĻ‚ āĻĒā§āϰāϤāĻŋāϟāĻž move-āĻāϰ āϏāĻžāĻĨ⧇ āĻāĻ•āϟāĻž **cost (g)** āφāϰ āĻāĻ•āϟāĻž **heuristic (h)** āĻĨāĻžāĻ•āĻŦ⧇āĨ¤

### 🔸 Step 1: Define the Problem

We want to find **X-āĻāϰ winning path** — āĻ…āĻ°ā§āĻĨāĻžā§Ž āĻāĻŽāύ move sequence āϝāĻžāϤ⧇ X āĻœā§‡āϤ⧇āĨ¤

### 🔸 Step 2: Define f(n), g(n), h(n)

* **g(n)** → āϝāϤ move āύ⧇āϝāĻŧāĻž āĻšāϝāĻŧ⧇āϛ⧇ (depth of the node)

* **h(n)** → āĻ•āϤāϟāĻž āĻ•āĻžāϛ⧇ āφāĻŽāϰāĻž āĻœā§‡āϤāĻžāϰ (winning) āĻ…āĻŦāĻ¸ā§āĻĨāĻžāϝāĻŧ āφāĻ›āĻŋ, āϤāĻžāϰ estimate

  āϝ⧇āĻŽāύ:

  * āϝāĻĻāĻŋ X already āϜāĻŋāϤ⧇ āϝāĻžāϝāĻŧ → `h = 0`

  * āϝāĻĻāĻŋ X-āĻāϰ āĻĻ⧁āχāϟāĻž mark āĻāĻ•āϏāĻžāĻĨ⧇ āφāϛ⧇ āĻ•āĻŋāĻ¨ā§āϤ⧁ āϤ⧃āϤ⧀āϝāĻŧāϟāĻž āĻĢāĻžāρāĻ•āĻž → `h = 1`

  * āϝāĻĻāĻŋ X-āĻāϰ chances āĻ•āĻŽ → `h = 2` āĻŦāĻž āϤāĻžāϰ āĻŦ⧇āĻļāĻŋ

āϤāĻžāĻšāϞ⧇ āφāĻŽāϰāĻž āϚāĻžāχ **minimum f(n)** → āĻ…āĻ°ā§āĻĨāĻžā§Ž **āĻ•āĻŽ move (g)** + **āĻ•āĻŽ remaining effort (h)**āĨ¤

### 🔸 Step 3: Example Board

āϧāϰāĻž āϝāĻžāĻ• game state āϟāĻž āĻāĻŽāύ

XO 
OX 
   

āĻāĻ–āύ X-āĻāϰ turn.

### 🔸 Step 4: Generate Possible Moves (Successors)

Possible moves for X:

1. Move A → X plays (1,3)

XOX
OX 
   

2. Move B → X plays (3,1)

XO 
OX 
X  

3. Move C → X plays (3,2)

XO 
OX 
 X 

4. Move D → X plays (3,3)

XO 
OX 
  X

### 🔸 Step 5: Evaluate Each State with f(n) = g(n) + h(n)

āϧāϰāĻŋ āφāĻŽāϰāĻž āĻāĻ–āύ 3rd move-āĻ āφāĻ›āĻŋ → so `g(n) = 3` (3 move done by X so far)

Now estimate **h(n)** for each move:

MoveDescriptiong(n)h(n)f(n)Comment
A-(1,3) X has diagonal 2 chance 3144-good
B-(3,1) X already wins diagonally (X,O,_) → X,X,X303✅ Best
C-(3,2)no immediate win 325average
D-(3,3)win diagonal 1 → X wins303✅ Best

So minimum **f(n) = 3** (Move B or D).

AI chooses one of them → **Winning move**!

### 🔸 Step 6: How A* Chooses Path

A* keeps an **Open List** (nodes yet to explore) and a **Closed List** (already explored).

Each step it:

1. Picks node with **lowest f(n)** from Open List

2. Expands its children

3. Updates Open and Closed Lists accordingly

In Tic Tac Toe, this means AI tests all possible moves, picks the one with lowest cost to win.

### 🔸 Step 7: Algorithm (Simplified)

“`python

def a_star_tictactoe(board, player):

    open_list = [board]

    g = {str(board): 0}  # move cost so far

    h = {str(board): heuristic(board, player)}  # estimated remaining

    f = {str(board): g[str(board)] + h[str(board)]}

    while open_list:

        current = min(open_list, key=lambda b: f[str(b)])

        if is_goal(current, player):

            return current  # winning board found

        open_list.remove(current)

        for move in possible_moves(current, player):

            new_board = apply_move(current, move, player)

            g[str(new_board)] = g[str(current)] + 1

            h[str(new_board)] = heuristic(new_board, player)

            f[str(new_board)] = g[str(new_board)] + h[str(new_board)]

            open_list.append(new_board)

    return None

“`

💡 āĻāχ algorithm-āϟāĻž game tree explore āĻ•āϰ⧇

āĻāĻŦāĻ‚ āϏāĻŦāϏāĻŽāϝāĻŧ **minimum f(n)** value-āĻāϰ node āĻŦ⧇āϛ⧇ āύ⧇āϝāĻŧ —

āĻ…āĻ°ā§āĻĨāĻžā§Ž **āϏāĻŦāĻšā§‡āϝāĻŧ⧇ āĻ•āĻžāϛ⧇āϰ winning path**āĨ¤

## ✅ Final Summary

So, in Tic Tac Toe,

A* search evaluates every possible move with **f(n) = g + h**,

and chooses the move that brings **fastest win** (minimum total cost).

👉 āϏāĻšāϜāĻ­āĻžāĻŦ⧇ āĻŦāϞāϞ⧇ —

**A*** āĻšāϞ⧋ āĻāĻŽāύ āĻāĻ•āϟāĻž smart system,

āϝāĻž āĻ­āĻžāĻŦ⧇ — *”Which move will take me closer to winning, in the fewest steps?”*