## đš 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 āĻāĻž āĻāĻŽāύ
| X | O | |
| O | X | |
āĻāĻāύ X-āĻāϰ turn.
### đ¸ Step 4: Generate Possible Moves (Successors)
Possible moves for X:
1. Move A â X plays (1,3)
| X | O | X |
| O | X | |
2. Move B â X plays (3,1)
| X | O | |
| O | X | |
| X |
3. Move C â X plays (3,2)
| X | O | |
| O | X | |
| X |
4. Move D â X plays (3,3)
| X | O | |
| O | X | |
| 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:
| Move | Description | g(n) | h(n) | f(n) | Comment |
| A-(1,3) | X has diagonal 2 chance | 3 | 1 | 4 | 4-good |
| B-(3,1) | X already wins diagonally (X,O,_) â X,X,X | 3 | 0 | 3 | â Best |
| C-(3,2) | no immediate win | 3 | 2 | 5 | average |
| D-(3,3) | win diagonal 1 â X wins | 3 | 0 | 3 | â 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?”*