Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
restarting game code
#1
hi guys this is my code and i tried to make a game restart function but i couldn't can you help me please
Output:
"""This program plays a game of Rock, Paper, Scissors between two Players, and reports both Player's scores each round.""" import time 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): 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 RandomPlayer(Player): def move(self): import random return random.choice(moves) class HumanPlayer(Player): def move(self): print("Pick your move:") pick = input() if pick in moves or pick == 'quit': return pick else: print("You made wrong choice!") return self.move() time.sleep(2.4) class ReflectPlayer(Player): def move(self): try: return self.their_move except AttributeError as their_move: import random return random.choice(moves) class CyclePlayer(Player): def move(self): try: prev_pick = moves.index(self.my_move) if prev_pick == 2: return moves[0] else: return moves[prev_pick+1] except AttributeError as my_move: import random return random.choice(moves) class Game: def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 def play_round(self): self.move1 = self.p1.move() self.move2 = self.p2.move() if beats(self.move1, self.move2): self.score_p1 += 1 elif beats(self.move2, self.move1): self.score_p2 += 1 print(f"Player 1: {self.move1} Player 2: {self.move2}") self.p1.learn(self.move1, self.move2) self.p2.learn(self.move2, self.move1) time.sleep(1) def play_game(self): print("Game start!") self.score_p1 = 0 self.score_p2 = 0 for round in range(1, 100): print(f"Round {round}:") self.play_round() if self.move1 == 'quit' or self.move2 == 'quit': print("Sorry, Player wants to quit the game") break if self.score_p1 == 3: print("Player1 win!") break elif self.score_p2 == 3: print("Player2 win!") break print(f"Score- Player1: {self.score_p1}, Player2: {self.score_p2}") print("Game over!") time.sleep(2) if __name__ == '__main__': game = Game(HumanPlayer(), CyclePlayer()) game.play_game()
Reply
#2
Can you show us what you did for the restart function/method? If you just want to reset the scores, what you need is right there at the start of play game.

And please use Python tags instead of output tags.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
actually the game just close and i cant stop that

(Sep-03-2019, 06:20 PM)ichabod801 Wrote: Can you show us what you did for the restart function/method? If you just want to reset the scores, what you need is right there at the start of play game.

And please use Python tags instead of output tags.
i try this but didnt work
def win (
        q = input("do you want to play again ? yes or no ?")
        if q == "yes"
         play_game()
Reply
#4
First of all, that needs to be def win(self):. You need the closing parenthesis and the colon to have proper syntax. And assuming this is a method of Game, the self parameter will be automatically provided and you need to be ready for that.

That does not reset your scores, but if you copy and paste the initial values of the scores from play_game, that will work. Mostly. I'm assuming you would call won from play_game, but then won calls play_game. This is called recursion (a function calling itself), and while it is supported in Python, there are limits to it in Python. I would avoid it unless there's a good reason.

Instead, I would write a reset method that just reset the scores to 0. Then put the code from play_game in a while loop:

while True:
   # play the game code here
   q = input("do you want to play again ? yes or no ?")
   if q == 'yes':
      self.reset()
   else:
      break
That way the scores will be reset if they want to play again, and the program will end if they don't.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
where exactly i but this code and what exactly i copy from play_game

im sorry if i bother you :"
i just didnt understand well sorry again
Reply
#6
I'm not going to write your code for you. Give it a try yourself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Asking for help in my code for a "Guess the number" game. Domz 5 3,745 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Code: Creating a basic python game? searching1 5 3,375 Nov-12-2018, 05:18 AM
Last Post: searching1
  Hangman-Game (German code) .. Unkreatief 1 3,676 Mar-22-2017, 10:30 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020