Ok, so... we continue to code about our Briscola game... I will explain the rule of the game and this code later... for now I will give you this code that is partially working... we got to cover a lot of stuff... but the aim of this is to get use to the pythonic style of coding (according to me, of course).
I wish you have installed the jupyter notebook, because is very nice. Another advise is to use Sublime Text 3 (but any text editor is good there are a lot of great editors for free and even IDLE, that comes with python, is very good).
It's a bit longer, but I think I will make a lot of changes in the future. PC2 doesn' know always what to play, but I choose a case in wich he won... but if you try this code you will see that he doesn't know what card to throw... we will fix it.
from random import shuffle
def val(card):
'gives the value of the card as return value'
valore = pcval[int(card[:-1])]
return valore
def num(card):
'gives the value of the card as return value'
valore = int(card[:-1])
return valore
def listcol(cards=[]):
'returns a list of col from a list of cards'
cardscol = []
for i in cards:
cardscol += [i[-1]]
return cardscol
def start():
'Create cards, shuffle them, pick pc1 (3 cards) and briscola, then choose the card to play'
global cards,pc1,pc2,briscola,cardschoice,pc1_tbl,pcval
cards = [str(num)+color for num in range(1,11) for color in ['♥','♦','♣','♠']]
pcval = {1:11,2:0,3:10,4:0,5:0,6:0,7:0,8:2,9:3,10:4}
shuffle(cards)
briscola = cards.pop()
pc1 = [cards.pop() for x in range(3)]
pc2 = [cards.pop() for x in range(3)]
cardschoice = []
for c in pc1:
card_color = c[-1]
briscola_color = briscola[-1]
match = card_color == briscola_color
if not match:
cardschoice += [c]
pc1v = []
for i in cardschoice:
pc1v += [val(i)]
pcmin = min(pc1v)
pc1_tbl = cardschoice[pc1v.index(pcmin)]
def whatIsIt(cs):
'sort of switch'
defs = {1:"Ha buttato un asso",
2:"scartina",
3:"Ha buttato un 3",
4:"Scartina",
5:"scartina",
6:"scartina",
7:"scartina",
8:"Sono due punti",
9:"Sono tre punti",
10:"Sono 4 punti"}
for value in defs:
if value == cs:
print(defs[cs])
return defs[cs]
def isScart(cs):
scartine = 2,4,5,6,7
if num(cs) in scartine:
return True
else:
return False
def isCar(cs):
carico = 1,3
if num(cs) in carico:
return True
else:
return False
def isFig(cs):
figure = 8,9,10
if num(cs) in figure:
return True
else:
return False
def isBrisc(cs):
if cs[-1] == briscola[-1]:
return True
else:
return False
def cartePunti(card):
'takes the color of the card and return a list of cards with points to answer to a scartina'
colore = card[-1]
cartep = [x+colore for x in ["1","3","10","9","8"]]
return cartep
def pc2Turn():
'the answer of pc2'
if isBrisc(pc1_tbl):
print("Pc1 throws a briscola ... to be continued ....")
else:
print("It is not a briscola")
if isScart(pc1_tbl):
print("He throwed a low card of",pc1_tbl[-1])
for c in cartePunti(pc1_tbl):
if c in pc2:
print(c, "<== tiro di PC2")
break
else:
print("*")
else:
print("He throws a high card of a figure")
def main():
start() # cards,pc1,briscola
print(briscola,"BRISCOLA")
print(pc1, "PC1")
print(pc2, "PC2")
print(pc1_tbl,"<== tiro di PC1")
pc2Turn()
if __name__=='__main__':
main()
Comments
Post a Comment