Skip to content

Instantly share code, notes, and snippets.

@paul-schwendenman
Forked from anonymous/Cee-Lo
Last active November 11, 2020 20:03
Show Gist options
  • Save paul-schwendenman/7d2c3238595cd19ece08 to your computer and use it in GitHub Desktop.
Save paul-schwendenman/7d2c3238595cd19ece08 to your computer and use it in GitHub Desktop.
Cee Lo dice game
'''
Cee-lo is a gambling game played with three six-sided dice.
'''
from __future__ import print_function
import random
from collections import Counter
def input_float(msg):
'''
This function will loop until user enters a valid number.
'''
while True:
try:
# Nonsense to make the code python3 compatible
try:
number = float(raw_input(msg))
except NameError:
number = float(input(msg))
except ValueError:
pass
else:
break
return number
def get_roll():
'''
This function will roll three dice and return the roll's value and a
message.
'''
roll = 0
msg = ''
# Roll until a valid roll
while roll == 0:
#Your 3 Dice Roll
dice = [
random.randint(1, 6),
random.randint(1, 6),
random.randint(1, 6),
]
msg += "This is the roll: {0} {1} {2}. ".format(*dice)
counter = Counter(dice).most_common()
# Rolling a 4 5 6 is an automatic win, the computer does not even
# roll if you roll this
if sorted(dice) == [4, 5, 6]:
roll = 100
msg += "{name} rolled a 4 5 6"
# Opposite of rolling a 4 5 6, rolling a 1 2 3 is an automatic loss,
# the computer does not roll
elif sorted(dice) == [1, 2, 3]:
roll = -100
msg += "{name} rolled a 1 2 3"
# Rolling three of a kind beats rolling any one integer, but three
# 6s beats three 5s
elif counter[0][1] == 3:
roll = (counter[0][0] * 11)
msg += "{{name}} rolled Trip {0}'s!!!".format(counter[0][0])
# The following 3 elif statements calculate the same thing. When two
# integers match, the unmatched integer is the score
elif counter[0][1] == 2:
roll = counter[1][0]
msg += "{{name}} rolled a {0}".format(roll)
# If none of the integers match, there is no score, reroll
else:
msg += "Roll Again\n"
return roll, msg
def main():
'''
The main function
'''
fbank = input_float("Enter your Deposit: $")
while fbank > 0:
print("Your bank is now: ${0}".format(fbank))
# Amount player is wagering
fbet = input_float("How much would you like to bet?: $")
player_roll, msg = get_roll()
print(msg.format(name='You'))
# The following occurs when the player rolled a 1 2 3 and
# automatically loses
if player_roll == -100:
computer_roll = 0
# The following occurs when the player rolled a 4 5 6 and
# automatically wins
elif player_roll == 100:
computer_roll = 0
# The following occurs when player rolls something else
elif player_roll >= 1 and player_roll <= 66:
computer_roll, msg = get_roll()
print(msg.format(name='Computer'))
if player_roll > computer_roll:
fbank += fbet
print("You win!")
elif player_roll < computer_roll:
fbank -= fbet
print("You lose!")
else:
print("You push, Re-roll")
print("You have no money in your bank, make a deposit to play")
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print("\nThanks for playing")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment