-5

It seems that I have everything working fine. I just don't know why every time I play, the computer's choice isn't what I wanted. The computer should choose something from the moves list, bust instead is returning numbers.

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

I suspect the problem might be in RandomPlayer

class RandomPlayer(Player):
    def move(self):
        index = random.randint(0, 2)  # Selects random moves from the move list
        return (index)


class Game():
    def __init__(self, p2):
        self.p1 = HumanPlayer()
        self.p2 = p2

    def play_game(self):
        print("\nLet's play Rock, Paper, Scissors!")
        for round in range(1, 4):
            print(f"\nRound {round}:")
            self.play_round()
        if self.p1.score > self.p2.score:
            print('Player 1 won!')
            # print(f"The score is {self.p1.score} to {self.p2.score}")
        elif self.p1.score < self.p2.score:
            print('Player 2 won!')
            # print(f"The score is {self.p1.score} to {self.p2.score}")
        else:
            print('The game was a tie!')
        print(f"Final score is {self.p1.score} to {self.p2.score}")

    # plays a single round if user chose to
    def play_single(self):
        print("Rock Paper Scissors, Go!")
        print(f"Round 1 of 1:")
        self.play_round()
        if self.p1.score > self.p2.score:
            print('Player 1 won!')
        elif self.p1.score < self.p2.score:
            print('Player 2 won!')
        else:
            print('The game was a tie!')
        print(f"Final score is {self.p1.score} to {self.p2.score}")

    def play_round(self):
        move1 = self.p1.move()
        move2 = self.p2.move()
        result = Game.play(move1, move2)
        self.p1.learn(move2)  # stores opponent move
        self.p2.learn(move1)  # stores opponent move

This is the input i'm giving and the output it gives me

Round 1:
Rock, Paper, Scissors?  rock
You played rock and opponent played 2
[ It's A TIE ]
[The score is 0 to 0]


Round 2:
Rock, Paper, Scissors?  paper
You played paper and opponent played 1
[ It's A TIE ]
[The score is 0 to 0]


Round 3:
Rock, Paper, Scissors?  scissors
You played scissors and opponent played 1
[ It's A TIE ]
[The score is 0 to 0]

The game was a tie!
Final score is 0 to 0 

I believe the problem might be in Class Game()

  • 1
    Welcome to SO. This is far too much code for one question. Try to break it up into smaller pieces and check whether each works as intended. If you get stuck again, ask about one of those smaller pieces – joel Apr 12 '19 at 19:57
  • 2
    That said, your problem might be that you're overriding the class `Game` with an instance of `Game`, in `Game = Game(mode)`. Why not `game = Game(mode)`? Then again it might be something entirely unrelated. – joel Apr 12 '19 at 20:00

2 Answers2

1

I believe the problem is in your RandomPlayer class. Instead of returning index in your move method, you should return the move related to that index. In other words, your RandomPlayer class should be:


class RandomPlayer(Player):
    def move(self):
        index = random.randint(0, 2)  # Selects random moves from the move list
        return moves[index]  # Changed from return (index)
SyntaxVoid
  • 2,501
  • 2
  • 15
  • 23
  • 2
    This is exactly right. The *index* was previously randomized and returned, rather than the *value* of `moves` at that index. – That1Guy Apr 12 '19 at 20:05
0

Fixing the RandomPlayer() class fixed the problem, but still, the computer kept choosing the same choice every time. So I changed a line in class Game() and now it seems to be doing what I want. The problem was that I wasn't assigning RandomPlayer() to the opponent

class Game:
    def __init__(self, p2):
        self.p1 = HumanPlayer()
        self.p2 = RandomPlayer()

Thanks all for the input!