mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-25 12:25:10 -08:00
Bullseye (Python): Refactoring
* Use functions to structure code * Remove 1-line function * Use dataclass * Replace dictionaries by a list
This commit is contained in:
@@ -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()
|
||||
110
18_Bullseye/python/bullseye.py
Normal file
110
18_Bullseye/python/bullseye.py
Normal file
@@ -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()
|
||||
13
18_Bullseye/python/test_bullseye.py
Normal file
13
18_Bullseye/python/test_bullseye.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user