-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeWork_Task_4.1.py
167 lines (157 loc) · 6.37 KB
/
HomeWork_Task_4.1.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Card game 21
import random
import os
x = ['2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '10C',
'JC', 'QC', 'KC', 'AC',
'2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', '10D',
'JD', 'QD', 'KD', 'AD',
'2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', '10H',
'JH', 'QH', 'KH', 'AH',
'2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', '10S',
'JS', 'QS', 'KS', 'AS'
]
class Deck(object):
def __init__(self, deck, selected_cards_check=[]):
self.deck_cards = deck
self.selected_cards_check = selected_cards_check
self.x = x
def choose_cards(self):
x = 2
card = []
while x:
y = random.choice(self.deck_cards)
if self.selected_cards_check.count(y) == 0:
card.append(y)
self.selected_cards_check.append(y)
x -= 1
return card
def add_cards(self):
x = 1
while x:
card = random.choice(self.deck_cards)
if self.selected_cards_check.count(card) == 0:
self.selected_cards_check.append(card)
x -= 1
return card
def sum_cards(self):
x = 0
for i in self.deck_cards:
if i in ('2C', '2D', '2H', '2S', 'JC', 'JD', 'JH', 'JS'):
x += 2
if i in ('3C', '3D', '3H', '3S', 'QC', 'QD', 'QH', 'QS'):
x += 3
if i in ('4C', '4D', '4H', '4S', 'KC', 'KD', 'KH', 'KS'):
x += 4
if i in ('5C', '5D', '5H', '5S'):
x += 5
if i in ('6C', '6D', '6H', '6S'):
x += 6
if i in ('7C', '7D', '7H', '7S'):
x += 7
if i in ('8C', '8D', '8H', '8S'):
x += 8
if i in ('9C', '9D', '9H', '9S'):
x += 9
if i in ('10C', '10D', '10H', '10S'):
x += 10
if i in ('AC', 'AD', 'AH', 'AS'):
x += 11
return x
def main():
score = [0, 0]
choise = None
while choise != 2:
print('''
1 - Start Game.
2 - Exit.
''')
choise = int(input('Your choice : '))
if choise == 1:
PlayerCards = []
CompCards = []
PlayerCards = y.choose_cards()
CompCards = y.choose_cards()
sumP = Deck(PlayerCards)
sumC = Deck(CompCards)
os.system('cls')
print('Score player: ', score[0], 'comp: ', score[1])
print('\n\nYou have this cards : ', PlayerCards,
'\nHis sum = ', sumP.sum_cards())
Take_Card = input('\n\nTake more cards yes/no? : ')
while Take_Card == 'yes':
os.system('cls')
PlayerCards.append(y.add_cards())
sumP = Deck(PlayerCards)
print('Score player: ', score[0], 'comp: ', score[1])
print('\n\nYou have this cards : ', PlayerCards,
'\nHis sum = ', sumP.sum_cards())
if sumP.sum_cards() == 21:
score[0] += 1
os.system('cls')
print('\nYou WIN!')
print('\nScore player: ', score[0], 'comp: ', score[1])
break
if sumP.sum_cards() > 21:
score[1] += 1
os.system('cls')
print('\nYou loss!')
print('\nScore player: ', score[0], 'comp: ', score[1])
break
if sumC.sum_cards() < 19:
CompCards.append(y.add_cards())
sumC = Deck(CompCards)
if sumC.sum_cards() == 21:
score[1] += 1
os.system('cls')
print('\nComp WIN!')
print('\nComputer have this cards : ', CompCards)
print('\nScore player: ', score[0], 'comp: ', score[1])
break
if sumC.sum_cards() > 21:
score[0] += 1
os.system('cls')
print('\nComp loss!')
print('\nComputer have this cards : ', CompCards)
print('\nScore player: ', score[0], 'comp: ', score[1])
break
Take_Card = input('\n\nTake more cards yes/no? : ')
else:
while sumC.sum_cards() < 19:
CompCards.append(y.add_cards())
sumC = Deck(CompCards)
if sumC.sum_cards() == 21:
score[1] += 1
os.system('cls')
print('\nComp WIN!')
print('\nComputer have this cards : ', CompCards)
print('\nScore player: ', score[0], 'comp: ', score[1])
break
if sumC.sum_cards() > 21:
score[0] += 1
os.system('cls')
print('\nComp loss!')
print('\nComputer have this cards : ', CompCards)
print('\nScore player: ', score[0], 'comp: ', score[1])
break
if sumP.sum_cards() <= 21 and sumC.sum_cards() <= 21:
if sumP.sum_cards() > sumC.sum_cards():
score[0] += 1
os.system('cls')
print('\nYou Win')
print('\nScore player: ', score[0], 'comp: ', score[1])
elif sumP.sum_cards() < sumC.sum_cards():
score[1] += 1
os.system('cls')
print('\nComp Win')
print('\nComputer have this cards : ', CompCards)
print('\nScore player: ', score[0], 'comp: ', score[1])
elif sumP.sum_cards() == sumC.sum_cards():
os.system('cls')
print('\nDraw!!!')
if choise == 2:
os.system('cls')
print('Goodby!')
print('\a')
y = Deck(x)
main()
input('\nPress \'Enter\' for exit!')