From f7bf099152bd64c96010c2cccbce318811ecbabc Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sat, 26 Mar 2022 07:05:40 +0100 Subject: [PATCH] Bullseye (Python): Refactoring * Use functions to structure code * Remove 1-line function * Use dataclass * Replace dictionaries by a list --- 18_Bullseye/python/Bullseye.py | 122 ---------------------------- 18_Bullseye/python/bullseye.py | 110 +++++++++++++++++++++++++ 18_Bullseye/python/test_bullseye.py | 13 +++ 3 files changed, 123 insertions(+), 122 deletions(-) delete mode 100644 18_Bullseye/python/Bullseye.py create mode 100644 18_Bullseye/python/bullseye.py create mode 100644 18_Bullseye/python/test_bullseye.py diff --git a/18_Bullseye/python/Bullseye.py b/18_Bullseye/python/Bullseye.py deleted file mode 100644 index e354ab4e..00000000 --- a/18_Bullseye/python/Bullseye.py +++ /dev/null @@ -1,122 +0,0 @@ -import random - - -def print_n_whitespaces(n: int) -> None: - print(" " * n, end="") - - -def print_n_newlines(n: int) -> None: - for _ in range(n): - print() - - -def main() -> None: - print_n_whitespaces(32) - print("BULLSEYE") - print_n_whitespaces(15) - print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print_n_newlines(3) - print("IN THIS GAME, UP TO 20 PLAYERS THROW DARTS AT A TARGET") - print("WITH 10, 20, 30, AND 40 POINT ZONES. THE OBJECTIVE IS") - print("TO GET 200 POINTS.") - print() - print("THROW", end="") - print_n_whitespaces(20) - print("DESCRIPTION", end="") - print_n_whitespaces(45) - print("PROBABLE SCORE") - print(" 1", end="") - print_n_whitespaces(20) - print("FAST OVERARM", end="") - print_n_whitespaces(45) - print("BULLSEYE OR COMPLETE MISS") - print(" 2", end="") - print_n_whitespaces(20) - print("CONTROLLED OVERARM", end="") - print_n_whitespaces(45) - print("10, 20 OR 30 POINTS") - print(" 3", end="") - print_n_whitespaces(20) - print("UNDERARM", end="") - print_n_whitespaces(45) - print("ANYTHING") - print() - - nb_winners = 0 - round = 0 - - winners = {} - for i in range(1, 11): - winners[i] = 0 - - total_score = {} - for i in range(1, 21): - total_score[i] = 0 - - nb_players = int(input("HOW MANY PLAYERS? ")) - player_names = {} - for i in range(1, nb_players + 1): - player_name = input("NAME OF PLAYER #") - player_names[i] = player_name - - while nb_winners == 0: - round = round + 1 - print() - print(f"ROUND {round}---------") - for i in range(1, nb_players + 1): - print() - while True: - throw = int(input(f"{player_names[i]}'S THROW? ")) - if throw not in [1, 2, 3]: - print("INPUT 1, 2, OR 3!") - else: - break - if throw == 1: - P1 = 0.65 - P2 = 0.55 - P3 = 0.5 - P4 = 0.5 - elif throw == 2: - P1 = 0.99 - P2 = 0.77 - P3 = 0.43 - P4 = 0.01 - elif throw == 3: - P1 = 0.95 - P2 = 0.75 - P3 = 0.45 - P4 = 0.05 - throwing_luck = random.random() - if throwing_luck >= P1: - print("BULLSEYE!! 40 POINTS!") - points = 40 - elif throwing_luck >= P2: - print("30-POINT ZONE!") - points = 30 - elif throwing_luck >= P3: - print("20-POINT ZONE") - points = 20 - elif throwing_luck >= P4: - print("WHEW! 10 POINTS.") - points = 10 - else: - print("MISSED THE TARGET! TOO BAD.") - points = 0 - total_score[i] = total_score[i] + points - print(f"TOTAL SCORE = {total_score[i]}") - for player_index in range(1, nb_players + 1): - if total_score[player_index] > 200: - nb_winners = nb_winners + 1 - winners[nb_winners] = player_index - - print() - print("WE HAVE A WINNER!!") - print() - for i in range(1, nb_winners + 1): - print(f"{player_names[winners[i]]} SCORED {total_score[winners[i]]} POINTS.") - print() - print("THANKS FOR THE GAME.") - - -if __name__ == "__main__": - main() diff --git a/18_Bullseye/python/bullseye.py b/18_Bullseye/python/bullseye.py new file mode 100644 index 00000000..490a0898 --- /dev/null +++ b/18_Bullseye/python/bullseye.py @@ -0,0 +1,110 @@ +import random +from dataclasses import dataclass +from typing import List + + +@dataclass +class Player: + name: str + score: int = 0 + + +def print_intro() -> None: + print(" " * 32 + "BULLSEYE") + print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print("\n" * 3, end="") + print("IN THIS GAME, UP TO 20 PLAYERS THROW DARTS AT A TARGET") + print("WITH 10, 20, 30, AND 40 POINT ZONES. THE OBJECTIVE IS") + print("TO GET 200 POINTS.") + print() + print("THROW", end="") + print(" " * 20 + "DESCRIPTION", end="") + print(" " * 45 + "PROBABLE SCORE") + print(" 1", end="") + print(" " * 20 + "FAST OVERARM", end="") + print(" " * 45 + "BULLSEYE OR COMPLETE MISS") + print(" 2", end="") + print(" " * 20 + "CONTROLLED OVERARM", end="") + print(" " * 45 + "10, 20 OR 30 POINTS") + print(" 3", end="") + print(" " * 20 + "UNDERARM", end="") + print(" " * 45 + "ANYTHING") + print() + + +def print_outro(players: List[Player], winners: List[int]) -> None: + print() + print("WE HAVE A WINNER!!") + print() + for winner in winners: + print(f"{players[winner].name} SCORED {players[winner].score} POINTS.") + print() + print("THANKS FOR THE GAME.") + + +def main() -> None: + print_intro() + players: List[Player] = [] + + winners: List[int] = [] # will point to indices of player_names + + nb_players = int(input("HOW MANY PLAYERS? ")) + for _ in range(nb_players): + player_name = input("NAME OF PLAYER #") + players.append(Player(player_name)) + + round_number = 0 + while len(winners) == 0: + round_number += 1 + print() + print(f"ROUND {round_number}---------") + for player in players: + print() + while True: + throw = int(input(f"{player.name}'S THROW? ")) + if throw not in [1, 2, 3]: + print("INPUT 1, 2, OR 3!") + else: + break + if throw == 1: + probability_1 = 0.65 + probability_2 = 0.55 + probability_3 = 0.5 + probability_4 = 0.5 + elif throw == 2: + probability_1 = 0.99 + probability_2 = 0.77 + probability_3 = 0.43 + probability_4 = 0.01 + elif throw == 3: + probability_1 = 0.95 + probability_2 = 0.75 + probability_3 = 0.45 + probability_4 = 0.05 + throwing_luck = random.random() + if throwing_luck >= probability_1: + print("BULLSEYE!! 40 POINTS!") + points = 40 + elif throwing_luck >= probability_2: + print("30-POINT ZONE!") + points = 30 + elif throwing_luck >= probability_3: + print("20-POINT ZONE") + points = 20 + elif throwing_luck >= probability_4: + print("WHEW! 10 POINTS.") + points = 10 + else: + print("MISSED THE TARGET! TOO BAD.") + points = 0 + player.score += points + print(f"TOTAL SCORE = {player.score}") + for player_index, player in enumerate(players): + if player.score > 200: + winners.append(player_index) + + print_outro(players, winners) + + +if __name__ == "__main__": + main() diff --git a/18_Bullseye/python/test_bullseye.py b/18_Bullseye/python/test_bullseye.py new file mode 100644 index 00000000..d3d0735f --- /dev/null +++ b/18_Bullseye/python/test_bullseye.py @@ -0,0 +1,13 @@ +import io + +from _pytest.monkeypatch import MonkeyPatch +from bullseye import main + + +def test_main(monkeypatch: MonkeyPatch) -> None: + nb_players = 1 + monkeypatch.setattr( + "sys.stdin", + io.StringIO(f"{nb_players}\nMartin\n3\n2\n1" + ("\n2" * 21)), + ) + main()