You are on page 1of 7

# Prolog:

# Name Jonathan cano


# section 012
# email Jonathan.cano@uky.edu
# Date 10/17/15
'''Purpose: to play the Chuck-A-Luck game with a user, letting
them bet on the outcome of 3 dice rolls
Play continues until the user quits or until they lose all their money
Pre-conditions: amount to bet, number to bet on, yes/no to continue
Post-conditions: displays rolls as dice, reports on win/loss,asks
'''

from graphics import *


from random import *

def draw_dice(x,y,num_die,win):
if num_die==1:
die="1.gif"
elif num_die==2:
die="2.gif"
elif num_die==3:
die="3.gif"
elif num_die==4:
die="4.gif"
elif num_die==5:
die="5.gif"
elif num_die==6:
die="6.gif"
die_image=Image(Point(x,y),die)
die_image.draw(win)

play again?

return(die_image)

def get_bet(amount,win):
bet_txt=Text(Point(250,75),"What do you want to bet?(1-"+str(amount)+")")
bet_txt.draw(win)
entry_bet=Entry(Point(250,100),5)
entry_bet.draw(win)
win.getMouse()
bet=int(entry_bet.getText())
while bet<1 or bet >amount:
if bet <1:
low_txt=Text(Point(250,125),"You have to bet at least 1 dollar")
low_txt.draw(win)
win.getMouse()
bet=int(entry_bet.getText())
low_txt.undraw()
elif bet>amount:
high_txt=Text(Point(250,125),"You do not have that much money!")
high_txt.draw(win)
win.getMouse()
bet=int(entry_bet.getText())
high_txt.undraw()
bet_txt.undraw()
entry_bet.undraw()
return(bet)

def get_number(win):
num_txt=Text(Point(250,75),"Which number to bet on?(1-6)")
num_txt.draw(win)

num_entry=Entry(Point(250,100),5)
num_entry.draw(win)
win.getMouse()
number=num_entry.getText()

while number<"1" or number>"6":


if number<"1" or number>"6":
wrong_num=Text(Point(250,125),"That is not a valid bet")
wrong_num.draw(win)
win.getMouse()
number=num_entry.getText()
wrong_num.undraw()
num_txt.undraw()
num_entry.undraw()
user_roll=int(number)
return user_roll

def check_matches(number,num_die,num_die2,num_die3):
counter=0
if number==num_die:
counter +=1
if number==num_die2:
counter +=1
if number==num_die3:
counter +=1
return counter

def in_box(p1x,p1y,p2x,p2y,p3_x,p3_y):
flag="false"

if (p1x<p3_x<p2x) and p2y<p3_y<p1y:


flag="true"
return flag

def play_again(win):
again_txt=Text(Point(250,75),"Do you want to play again? Y/N")
again_txt.draw(win)
no_box=Rectangle(Point(350,350),Point(400,300))
no_box.draw(win)
no_txt=Text(Point(370,330),"No")
no_txt.draw(win)
yes_box=Rectangle(Point(150,350),Point(200,300))
yes_box.draw(win)
yes_txt=Text(Point(175,330),"Yes")
yes_txt.draw(win)
p3=win.getMouse()
p3_x=p3.getX()
p3_y=p3.getY()

while in_box(350,350,400,300,p3_x,p3_y)!="true" and


in_box(150,350,200,300,p3_x,p3_y)!="true":
another_click=Text(Point(250,250),"not valid, click again")
another_click.draw(win)
p3=win.getMouse()
p3_x=p3.getX()
p3_y=p3.getY()
another_click.undraw()
if in_box(150,350,200,300,p3_x,p3_y)=="true":
play_flag="true"
else:

play_flag="false"
again_txt.undraw()
no_box.undraw()
yes_box.undraw()
no_txt.undraw()
yes_txt.undraw()
return play_flag

def main():
play_flag="true"
amount=100
win=GraphWin("Chuck-A-Luck",500,500)
txt=Text(Point(250,45),"Chuck-A-Luck")
txt.setSize(20)
txt.draw(win)
while play_flag=="true" and amount>0:
num_die=randrange(1,7)
num_die2=randrange(1,7)
num_die3=randrange(1,7)
bet=(get_bet(amount,win))
number=get_number(win)
die1=draw_dice(70,150,num_die,win)
die2=draw_dice(250,150,num_die2,win)
die3=draw_dice(420,150,num_die3,win)
matches=check_matches(number,num_die,num_die2,num_die3)

if matches==1:
winnings=bet
amount +=winnings

won_text=Text(Point(300,300),"You matched one, you win $"+str(winnings))


elif matches==2:
winnings=bet*5
amount +=winnings
won_text=Text(Point(300,300),"You matched two, you win $"+str(winnings))
elif matches==3:
winnings=bet*5
amount +=winnings
won_text=Text(Point(300,300),"You matched three, you win $"+str(winnings))
elif matches==0:
amount -=bet
won_text=Text(Point(300,300),"You matched none, you lose $"+str(bet))
won_text.draw(win)
adjusted_amount=Text(Point(300,350),"You have $"+str(amount))
adjusted_amount.draw(win)
win.getMouse()
die1.undraw()
die2.undraw()
die3.undraw()
won_text.undraw()
adjusted_amount.undraw()

if amount!=0:
play_flag=play_again(win)
if play_flag=="false" and amount>0:
leaving_txt=Text(Point(250,350),"You leave with $"+str(amount))
leaving_txt.draw(win)
win.getMouse()
win.close()
elif amount==0:

bankruptcy_txt=Text(Point(250,350),"Sorry you lost!")


bankruptcy_txt.draw(win)
win.getMouse()
win.close()

main()

You might also like