|
| 1 | +from random import randint |
| 2 | +from time import sleep |
| 3 | +from os import system as OSsys, name as OSname |
| 4 | + |
| 5 | + |
| 6 | +# Clear screen after each round |
| 7 | +def clearScreen(): |
| 8 | + if OSname == 'nt': OSsys('cls') |
| 9 | + else: OSsys('clear') |
| 10 | + |
| 11 | + |
| 12 | +# Creating throw dice function |
| 13 | +def throw_dice(player_name): |
| 14 | + top = randint(1, 6) |
| 15 | + print(f'\n{player_name} threw the dice...\n'); sleep(0.3) |
| 16 | + sleep(randint(10, 30)*0.05) |
| 17 | + print_dice(top) |
| 18 | + return top |
| 19 | + |
| 20 | + |
| 21 | +# Creating function to visualize dice top |
| 22 | +def print_dice(top): |
| 23 | + sides = [ |
| 24 | + '| |\n| * |\n| |', |
| 25 | + '| * |\n| |\n| * |', |
| 26 | + '|* |\n| * |\n| *|', |
| 27 | + '|* *|\n| |\n|* *|', |
| 28 | + '|* *|\n| * |\n|* *|', |
| 29 | + '|* *|\n|* *|\n|* *|' |
| 30 | + ] |
| 31 | + print('+-----+'); sleep(0.3) |
| 32 | + print(sides[top-1]) |
| 33 | + print('+-----+'); sleep(0.3) |
| 34 | + |
| 35 | +def show_score(roundnum, p1, p2, p1_points, p2_points): |
| 36 | + print(f'\nRound {roundnum}'); sleep(0.3) |
| 37 | + print('------------------------------------------'); sleep(0.3) |
| 38 | + print(f'{p1} : {p1_points}'); sleep(0.3) |
| 39 | + print(f'{p2} : {p2_points}'); sleep(0.3) |
| 40 | + print('------------------------------------------\n'); sleep(0.3) |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + # Number of rounds |
| 45 | + done = False |
| 46 | + while not done: |
| 47 | + try: |
| 48 | + n = int(input("Enter the number of rounds: ")) |
| 49 | + if n > 0: |
| 50 | + done = True |
| 51 | + else: |
| 52 | + print("Please Enter a positive number!"); sleep(0.3) |
| 53 | + except ValueError: |
| 54 | + print("please Enter an integer."); sleep(0.3) |
| 55 | + |
| 56 | + |
| 57 | + # Name of players and initialize points |
| 58 | + p1 = input('\nName of first player? ') |
| 59 | + p2 = input('\nName of second player? ') |
| 60 | + |
| 61 | + p1_points = 0 |
| 62 | + p2_points = 0 |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + for i in range(n): |
| 67 | + print("Scoreboard:"); sleep(0.3) |
| 68 | + show_score(i+1, p1, p2, p1_points, p2_points); sleep(0.3) |
| 69 | + |
| 70 | + r1 = throw_dice(p1); sleep(0.3) |
| 71 | + r2 = throw_dice(p2); sleep(0.3) |
| 72 | + |
| 73 | + if r1 > r2: |
| 74 | + print(f'\n{p1} won this round.'); sleep(0.3) |
| 75 | + p1_points += 1 |
| 76 | + elif r2 > r1: |
| 77 | + print(f'\n{p2} won this round.'); sleep(0.3) |
| 78 | + p2_points += 1 |
| 79 | + else: |
| 80 | + print('\nIt\'s a draw.'); sleep(0.3) |
| 81 | + |
| 82 | + q = input("\n\nPress enter to continue to next round. (Q to stop rounds)") |
| 83 | + |
| 84 | + if q.upper() == 'Q': break |
| 85 | + |
| 86 | + clearScreen() |
| 87 | + |
| 88 | + print('\n\n') |
| 89 | + if p1_points > p2_points: |
| 90 | + print(f"{p1} won by {p1_points - p2_points} points."); sleep(0.3) |
| 91 | + elif p2_points > p1_points: |
| 92 | + print(f"{p2} won by {p2_points - p1_points} points."); sleep(0.3) |
| 93 | + else: |
| 94 | + print(f"Game ended in a draw..."); sleep(0.3) |
| 95 | + |
| 96 | + print('\nFinal Score Board:') |
| 97 | + show_score(i+1, p1, p2, p1_points, p2_points) |
| 98 | + |
| 99 | + with open('result_rolladice.txt', 'a') as file: |
| 100 | + cov = '\n------------------------------------\n' |
| 101 | + r = f'Number of rounds: {n}\n{p1}: {p1_points}\n{p2}: {p2_points}' |
| 102 | + res = cov + r + cov |
| 103 | + |
| 104 | + file.write(res) |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + main() |
0 commit comments