私はPythonでじゃんけんを開発していますが、行き詰まっています。じゃんけんを繰り返すクラスを作ったので、コンピューターに前の遊びを知らせてもらいたい。たとえば(最初のラウンドのコンピューターはロックを再生し、次のラウンドでは紙を再生する必要があります)
が、学習関数を呼び出して機能させる方法がわかりません
class Player:
def __init__(self):
self.score = 0
def move(self):
return 'rock'
def learn(self, my_move, their_move):
self.my_move = my_move
self.their_move = their_move
def beats(one, two):
return ((one == 'rock' and two == 'scissors') or
(one == 'scissors' and two == 'paper') or
(one == 'paper' and two == 'rock'))
class Game:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def play_round(self):
move1 = input("Pick something!\n")
move2 = self.p2.move()
print(f"Player 1: {move1} Player 2: {move2}")
self.p1.learn(move1, move2)
self.p2.learn(move2, move1)
if beats(move1, move2):
self.p1.score += 1
print ("You win")
print ("Human score = " + str(self.p1.score) + " " + "Computer score = " + str(self.p2.score) )
elif beats(move2,move1):
self.p2.score += 1
print ("Computer wins")
print ("Human score = " + str(self.p1.score) + " " + "Computer score = " + str(self.p2.score) )
else:
print ("Draw")
print ("Human score = " + str(self.p1.score) + " " + "Computer score = " + str(self.p2.score) )
def play_game(self):
print("Game start!")
for round in range(3):
print(f"Round {round}:")
self.play_round()
print("Game over!")
class human_player(Player):
def move(self):
return input("Pick something!\n")
class randomplayer(Player):
def move(self):
return random.choice(moves)
class repeat(Player):
def move(self):
return 'rock'
class cycleplayer(Player):
def move(self):
# calling my_move from the Player class
if self.learn.my_move == "rock" or "paper" or "scissors" :
return 'rock'
elif self.their_move == 'rock':
return "paper"
elif self.their_move == 'paper':
return "scissors"
elif self.their_move == 'scissors':
return "rock"
if HumanPlayer_choice == "cycle" :
game = Game(Player(), cycleplayer())
game.play_game()
これは私が得ているエラーです。
例外が発生しました:AttributeError'function 'オブジェクトに属性' my_move 'がありません
init関数をlearn関数と一緒に使用して機能させる必要があることは知っていますが、その方法がわかりません。
私が追跡できる限り、あなたのコードにはいくつかの問題がありました。コードに基づいて、以下の例を見てください。私が従うことができなかったいくつかの論理があるので、それはあなたが望むように正確に振る舞わないかもしれません。提案:
一部のメソッドは、各インスタンスメソッドで再作成されるよりも静的として優れています
import random
class Player:
def __init__(self):
self.score = 0
class Game:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
@staticmethod
def check_result(a, b): #Return win, draw, loose for a over b
if (a == b):
return 'draw'
if (a == 'rock' and b == 'scissors') or (a == 'scissors' and b == 'paper') or (a == 'paper' and b == 'rock'):
return 'win'
return 'loose'
def play_round(self):
#move1 = input("Pick something!\n")
move1 = self.p1.move()
move2 = self.p2.move()
print(f"Player 1: {move1} Player 2: {move2}")
result = Game.check_result(move1, move2) # a over b
if result == 'win':
self.p1.score += 1
print ("You win")
elif result == 'loose':
self.p2.score += 1
print ("Computer wins")
else:
print ("Draw")
print ("Human score = " + str(self.p1.score) + " " + "Computer score = " + str(self.p2.score))
def play_game(self):
print("Game start!")
for round in range(3):
print(f"Round {round + 1}:")
self.play_round()
print("Game over!")
class human_player(Player):
def move(self):
return input("Pick something!\n") #TODO validate input
class random_player(Player): #Option in game for computer to make random moves
def move(self):
return random.choice(["scissors","paper","rock"])
class repeat_player(Player): #Option
def move(self):
return 'rock'
class cycle_player(Player):
def __init__(self):
self.previous_move = None
super().__init__()
def move(self):
# calling my_move from the Player class
if self.previous_move is None :
next_move = 'rock'
elif self.previous_move == 'rock':
next_move = "paper"
elif self.previous_move == 'paper':
next_move = "scissors"
elif self.previous_move == 'scissors':
next_move = "rock"
self.previous_move = next_move
return next_move
#game = Game(human_player(), random_player())
#game = Game(human_player(), repeat_player())
game = Game(human_player(), cycle_player())
game.play_game()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加