MCPcopy Create free account
hub / github.com/geekcomputers/Python / play_game

Function play_game

Sanke-water-gun game.py:75–130  ·  view source on GitHub ↗

Play Snake-Water-Gun game for given rounds. Parameters ---------- rounds : int Number of rounds to play (default 10).

(rounds: int = 10)

Source from the content-addressed store, hash-verified

73
74
75def play_game(rounds: int = 10) -> None:
76 """
77 Play Snake-Water-Gun game for given rounds.
78
79 Parameters
80 ----------
81 rounds : int
82 Number of rounds to play (default 10).
83 """
84 print("Welcome to the Snake-Water-Gun Game\n")
85 print(f"I am Mr. Computer, We will play this game {rounds} times")
86 print("Whoever wins more matches will be the winner\n")
87
88 user_win = 0
89 comp_win = 0
90 draw = 0
91 round_no = 0
92
93 while round_no < rounds:
94 print(f"Game No. {round_no + 1}")
95 for key, val in CHOICES.items():
96 print(f"Choose {key.upper()} for {val}")
97
98 comp_choice = random.choice(list(CHOICES.keys()))
99 user_choice = input("\n-----> ").strip().lower()
100
101 result = determine_winner(user_choice, comp_choice)
102
103 if result == "user":
104 user_win += 1
105 elif result == "computer":
106 comp_win += 1
107 elif result == "draw":
108 draw += 1
109 else:
110 print("\nInvalid input, restarting the game...\n")
111 time.sleep(1)
112 round_no = 0
113 user_win = comp_win = draw = 0
114 continue
115
116 round_no += 1
117 print(f"Computer chose {CHOICES[comp_choice]}")
118 print(f"You chose {CHOICES.get(user_choice, 'Invalid')}\n")
119
120 print("\nHere are final stats:")
121 print(f"Mr. Computer won: {comp_win} matches")
122 print(f"You won: {user_win} matches")
123 print(f"Matches Drawn: {draw}")
124
125 if comp_win > user_win:
126 print("\n------- Mr. Computer won -------")
127 elif comp_win < user_win:
128 print("\n----------- You won -----------")
129 else:
130 print("\n---------- Match Draw ----------")
131
132

Callers 1

Calls 2

determine_winnerFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected