Schere Stein Papier / Rock Paper Scissors
Verfasst: Donnerstag 1. November 2018, 22:39
Liebe Wissende!
Ich brauche Eure konkrete Hilfe und damit meine ich Code-Snippets. Es geht um eine Code-Erstellung, die ich derzeit nicht ohne Weiteres hinbekomme.
Ich „besuche“ gerade einen E-Learning-Kurs zum Thema Programmieren. Eingestiegen bin ich mit HTML und CSS, war recht gut soweit, auch die Aufgabe, die zu lösen war ging recht easy von der Hand.
Dann kam Python. Anfangs ließen wir Turtles alles mögliche machen und ich fühlte mich auch da recht gut. Dann kam der „Cut“. Zum einen war unsere Kleine über Wochen krank und hat uns wach gehalten. Zum anderen war ein Lernen neben dem Vollzeitberuf dadurch sehr schwierig und ich bin nicht so richtig mitgekommen.
Nun ist ein weiteres Projekt abzugeben und zwar: Schere Stein Paper. Es gibt einen Starter-Code (den poste ich weiter unten), das, was ich bisher so zusammengestopselt haben (poste ich weiter unten) und diese Vorgaben:
Der Zug soll zufällig sein:
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.
Das Programm soll mitzählen und Unentschieden anzeigen:
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!
Das Programm soll menschliche Spieler zulassen
Create a HumanPlayer subclass, whose move method asks the human user what move to make. Set the program to play a game between HumanPlayer and RandomPlayer.
Das Programm soll “lernen”/sich einen Zug merken und ihn ausspielen
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.)
Rechtschreibfehler berücksichtigen:
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.)
Den Gewinner bekanntgeben
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.
Und dann alles durch pycodestyle jagen.
Hier ist das Ergebnis/das Programm, mit dem man Rock Papers Scissors spielen kann. Datei heißt RPS demo 37
https://github.com/udacity/ipnd_rps_starter_code
Im Folgenden der 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()
Und hier was ich bisher habe:
#!/usr/bin/env python3
"""This program plays a game of Rock, Paper, Scissors between two Players,
and reports both Player's scores each round."""
#we have parts in the code which shall answer randomly so
import random
moves = ['rock', 'paper', 'scissors']
"""The Player class is the parent class for all of the Players
in this game"""
#the "normal" player
class Player:
def __init__(self):
self.my_move = None
self.notmy_move = None
self.score = 0
def learn(self, my_move, notmy_move):
self.my_move = my_move
self.notmy_move = notmy_move
#the reflect player
class ReflectPlayer:
def move(self):
if not self.notmy_move:
return random.choice(moves)
else:
return self.notmy_move
#the cycle player
class CyclePlayer(Player):
def move(self):
for n in range(rounds):
return moves[n % 3]
#nothing to do here - code makes clear which value beats the other
def beats(one, two):
return ((one == 'rock' and two == 'scissors') or
(one == 'scissors' and two == 'paper') or
(one == 'paper' and two == 'rock'))
#nothing to do here - code creates the players
class Game:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
#With this code I want to start the game
def play_game(self):
print("Game start! Let's play RPS! Like we did when we were children.")
print(f"{self.player1.behavior} versus "
f"{self.player2.behavior}!")
for round in range(self.rounds):
print(f"Round {round+1}:")
self.play_round()
if self.player1_score > self.player2_score:
print(f"{self.player1.behavior} Win!!! Player 1 is the winner here, oooooh yeah! Haha!")
else:
print(f"{self.player2.behavior} Win!!! Player 2 is the rps king. Well, for now. Wohooo!")
print("Game over! But why shouldn't we play right again?")
#Code to process each game round
def play_round(self):
while True:
move1 = self.player1.move()
move2 = self.player2.move()
print(f"{self.player1.behavior}: {move1} and "
f"{self.player2.behavior}: {move2}")
if move1 != move2:
break
else:
print("There was a tie, play again! Now! Do it!")
self.compute_score(move1, move2)
self.player1.learn(move1, move2)
self.player2.learn(move2, move1)
#Count the score
def compute_score(self, move1, move2):
if self.beats(move1, move2):
self.player1_score += 1
else:
self.player2_score += 1
print(f"Scores - {self.player1.behavior}: {self.player1_score}, "
f"{self.player2.behavior}: {self.player2_score}")
if __name__ == '__main__':
game = Game(Player(), Player())
game.play_game()
Danke Euch schon jetzt im Voraus für Eure Hilfe.
Ich brauche Eure konkrete Hilfe und damit meine ich Code-Snippets. Es geht um eine Code-Erstellung, die ich derzeit nicht ohne Weiteres hinbekomme.
Ich „besuche“ gerade einen E-Learning-Kurs zum Thema Programmieren. Eingestiegen bin ich mit HTML und CSS, war recht gut soweit, auch die Aufgabe, die zu lösen war ging recht easy von der Hand.
Dann kam Python. Anfangs ließen wir Turtles alles mögliche machen und ich fühlte mich auch da recht gut. Dann kam der „Cut“. Zum einen war unsere Kleine über Wochen krank und hat uns wach gehalten. Zum anderen war ein Lernen neben dem Vollzeitberuf dadurch sehr schwierig und ich bin nicht so richtig mitgekommen.
Nun ist ein weiteres Projekt abzugeben und zwar: Schere Stein Paper. Es gibt einen Starter-Code (den poste ich weiter unten), das, was ich bisher so zusammengestopselt haben (poste ich weiter unten) und diese Vorgaben:
Der Zug soll zufällig sein:
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.
Das Programm soll mitzählen und Unentschieden anzeigen:
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!
Das Programm soll menschliche Spieler zulassen
Create a HumanPlayer subclass, whose move method asks the human user what move to make. Set the program to play a game between HumanPlayer and RandomPlayer.
Das Programm soll “lernen”/sich einen Zug merken und ihn ausspielen
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.)
Rechtschreibfehler berücksichtigen:
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.)
Den Gewinner bekanntgeben
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.
Und dann alles durch pycodestyle jagen.
Hier ist das Ergebnis/das Programm, mit dem man Rock Papers Scissors spielen kann. Datei heißt RPS demo 37
https://github.com/udacity/ipnd_rps_starter_code
Im Folgenden der 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()
Und hier was ich bisher habe:
#!/usr/bin/env python3
"""This program plays a game of Rock, Paper, Scissors between two Players,
and reports both Player's scores each round."""
#we have parts in the code which shall answer randomly so
import random
moves = ['rock', 'paper', 'scissors']
"""The Player class is the parent class for all of the Players
in this game"""
#the "normal" player
class Player:
def __init__(self):
self.my_move = None
self.notmy_move = None
self.score = 0
def learn(self, my_move, notmy_move):
self.my_move = my_move
self.notmy_move = notmy_move
#the reflect player
class ReflectPlayer:
def move(self):
if not self.notmy_move:
return random.choice(moves)
else:
return self.notmy_move
#the cycle player
class CyclePlayer(Player):
def move(self):
for n in range(rounds):
return moves[n % 3]
#nothing to do here - code makes clear which value beats the other
def beats(one, two):
return ((one == 'rock' and two == 'scissors') or
(one == 'scissors' and two == 'paper') or
(one == 'paper' and two == 'rock'))
#nothing to do here - code creates the players
class Game:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
#With this code I want to start the game
def play_game(self):
print("Game start! Let's play RPS! Like we did when we were children.")
print(f"{self.player1.behavior} versus "
f"{self.player2.behavior}!")
for round in range(self.rounds):
print(f"Round {round+1}:")
self.play_round()
if self.player1_score > self.player2_score:
print(f"{self.player1.behavior} Win!!! Player 1 is the winner here, oooooh yeah! Haha!")
else:
print(f"{self.player2.behavior} Win!!! Player 2 is the rps king. Well, for now. Wohooo!")
print("Game over! But why shouldn't we play right again?")
#Code to process each game round
def play_round(self):
while True:
move1 = self.player1.move()
move2 = self.player2.move()
print(f"{self.player1.behavior}: {move1} and "
f"{self.player2.behavior}: {move2}")
if move1 != move2:
break
else:
print("There was a tie, play again! Now! Do it!")
self.compute_score(move1, move2)
self.player1.learn(move1, move2)
self.player2.learn(move2, move1)
#Count the score
def compute_score(self, move1, move2):
if self.beats(move1, move2):
self.player1_score += 1
else:
self.player2_score += 1
print(f"Scores - {self.player1.behavior}: {self.player1_score}, "
f"{self.player2.behavior}: {self.player2_score}")
if __name__ == '__main__':
game = Game(Player(), Player())
game.play_game()
Danke Euch schon jetzt im Voraus für Eure Hilfe.