موضوعات كثيرة على الانترنت تتحدث عم هذه اللعبة لكني لم أجد أحد فعلها بطريقة الClasses
في لغة python
هذه النتيجة حتى الآن في الـ Terminal:
C:\Users\Nasr\Desktop>py starterGame.py
Game start!
------- Round 0 ------- :
Player 1: paper Player 2: rock
------- Round 1 ------- :
Player 1: scissors Player 2: scissors
------- Round 2 ------- :
Player 1: scissors Player 2: rock
****** Game over! ******
يبدو الموضوع جميل… لكن ليس هناك score و من الفائز في اللعبة !
أيضا سأضيف أشياء أخرى مثل لاعب بشري يستطيع أن يتفاعل مع الكمبيوتر
في الكود التالي عرفت كل الـ Classes
وعملت comment لمحاولتي في اظهار الفائز ومرفق ايضا نوع الerror
#!/usr/bin/env python3
"""This program plays a game of Rock, Paper, Scissors between two Players,
and reports both Player's scores each round."""
moves = ['rock', 'paper', 'scissors']
"""The Player class is the parent class for all of the Players
in this game"""
import random
# Player
class Player:
def move(self):
return 'rock'
def learn(self, my_move, their_move):
pass
# specifies winning rules
def beats(one, two):
return ((one == 'rock' and two == 'scissors') or
(one == 'scissors' and two == 'paper') or
(one == 'paper' and two == 'rock'))
# subclass
class RandomPlayer(Player):
def move(self):
for move in moves:
move = random.choice(moves)
return move
# Game
class Game:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
self.p1_score = 0
self.p2_score = 0
def play_round(self):
move1 = self.p1.move()
move2 = self.p2.move()
print(f"Player 1: {move1} Player 2: {move2}\n")
self.p1.learn(move1, move2)
self.p2.learn(move2, move1)
# هنا المشكلة >> ARRTIBUTE ERROR >> object has
# no attribute BEATS
# if beats(move1, move2) == True:
# print("Player 1 wins")
# elif beats(move2, move1) == True:
# print("Player 2 wins")
#else:
# print("Tie round!")
def play_game(self):
print("\nGame start! \n")
for round in range(3):
print(f"\n------- Round {round} ------- : \n")
self.play_round()
print("\n****** Game over! ******")
if __name__ == '__main__':
game = Game(RandomPlayer(), RandomPlayer())
game.play_game()