Skip to content

Commit

Permalink
Merge branch 'sourcery/main' of https://github.com/amjadkofahi/basic-…
Browse files Browse the repository at this point in the history
…computer-games into sourcery/main
  • Loading branch information
amjadkofahi committed Aug 19, 2024
2 parents 5f01f03 + 71d0267 commit a20b3ff
Show file tree
Hide file tree
Showing 70 changed files with 583 additions and 836 deletions.
4 changes: 2 additions & 2 deletions 02_Amazing/python/amazing.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def display(self) -> None:
print(" ", end="")
print()
for col in range(self.width):
if self.walls[row][col] == 0 or self.walls[row][col] == 2:
if self.walls[row][col] in [0, 2]:
print(":--", end="")
else:
print(": ", end="")
Expand Down Expand Up @@ -148,7 +148,7 @@ def make_opening(
maze.walls[pos.row][pos.col] = maze.walls[pos.row][pos.col] + EXIT_DOWN
pos.row = pos.row + 1
maze.used[pos.row][pos.col] = count
count = count + 1
count += 1
return pos, count


Expand Down
22 changes: 9 additions & 13 deletions 03_Animal/python/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,13 @@ def parse_input(message: str, check_list: bool, root_node: Optional[Node]) -> st
list_known_animals(root_node)
print("\n")

if len(inp) > 0:
token = inp[0].lower()
else:
token = ""

token = inp[0].lower() if len(inp) > 0 else ""
return token


def avoid_void_input(message: str) -> str:
answer = ""
while answer == "":
while not answer:
answer = input(message)
return answer

Expand All @@ -134,8 +130,12 @@ def main() -> None:

# Main loop of game
print_intro()
keep_playing = parse_input("Are you thinking of an animal? ", True, root) == "y"
while keep_playing:
while (
keep_playing := parse_input(
"Are you thinking of an animal? ", True, root
)
== "y"
):
keep_asking = True
# Start traversing the tree by the root
actual_node: Node = root
Expand Down Expand Up @@ -170,17 +170,13 @@ def main() -> None:
f"for a {new_animal} the answer would be: ", False, None
)

actual_node.update_node(
new_question + "?", answer_new_question, new_animal
)
actual_node.update_node(f"{new_question}?", answer_new_question, new_animal)

else:
print("Why not try another animal?")

keep_asking = False

keep_playing = parse_input("Are you thinking of an animal? ", True, root) == "y"


########################################################
# Porting Notes
Expand Down
16 changes: 6 additions & 10 deletions 04_Awari/python/awari.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@

def draw_pit(line: str, board, pit_index) -> str:
val = board[pit_index]
line = line + " "
line += " "
if val < 10:
line = line + " "
line = line + str(val) + " "
return line
line += " "
return line + str(val) + " "


def draw_board(board) -> None:
Expand Down Expand Up @@ -148,7 +147,7 @@ def play_game(board: List[int]) -> None:
print(msg)
break
if landing_spot == home:
landing_spot, is_still_going, home, msg = computer_move(msg + " , ", board)
landing_spot, is_still_going, home, msg = computer_move(f"{msg} , ", board)
if not is_still_going:
print(msg)
break
Expand Down Expand Up @@ -248,7 +247,7 @@ def computer_move(msg: str, board) -> Tuple[int, bool, int, str]:

move_str = chr(42 + selected_move)
if msg:
msg += ", " + move_str
msg += f", {move_str}"
else:
msg = move_str

Expand Down Expand Up @@ -323,10 +322,7 @@ def execute_move(move, home: int, board) -> Tuple[int, bool, int]:
# losses.
losing_book[game_number] = losing_book[game_number] * 6 + move_digit

if player_has_stones(board) and computer_has_stones(board):
is_still_going = True
else:
is_still_going = False
is_still_going = bool(player_has_stones(board) and computer_has_stones(board))
return last_location, is_still_going, home


Expand Down
5 changes: 2 additions & 3 deletions 05_Bagels/python/bagels.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ def pick_number() -> List[str]:
# as separate strings, not a single integer or string
numbers = list(range(10))
random.shuffle(numbers)
num = numbers[0:3]
num_str = [str(i) for i in num]
return num_str
num = numbers[:3]
return [str(i) for i in num]


def get_valid_guess(guesses: int) -> str:
Expand Down
6 changes: 2 additions & 4 deletions 06_Banner/python/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def print_banner() -> None:

except ValueError:
print("Please enter a number greater than zero")
g1 = 0
if input("Centered ").lower().startswith("y"):
g1 = 1
g1 = 1 if input("Centered ").lower().startswith("y") else 0
character = input(
"Character (type 'ALL' if you want character being printed) "
).upper()
Expand All @@ -87,7 +85,7 @@ def print_banner() -> None:
for statement_char in statement:
s = letters[statement_char].copy()
x_str = character
if character == "ALL":
if x_str == "ALL":
x_str = statement_char
if x_str == " ":
print("\n" * (7 * horizontal))
Expand Down
167 changes: 85 additions & 82 deletions 07_Basketball/python/basketball.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,30 @@ def dartmouth_jump_shot(self) -> None:
if random.random() > 0.782 * self.defense / 8:
if random.random() > 0.843 * self.defense / 8:
print("Charging foul. Dartmouth loses ball.\n")
self.opponent_ball()
else:
# player is fouled
self.foul_shots(1)
self.opponent_ball()
self.opponent_ball()
elif random.random() > 0.5:
print(
"Shot is blocked. Ball controlled by "
+ self.opponent
+ ".\n"
)
self.opponent_ball()
else:
if random.random() > 0.5:
print(
"Shot is blocked. Ball controlled by "
+ self.opponent
+ ".\n"
)
self.opponent_ball()
else:
print("Shot is blocked. Ball controlled by Dartmouth.")
self.dartmouth_ball()
print("Shot is blocked. Ball controlled by Dartmouth.")
self.dartmouth_ball()
else:
print("Shot is off target.")
if self.defense / 6 * random.random() > 0.45:
print("Rebound to " + self.opponent + "\n")
print(f"Rebound to {self.opponent}" + "\n")
self.opponent_ball()
else:
print("Dartmouth controls the rebound.")
if random.random() > 0.4:
if self.defense == 6 and random.random() > 0.6:
print("Pass stolen by " + self.opponent + ", easy lay up")
print(f"Pass stolen by {self.opponent}, easy lay up")
self.add_points(0, 2)
self.dartmouth_ball()
else:
Expand Down Expand Up @@ -186,13 +184,11 @@ def dartmouth_non_jump_shot(self) -> None:
if 7 / self.defense * random.random() > 0.875:
if 7 / self.defense * random.random() > 0.925:
print("Charging foul. Dartmouth loses the ball.\n")
self.opponent_ball()
else:
print("Shot blocked. " + self.opponent + "'s ball.\n")
self.opponent_ball()
print(f"Shot blocked. {self.opponent}" + "'s ball.\n")
else:
self.foul_shots(1)
self.opponent_ball()
self.opponent_ball()
else:
print("Shot is off the rim.")
if random.random() > 2 / 3:
Expand All @@ -216,35 +212,35 @@ def dartmouth_ball(self) -> None:
self.shot = shot

if self.time < 100 or random.random() < 0.5:
if self.shot == 1 or self.shot == 2:
if self.shot in [1, 2]:
self.dartmouth_jump_shot()
else:
self.dartmouth_non_jump_shot()
elif self.score[0] == self.score[1]:
print("\n ***** End Of Second Half *****")
print("Score at end of regulation time:")
print(
" Dartmouth: "
+ str(self.score[1])
+ " "
+ self.opponent
+ ": "
+ str(self.score[0])
)
print("Begin two minute overtime period")
self.time = 93
self.start_of_period()

else:
if self.score[0] != self.score[1]:
print("\n ***** End Of Game *****")
print(
"Final Score: Dartmouth: "
+ str(self.score[1])
+ " "
+ self.opponent
+ ": "
+ str(self.score[0])
)
else:
print("\n ***** End Of Second Half *****")
print("Score at end of regulation time:")
print(
" Dartmouth: "
+ str(self.score[1])
+ " "
+ self.opponent
+ ": "
+ str(self.score[0])
)
print("Begin two minute overtime period")
self.time = 93
self.start_of_period()
print("\n ***** End Of Game *****")
print(
"Final Score: Dartmouth: "
+ str(self.score[1])
+ " "
+ self.opponent
+ ": "
+ str(self.score[0])
)

def opponent_jumpshot(self) -> None:
"""Simulate the opponents jumpshot"""
Expand All @@ -253,32 +249,35 @@ def opponent_jumpshot(self) -> None:
if 8 / self.defense * random.random() > 0.75:
if 8 / self.defense * random.random() > 0.9:
print("Offensive foul. Dartmouth's ball.\n")
self.dartmouth_ball()
else:
self.foul_shots(0)
self.dartmouth_ball()
self.dartmouth_ball()
else:
print("Shot is off the rim.")
if self.defense / 6 * random.random() > 0.5:
print(self.opponent + " controls the rebound.")
if self.defense == 6:
if random.random() > 0.75:
print("Ball stolen. Easy lay up for Dartmouth.")
self.add_points(1, 2)
self.opponent_ball()
else:
if random.random() > 0.5:
print()
self.opponent_non_jumpshot()
else:
print("Pass back to " + self.opponent + " guard.\n")
self.opponent_ball()
print(f"{self.opponent} controls the rebound.")
if (
self.defense == 6
and random.random() <= 0.75
and random.random() > 0.5
):
print()
self.opponent_non_jumpshot()
elif (
self.defense == 6
and random.random() <= 0.75
and random.random() <= 0.5
or self.defense != 6
and random.random() <= 0.5
):
print(f"Pass back to {self.opponent}" + " guard.\n")
self.opponent_ball()
elif self.defense == 6 and random.random() > 0.75:
print("Ball stolen. Easy lay up for Dartmouth.")
self.add_points(1, 2)
self.opponent_ball()
else:
if random.random() > 0.5:
self.opponent_non_jumpshot()
else:
print("Pass back to " + self.opponent + " guard.\n")
self.opponent_ball()
self.opponent_non_jumpshot()
else:
print("Dartmouth controls the rebound.\n")
self.dartmouth_ball()
Expand All @@ -296,26 +295,30 @@ def opponent_non_jumpshot(self) -> None:
if 7 / self.defense * random.random() > 0.413:
print("Shot is missed.")
if self.defense / 6 * random.random() > 0.5:
print(self.opponent + " controls the rebound.")
if self.defense == 6:
if random.random() > 0.75:
print("Ball stolen. Easy lay up for Dartmouth.")
self.add_points(1, 2)
self.opponent_ball()
else:
if random.random() > 0.5:
print()
self.opponent_non_jumpshot()
else:
print("Pass back to " + self.opponent + " guard.\n")
self.opponent_ball()
print(f"{self.opponent} controls the rebound.")
if (
self.defense == 6
and random.random() <= 0.75
and random.random() > 0.5
or self.defense != 6
and random.random() > 0.5
):
print()
self.opponent_non_jumpshot()
elif (
self.defense == 6
and random.random() <= 0.75
and random.random() <= 0.5
):
print(f"Pass back to {self.opponent}" + " guard.\n")
self.opponent_ball()
elif self.defense == 6 and random.random() > 0.75:
print("Ball stolen. Easy lay up for Dartmouth.")
self.add_points(1, 2)
self.opponent_ball()
else:
if random.random() > 0.5:
print()
self.opponent_non_jumpshot()
else:
print("Pass back to " + self.opponent + " guard\n")
self.opponent_ball()
print(f"Pass back to {self.opponent}" + " guard\n")
self.opponent_ball()
else:
print("Dartmouth controls the rebound.\n")
self.dartmouth_ball()
Expand Down
9 changes: 3 additions & 6 deletions 08_Batnum/python/batnum.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,8 @@ def computer_pick(
q = pile_size - 1 if win_option == WinOptions.AvoidLast else pile_size
c = min_select + max_select
computer_pick = q - (c * int(q / c))
if computer_pick < min_select:
computer_pick = min_select
if computer_pick > max_select:
computer_pick = max_select
return computer_pick
computer_pick = max(computer_pick, min_select)
return min(computer_pick, max_select)


def computer_move(
Expand All @@ -184,7 +181,7 @@ def computer_move(

# Otherwise, we determine how many the computer selects
curr_sel = computer_pick(pile_size, min_select, max_select, win_option)
pile_size = pile_size - curr_sel
pile_size -= curr_sel
print(f"COMPUTER TAKES {curr_sel} AND LEAVES {pile_size}")
return (False, pile_size)

Expand Down
Loading

0 comments on commit a20b3ff

Please sign in to comment.