This blog explores data science and networking, combining theoretical concepts with practical implementations. Topics include routing protocols, network operations, and data-driven problem solving, presented with clarity and reproducibility in mind.
Sunday, October 20, 2024
Teaching an AI to Play Tic-Tac-Toe Using Reinforcement Learning
Reinforcement Learning for Tic-Tac-Toe Using Q-Learning
Thursday, August 15, 2024
Building a Tic-Tac-Toe Game: Code Structure and AI Strategies
๐ฎ Building a Tic-Tac-Toe Game in Python (With AI)
Let’s walk through how a simple Tic-Tac-Toe game evolves into something smarter—with AI that actually thinks.
๐ Table of Contents
- Game Initialization
- Game Loop
- Human Moves
- AI Strategies
- AI Math Logic
- Code Example
- CLI Output
- Game End Logic
- Key Takeaways
- Related Articles
๐ Game Initialization
The game begins with an empty board:
board = [" " for _ in range(9)]
The user selects player types:
start user medium
๐ Game Loop
The loop controls turn-taking:
- X plays
- O plays
- Repeat until game ends
It ensures fairness and flow.
๐ค Human Moves
The player inputs a position (0–8).
Validation checks:
- Is the index valid?
- Is the cell empty?
๐ค AI Strategies
Easy AI
Random move:
random.choice(empty_cells)
Medium AI
- Try to win
- Block opponent
- Pick center/corner
Hard AI
Uses advanced logic like Minimax.
๐ AI Decision Math (Minimax Simplified)
\[ Score = \max(\text{AI moves}) - \min(\text{Opponent moves}) \]
Explanation:
- AI tries to maximize its score
- Opponent tries to minimize it
Recursive evaluation:
\[ V(s) = \begin{cases} +1 & \text{if AI wins}\\ 0 & \text{draw}\\ -1 & \text{if opponent wins} \end{cases} \]
๐ป Code Example
def is_winner(board, player):
win_combinations = [
[0,1,2],[3,4,5],[6,7,8],
[0,3,6],[1,4,7],[2,5,8],
[0,4,8],[2,4,6]
]
return any(all(board[i]==player for i in combo) for combo in win_combinations)
๐ฅ️ CLI Output
Click to Expand Gameplay
X | O | X --------- O | X | --------- | O | X Result: X wins
๐ Game End Logic
The game ends when:
- A player wins
- All cells are filled (draw)
This is checked after every move.
๐ก Key Takeaways
- Game loop controls flow
- AI difficulty changes behavior
- Math (Minimax) powers smart decisions
- Validation ensures stable gameplay
๐ฏ Final Thoughts
This Tic-Tac-Toe project may look simple, but it introduces powerful ideas—decision-making, AI strategy, and algorithmic thinking.
Once you understand this, you can scale to much more complex games.
Featured Post
How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing
The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...
Popular Posts
-
EIGRP Stub Routing In complex network environments, maintaining stability and efficienc...
-
Modern NTP Practices – Interactive Guide Modern NTP Practices – Interactive Guide Network Time Protocol (NTP)...
-
DeepID-Net and Def-Pooling Layer Explained | Interactive Guide DeepID-Net and Def-Pooling Layer Explaine...
-
GET VPN COOP Explained Simply: Key Server Redundancy Made Easy GET VPN COOP Explained (Simple + Practica...
-
Modern Cisco ASA Troubleshooting (Post-9.7) Modern Cisco ASA Troubleshooting (Post-9.7) With evolving netwo...
-
When Machine Learning Looks Right but Goes Wrong When Machine Learning Looks Right but Goes Wrong Picture a f...
-
Latent Space & Vector Arithmetic Explained | AI Image Transformations Latent Space & Vector Arit...
-
Process Synchronization – Interactive OS Guide Process Synchronization – Interactive Operating Systems Guide In an operati...
-
Event2Mind – Teaching Machines Human Intent and Emotion Event2Mind: Teaching Machines to Understand Human Intent...
-
Linear Regression vs Classification – Interactive Guide Linear Regression vs Classification – Interactive Theory Guide Line...