Skip to content

Instantly share code, notes, and snippets.

@cheny0y0
Last active July 17, 2022 09:45
Show Gist options
  • Save cheny0y0/30c06de1f512e1eb5f88269f8ac33c75 to your computer and use it in GitHub Desktop.
Save cheny0y0/30c06de1f512e1eb5f88269f8ac33c75 to your computer and use it in GitHub Desktop.
Python Card Games Collection

Python Card Games Collection

Progress of it

Finished: Solitaire(sol.py, for Python 1.5+), Hearts(hearts.py, for Python 1.5+)

In-developing: Spider Solitaire(spisol.py, for Python 1.5+)

Preparing: 24 Points(24points.py, for Python 1.5+), Put It Back(putitbk.py, for Python 2.4+)

Notes

All of them are only in English.

#!/usr/bin/env python
# coding=UTF-8
try :
input = raw_input
range = xrange
except NameError :
pass
import sys
try :
import random
except ImportError :
try :
import whrandom
random = whrandom
except ImportError :
try :
import rand
random = rand
except ImportError :
print("Cannot import any standard random modules!")
sys.exit(1)
A = 14 # it is not 1 here
J = 11
Q = 12
K = 13
def strip(source, chars="\t\n\x0b\x0c\r ") : # to solve the problem of Python
# 2.0 or earlier
res = source
while res[0:1] in tuple(chars) :
res = res[1:len(res)]
while res[-1:len(res)] in tuple(chars) :
res = res[0:-1]
return res
south_scores = ()
west_scores = ()
north_scores = ()
east_scores = ()
def sum(tup) :
r = 0
for i in tup :
r = r + i
return r
def rjust(source, width, fillchar=" ") :
while len(source) < width :
source = fillchar + source
return source
def compare(tup) :
maxa = tup[0]
for i in tup :
if i[0] == maxa[0] and i[1] > maxa[1] :
maxa = i
return maxa
while 1 :
south_score = 0
west_score = 0
north_score = 0
east_score = 0
heart_put = 0 # False
# 3 is the heart; 2 is the spade; 1 is the club
cards = ((0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9),
(0, 10), (0, J), (0, Q), (0, K), (0, A),
(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9),
(1, 10), (1, J), (1, Q), (1, K), (1, A),
(2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9),
(2, 10), (2, J), (2, Q), (2, K), (2, A),
(3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9),
(3, 10), (3, J), (3, Q), (3, K), (3, A))
first_turn = "e"
south_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "s"
south_hand = south_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
west_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "w"
west_hand = west_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
north_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "n"
north_hand = north_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
east_hand = cards
for i in range(13) :
cards = ()
south_put = None
west_put = None
north_put = None
east_put = None
j = 0
while j < 4 :
showc = \
"Hearts - North:%s/%s East:%s/%s South:%s/%s West:%s/%s\n" % \
(north_score, sum(north_scores), east_score, sum(east_scores),
south_score, sum(south_scores), west_score, sum(west_scores))
if first_turn == "n" :
showc = showc + (" "*(13-len(north_hand)))
for k in range(len(north_hand), 0, -1) :
showc = showc + ("%Xv "%k)
showc = showc + "\n" + (" "*(13-len(north_hand)))
for k in range(len(north_hand)-1, -1, -1) :
showc = showc + "DCSH"[north_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[north_hand[k][1]-2] + " "
else :
showc = showc + "\n" + (" "*(13-len(north_hand)))
for k in range(len(north_hand)-1, -1, -1) :
showc = showc + "??? "
if north_put is None :
showc = showc + "\n\n"
else :
showc = showc + "\n " + \
"DCSH"[north_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[north_put[1]-2] + "\n"
for k in range(6) :
try :
west_hand[k]
if first_turn == "w" :
showc = showc + ("%X>"%(k+1)) + \
"DCSH"[west_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[west_hand[k][1]-2]
else :
showc = showc + " ???"
except IndexError :
showc = showc + " "
showc = showc + " "
try :
east_hand[12-k]
if first_turn == "e" :
showc = showc + "DCSH"[east_hand[12-k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[east_hand[12-k][1]-2] + ("<%X\n"%(13-k))
else :
showc = showc + "???\n"
except IndexError :
showc = showc + "\n"
try :
west_hand[6]
if first_turn == "w" :
showc = showc + "7>" + "DCSH"[west_hand[6][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ", "A ")[west_hand[6][1]-2]
else :
showc = showc + " ???"
except IndexError :
showc = showc + " "
if west_put is None :
showc = showc + " "
else :
showc = showc + " " + "DCSH"[west_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[west_put[1]-2]
showc = showc + " "
if east_put is None :
showc = showc + " "
else :
showc = showc + "DCSH"[east_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[east_put[1]-2] + " "
try :
east_hand[6]
if first_turn == "e" :
showc = showc + "DCSH"[east_hand[6][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[east_hand[6][1]-2] + "<7\n"
else :
showc = showc + "???\n"
except IndexError :
showc = showc + "\n"
for k in range(7, 13) :
try :
west_hand[k]
if first_turn == "w" :
showc = showc + ("%X>"%(k+1)) + \
"DCSH"[west_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[west_hand[k][1]-2]
else :
showc = showc + " ???"
except IndexError :
showc = showc + " "
showc = showc + " "
try :
east_hand[12-k]
if first_turn == "e" :
showc = showc + "DCSH"[east_hand[12-k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[east_hand[12-k][1]-2] + ("<%X\n"%(13-k))
else :
showc = showc + "???\n"
except IndexError :
showc = showc + "\n"
if south_put is None :
showc = showc + "\n"
else :
showc = showc + " " + \
"DCSH"[south_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[south_put[1]-2] + "\n"
if first_turn == "s" :
for k in range(len(south_hand)) :
showc = showc + "DCSH"[south_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[south_hand[k][1]-2] + " "
showc = showc + "\n"
for k in range(1, 1+len(south_hand)) :
showc = showc + ("%X^ "%k)
else :
for k in range(len(south_hand)) :
showc = showc + "??? "
showc = showc + "\n"
print(showc)
putid = strip(input("Please input the card # to put it:"))
if strip(putid, "1") == "" :
putid = 0
elif strip(putid, "2") == "" :
putid = 1
elif strip(putid, "3") == "" :
putid = 2
elif strip(putid, "4") == "" :
putid = 3
elif strip(putid, "5") == "" :
putid = 4
elif strip(putid, "6") == "" :
putid = 5
elif strip(putid, "7") == "" :
putid = 6
elif strip(putid, "8") == "" :
putid = 7
elif strip(putid, "9") == "" :
putid = 8
elif strip(putid, "Aa") == "" :
putid = 9
elif strip(putid, "Bb") == "" :
putid = 10
elif strip(putid, "Cc") == "" :
putid = 11
elif strip(putid, "Dd") == "" :
putid = 12
else :
print("Invalid #")
continue
if first_turn == "s" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and south_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if south_hand[putid][0] != 3 or \
(heart_put and south_hand[putid][0] == 3) :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
else :
for k in south_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
if south_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == south_hand[putid][0] :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
else :
for k in south_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
if south_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
elif first_turn == "w" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and west_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if west_hand[putid][0] != 3 or \
(heart_put and west_hand[putid][0] == 3) :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
else :
for k in west_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
if west_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == west_hand[putid][0] :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
else :
for k in west_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
if west_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
elif first_turn == "n" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and north_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if north_hand[putid][0] != 3 or \
(heart_put and north_hand[putid][0] == 3) :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
else :
for k in north_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
if north_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == north_hand[putid][0] :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
else :
for k in north_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
if north_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
elif first_turn == "e" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and east_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if east_hand[putid][0] != 3 or \
(heart_put and east_hand[putid][0] == 3) :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
else :
for k in east_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
if east_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == east_hand[putid][0] :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
else :
for k in east_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
if east_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
showc = " " + "DCSH"[north_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[north_put[1]-2] + "\n\n\n\n\n\n\n " + \
"DCSH"[west_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[west_put[1]-2] + \
" " + "DCSH"[east_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[east_put[1]-2] + \
"\n\n\n\n\n\n\n " + \
"DCSH"[south_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[south_put[1]-2]
print(showc)
biggest = compare(cards)
if biggest == south_put :
for i in cards :
if i[0] == 3 :
south_score = south_score + 1
elif i == (2, Q) :
south_score = south_score + 13
first_turn = "s"
input("South side is the biggest. Press enter to continue ")
elif biggest == west_put :
for i in cards :
if i[0] == 3 :
west_score = west_score + 1
elif i == (2, Q) :
west_score = west_score + 13
first_turn = "w"
input("West side is the biggest. Press enter to continue ")
elif biggest == north_put :
for i in cards :
if i[0] == 3 :
north_score = north_score + 1
elif i == (2, Q) :
north_score = north_score + 13
first_turn = "n"
input("North side is the biggest. Press enter to continue ")
elif biggest == east_put :
for i in cards :
if i[0] == 3 :
east_score = east_score + 1
elif i == (2, Q) :
east_score = east_score + 13
first_turn = "e"
input("East side is the biggest. Press enter to continue ")
south_scores = south_scores + (south_score,)
west_scores = west_scores + (west_score,)
north_scores = north_scores + (north_score,)
east_scores = east_scores + (east_score,)
south_scores_len = len(str(sum(south_scores)))
if south_scores_len < 5 :
south_scores_len = 5
west_scores_len = len(str(sum(west_scores)))
if west_scores_len < 4 :
west_scores_len = 4
north_scores_len = len(str(sum(north_scores)))
if north_scores_len < 5 :
north_scores_len = 5
east_scores_len = len(str(sum(east_scores)))
if east_scores_len < 4 :
east_scores_len = 4
print(rjust("South", south_scores_len)+" "+rjust("West", west_scores_len)+\
" "+rjust("North", north_scores_len)+" "+\
rjust("East", east_scores_len)+" No.")
for i in range(len(south_scores)) :
print(rjust(str(south_scores[i]), south_scores_len)+" "+\
rjust(str(west_scores[i]), west_scores_len)+" "+\
rjust(str(north_scores[i]), north_scores_len)+" "+\
rjust(str(east_scores[i]), east_scores_len)+" "+str(i+1))
print("-"*(south_scores_len+west_scores_len+north_scores_len+\
east_scores_len+3))
print(rjust(str(sum(south_scores)), south_scores_len)+" "+\
rjust(str(sum(west_scores)), west_scores_len)+" "+\
rjust(str(sum(north_scores)), north_scores_len)+" "+\
rjust(str(sum(east_scores)), east_scores_len)+" Total")
input("Press enter to continue ")
#!/usr/bin/env python
# coding=UTF-8
try :
input = raw_input
range = xrange
except NameError :
pass
import sys
try :
import random
except ImportError :
try :
import whrandom
random = whrandom
except ImportError :
try :
import rand
random = rand
except ImportError :
print("Cannot import any standard random modules!")
sys.exit(1)
A = 14 # it is not 1 here
J = 11
Q = 12
K = 13
def strip(source, chars="\t\n\x0b\x0c\r ") : # to solve the problem of Python
# 2.0 or earlier
res = source
while res[0:1] in tuple(chars) :
res = res[1:len(res)]
while res[-1:len(res)] in tuple(chars) :
res = res[0:-1]
return res
south_scores = ()
west_scores = ()
north_scores = ()
east_scores = ()
def sum(tup) :
r = 0
for i in tup :
r = r + i
return r
def rjust(source, width, fillchar=" ") :
while len(source) < width :
source = fillchar + source
return source
def compare(tup) :
maxa = tup[0]
for i in tup :
if i[0] == maxa[0] and i[1] > maxa[1] :
maxa = i
return maxa
class Heartsai :
def pick(self, hand, put, heart=1) :
for i in range(len(hand)) :
if hand[i] == (1, 2) :
return "123456789abcd"[i]
res = 0
if put == () :
outheart = 0
for i in hand :
if i[0] != 3 :
outheart = 1
if heart or not outheart :
for i in range(len(hand)) :
if hand[i][1] < hand[res][1] :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] < hand[res][0] :
res = i
else :
for i in range(len(hand)) :
if hand[res][0] == 3 :
res = i
if hand[i][1] < hand[res][1] and hand[i][0] != 3 :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] < hand[res][0] and hand[i][0] != 3 :
res = i
else :
outheart = 0
for i in hand :
if i[0] == put[0][0] :
outheart = 1
if not outheart :
for i in range(len(hand)) :
if hand[i][1] > hand[res][1] :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] > hand[res][0] :
res = i
else :
for i in range(len(hand)) :
if hand[res][0] != put[0][0] :
res = i
if hand[i][1] < hand[res][1] and hand[i][0] == put[0][0] :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] < hand[res][0] and \
hand[i][0] == put[0][0] :
res = i
return "123456789abcd"[res]
heartsai = Heartsai()
while 1 :
south_score = 0
west_score = 0
north_score = 0
east_score = 0
heart_put = 0 # False
# 3 is the heart; 2 is the spade; 1 is the club
cards = ((0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9),
(0, 10), (0, J), (0, Q), (0, K), (0, A),
(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9),
(1, 10), (1, J), (1, Q), (1, K), (1, A),
(2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9),
(2, 10), (2, J), (2, Q), (2, K), (2, A),
(3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9),
(3, 10), (3, J), (3, Q), (3, K), (3, A))
first_turn = "e"
south_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "s"
south_hand = south_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
west_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "w"
west_hand = west_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
north_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "n"
north_hand = north_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
east_hand = cards
for i in range(13) :
cards = ()
south_put = None
west_put = None
north_put = None
east_put = None
j = 0
while j < 4 :
showc = \
"Hearts - North:%s/%s East:%s/%s You:%s/%s West:%s/%s\n" % \
(north_score, sum(north_scores), east_score, sum(east_scores),
south_score, sum(south_scores), west_score, sum(west_scores))
if first_turn == "n" :
showc = showc + (" "*(13-len(north_hand)))
for k in range(len(north_hand), 0, -1) :
showc = showc + ("%Xv "%k)
showc = showc + "\n" + (" "*(13-len(north_hand)))
for k in range(len(north_hand)-1, -1, -1) :
showc = showc + "DCSH"[north_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[north_hand[k][1]-2] + " "
else :
showc = showc + "\n" + (" "*(13-len(north_hand)))
for k in range(len(north_hand)-1, -1, -1) :
showc = showc + "??? "
if north_put is None :
showc = showc + "\n\n"
else :
showc = showc + "\n " + \
"DCSH"[north_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[north_put[1]-2] + "\n"
for k in range(6) :
try :
west_hand[k]
if first_turn == "w" :
showc = showc + ("%X>"%(k+1)) + \
"DCSH"[west_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[west_hand[k][1]-2]
else :
showc = showc + " ???"
except IndexError :
showc = showc + " "
showc = showc + " "
try :
east_hand[12-k]
if first_turn == "e" :
showc = showc + "DCSH"[east_hand[12-k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[east_hand[12-k][1]-2] + ("<%X\n"%(13-k))
else :
showc = showc + "???\n"
except IndexError :
showc = showc + "\n"
try :
west_hand[6]
if first_turn == "w" :
showc = showc + "7>" + "DCSH"[west_hand[6][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ", "A ")[west_hand[6][1]-2]
else :
showc = showc + " ???"
except IndexError :
showc = showc + " "
if west_put is None :
showc = showc + " "
else :
showc = showc + " " + "DCSH"[west_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[west_put[1]-2]
showc = showc + " "
if east_put is None :
showc = showc + " "
else :
showc = showc + "DCSH"[east_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[east_put[1]-2] + " "
try :
east_hand[6]
if first_turn == "e" :
showc = showc + "DCSH"[east_hand[6][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[east_hand[6][1]-2] + "<7\n"
else :
showc = showc + "???\n"
except IndexError :
showc = showc + "\n"
for k in range(7, 13) :
try :
west_hand[k]
if first_turn == "w" :
showc = showc + ("%X>"%(k+1)) + \
"DCSH"[west_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[west_hand[k][1]-2]
else :
showc = showc + " ???"
except IndexError :
showc = showc + " "
showc = showc + " "
try :
east_hand[12-k]
if first_turn == "e" :
showc = showc + "DCSH"[east_hand[12-k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[east_hand[12-k][1]-2] + ("<%X\n"%(13-k))
else :
showc = showc + "???\n"
except IndexError :
showc = showc + "\n"
if south_put is None :
showc = showc + "\n"
else :
showc = showc + " " + \
"DCSH"[south_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10",
"J ", "Q ", "K ", "A ")[south_put[1]-2] + "\n"
if first_turn == "s" :
for k in range(len(south_hand)) :
showc = showc + "DCSH"[south_hand[k][0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ",
"A ")[south_hand[k][1]-2] + " "
showc = showc + "\n"
for k in range(1, 1+len(south_hand)) :
showc = showc + ("%X^ "%k)
else :
for k in range(len(south_hand)) :
showc = showc + "??? "
showc = showc + "\n"
print(showc)
if first_turn == "s" :
putid = strip(input("Please input the card # to put it:"))
elif first_turn == "w" :
putid = heartsai.pick(west_hand, cards, heart_put)
elif first_turn == "n" :
putid = heartsai.pick(north_hand, cards, heart_put)
elif first_turn == "e" :
putid = heartsai.pick(east_hand, cards, heart_put)
if strip(putid, "1") == "" :
putid = 0
elif strip(putid, "2") == "" :
putid = 1
elif strip(putid, "3") == "" :
putid = 2
elif strip(putid, "4") == "" :
putid = 3
elif strip(putid, "5") == "" :
putid = 4
elif strip(putid, "6") == "" :
putid = 5
elif strip(putid, "7") == "" :
putid = 6
elif strip(putid, "8") == "" :
putid = 7
elif strip(putid, "9") == "" :
putid = 8
elif strip(putid, "Aa") == "" :
putid = 9
elif strip(putid, "Bb") == "" :
putid = 10
elif strip(putid, "Cc") == "" :
putid = 11
elif strip(putid, "Dd") == "" :
putid = 12
else :
print("Invalid #")
continue
if first_turn == "s" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and south_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if south_hand[putid][0] != 3 or \
(heart_put and south_hand[putid][0] == 3) :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
else :
for k in south_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
if south_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == south_hand[putid][0] :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
else :
for k in south_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
j = j + 1
if south_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
elif first_turn == "w" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and west_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if west_hand[putid][0] != 3 or \
(heart_put and west_hand[putid][0] == 3) :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
else :
for k in west_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
if west_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == west_hand[putid][0] :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
else :
for k in west_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
j = j + 1
if west_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
elif first_turn == "n" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and north_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if north_hand[putid][0] != 3 or \
(heart_put and north_hand[putid][0] == 3) :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
else :
for k in north_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
if north_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == north_hand[putid][0] :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
else :
for k in north_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
j = j + 1
if north_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
elif first_turn == "e" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and east_hand[putid] != (1, 2) :
print("You must put Club 2 at the beginning")
elif cards == () :
if east_hand[putid][0] != 3 or \
(heart_put and east_hand[putid][0] == 3) :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
else :
for k in east_hand :
if k[0] != 3 :
print("Cannot put a heart unless it has alr\
eady been put in other")
break
else :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
if east_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == east_hand[putid][0] :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
else :
for k in east_hand :
if k[0] == cards[0][0] :
print("Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
j = j + 1
if east_put[0] == 3 :
heart_put = 1 # True
except IndexError :
print("Invalid #")
showc = " " + "DCSH"[north_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[north_put[1]-2] + "\n\n\n\n\n\n\n " + \
"DCSH"[west_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[west_put[1]-2] + \
" " + "DCSH"[east_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[east_put[1]-2] + \
"\n\n\n\n\n\n\n " + \
"DCSH"[south_put[0]] + \
("2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ",
"Q ", "K ", "A ")[south_put[1]-2]
print(showc)
biggest = compare(cards)
if biggest == south_put :
for i in cards :
if i[0] == 3 :
south_score = south_score + 1
elif i == (2, Q) :
south_score = south_score + 13
first_turn = "s"
input("Your side is the biggest. Press enter to continue ")
elif biggest == west_put :
for i in cards :
if i[0] == 3 :
west_score = west_score + 1
elif i == (2, Q) :
west_score = west_score + 13
first_turn = "w"
input("West side is the biggest. Press enter to continue ")
elif biggest == north_put :
for i in cards :
if i[0] == 3 :
north_score = north_score + 1
elif i == (2, Q) :
north_score = north_score + 13
first_turn = "n"
input("North side is the biggest. Press enter to continue ")
elif biggest == east_put :
for i in cards :
if i[0] == 3 :
east_score = east_score + 1
elif i == (2, Q) :
east_score = east_score + 13
first_turn = "e"
input("East side is the biggest. Press enter to continue ")
south_scores = south_scores + (south_score,)
west_scores = west_scores + (west_score,)
north_scores = north_scores + (north_score,)
east_scores = east_scores + (east_score,)
south_scores_len = len(str(sum(south_scores)))
if south_scores_len < 3 :
south_scores_len = 3
west_scores_len = len(str(sum(west_scores)))
if west_scores_len < 4 :
west_scores_len = 4
north_scores_len = len(str(sum(north_scores)))
if north_scores_len < 5 :
north_scores_len = 5
east_scores_len = len(str(sum(east_scores)))
if east_scores_len < 4 :
east_scores_len = 4
print(rjust("You", south_scores_len)+" "+rjust("West", west_scores_len)+\
" "+rjust("North", north_scores_len)+" "+\
rjust("East", east_scores_len)+" No.")
for i in range(len(south_scores)) :
print(rjust(str(south_scores[i]), south_scores_len)+" "+\
rjust(str(west_scores[i]), west_scores_len)+" "+\
rjust(str(north_scores[i]), north_scores_len)+" "+\
rjust(str(east_scores[i]), east_scores_len)+" "+str(i+1))
print("-"*(south_scores_len+west_scores_len+north_scores_len+\
east_scores_len+3))
print(rjust(str(sum(south_scores)), south_scores_len)+" "+\
rjust(str(sum(west_scores)), west_scores_len)+" "+\
rjust(str(sum(north_scores)), north_scores_len)+" "+\
rjust(str(sum(east_scores)), east_scores_len)+" Total")
input("Press enter to continue ")
#!/usr/bin/env python3
# coding=UTF-8
try :
input = raw_input
range = xrange
except NameError :
pass
import sys, time
try :
import random
except ImportError :
try :
import whrandom as random
except ImportError :
try :
import rand as random
except ImportError :
print("Cannot import any standard random modules!")
sys.exit(1)
try :
import tkinter as tk
import tkinter.messagebox as msgbox
except ImportError :
print("Tcl/Tk module hasn't been installed correctly!")
sys.exit(1)
A = 14 # it is not 1 here
J = 11
Q = 12
K = 13
def strip(source, chars="\t\n\x0b\x0c\r ") : # to solve the problem of Python
# 2.0 or earlier
res = source
while res[0:1] in tuple(chars) :
res = res[1:len(res)]
while res[-1:len(res)] in tuple(chars) :
res = res[0:-1]
return res
south_scores = ()
west_scores = ()
north_scores = ()
east_scores = ()
def sum(tup) :
r = 0
for i in tup :
r = r + i
return r
def rjust(source, width, fillchar=" ") :
while len(source) < width :
source = fillchar + source
return source
def compare(tup) :
maxa = tup[0]
for i in tup :
if i[0] == maxa[0] and i[1] > maxa[1] :
maxa = i
return maxa
class Heartsai :
def pick(self, hand, put, heart=1) :
for i in range(len(hand)) :
if hand[i] == (1, 2) :
return "123456789abcd"[i]
res = 0
if put == () :
outheart = 0
for i in hand :
if i[0] != 3 :
outheart = 1
if heart or not outheart :
for i in range(len(hand)) :
if hand[i][1] < hand[res][1] :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] < hand[res][0] :
res = i
else :
for i in range(len(hand)) :
if hand[res][0] == 3 :
res = i
if hand[i][1] < hand[res][1] and hand[i][0] != 3 :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] < hand[res][0] and hand[i][0] != 3 :
res = i
else :
outheart = 0
for i in hand :
if i[0] == put[0][0] :
outheart = 1
if not outheart :
for i in range(len(hand)) :
if hand[i][1] > hand[res][1] :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] > hand[res][0] :
res = i
else :
for i in range(len(hand)) :
if hand[res][0] != put[0][0] :
res = i
if hand[i][1] < hand[res][1] and hand[i][0] == put[0][0] :
res = i
elif hand[i][1] == hand[res][1] \
and hand[i][0] < hand[res][0] and \
hand[i][0] == put[0][0] :
res = i
return "123456789abcd"[res]
heartsai = Heartsai()
mainWin = tk.Tk()
showd = tk.Label(mainWin)
showd.pack()
buttons = (
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=0;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=1;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=2;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=3;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=4;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=5;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=6;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=7;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=8;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=9;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=10;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=11;put();puts()")),
tk.Button(mainWin, command=lambda: exec("global putid,aput;putid=aput=12;put();puts()")))
for i in buttons :
i.pack(side="left")
def getshowc() :
showc = ""
if north_put is None :
showc += "\n"
else :
showc += "♦♣♠♥"[north_put[0]] + \
("2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A")[north_put[1]-2] + "\n"
showc += "\n\n\n\n\n\n"
if west_put is None :
showc += " "
else :
showc += "♦♣♠♥"[west_put[0]] + \
("2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A")[west_put[1]-2]
showc += " "
if east_put is None :
showc += " "
else :
showc += "♦♣♠♥"[east_put[0]] + \
("2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A")[east_put[1]-2] + " "
showc += "\n\n\n\n\n\n"
if south_put is None :
showc += "\n"
else :
showc += "♦♣♠♥"[south_put[0]] + \
("2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A")[south_put[1]-2] + "\n"
return showc
def newcards() :
global south_score, west_score, north_score, east_score, heart_put, \
first_turn, south_hand, west_hand, north_hand, east_hand
south_score = 0
west_score = 0
north_score = 0
east_score = 0
heart_put = 0 # False
# 3 is the heart; 2 is the spade; 1 is the club
cards = ((0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9),
(0, 10), (0, J), (0, Q), (0, K), (0, A),
(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9),
(1, 10), (1, J), (1, Q), (1, K), (1, A),
(2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9),
(2, 10), (2, J), (2, Q), (2, K), (2, A),
(3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9),
(3, 10), (3, J), (3, Q), (3, K), (3, A))
first_turn = "e"
south_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "s"
south_hand = south_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
west_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "w"
west_hand = west_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
north_hand = ()
for i in range(13) :
x = random.randint(0,len(cards)-1)
if cards[x] == (1, 2) :
first_turn = "n"
north_hand = north_hand + (cards[x],)
cards = cards[:x] + cards[x+1:]
east_hand = cards
def put() :
global first_turn, cards, south_hand, south_put, west_hand, west_put, \
north_hand, north_put, east_hand, east_put, heart_put
for i in buttons :
i["state"] = "disabled"
if first_turn == "s" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and south_hand[putid] != (1, 2) :
msgbox.showinfo("Note", "You must put Club 2 at the beginning")
for i in range(13) :
try :
south_hand[i]
buttons[i]["state"] = "normal"
except IndexError :
buttons[i]["state"] = "disabled"
elif cards == () :
if south_hand[putid][0] != 3 or \
(heart_put and south_hand[putid][0] == 3) :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
else :
for k in south_hand :
if k[0] != 3 :
msgbox.showinfo("Note", "Cannot put a heart unless it has alr\
eady been put in other")
for i in range(13) :
try :
south_hand[i]
buttons[i]["state"] = "normal"
except IndexError :
buttons[i]["state"] = "disabled"
break
else :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
if south_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == south_hand[putid][0] :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
else :
for k in south_hand :
if k[0] == cards[0][0] :
msgbox.showinfo("Note", "Cannot put a different pattern unless yo\
u do not have any of it")
for i in range(13) :
try :
south_hand[i]
buttons[i]["state"] = "normal"
except IndexError :
buttons[i]["state"] = "disabled"
break
else :
south_put = south_hand[putid]
south_hand = south_hand[:putid] + \
south_hand[putid+1:]
cards = cards + (south_put,)
first_turn = "w"
if south_put[0] == 3 :
heart_put = 1 # True
except IndexError :
msgbox.showinfo("Note", "Invalid #")
for i in range(13) :
try :
south_hand[i]
buttons[i]["state"] = "normal"
except IndexError :
buttons[i]["state"] = "disabled"
elif first_turn == "w" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and west_hand[putid] != (1, 2) :
msgbox.showinfo("Note", "You must put Club 2 at the beginning")
elif cards == () :
if west_hand[putid][0] != 3 or \
(heart_put and west_hand[putid][0] == 3) :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
else :
for k in west_hand :
if k[0] != 3 :
msgbox.showinfo("Note", "Cannot put a heart unless it has alr\
eady been put in other")
break
else :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
if west_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == west_hand[putid][0] :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
else :
for k in west_hand :
if k[0] == cards[0][0] :
msgbox.showinfo("Note", "Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
west_put = west_hand[putid]
west_hand = west_hand[:putid] + \
west_hand[putid+1:]
cards = cards + (west_put,)
first_turn = "n"
if west_put[0] == 3 :
heart_put = 1 # True
except IndexError :
msgbox.showinfo("Note", "Invalid #")
elif first_turn == "n" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and north_hand[putid] != (1, 2) :
msgbox.showinfo("Note", "You must put Club 2 at the beginning")
elif cards == () :
if north_hand[putid][0] != 3 or \
(heart_put and north_hand[putid][0] == 3) :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
else :
for k in north_hand :
if k[0] != 3 :
msgbox.showinfo("Note", "Cannot put a heart unless it has alr\
eady been put in other")
break
else :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
if north_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == north_hand[putid][0] :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
else :
for k in north_hand :
if k[0] == cards[0][0] :
msgbox.showinfo("Note", "Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
north_put = north_hand[putid]
north_hand = north_hand[:putid] + \
north_hand[putid+1:]
cards = cards + (north_put,)
first_turn = "e"
if north_put[0] == 3 :
heart_put = 1 # True
except IndexError :
msgbox.showinfo("Note", "Invalid #")
elif first_turn == "e" :
try :
if len(south_hand) + len(west_hand) + len(north_hand) + \
len(east_hand) == 52 and east_hand[putid] != (1, 2) :
msgbox.showinfo("Note", "You must put Club 2 at the beginning")
elif cards == () :
if east_hand[putid][0] != 3 or \
(heart_put and east_hand[putid][0] == 3) :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
else :
for k in east_hand :
if k[0] != 3 :
msgbox.showinfo("Note", "Cannot put a heart unless it has alr\
eady been put in other")
break
else :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
if east_put[0] == 3 :
heart_put = 1 # True
elif cards[0][0] == east_hand[putid][0] :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
else :
for k in east_hand :
if k[0] == cards[0][0] :
msgbox.showinfo("Note", "Cannot put a different pattern unless yo\
u do not have any of it")
break
else :
east_put = east_hand[putid]
east_hand = east_hand[:putid] + \
east_hand[putid+1:]
cards = cards + (east_put,)
first_turn = "s"
if east_put[0] == 3 :
heart_put = 1 # True
except IndexError :
msgbox.showinfo("Note", "Invalid #")
showd.config(text=getshowc())
def puts() :
global first_turn, south_score, west_score, north_score, east_score, cards,\
south_put, west_put, north_put, east_put, south_scores, west_scores,\
north_scores, east_scores, putid, south_hand
while len(cards) < 4 :
if first_turn == "s" :
for i in range(13) :
try :
buttons[i].config(text="♦♣♠♥"[south_hand[i][0]] + \
("2", "3", "4", "5", "6", "7", "8",
"9", "10", "J", "Q", "K",
"A")[south_hand[i][1]-2])
buttons[i]["state"] = "normal"
except IndexError :
buttons[i].config(text="None")
buttons[i]["state"] = "disabled"
showd.config(text=getshowc())
return
elif first_turn == "w" :
putid = heartsai.pick(west_hand, cards, heart_put)
elif first_turn == "n" :
putid = heartsai.pick(north_hand, cards, heart_put)
elif first_turn == "e" :
putid = heartsai.pick(east_hand, cards, heart_put)
if strip(putid, "1") == "" :
putid = 0
elif strip(putid, "2") == "" :
putid = 1
elif strip(putid, "3") == "" :
putid = 2
elif strip(putid, "4") == "" :
putid = 3
elif strip(putid, "5") == "" :
putid = 4
elif strip(putid, "6") == "" :
putid = 5
elif strip(putid, "7") == "" :
putid = 6
elif strip(putid, "8") == "" :
putid = 7
elif strip(putid, "9") == "" :
putid = 8
elif strip(putid, "Aa") == "" :
putid = 9
elif strip(putid, "Bb") == "" :
putid = 10
elif strip(putid, "Cc") == "" :
putid = 11
elif strip(putid, "Dd") == "" :
putid = 12
else :
msgbox.showinfo("Invalid #")
put()
if len(cards) == 4 :
assert len(south_hand) == len(west_hand) == len(north_hand) \
== len(east_hand), \
"Card amounts are not the same. Please close the window."
assert len(south_hand) == len(west_hand) == len(north_hand) \
== len(east_hand), sys.exit(2)
#while len(south_hand) > len(east_hand) :
#south_hand = south_hand[:aput] + south_hand[aput+1:]
biggest = compare(cards)
if biggest == south_put :
for i in cards :
if i[0] == 3 :
south_score = south_score + 1
elif i == (2, Q) :
south_score = south_score + 13
first_turn = "s"
msgbox.showinfo("Biggest: You",
"Your side is the biggest. Press enter to continue")
elif biggest == west_put :
for i in cards :
if i[0] == 3 :
west_score = west_score + 1
elif i == (2, Q) :
west_score = west_score + 13
first_turn = "w"
msgbox.showinfo("Biggest: West",
"West side is the biggest. Press enter to continue")
elif biggest == north_put :
for i in cards :
if i[0] == 3 :
north_score = north_score + 1
elif i == (2, Q) :
north_score = north_score + 13
first_turn = "n"
msgbox.showinfo("Biggest: North",
"North side is the biggest. Press enter to continue")
elif biggest == east_put :
for i in cards :
if i[0] == 3 :
east_score = east_score + 1
elif i == (2, Q) :
east_score = east_score + 13
first_turn = "e"
msgbox.showinfo("Biggest: East",
"East side is the biggest. Press enter to continue")
mainWin.title(
"Hearts - North:%s/%s East:%s/%s You:%s/%s West:%s/%s " % \
(north_score, sum(north_scores), east_score, sum(east_scores),
south_score, sum(south_scores), west_score, sum(west_scores)))
cards = ()
south_put = None
west_put = None
north_put = None
east_put = None
if len(south_hand) + len(west_hand) + len(north_hand) + len(east_hand) :
if cards == () :
puts()
else :
buttons[0]["state"] = "disabled"
buttons[0].config(text="None")
south_scores = south_scores + (south_score,)
west_scores = west_scores + (west_score,)
north_scores = north_scores + (north_score,)
east_scores = east_scores + (east_score,)
south_scores_len = len(str(sum(south_scores)))
if south_scores_len < 3 :
south_scores_len = 3
west_scores_len = len(str(sum(west_scores)))
if west_scores_len < 4 :
west_scores_len = 4
north_scores_len = len(str(sum(north_scores)))
if north_scores_len < 5 :
north_scores_len = 5
east_scores_len = len(str(sum(east_scores)))
if east_scores_len < 4 :
east_scores_len = 4
showf = rjust("You", south_scores_len)+" "+rjust("West", west_scores_len)+\
" "+rjust("North", north_scores_len)+" "+\
rjust("East", east_scores_len)+" No.\n"
for i in range(len(south_scores)) :
showf += rjust(str(south_scores[i]), south_scores_len)+" "+\
rjust(str(west_scores[i]), west_scores_len)+" "+\
rjust(str(north_scores[i]), north_scores_len)+" "+\
rjust(str(east_scores[i]), east_scores_len)+" "+str(i+1) + "\n"
showf += ("-"*(south_scores_len+west_scores_len+north_scores_len+\
east_scores_len+3))
showf += ("\n"+rjust(str(sum(south_scores)), south_scores_len)+" "+\
rjust(str(sum(west_scores)), west_scores_len)+" "+\
rjust(str(sum(north_scores)), north_scores_len)+" "+\
rjust(str(sum(east_scores)), east_scores_len)+" Total")
showf += "\nClose this board to continue"
subWindow = tk.Tk()
subWindow.title("Scoreboard")
subSb = tk.Scrollbar(subWindow)
subSb.pack(side="right", fill="y")
subResult = tk.Text(subWindow, yscrollcommand=subSb.set)
subResult.pack()
subResult.insert("end", showf)
subResult.config(state="disabled")
subSb.config(command=subResult.yview)
subWindow.wait_window()
print(2) # For test
cards = ()
south_put = west_put = north_put = east_put = None
newcards()
puts()
cards = ()
south_put = west_put = north_put = east_put = None
newcards()
puts()
print(1) # For test
mainWin.mainloop()
#!/usr/bin/env python
# coding=UTF-8
import sys
import random
try :
input = raw_input
except NameError :
pass
def strip(source, chars="\t\n\x0b\x0c\r ") : # to solve the problem of Python
# 2.0 or earlier
res = source
while res[0:1] in tuple(chars) :
res = res[1:len(res)]
while res[-1:len(res)] in tuple(chars) :
res = res[0:-1]
return res
cards = ()
A = 1
J = 11
Q = 12
K = 13
while len(cards) < 52 :
let1_continue = 0
new_card = random.choice(((1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1),
(2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3),
(4, 0), (4, 1), (4, 2), (4, 3), (5, 0), (5, 1),
(5, 2), (5, 3), (6, 0), (6, 1), (6, 2), (6, 3),
(7, 0), (7, 1), (7, 2), (7, 3), (8, 0), (8, 1),
(8, 2), (8, 3), (9, 0), (9, 1), (9, 2), (9, 3),
(10, 0), (10, 1), (10, 2), (10, 3), (11, 0),
(11, 1), (11, 2), (11, 3), (12, 0), (12, 1),
(12, 2), (12, 3), (13, 0), (13, 1), (13, 2),
(13, 3)))
for i in cards :
if new_card == i :
let1_continue = 1
break
if let1_continue :
continue
cards = cards + (new_card,)
cosets = ((), (), (), ())
stock = cards[0:24]
sellines = (((cards[24], 0),), ((cards[25], 1), (cards[26], 0)),
((cards[27], 1), (cards[28], 1), (cards[29], 0)),
((cards[30], 1), (cards[31], 1), (cards[32], 1), (cards[33], 0)),
((cards[34], 1), (cards[35], 1), (cards[36], 1), (cards[37], 1),
(cards[38], 0)),
((cards[39], 1), (cards[40], 1), (cards[41], 1), (cards[42], 1),
(cards[43], 1), (cards[44], 0)),
((cards[45], 1), (cards[46], 1), (cards[47], 1), (cards[48], 1),
(cards[49], 1), (cards[50], 1), (cards[51], 0)))
shownumberpoints = (None, "A ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ",
"10", "J ", "Q ", "K ")
try :
stocknow = -1
operation_times = 0
hint_message = ""
while 1 :
showc = "SOL - Operation used: " + str(operation_times) + "\n" + \
("??? ", str(stock[stocknow][1])+":"+\
shownumberpoints[stock[stocknow][0]])[stocknow>-1]
for i in (0, 1, 2, 3) :
showc = showc + " " + str(i) + ":"
if cosets[i] == () :
showc = showc +" "
else :
showc = showc + shownumberpoints[cosets[i][-1][0]]
showc = showc + "\n"
for i in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18) :
showc = showc + "\n"
for j in (0, 1, 2, 3, 4, 5, 6) :
if j != 0 :
showc = showc + " "
if len(sellines[j]) > i :
showc = showc + (str(sellines[j][i][0][1])+":"+\
shownumberpoints[sellines[j][i][0][0]],
"??? ")[sellines[j][i][1]]
else :
showc = showc + " "
print(showc+"\n"+hint_message)
sourcecard = strip(input("Please input a source card(s):(or press enter\
directly to open a stock)"))
if sourcecard == "" :
if stocknow == len(stock) - 1 :
stocknow = -1
else :
stocknow = stocknow + 1
operation_times = operation_times + 1
elif sourcecard == "stock" :
if stocknow == -1 :
hint_message = "The stock is not open yet"
else :
destplace = strip(input("Please input a destination place:"))
if destplace == "co" :
hint_message = "Cannot move it to the cobar"
for i in (0, 1, 2, 3) :
if stock[stocknow][1] == i :
if cosets[i] == () :
if stock[stocknow][0] == 1 :
cosets = cosets[0:i] + \
((stock[stocknow],),) + \
cosets[i+1:4]
stock = stock[0:stocknow] + \
stock[stocknow+1:24]
stocknow = stocknow - 1
hint_message = ""
operation_times = operation_times + 1
break
elif cosets[i][-1][0] + 1 == stock[stocknow][0] :
cosets = cosets[0:i] + \
(cosets[i]+(stock[stocknow],),) + \
cosets[i+1:4]
stock = stock[0:stocknow] + stock[stocknow+1:24]
stocknow = stocknow - 1
hint_message = ""
operation_times = operation_times + 1
break
elif destplace in ("0", "1", "2", "3", "4", "5", "6") :
hint_message = "Cannot move it to the sel-line"
if sellines[int(destplace)] == () :
if stock[stocknow][0] == 13 :
sellines = sellines[0:int(destplace)] + \
(((stock[stocknow],0),),) + \
sellines[int(destplace)+1:7]
stock = stock[0:stocknow] + stock[stocknow+1:24]
stocknow = stocknow - 1
hint_message = ""
operation_times = operation_times + 1
else :
hint_message = "Must be K"
elif sellines[int(destplace)][-1][0][0] - 1 == \
stock[stocknow][0] and \
(sellines[int(destplace)][-1][0][1]+\
stock[stocknow][1]) % 2 == 1 :
sellines = sellines[0:int(destplace)] + \
(sellines[int(destplace)]+\
((stock[stocknow],0),),) + \
sellines[int(destplace)+1:7]
stock = stock[0:stocknow] + stock[stocknow+1:24]
stocknow = stocknow - 1
hint_message = ""
operation_times = operation_times + 1
else :
hint_message = "Invalid place"
elif sourcecard in ("0", "1", "2", "3") :
if cosets[int(sourcecard)] == () :
hint_message = "There is no card in the cobar"
else :
destplace = strip(input("Please input a destination place:"))
if destplace in ("0", "1", "2", "3", "4", "5", "6") :
hint_message = "Cannot move it to the sel-line"
if sellines[int(destplace)] == () :
if cosets[int(sourcecard)][-1][0] == 13 :
sellines = sellines[0:int(destplace)] + \
(((cosets[int(sourcecard)][-1],0),),) + \
sellines[int(destplace)+1:7]
cosets = cosets[0:int(sourcecard)] + \
(cosets[int(sourcecard)][0:-1],) + \
cosets[int(sourcecard)+1:4]
hint_message = ""
operation_times = operation_times + 1
else :
hint_message = "Must be K"
elif sellines[int(destplace)][-1][0][0] - 1 == \
cosets[int(sourcecard)][-1][0] and \
(sellines[int(destplace)][-1][0][1]+\
cosets[int(sourcecard)][-1][1]) % 2 == 1 :
sellines = sellines[0:int(destplace)] + \
(sellines[int(destplace)]+\
((cosets[int(sourcecard)][-1],0),),) + \
sellines[int(destplace)+1:7]
cosets = cosets[0:int(sourcecard)] + \
(cosets[int(sourcecard)][0:-1],) + \
cosets[int(sourcecard)+1:4]
hint_message = ""
operation_times = operation_times + 1
else :
hint_message = "Invalid place"
elif sourcecard in ("00", "01", "02", "03", "04", "05", "06", "07",
"08", "09", "010", "011", "012", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "110",
"111", "112", "113", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "210", "211", "212",
"213", "214", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "310", "311", "312", "313",
"314", "315", "40", "41", "42", "43", "44", "45",
"46", "47", "48", "49", "410", "411", "412", "413",
"414", "415", "416", "50", "51", "52", "53", "54",
"55", "56", "57", "58", "59", "510", "511", "512",
"513", "514", "515", "516", "517", "60", "61", "62",
"63", "64", "65", "66", "67", "68", "69", "610",
"611", "612", "613", "614", "615", "616", "617",
"618") :
if int(sourcecard[1:3]) >= len(sellines[int(sourcecard[0])]) :
hint_message = "No such card"
elif sellines[int(sourcecard[0])][int(sourcecard[1:3])][1] :
hint_message = "Covered card"
else :
destplace = strip(input("Please input a destination place:"))
if destplace == "co" :
hint_message = "Cannot move it to the cobar"
if len(sellines[int(sourcecard[0])]) - \
int(sourcecard[1:3]) > 1 :
hint_message = "Cannot move multiple cards to the cobar"
else :
for i in (0, 1, 2, 3) :
if sellines[int(sourcecard[0])]\
[int(sourcecard[1:3])][0][1] == i :
if cosets[i] == () :
if sellines[int(sourcecard[0])]\
[int(sourcecard[1:3])]\
[0][0] == 1 :
cosets = cosets[0:i] + \
((sellines[int(sourcecard[0])]\
[int(sourcecard\
[1:3])]\
[0],),) + \
cosets[i+1:4]
sellines = sellines[0:int(sourcecard\
[0])] + \
(sellines[int(sourcecard\
[0])][0:-1],) + \
sellines[int(sourcecard\
[0])+1:7]
if len(sellines[int(sourcecard[0])]) :
if sellines[int(sourcecard[0])][-1]\
[1] :
sellines = sellines\
[0:int(sourcecard\
[0])] + \
(sellines\
[int(sourcecard\
[0])][0:-1]+\
((sellines\
[int(sourcecard\
[0])][-1]\
[0], 0),),) + \
sellines\
[int(sourcecard[0])+\
1:7]
hint_message = ""
operation_times = operation_times + 1
break
elif sellines[int(sourcecard[0])]\
[int(sourcecard[1:3])]\
[0][0] == cosets[i][-1][0] + 1 :
cosets = cosets[0:i] + \
(cosets[i]+(sellines\
[int(sourcecard[0])]\
[int(sourcecard[1:3])]\
[0],),) + cosets[i+1:4]
sellines = sellines[0:int(sourcecard\
[0])] + \
(sellines[int(sourcecard[0])]\
[0:-1],) + \
sellines[int(sourcecard[0])+1:7]
if len(sellines[int(sourcecard[0])]) :
if sellines[int(sourcecard[0])][-1][1] :
sellines = sellines\
[0:int(sourcecard\
[0])] + \
(sellines[int(sourcecard\
[0])]\
[0:-1]+\
((sellines\
[int(sourcecard[0])]\
[-1][0], 0),),) + \
sellines\
[int(sourcecard[0])+1:7]
hint_message = ""
operation_times = operation_times + 1
break
elif destplace in ("0", "1", "2", "3", "4", "5", "6") :
if sourcecard[0] == destplace :
hint_message = "Place unchanged"
else :
hint_message = "Cannot move it to the sel-line"
if sellines[int(destplace)] == () :
if sellines[int(sourcecard[0])]\
[int(sourcecard[1:3])][0][0] == 13 :
sellines = sellines[0:int(destplace)] + \
(sellines[int(sourcecard[0])]\
[int(sourcecard\
[1:3]):19],) + \
sellines[int(destplace)+1:7]
sellines = sellines[0:int(sourcecard[0])] + \
(sellines[int(sourcecard[0])]\
[0:int(sourcecard\
[1:3])],) + \
sellines[int(sourcecard[0])+1:7]
if len(sellines[int(sourcecard[0])]) :
if sellines[int(sourcecard[0])][-1][1] :
sellines = sellines[0:int(sourcecard\
[0])] + \
(sellines[int(sourcecard\
[0])]\
[0:-1]+\
((sellines\
[int(sourcecard[0])]\
[-1][0], 0),),) + \
sellines\
[int(sourcecard[0])+1:7]
hint_message = ""
operation_times = operation_times + 1
else :
hint_message = "Must be K at the first"
elif sellines[int(destplace)][-1][0][0] - 1 == \
sellines[int(sourcecard[0])][int(sourcecard[1:3])]\
[0][0] and \
(sellines[int(destplace)][-1][0][1]+\
sellines[int(sourcecard[0])]\
[int(sourcecard[1:3])][0][1]) % 2 == 1 :
sellines = sellines[0:int(destplace)] + \
(sellines[int(destplace)]+\
sellines[int(sourcecard[0])]\
[int(sourcecard\
[1:3]):19],) + \
sellines[int(destplace)+1:7]
sellines = sellines[0:int(sourcecard[0])] + \
(sellines[int(sourcecard[0])]\
[0:int(sourcecard[1:3])],) + \
sellines[int(sourcecard[0])+1:7]
if len(sellines[int(sourcecard[0])]) :
if sellines[int(sourcecard[0])][-1][1] :
sellines = sellines[0:int(sourcecard\
[0])] + \
(sellines[int(sourcecard[0])]\
[0:-1]+\
((sellines[int(sourcecard[0])]\
[-1][0], 0),),) + \
sellines[int(sourcecard[0])+1:7]
hint_message = ""
operation_times = operation_times + 1
else :
hint_message = "Invalid place"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment