Skip to content

Instantly share code, notes, and snippets.

@partrita
Last active April 18, 2024 03:04
Show Gist options
  • Select an option

  • Save partrita/c392a009cb5c489f1607cec751333e74 to your computer and use it in GitHub Desktop.

Select an option

Save partrita/c392a009cb5c489f1607cec751333e74 to your computer and use it in GitHub Desktop.
ladder_game_generator
import random
# Define the probability
probability = 0.33
ladder_h = "╬"
ladder_w = "═"
empty = "░"
def create_ladder(num_people, num_lines):
# Initialize the ladder with vertical lines
ladder = [[ladder_h for _ in range(2 * num_people - 1)] for _ in range(num_lines)]
# Randomly add horizontal lines to create the ladder
for i in range(num_lines):
for j in range(2 * num_people - 1):
if j % 2 == 1:
if ladder[i][j - 2] == ladder_w:
ladder[i][j] = empty
else:
random_number = random.random()
ladder[i][j] = ladder_w if random_number < probability else empty
return ladder
def print_ladder(ladder):
# Print the ladder
for row in ladder:
print("".join(row))
def simulate_ladder_game(ladder, num_people):
# Simulate the ladder game and return the final positions
current_positions = list(range(num_people))
for line in ladder:
for i in range(1, num_people * 2 - 2, 2):
if line[i] == ladder_w:
current_positions[i // 2], current_positions[i // 2 + 1] = (
current_positions[i // 2 + 1],
current_positions[i // 2],
)
return current_positions
def print_row_numbers(num_people):
# Print row numbers
for i in range(num_people):
digit = len(str(i + 1))
print(f"{i + 1}", end="" * (4 - digit))
print("\n")
def print_row_letters(num_people):
# Print row letters
for i in range(num_people):
letter = chr(ord("A") + i)
digit = len(str(letter))
print(letter, end="" * (4 - digit))
if __name__ == "__main__":
num_people = int(input("Enter the number of participants: "))
num_lines = int(0.3 * num_people * (num_people - 1) ** 2)
ladder = create_ladder(num_people, num_lines)
print_row_numbers(num_people)
print("\n")
print_ladder(ladder)
print_row_letters(num_people)
print("\n")
input("# Press any key if you want results")
winners = simulate_ladder_game(ladder, num_people)
print("\nGame Results:")
for i, winner in enumerate(winners):
print(f"Participant {i + 1}: {chr(ord('A') + winner)}")
@partrita
Copy link
Copy Markdown
Author

partrita commented Sep 12, 2023

Output result

$python ladder_game.py
# Enter the number of participants: 3


1 2 3
╬░╬═╬
╬░╬═╬
╬═╬░╬
A B C


= Press any key see the results. =

# Results:
Participant 1=B
Participant 2=A
Participant 3=C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment