Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rock Paper Scissors using Python the starter code : #!/usr/bin/env python3 This program plays a game of Rock, Paper, Scissors between two Players, and reports

Rock Paper Scissors using Python

the starter code :

#!/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""" class Player: def move(self): return 'rock' def learn(self, my_move, their_move): pass 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 = self.p1.move() move2 = self.p2.move() print(f"Player 1: {move1} Player 2: {move2}") self.p1.learn(move1, move2) self.p2.learn(move2, move1) def play_game(self): print("Game start!") for round in range(3): print(f"Round {round}:") self.play_round() print("Game over!") if __name__ == '__main__': game = Game(Player(), Player()) game.play_game()

__

The starter code gives you a place to begin, with Player and Game classes that are mostly empty. Over the course of the project, you will be greatly expanding the classes and methods in this program.

Read the starter code, and run it on your computer to see what it does.

Try importing it into the Python interpreter and experimenting with the Player and Game objects.

2. Create a player subclass that plays randomly

The starter Player class always plays 'rock'. That's not a very good strategy! Create a subclass called RandomPlayer that chooses its move at random. When you call the move method on a RandomPlayer object, it should return one of 'rock', 'paper', or 'scissors' at random.

Change the code so it plays a game between two RandomPlayer objects.

3. Keep score

The starter Game class does not keep score. It doesn't even notice which player won each round. Update the Game class so that it displays the outcome of each round, and keeps score for both players. You can use the provided beats function, which tells whether one move beats another one.

Make sure to handle ties when both players make the same move!

4. Create a subclass for a human player.

The game is a lot more interesting if you can actually play it, instead of just watching the computer play against itself. Create a HumanPlayer subclass, whose move method asks the human user what move to make. (Take another look back at the project demo to see what this can look like!)

Set the program to play a game between HumanPlayer and RandomPlayer.

5. Create player classes that remember

At the end of each game round, the Game class calls the learn method on each player object, to tell that player what the other player's move was. This means you can have computer players that change their moves depending on what has happened earlier in the game. To do this, you will need to implement learn methods that save information into instance variables.

Create a ReflectPlayer class that remembers what move the opponent played last round, and plays that move this round. (In other words, if you play 'paper' on the first round, a ReflectPlayer will play 'paper' on the second round.)

Create a CyclePlayer class that remembers what move it played last round, and cycles through the different moves. (If it played 'rock' this round, it should play 'paper' in the next round.)

(Something to think about: What should these classes do on the first move?)

Test each of these player classes versus HumanPlayer.

6. Validate user input

The human player might sometimes make typos. If they enter roxk instead of rock, the HumanPlayer code should let them try again. (See how this works in the demo if you type something in that isn't a valid move.)

7. Announce the winner

It's up to you how long the game should run. The starter code always plays three rounds, but that's not the only way it could work. You could choose to continue until the player types quit, or you could have the game run until one player is ahead by three points, or any other rule that makes sense to you.

At the end of the game, have it print out which player won, and what the final scores are.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions