لقد كتبت هذه اللعبة لعبة X O
import os
def clear():
os.system("cls "if os.name == "nt" else "clear")
class Player:
def __init__(self):
self.name = ""
self.symbol=""
def choose_name(self):
while True :
name = input("enter your name (letters only)")
if name.isalpha()==True:
self.name=name
break
else:
print("invalid name . please use letters only")
def choose_symbol(self):
h=["X","O"]
while True:
symbol=input(f"{self.name},choose your symbol ({"/".join(h)})")
if symbol.isalpha() and len (symbol)==1:
if symbol in h:
self.symbol=symbol
h.remove(symbol)
break
else:
print(f"please enter{h}")
continue
else:
print("please enter one leater only")
continue
class menu:
def display_main_menu(self):
print("welcome to my X-O game!")
print("1.Start game")
print("2.Quit Game")
while True:
choice=input("Enter your choice (1 or 2): ")
if choice==1 or 2:
break
else:
print("wrong!! please inter 1 to start game or 2 to Quit game")
continue
return choice
def display_endgame_menu(self):
menu_text="""
Game over!
1. Restart Game
2.Quit Game
Enter your choice (1 or 2):"""
while True:
choice=input(menu_text)
if choice==1 or 2:
break
else:
print("wrong!! please inter 1 to restart game or 2 to Quit game")
return choice
class Board:
def __init__(self) :
self.board=[str(i)for i in range(1,10)]
def display_board(self):
for i in range(0,9,3):
print("|".join(self.board[i:i+3]))
if i <6:
print("-"*5)
def update_board(self, choice, symbol):
if self.is_valid_move(choice):
self.board[choice-1]=symbol
return True
return False
def is_valid_move(self,choice):
return self.board[choice-1].isdigit()
def reset_baord(self):
self.board=[str(i)for i in range(1,10)]
class Game:
def __init__(self):
self.players= [Player(),Player()]
self.board = Board()
self.menu = menu()
self.current_player_index = 0
def start_game(self):
choise = self.menu.display_main_menu()
if choise=="1":
self.setup_players()
self.play_game()
else:
self.quit_game()
def setup_players(self):
for number ,player in enumerate( self.players,start=1):
print(f"Player{number},enter your details:")
player.choose_name()
player.choose_symbol()
print("-"*20)
clear()
def play_game(self):
while True:
self.play_turn()
if self.check_win() or self.check_drow():
self.who_win()
choice = self.menu.display_endgame_menu()
if choice =="1":
self.restart_game()
else:
self.quit_game()
break
def restart_game(self):
self.board.reset_baord()
self.current_player_index = 0
self.play_game()
def check_win(self):
win_combinstions=[
[0,1,2],[3,4,5],[6,7,8],
[0,3,6],[1,4,7],[2,5,8],
[0,4,8],[2,4,6]
]
for combo in win_combinstions:
if (self.board.board[combo[0]] == self.board.board[combo[1]]==self.board.board[combo[2]]):
return True
return False
def check_drow(self):
return all(not cell.isdigit() for cell in self.board.board)
def play_turn(self):
player = self.players[self.current_player_index]
self.board.display_board()
print(f"{player.name}'s turn ({player.symbol})")
while True:
try:
cell_choice = int(input("choose a cell (1-9): "))
if 1 <= cell_choice <= 9 and self.board.update_board(cell_choice, player.symbol):
# Valid move, switch turns and clear screen
self.switch_player()
clear() # Call clear() after switching player
break
else:
print("invalid move , try again.")
except:
print("please enter a number between 1 and 9.")
def who_win(self):
H=self.players[self.current_player_index]
if self.check_win:
print(f"{H.name} is win")
def switch_player(self):
self.current_player_index= 1 - self.current_player_index
clear()
def quit_game(self):
print("thank you for playing!")
game = Game()
game.start_game()
هذا هو الكود
و الخطا في هذا الجزء من الكود
def choose_symbol(self):
h=["X","O"]
while True:
symbol=input(f"{self.name},choose your symbol ({"/".join(h)})")
if symbol.isalpha() and len (symbol)==1:
if symbol in h:
self.symbol=symbol
h.remove(symbol)
break
else:
print(f"please enter{h}")
continue
else:
print("please enter one leater only")
continue
الدالة remov لا تعمل