I am a Python noob and over the last week it has been my goal to learn how to write python code. As my first project I decided to create a dice game called C low. This is a version of that game that my friends and I played in highschool. I want to ensure I follow Python best practices, also I'm sure there are easier more pythonic ways to write this program, and I am interested in those as well. How can I improve my code?
Game Rules:
- Two players roll 3 dice to decide who won the hand.
- 456 = best hand
- 123 = worst hand
- Triples beat doubles in order.
- 666 beats 555 etc
- For doubles only the remaining die matters
- ex)
- 661 = 1
- 446 = 6
- If your roll doesn't fall into one of these categories you roll again.
#Dice
#C LOW Highschool edition
import random
from collections import Counter as c
import time
#Create dice art
DICE_ART = ["""
-------
| |
| N |
| |
------- ""","""
-------
| |
| 1 |
| |
------- ""","""
-------
| 2 |
| |
| 2 |
------- ""","""
-------
| 3 |
| 3 |
| 3 |
------- ""","""
-------
| 4 4 |
| |
| 4 4 |
------- ""","""
-------
| 5 5 |
| 5 |
| 5 5 |
------- ""","""
-------
| 6 6 6 |
| |
| 6 6 6 |
------- """]
dice = [1, 2, 3, 4, 5, 6]
player_1 = []
player_2 = []
roll = []
#roll = ["3", "5", "6"]
def roll_dice():
#This function will simulate rolling the dice.
for r in range(0, 3): # Pick 3 dice
r = dice[random.randint(0, 5)] # At random
r = int(r) #Capture the integer of the dice you rolled
print(DICE_ART[r]) #Print the ASCII art of the roll
roll.append(r) #Add dice to list roll
print(roll)
def roll_again(player_name):
if c(roll) != c([4, 5, 6]) and c(roll) != c([1, 2, 3]):
while roll[0] != roll[1] and roll[0] != roll[2] and roll[1] != roll[2]:
print("No matches, " + player_name + " is rolling again...")
time.sleep(3)
del roll[:]
roll_dice()
def winner(roll, player_name):
# Decide who won by summing dice
total = 0
if c(roll) == c([4, 5, 6]):
total = 456 #always wins
elif c(roll) == c([1, 2, 3]):
total = 0 #always loses
elif roll[0] == roll[1] and roll[1] == roll[2]:
total = sum(roll)
total = total + 100 # 3 of a kind beats 2 of a kind
else:
total = sum([item for item in roll if roll.count(item) == 1])
return total
print(player + " rolled a " + total)
def roll_statement(roll, player_name):
#Create a statement to print based on the dice rolled
if c(roll) == c([4, 5, 6]):
print(player_name + " rolled a " + "4 5 6!")
elif c(roll) == c([1, 2, 3]):
print(player_name + " rolled a " + "1 2 3!")
elif roll[0] == roll[1] and roll[1] == roll[2]:
print(player_name + " rolled Triple " + str(roll[0]) + "'s" + ".")
else:
x = [item for item in roll if roll.count(item) == 1]
print(player_name + " rolled a " + str(*x) + ".")
def score_board(player_1, player_2):
#Create a scoreboard to display at the end
time.sleep(5)
print("*********FINAL SCORE*********")
print()
print(" Player 1")
print(" ________")
lines = [DICE_ART[i].splitlines() for i in player_1]
for l in zip(*lines):
print(*l, sep=' ')
print("")
print(" Player 2")
print(" ________")
lines = [DICE_ART[i].splitlines() for i in player_2]
for l in zip(*lines):
print(*l, sep=' ')
print()
print("*****************************")
print()
if player_1_score == player_2_score:
print(" Tie! Play again!")
elif player_1_score > player_2_score:
print(" Player 1 wins!")
else:
print(" Player 2 wins!")
print()
print("*****************************")
play = True
while play:
print("Player 1 is rolling...")
time.sleep(3)
roll_dice()
time.sleep(2)
roll_again("Player 1")
player_1 = roll[:]
player_1_score = winner(player_1, "player 1")
roll_statement(player_1, "Player 1")
time.sleep(3)
roll = []
print("Player 2 is rolling...")
time.sleep(3)
roll_dice()
time.sleep(2)
roll_again("player 2")
player_2 = roll[:]
player_2_score = winner(player_2, "Player 2")
#print("Player 2 rolled a: " + str(player_2) + "\n")
roll_statement(player_2, "Player 2")
score_board(player_1, player_2)
print("Would you like to play again?")
play_again = input().lower().startswith('y')
roll = []
if not play_again:
break