DAY 26 — 60 DAY PYTHON CHALLENGE
💻 Tic Tac Toe
Python में Tic Tac Toe Game — टिक टैक टो
Advertisement
Project kya hai?
Day 26 ka project hai Tic Tac Toe!
Neeche complete code hai — copy karo aur chalao!
Complete Python Code
python — ttt.py
def print_board(board):
print("
1 2 3")
for i, row in enumerate(board):
cells = " | ".join(row)
print(f"{i+1} {cells}")
if i < 2:
print(" ---------")
print()
def check_winner(board, mark):
# Rows
for row in board:
if all(c == mark for c in row): return True
# Columns
for col in range(3):
if all(board[row][col] == mark for row in range(3)): return True
# Diagonals
if all(board[i][i] == mark for i in range(3)): return True
if all(board[i][2-i] == mark for i in range(3)): return True
return False
def is_full(board):
return all(board[r][c] != " " for r in range(3) for c in range(3))
print("=== Tic Tac Toe ===")
players = ["X", "O"]
names = [input("Player 1 naam (X): "), input("Player 2 naam (O): ")]
score = [0, 0]
while True:
board = [[" "]*3 for _ in range(3)]
turn = 0
print_board(board)
while True:
name = names[turn]
mark = players[turn]
while True:
try:
move = input(f"{name} ({mark}) - Row Col (jaise 1 2): ").split()
r, c = int(move[0])-1, int(move[1])-1
if board[r][c] == " ":
board[r][c] = mark
break
else:
print("Yeh jagah bhari hai!")
except:
print("1-3 ke beech row aur col likho!")
print_board(board)
if check_winner(board, mark):
print(f"{name} JIT GAYE!")
score[turn] += 1
break
if is_full(board):
print("DRAW! Koi nahi jeeta.")
break
turn = 1 - turn
print(f"Score: {names[0]}={score[0]}, {names[1]}={score[1]}")
if input("
Phir khelna hai? (y/n): ").lower() != "y":
break
OUTPUT
Player 1: Rahul
Player 2: Priya
1 2 3
1 | |
---------
2 | |
Rahul (X) - Row Col: 2 2
1 2 3
1 | |
2 | X |
Priya (O) - 1 1
O | |
---------
| X |
Rahul JIT GAYE!
Code kaise kaam karta hai?
2D list se board represent hota hai. check_winner() rows, columns, diagonals sab check karta hai. 1-turn se turns switch hote hain.
Advertisement
📋 Project ka Introduction
Tic Tac Toe Python ka Day 26 ka project hai. Is project mein aap Python ke important concepts practice karenge jo real-world applications mein bahut use hote hain.
Yeh project beginners ke liye design kiya gaya hai lekin kaafi concepts cover karta hai. Step by step samjho, code chalao, aur khud modify karke practice karo.
Is tarah ke projects banane se aapka Python confidence badh jaata hai aur aap asli problems solve karna seekh jaate hain. Chaliye code samjhte hain!
Yeh project beginners ke liye design kiya gaya hai lekin kaafi concepts cover karta hai. Step by step samjho, code chalao, aur khud modify karke practice karo.
Is tarah ke projects banane se aapka Python confidence badh jaata hai aur aap asli problems solve karna seekh jaate hain. Chaliye code samjhte hain!
🧠 Is Project mein kya seekhoge?
Yeh project banate waqt aap ye Python concepts use karoge:
| Concept | Kya karta hai |
|---|---|
Variables | Data store karna |
Functions | Reusable code blocks |
Loops | Repeat karna |
Conditions | Decisions lena |
Input/Output | User se interact karna |
📝 Code kaise kaam karta hai — Step by Step
Neeche code ki poori logic step-by-step samjhayi gayi hai:
- Problem samjho: Tic Tac Toe mein kya karna hai
- Required variables aur data structures decide karo
- Logic step-by-step likhó
- Code mein implement karo
- Test karo aur bugs fix karo
⚠️ Common Mistakes — Bhool mat jaana!
Beginners yeh galtiyan aksar karte hain — dhyan rakho:
- ⚠️ Indentation sahi rakho — Python mein spaces matter karti hain
- ⚠️ Variables ko use se pehle define karo
- ⚠️ Input ko int()/float() mein convert karo agar number chahiye
- ⚠️ Edge cases handle karo — kya hoga agar user galat input de?
🏋️ Practice Exercises — Aage badho!
Yeh project complete karne ke baad in exercises se practice karo:
- 💡 Tic Tac Toe mein naya feature add karo
- 💡 Code ko functions mein refactor karo
- 💡 Error handling improve karo
- 💡 File mein data save karo
❓ Aksar Pooche Jane Wale Sawal (FAQ)
Q: Tic Tac Toe project kyon banana chahiye?
A: Har project ek naya concept sikhata hai. Practice se hi Python fluent aati hai.
A: Har project ek naya concept sikhata hai. Practice se hi Python fluent aati hai.
Q: Code run nahi ho raha?
A: Indentation check karo, syntax errors dekho, variables define hain ya nahi check karo.
A: Indentation check karo, syntax errors dekho, variables define hain ya nahi check karo.
Q: Kaise improve karein?
A: Pehle basic version complete karo, phir ek ek feature add karte jao.
A: Pehle basic version complete karo, phir ek ek feature add karte jao.