Last active
April 18, 2024 03:04
-
-
Save partrita/c392a009cb5c489f1607cec751333e74 to your computer and use it in GitHub Desktop.
ladder_game_generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)}") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output result