Last active
May 21, 2020 13:18
-
-
Save nktnlx/ce0522ba4971b562d6bfbac3abc06193 to your computer and use it in GitHub Desktop.
Games of Chance [Codecademy project: https://www.codecademy.com/practice/projects/games-of-chance]
This file contains 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 | |
# Starting amount of money | |
money = 100 | |
# Flip a coin game | |
def coin_flip(choice, bet): | |
print("Welcome to the Flip a Coin Game!") | |
if bet > 0: | |
print("Your choice is " + choice + ", your bet is " + str(bet) + " USD.") | |
else: | |
print("Your bet should be above 0 USD.") | |
return 0 | |
win = 0 | |
coin = random.randint(0, 1) | |
if coin == 1: | |
print("We've flipped and it is Heads!") | |
else: | |
print("We've flipped and it is Tails") | |
# coin = 1 -- Heads | |
# coint = 0 -- Tails | |
if choice == "Heads" and coin == 1: | |
win += bet | |
print("Congrats! You've won " + str(win) + " USD.\n") | |
elif choice == "Tails" and coin == 0: | |
win += bet | |
print("Congrats! You've won " + str(win) + " USD.\n") | |
else: | |
win -= bet | |
print("You've lost " + str(bet) + " USD." + " Give it one more chance to win!\n") | |
return win | |
# Cho-Han game | |
def cho_han(choice, bet): | |
print("Welcome to the Cho-Han Game!") | |
if bet > 0: | |
print("Your choice is " + choice + ", your bet is " + str(bet) + " USD.") | |
else: | |
print("Your bet should be above 0 USD.") | |
return 0 | |
win = 0 | |
dice1 = random.randint(1, 6) | |
print("Dice #1 is " + str(dice1) + ".") | |
dice2 = random.randint(1, 6) | |
print("Dice #2 is " + str(dice2) + ".") | |
summ = dice1 + dice2 | |
print("The sum of two dices is " + str(summ) + ".") | |
if choice == "Even" and summ % 2 == 0: | |
win += bet | |
print("Congrats! You've won " + str(win) + " USD.\n") | |
elif choice == "Odd" and summ % 2 != 0: | |
win += bet | |
print("Congrats! You've won " + str(win) + " USD.\n") | |
else: | |
win -= bet | |
print("You've lost " + str(bet) + " USD." + " Give it one more chance to win!\n") | |
return win | |
# Cards Game | |
def cards(bet): | |
print("Welcome to the Cards Game!") | |
if bet > 0: | |
print("Your bet is " + str(bet) + " USD.") | |
else: | |
print("Your bet should be above 0 USD.") | |
return 0 | |
win = 0 | |
all_hearts = ["2H", "2D", "2C", "2S", "3H", "3D", "3C", "3S", "4H", "4D", "4C", "4S", "5H", "5D", "5C", "5S", "6H", "6D", "6C", "6S", "7H", "7D", "7C", "7S", "8H", "8D", "8C", "8S", "9H", "9D", "9C", "9S", "10H", "10D", "10C", "10S", "JH", "JD", "JC", "JS", "QH", "QD", "QC", "QS", "KH", "KD", "KC", "KS", "AH", "AD", "AC", "AS"] | |
hearts = ["2H", "2D", "2C", "2S", "3H", "3D", "3C", "3S", "4H", "4D", "4C", "4S", "5H", "5D", "5C", "5S", "6H", "6D", "6C", "6S", "7H", "7D", "7C", "7S", "8H", "8D", "8C", "8S", "9H", "9D", "9C", "9S", "10H", "10D", "10C", "10S", "JH", "JD", "JC", "JS", "QH", "QD", "QC", "QS", "KH", "KD", "KC", "KS", "AH", "AD", "AC", "AS"] | |
cards = [("2H", "2D", "2C", "2S"), ("3H", "3D", "3C", "3S"), ("4H", "4D", "4C", "4S"), ("5H", "5D", "5C", "5S"), ("6H", "6D", "6C", "6S"), ("7H", "7D", "7C", "7S"), ("8H", "8D", "8C", "8S"), ("9H", "9D", "9C", "9S"), ("10H", "10D", "10C", "10S"), ("JH", "JD", "JC", "JS"), ("QH", "QD", "QC", "QS"), ("KH", "KD", "KC", "KS"), ("AH", "AD", "AC", "AS")] | |
card1 = random.choice(hearts) | |
print("Your card is: " + card1 + ".") | |
hearts.remove(card1) | |
# print(hearts) | |
card2 = random.choice(hearts) | |
print("Your opponent card is: " + card2 + ".") | |
card1_indx = all_hearts.index(card1) | |
card2_indx = all_hearts.index(card2) | |
#print(card1_indx) | |
#print(card2_indx) | |
list_no_card1 = 0 | |
list_no_card2 = 0 | |
pos_card1 = 0 | |
pos_card2 = 0 | |
for list_no_card1 in range(0, len(cards)): | |
try: | |
pos_card1 = cards[list_no_card1].index(card1) | |
break | |
except: | |
pass | |
for list_no_card2 in range(0, len(cards)): | |
try: | |
pos_card2 = cards[list_no_card2].index(card2) | |
break | |
except: | |
pass | |
#print(list_no_card1) | |
#print(list_no_card2) | |
if list_no_card1 == list_no_card2: | |
print("It's a tie!\n") | |
elif card1_indx > card2_indx: | |
print("You won!\n") | |
win += bet | |
else: | |
print("You lost!\n") | |
win -= bet | |
return win | |
# Roulette Odd or Even Game | |
def roulette_1_to_1(choice, bet): | |
print("Welcome to the Roulette Odd or Even Game!") | |
if bet > 0: | |
print("Your choice is " + choice + ", your bet is " + str(bet) + " USD.") | |
else: | |
print("Your bet should be above 0 USD.") | |
return 0 | |
win = 0 | |
ball = random.randint(0, 36) | |
print("The ball is at " + str(ball) + ".") | |
if ball == 0: | |
win -= bet | |
print("You've lost " + str(bet) + " USD." + " Give it one more chance to win!\n") | |
elif choice == "Even" and ball % 2 == 0: | |
win += bet | |
print("Congrats! You've won " + str(win) + " USD.\n") | |
elif choice == "Odd" and ball % 2 != 0: | |
win += bet | |
print("Congrats! You've won " + str(win) + " USD.\n") | |
else: | |
win -= bet | |
print("You've lost " + str(bet) + " USD." + " Give it one more chance to win!\n") | |
return win | |
# Roulette Pick a number Game | |
def roulette(choice, bet): | |
print("Welcome to the Roulette Pick a Number Game!") | |
if bet > 0: | |
print("Your choice is " + str(choice) + ", your bet is " + str(bet) + " USD.") | |
else: | |
print("Your bet should be above 0 USD.") | |
return 0 | |
win = 0 | |
ball = random.randint(0, 36) | |
print("The ball is at " + str(ball) + ".") | |
if choice == ball: | |
win += bet * 35 | |
print("Congrats! You've won " + str(win) + " USD.\n") | |
else: | |
win -= bet | |
print("You've lost " + str(bet) + " USD." + " Give it one more chance to win!\n") | |
return win | |
#Call your game of chance functions here | |
# Flip a coin round | |
money += coin_flip("Heads", 10) | |
# Cho-Han round | |
money += cho_han("Even", 50) | |
# Card Game round | |
money += cards(20) | |
# Roulette Odd or Even Game | |
money += roulette_1_to_1("Even", 15) | |
# Roulette Pick a number Game | |
money += roulette(23, 10) | |
print("Your total amount of money after played rounds is " + str(money) + " USD.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment