-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctd 5.py
112 lines (112 loc) · 3.8 KB
/
ctd 5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import random
import cv2
def play(bag1,bag2,bag3):
user=input("Enter bag number: ")
while 1:
if user=='1':
if bag1==0:
print("This bag is empty")
else:
ball_number= input("Enter the number of balls from 1 to 5: ")
bag1=take_value(bag1,ball_number)
break
elif user=='2':
if bag2==0:
print("This bag is empty")
else:
ball_number= input("Enter the number of balls from 1 to 5: ")
bag2=take_value(bag2,ball_number)
break
elif user=='3':
if bag3==0:
print("This bag is empty")
else:
ball_number= input("Enter the number of balls from 1 to 5: ")
bag3=take_value(bag3,ball_number)
break
else:
print("please choose one of the three bags!!!")
user=input("Enter a valid bag: ")
return bag1, bag2, bag3
def computer(bag1,bag2,bag3):
while 1:
bag=None
bags=['1','2','3']
bag=random.choice(bags)
if (bag=='1' and bag1!=0) or (bag3==0 and bag2==0):
if bag1<=5:
ball_number=bag1
else:
ball_number= random.randint(1,5)
bag1=take_value(bag1,ball_number)
break
elif (bag=='2' and bag2!=0) or (bag1==0 and bag3==0):
if bag2<=6:
ball_number=bag2
else:
ball_number= random.randint(1,5)
bag2=take_value(bag2,ball_number)
break
elif (bag=='3' and bag3!=0) or (bag1==0 and bag2==0):
if bag3<=5:
ball_number=bag3
else:
ball_number=random.randint(1,5)
bag3=take_value(bag3,ball_number)
break
return bag1, bag2, bag3
def take_value(bag,ball_number):
while 1:
try:
ball_number=int(ball_number)
bag = bag-ball_number
if int(ball_number)<1 or int(ball_number)>5 :
bag=bag+ball_number
ball_number= input("RE-enter a number from 1 to 5: ")
elif bag<0:
bag=bag+ball_number
print("You have excided the limit of the bag!")
ball_number= input("RE-enter a number from 1 to the limit: ")
else:
break
except ValueError:
ball_number= input("RE-enter a number from 1 to 5: ")
except TypeError:
ball_number= input("RE-enter a number from 1 to 5: ")
return bag
def display(bag1,bag2,bag3):
print(str(bag1) + " ball remain in the bag_1")
print(str(bag2) + " ball remain in the bag_2")
print(str(bag3) + " ball remain in the bag_3")
print("")
def check_win(bag1,bag2,bag3,c):
if bag1==bag2==bag3==0:
c=1
return c
def game(bag1,bag2,bag3,c):
while bag1>0 or bag2>0 or bag3>0:
bag1, bag2, bag3=play(bag1,bag2,bag3)
display(bag1,bag2,bag3)
c=check_win(bag1,bag2,bag3,c)
if c==1:
window_name= 'image'
image=cv2.imread("win.jpg")
cv2.imshow(window_name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print("You won")
exit()
bag1, bag2, bag3=computer(bag1,bag2,bag3)
display(bag1,bag2,bag3)
c=check_win(bag1,bag2,bag3,c)
if c==1:
window_name= 'image'
image=cv2.imread("lose.jpg")
cv2.imshow(window_name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print("You lose")
exit()
bag1=bag2=bag3=10
c=0
game(bag1,bag2,bag3,c)