import os
def clear():
os.system("cls "if os.name =="nt"else"clear")classPlayer:def __init__(self):
self.name =""
self.symbol=""def choose_name(self):whileTrue:
name = input("enter your name (letters only)")if name.isalpha()==True:
self.name=name
breakelse:print("invalid name . please use letters only")def choose_symbol(self):
h=["X","O"]whileTrue:
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)breakelse:print(f"please enter{h}")continueelse:print("please enter one leater only")continueclass menu:def display_main_menu(self):print("welcome to my X-O game!")print("1.Start game")print("2.Quit Game")whileTrue:
choice=input("Enter your choice (1 or 2): ")if choice==1or2:breakelse:print("wrong!! please inter 1 to start game or 2 to Quit game")continuereturn choice
def display_endgame_menu(self):
menu_text="""
Game over!
1. Restart Game
2.Quit Game
Enter your choice (1 or 2):"""whileTrue:
choice=input(menu_text)if choice==1or2:breakelse:print("wrong!! please inter 1 to restart game or 2 to Quit game")return choice
classBoard: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
returnTruereturnFalsedef 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)]classGame:def __init__(self):
self.players=[Player(),Player()]
self.board =Board()
self.menu = menu()
self.current_player_index =0def 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):whileTrue:
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()breakdef 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]]):returnTruereturnFalsedef 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})")whileTrue:try:
cell_choice = int(input("choose a cell (1-9): "))if1<= cell_choice <=9and self.board.update_board(cell_choice, player.symbol):# Valid move, switch turns and clear screen
self.switch_player()
clear()# Call clear() after switching playerbreakelse: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"]whileTrue:
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)breakelse:print(f"please enter{h}")continueelse:print("please enter one leater only")continue
السؤال
Baraa Mohsen Elalfy
لقد كتبت هذه اللعبة لعبة X O
هذا هو الكود
و الخطا في هذا الجزء من الكود
الدالة remov لا تعمل
7 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.