Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added jail functionality fixes#10 #30

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions board.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Board:
Contains location data.\n
"""
def __init__(self, num_players) -> None:
# owner var indicates who owns, but also is used for special codes below:
# Special codes: -1 is not owned, -2 is mortaged, -3 is community chest, -4 is chance, -5 is tax
# -6 is jail, -7 is go to jail, -8 is free parking, -9 is luxury, -10 is go
property = Property(num_players, "Go", -10, (32,72), COLORS.LIGHTGRAY, 0, 0, 0, 0, 0, 0, 0, 0, 0)
self.locations = {
0: Property(num_players, "Go", -10, (32,72), COLORS.LIGHTGRAY, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Expand All @@ -20,7 +23,7 @@ def __init__(self, num_players) -> None:
7: Property(0, "Chance", -4, (32,23), COLORS.CHANCE, 0, 0, 0, 0, 0, 0, 0, 0, 0),
8: Property(0, "Vermont Avenue", -1, (32,16), COLORS.LIGHTBLUE, 100, 50, 6, 30, 90, 270, 400, 550, 50),
9: Property(0, "Connecticut Avenue", -1, (32,9), COLORS.LIGHTBLUE, 120, 50, 8, 40, 100, 300, 450, 600, 60),
10: Property(0, "Jail", -7, (32,23), COLORS.LIGHTGRAY, 0, 0, 0, 0, 0, 0, 0, 0, 0),
10: Property(0, "Jail", -6, (32,2), COLORS.LIGHTGRAY, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printing bug fixed

11: Property(0, "St. Charles Place", -1, (29,2), COLORS.ROUGE, 140, 100, 10, 50, 150, 450, 625, 750, 70),
12: Property(0, "Electric Company", -1, (26,2), COLORS.YELLOW, 150, 0, 0, 4, 10, 0, 0, 0, 75),
13: Property(0, "States Avenue", -1, (23, 2), COLORS.ROUGE, 140, 100, 10, 50, 150, 450, 625, 750, 70),
Expand All @@ -30,7 +33,7 @@ def __init__(self, num_players) -> None:
17: Property(0, "Community Chest", -3, (11,2), COLORS.COMMUNITY, 0, 0, 0, 0, 0, 0, 0, 0, 0),
18: Property(0, "Tennessee Avenue", -1, (8,2), COLORS.ORANGE, 180, 100, 14, 70, 200, 550, 750, 950, 90),
19: Property(0, "New York Avenue", -1, (5,2), COLORS.ORANGE, 200, 100, 16, 80, 220, 600, 800, 1000, 100),
20: Property(0, "Free Parking", -1, (2,2), COLORS.LIGHTGRAY, 0, 0, 0, 0, 0, 0, 0, 0, 0),
20: Property(0, "Free Parking", -8, (2,2), COLORS.LIGHTGRAY, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be identified as special code -8

21: Property(0, "Kentucky Avenue", -1, (2,9), COLORS.RED, 220, 150, 18, 90, 250, 700, 875, 1050, 110),
22: Property(0, "Chance", -4, (2,16), COLORS.CHANCE, 0, 0, 0, 0, 0, 0, 0, 0, 0),
23: Property(0, "Indiana Avenue", -1, (2,23), COLORS.RED, 220, 150, 18, 90, 250, 700, 875, 1050, 110),
Expand All @@ -52,7 +55,7 @@ def __init__(self, num_players) -> None:
39: Property(0, "Boardwalk", -1, (29,72), COLORS.BLUE, 400, 200, 50, 200, 600, 1400, 1700, 2000, 200),
}

def update_location(self, player:Player, roll: int, update_history, new = None) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed this definition because update_history is used exclusively once.. makes more sense to hardcode in monopoly.py (see change there)

def update_location(self, player:Player, roll: int, new = None) -> None:
"""
Update location with player\n
@location: int\n
Expand All @@ -63,7 +66,7 @@ def update_location(self, player:Player, roll: int, update_history, new = None)
if new_location > 39:
new_location -= 40
player.receive(200)
update_history(f"Player {player.order} passed Go and received $200")

self.locations[player.location].players.remove(player.order)
self.locations[new_location].players.append(player.order)
player.location = new_location
Expand Down
97 changes: 69 additions & 28 deletions monopoly.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def update_history(message: str):
history.append(message[:40] + " " * (40 - len(message)))
message = message[40:]
history.append(message + " " * (40 - len(message)))
if len(history) > 31:
while(len(history) > 31):
if len(history) > 30:
while(len(history) > 30):
history.pop(0)
refresh_h_and_s()

Expand Down Expand Up @@ -161,7 +161,7 @@ def refresh_h_and_s():
for i in range(len(sorted_players)):
if(sorted_players[i].order != -1):
color = COLORS.playerColors[sorted_players[i].order]
print(color + f"\033[{31+i};122H{sorted_players[i].order} - ${sorted_players[i].cash}", end=COLORS.RESET)
print(color + f"\033[{31+i};122H{sorted_players[i].name} - ${sorted_players[i].cash}", end=COLORS.RESET)

def buy_logic():
CL = players[turn].location
Expand Down Expand Up @@ -374,6 +374,7 @@ def roll():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
return(die1,die2)

#The function that handles the players
#second and third correspond to if its the players second or third consecutive turn, they are bools
def player_roll(num_rolls):
Expand All @@ -382,30 +383,69 @@ def player_roll(num_rolls):
if(players[turn].order != -1): # If player is not bankrupt
player_color = COLORS.playerColors[turn]
update_history(player_color + f"{players[turn].name}'s turn")
refresh_h_and_s()
print_commands()

was_in_jail = players[turn].jail # Flag to check if player was in jail before rolling

if players[turn].jail:
if players[turn].jail_turns < 3:
while True:
choice = input("\033[36;0HYou're in jail. Pay $50 fine (f) or attempt to roll doubles (r)?").lower().strip()
if choice == 'f':
players[turn].pay_jail_fine()
update_history(f"{players[turn].name} paid $50 to post bail.")
break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed terminology and allow player to move after paying fine. this is typical monopoly gameplay

elif choice == 'r':
update_history(f"{players[turn].name} will attempt to roll doubles.")
break
else:
update_history(f"Invalid choice. Please enter 'f' to pay fine or 'r' to roll.")
else:
update_history(f"This is {players[turn].name}'s third turn in jail. They must attempt to roll doubles.")

input("\033[36;0HRoll dice?")
dice = roll()
bottom_screen_wipe()
update_history(f"Player {turn} rolled {dice[0]} and {dice[1]}")
update_history(f"{players[turn].name} rolled {dice[0]} and {dice[1]}")

if players[turn].jail:
left_jail, reason = players[turn].attempt_jail_roll(dice)
if left_jail:
if reason == "doubles":
update_history(f"{players[turn].name} rolled doubles and got out of jail!")
elif reason == "third_turn":
update_history(f"{players[turn].name} didn't roll doubles on their third turn. They paid $50 to post bail.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

terminology changes

players[turn].pay_jail_fine()
else:
update_history(f"{players[turn].name} didn't roll doubles and is still in jail. Turns in jail: {players[turn].jail_turns}")
return

if dice[0] == dice[1]:
if num_rolls == 1:
update_history(f"{players[turn]} rolled doubles! Roll again.")

refresh_board()

# Only check for doubles if the player wasn't in jail at the start of their turn
if dice[0] == dice[1] and not was_in_jail:
if num_rolls == 1:
update_history(f"{players[turn].name} rolled doubles! Roll again.")
elif num_rolls == 2:
update_history(f"{players[turn]} rolled doubles!(X2) Roll again.")

update_history(f"{players[turn].name} rolled doubles!(X2) Roll again.")
elif num_rolls == 3:
update_history(f"Player {turn} rolled doubles three times\n in a row!")
update_history(f"Player {turn} is going to jail!")
players[turn].jail = True
board.update_location(players[turn], -1, update_history)
update_history(f"{players[turn].name} rolled doubles three times in a row!")
update_history(f"{players[turn].name} is going to jail!")
players[turn].go_to_jail()
# board.locations[players[turn].location].players.remove(turn)
board.locations[10].players.append(turn)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relocated this to properly print after movement. looks better

refresh_board()
return

prev_loc = players[turn].location
board.update_location(players[turn], dice[0] + dice[1])
if prev_loc > players[turn].location:
players[turn].pay(200)
update_history(f"{players[turn].name} passed Go and collected $200!")
update_history(f"{players[turn].name} landed on {board.locations[players[turn].location].name}")
refresh_board()
#if player rolled their third double they will be in jail and their location doesn't update
if players[turn].jail == False:
board.update_location(players[turn], dice[0] + dice[1], update_history)
update_history(f"{players[turn].name} landed on {board.locations[players[turn].location].name}")
refresh_board()

if board.locations[players[turn].location].owner < 0:
match board.locations[players[turn].location].owner:
case -1: #unowned
Expand All @@ -427,10 +467,10 @@ def player_roll(num_rolls):
players[turn].pay(200)
update_history(f"{players[turn].name} paid income tax ($200)")
case -6: #jail
pass
update_history("Just visiting!")
case -7: #go to jail
players[turn].jail = True
board.update_location(players[turn], -1, update_history)
board.update_location(players[turn], -1)
case -8: #free parking
pass
case -9: #luxury tax
Expand All @@ -448,13 +488,14 @@ def player_roll(num_rolls):
players[board.locations[cl].owner].receive(rent)
update_history(f"{players[turn].name} paid ${rent} to {players[board.locations[cl].owner].name}")
refresh_board()
#checks if player rolled a double, and has them roll again if they did.
if dice[0] == dice[1] and players[turn].jail == False:
num_rolls +=1

# Check for doubles and roll again only if player wasn't in jail at the start of their turn
if dice[0] == dice[1] and not was_in_jail:
num_rolls += 1
player_roll(num_rolls)

while(True):
# First time the player who's turn it is rolls their dice
# First time the player whose turn it is rolls their dice
#if they roll a double the function calls itself and updates its their number of consecutive rolls
player_roll(num_rolls=1)
if(players[turn].cash > 0):
Expand All @@ -469,12 +510,12 @@ def player_roll(num_rolls):
else:
print("Invalid option!")
choice = input("\033[38;0H'e' to end turn, p to manage properties, ?")
update_history(f"{players[turn]} ended their turn.")
update_history(f"{players[turn].name} ended their turn.")
else:
update_history(f"Player {turn} is in debt. Resolve debts before ending turn.")
update_history(f"{players[turn].name} is in debt. Resolve debts before ending turn.")
option = input("\033[38;0HResolve debts before ending turn.").lower().strip()
if(option == "b"): # Declare bankruptcy
update_history(f"Player {turn} declared bankruptcy.")
update_history(f"{players[turn].name} declared bankruptcy.")
players[turn].order = -1
elif(option == "m"): # Mortgage properties
pass
Expand Down
25 changes: 23 additions & 2 deletions player_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self, cash:int, order:int) -> None:
self.jail = False
self.jailcards = 0
self.name = "Player #" + str(order + 1)
self.jail_turns = 0
"""
Player cash\n
@cash: int\n
Expand Down Expand Up @@ -56,17 +57,37 @@ def receive(self, amount:int) -> None:
@amount: int\n
"""
self.cash += amount
def jail(self) -> None:
def go_to_jail(self) -> None:
"""
Go to jail\n
"""
self.location = 10
self.jail = True
self.jail_turns = 0
def leave_jail(self) -> None:
"""
Leave jail\n
"""
self.jail = False

self.jail_turns = 0
def attempt_jail_roll(self, dice: tuple) -> tuple:
"""
Attempt to leave jail by rolling doubles
Returns (left_jail: bool, reason: str)
"""
self.jail_turns += 1
if dice[0] == dice[1]:
self.leave_jail()
return True, "doubles"
elif self.jail_turns == 3:
self.pay_jail_fine()
return True, "third_turn"
return False, ""
def pay_jail_fine(self) -> None:
"""
Pay jail fine of $50
"""
self.pay(50)
self.leave_jail()
def __str__(self) -> str:
return f"Player {self.order}"