TGViewer
PyCoders (پایتون) PyCoders (پایتون) @python_rd · 4.1K subscribers
Post #2038 660
🔸 کد بازی دوز یا Tic Tac Toe دو نفره


"""
Tic-Tac-Toe (2-player, console version)
Python 3.x
"""

def print_board(board):
"""Prints the board in a nice 3x3 grid."""
print()
print(f" {board[0]} | {board[1]} | {board[2]} ")
print("---+---+---")
print(f" {board[3]} | {board[4]} | {board[5]} ")
print("---+---+---")
print(f" {board[6]} | {board[7]} | {board[8]} ")
print()

def check_winner(board, player):
"""Returns True if player ('X' or 'O') has won."""
winning_combos = [
(0, 1, 2), (3, 4, 5), (6, 7, 8), # rows
(0, 3, 6), (1, 4, 7), (2, 5, 8), # columns
(0, 4, 8), (2, 4, 6) # diagonals
]
for a, b, c in winning_combos:
if board[a] == board[b] == board[c] == player:
return True
return False

def is_draw(board):
"""Returns True if all cells are filled and no winner."""
return all(cell in ["X", "O"] for cell in board)

def get_valid_move(board):
"""Asks the current player for a valid move (1-9)."""
while True:
move_str = input("شماره خانه (1 تا 9) را وارد کن: ")
if not move_str.isdigit():
print("❌ فقط عدد وارد کن.")
continue

move = int(move_str)
if move < 1 or move > 9:
print("❌ عدد باید بین 1 تا 9 باشد.")
continue

index = move - 1
if board[index] in ["X", "O"]:
print("❌ این خانه قبلاً پر شده. یکی دیگه انتخاب کن.")
continue

return index

def main():
print("🎮 بازی دوز (Tic Tac Toe)")
print("بازیکن 1 = X | بازیکن 2 = O")
print("چینش خانه‌ها روی صفحه به شکل زیر است:")
demo_board = [str(i) for i in range(1, 10)]
print_board(demo_board)

# شروع بازی
board = [str(i) for i in range(1, 10)]
current_player = "X"

while True:
print_board(board)
print(f"نوبت بازیکن {current_player} است.")
move_index = get_valid_move(board)
board[move_index] = current_player

# چک برنده شدن
if check_winner(board, current_player):
print_board(board)
print(f"🏆 بازیکن {current_player} برنده شد!")
break

# چک مساوی شدن
if is_draw(board):
print_board(board)
print("🤝 بازی مساوی شد!")
break

# تعویض بازیکن
current_player = "O" if current_player == "X" else "X"

if __name__ == "__main__":
main()



با دوستاتون بازی کنید و لذت ببرید🫶🏻❤️


🐍 @python_rd
  • 🔥 4
  • 👍 1
More from @python_rd
  1. Oct 17, 2026🔆 یادگیری پایتون با پروژه های ساده و جذاب 🔹 codeclubprojects.org/en-GB/python 🐍 @python…
  2. Oct 16, 2026🔆 یک سایت خوب پر از پادکست‌های مرتبط با زبان پایتون 🔹 talkpython.fm 🐍 @python_rd
  3. Oct 15, 2026🌐 خروجی این کد پایتون چیست؟ x = [1, 2, 3] y = x x += [4, 5] print(x) print(y) 🔹 گزینه‌ها…
  4. Oct 15, 2026🔆 آیا enumerate() بهتر از index-based loop است؟ 🔹 خیلی‌ها هنوز این شکلی می‌نویسند: items…
  5. Oct 15, 2026🔆 یک تله عجیب در پایتون: bool در واقع زیرکلاس int است 🔹 خیلی‌ها می‌دانند True == 1 و Fal…
  6. Oct 13, 2026کوییز پایتون 🔆 خروجی اجرای کد زیر چیست؟ t = (1, 2, [3, 4]) try: t[2] += [5] except TypeEr…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →