简体   繁体   中英

The code to update the score doen't work properly

My code works fine and I can even play the game; but, the score in the game doesn't update, it seems that I have an issue with the beat function, which I cannot figure out..

See the entire code below

"""
Created on Mon Dec 17 17:33:01 2018
@author: Jennie
"""

moves = ['rock', 'paper', 'scissors']

import random

#Create player class
class Player:
    def move(self):
        return 'rock'

    def learn(self, my_move, their_move):
        pass


#Create random player class
class RandomPlayer:
    def __init__(self):
        Player.__init__(self)

    def move(self):

        #use imported random function & choice
        choices = ['Rock', 'Paper', 'Scissors']
        random_player = random.choice(choices)

        #Computer choice is either rock, paper, or scissors 
        if random_player == ("Rock"): 
            print("Opponent played Rock")

        elif random_player == ("Paper"): 
            print("Opponent played Paper")

        else: 
            print("Opponent played Scissors") 

        #return value 
        return random_player  


#Create human player class        
class HumanPlayer:
    def __init__(self):
        Player.__init__(self)

    def move(self):
        while True:
            human_player = input("'Rock', 'Paper', or 'Scissors' ")
        #Detect invalid entry
            if human_player.lower() not in moves:
                print('Please choose Paper, Rock or Scissors: ')
            else:
                break

        return human_player



##class that remembers what move the opponent played last round
class ReflectPlayer:
    def __init__(self, ReflectPlayer):
        Player.__init__(self)
        self.ReflectPlayer = ReflectPlayer

    # def move 
    def move(self, move):
        self.move = move

    def getmove(self, move):
        return self.move


#define cycleplayer class that remembers what move it played last round,
# and cycles through the different moves. 
class CyclePlayer:
    def __init__(self, CyclePlayer):
        Player.__init__(self)
        self.CyclePlayer = CyclePlayer

        self.human_player_history = {}  # stores the frequency of human player moves
        for move in moves:
            self.human_player_history[move] = 0


    def move(self, max_move):
        max_move = max(self.human_player_history.items(), key=lambda elem: elem[1])[0]
        if max_move == 'rock':
            return 'paper'
        if max_move == 'scissors':
            return 'rock'
        if max_move == 'paper':
            return 'rock' 


def beats(move1, move2):

    if ((move1 == 'rock' and move2 == 'rock') or

         (move1 == 'paper' and move2 == 'paper') or

         (move1 == 'scissors' and move2 == 'scissors')):

        return "** It's a TIE **"


    elif ((move1 == 'rock' and move2 == 'scissors') or

          (move1 == 'scissors' and move2 == 'paper') or

          (move1 == 'paper' and move2 == 'rock')):

        return "** Human WINS **"

    else:

        return "** Random Player WINS **"



#Create game class
class Game:
    def __init__(self, human_player, random_player):
        self.player1 = human_player
        self.player2 = random_player
        self.player1_score = 0
        self.player2_score = 0


    def play_round(self):            
        move1 = self.player1.move()
        move2 = self.player2.move()
        print(f"Player 1: {move1}  Player 2: {move2}")

        if (move1 == move2):
            print("it's a tie!")

        elif beats(move1, move2) is True:
                self.player1_score += 1

        elif beats(move2, move1) is True:
                self.player2_score += 1

        print(f"Scores, HumanPlayer: {self.player1_score} RandomPlayer: {self.player2_score}")


    def play_game(self):    
        print("Game start!")
        for round in range(4):
            print(f"Round {round}:")
            self.play_round()
        print("Game over!")


if __name__ == '__main__':
    game = Game(HumanPlayer(), RandomPlayer())
    game.play_game()

Note that the beats(move1, move2) function returns a string always, not a bool. Also, in Game.play_round you can omit the is True bits. Just if <condition> works.

I would introduce a some states (Tie, Human Won, Bot(?) Won) into your beats() function:

RESULT_HUMAN_WIN = 0
RESULT_RANDOM_WIN = 1
RESULT_TIE = 2

def beats(move1, move2):
    if move1 == move2:
        return RESULT_TIE

    elif ((move1 == 'rock' and move2 == 'scissors') or
          (move1 == 'scissors' and move2 == 'paper') or
          (move1 == 'paper' and move2 == 'rock')):
        return RESULT_HUMAN_WIN
    else:
        return RESULT_RANDOM_WIN

While also changing your comparator over at your play_round function to use those new states to determine where to place the point:

match_result = beats(move1, move2)

if match_result == RESULT_TIE:
    print("it's a tie!")
elif match_result == RESULT_HUMAN_WIN:
        self.player1_score += 1
elif match_result ==  RESULT_RANDOM_WIN:
        self.player2_score += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM