Created
February 10, 2020 15:04
-
-
Save louishopgood/f2e2b84ad91c95515e5bab9950059c8a to your computer and use it in GitHub Desktop.
Simulation of a player playing games of chance one after the other whilst keeping a running total of his total cash
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 | |
money = 100 | |
#Write your game of chance functions here | |
def flip_coin_game(bet, choice): | |
#checking for errors and if they have enough money for their bet | |
if bet < 0: | |
print("Please bet a positive amount") | |
return 0 | |
if bet > money: | |
print("Sorry you don't have enough money for that bet!") | |
return 0 | |
#generates result of coin toss | |
number = random.randint(0,1) | |
if number == 1: | |
result = "Heads" | |
else: | |
result = "Tails" | |
print("Result is " + result + ", player guessed " + str(choice)) | |
#checks if the player was correct and returns cash outcome | |
if result == str(choice): | |
print("correct guess, player wins £" + str(bet)) | |
return bet | |
else: | |
print("incorrect guess, player loses £" + str(bet)) | |
return -bet | |
def play_cho_han(bet, prediction): | |
#checking for errors and if they have enough money for their bet | |
if bet < 0: | |
print("Please bet a positive amount") | |
return 0 | |
if bet > money: | |
print("Sorry you don't have enough money for that bet!") | |
return 0 | |
if prediction == "even" or prediction == "odd": | |
print("Please enter either Odd or Even, remembering to capitalise") | |
return 0 | |
#generates result of dice throws | |
dice1 = random.randint(1,6) | |
dice2 = random.randint(1,6) | |
total = dice1 + dice2 | |
print("Dice total is " + str(total)) | |
#checks if total of dice is even or odd | |
if 0 == total%2: | |
result = "Even" | |
else: | |
result = "Odd" | |
print("Result is " + result + ", player guessed " + str(prediction)) | |
#checks if the player was correct and returns cash outcome | |
if prediction == result: | |
print("Correct, player wins £"+str(bet)) | |
return bet | |
else: | |
print("Incorrect. player loses £" + str(bet)) | |
return -bet | |
def higher_card_wins(bet): | |
#checking for errors and if they have enough money for their bet | |
if bet < 0: | |
print("Please bet a positive amount") | |
return 0 | |
if bet > money: | |
print("Sorry you don't have enough money for that bet!") | |
return 0 | |
#creating list for card values to choose from | |
available_cards = list(range(1,14))*4 | |
available_cards.sort() | |
#un-comment blow line to also print all the available cards to check it works | |
#print(available_cards) | |
#select first card from available cards | |
card1 = available_cards[random.randint(0,len(available_cards)-1)] | |
print("Player's card is "+str(card1)) | |
#removes first instance of the already drawn card from available cards | |
available_cards.remove(card1) | |
#un-comment below line to also print all the available cards to check it works | |
#print(available_cards) | |
#selects second card from new available cards | |
card2 = available_cards[random.randint(0,len(available_cards)-1)] | |
print("Opponents card is "+str(card2)) | |
#checks if player has won and returns cash outcome | |
if card1 > card2: | |
print("Player wins £"+str(bet)+" with "+str(card1)+" vs "+str(card2)) | |
return bet | |
if card1 < card2: | |
print("Player loses £"+str(bet)+" with "+str(card1)+" vs "+str(card2)) | |
return -bet | |
else: | |
print("It is a draw!") | |
return 0 | |
def french_roulette(bet, prediction): | |
#checking for errors and if they have enough money for their bet | |
if bet < 0: | |
print("Please bet a positive amount") | |
return 0 | |
if bet > money: | |
print("Sorry you don't have enough money for that bet!") | |
return 0 | |
#generates the result. 0 is 0 (duh), 1 - 36 are themselves | |
result = random.randint(0,36) | |
print("Player guessed " + str(prediction) + ", Ball landed on " + str(result)) | |
#checks if player was right and returns the cash result | |
if (prediction == "Odd" or prediction == "odd") and (result%2 != 0 and result !=0): | |
print("Player was right and wins £"+str(bet)) | |
return bet | |
if (prediction == "Even" or prediction == "even") and (result%2 == 0 and result !=0): | |
print("Player was right and wins £"+str(bet)) | |
return bet | |
if prediction == result: | |
print("Player was correct and wins £" + str(bet*35)) | |
return bet*35 | |
else: | |
print("Player was incorrect and loses £" + str(bet)) | |
return -bet | |
#Call your game of chance functions here | |
money += flip_coin_game(50,"Heads") | |
print("New total cash is £" + str(money)) | |
money += play_cho_han(50,"Even") | |
print("New total cash is £" + str(money)) | |
money += higher_card_wins(50) | |
print("New total cash is £" + str(money)) | |
money += french_roulette(50,"odd") | |
print("New total cash is £" + str(money)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment