Belote/belote_jeu.py

149 lines
4.1 KiB
Python
Raw Normal View History

2020-04-25 10:08:19 +00:00
# -*- coding: utf-8 -*-
N_PLAYERS = 4
N_TURN = int(32/4)
# P : pique, C : Coeur, F : Carreau, T : Trèfle
# 7..9, 0 puis VDRA
cards =['AP', 'RP', 'DP', 'VP', '0P', '9P', '8P', '7P',
'AC', 'RC', 'DC', 'VC', '0C', '9C', '8C', '7C',
'AF', 'RF', 'DF', 'VF', '0F', '9F', '8F', '7F',
'AT', 'RT', 'DT', 'VT', '0T', '9T', '8T', '7T'
]
points_atout = {'V' : 20, '9' : 14, 'A': 11, '0': 10, 'R': 4, 'D': 3, '8':0, '7':0}
points_atout_order = {'V' : 20, '9' : 14, 'A': 11, '0': 10, 'R': 4, 'D': 3, '8':1, '7':0}
points_order = {'A': 11, '0': 10, 'R': 4, 'D': 3, 'V': 2, '9': 1, '8':0.5, '7':0}
points = {'A': 11, '0': 10, 'R': 4, 'D': 3, 'V': 2, '9': 0, '8':0, '7':0}
translate = {'A': 'As', 'R' : 'Roi', 'D': 'Dame', 'V': 'Valet', '0': '10', 'P': 'Pique', 'C': 'Coeur', 'T':'Trèfle', 'F':'Carreau'}
colors = ['P', 'C', 'F', 'T']
def translator(card):
s = []
for c in card:
if c in translate:
s.append(translate[c])
else:
s.append(c)
return ' '.join(s)
def winner(cards, atout):
"""
Returns id winner (0-3), number of points
"""
if isinstance(cards, str):
cardsl = cards.split(',')
cardss = cards
else:
cardss = ''.join(cards)
cardsl = cards
if len(cardss)==0:
return 0, 0
winner = None
if atout in cardss:
for i in range(len(cardsl)):
if cardsl[i][1] == atout:
if winner is None or points_atout_order[cardsl[i][0]] > points_atout_order[cardsl[winner][0]]:
winner = i
if winner is None:
couldemandee = cardss[1]
for i in range(len(cardsl)):
if cardsl[i][1] == couldemandee:
if winner is None or points_order[cardsl[i][0]] > points_order[cardsl[winner][0]]:
winner = i
pointsr = 0
for c in cardsl:
if c[1] == atout:
pointsr += points_atout[c[0]]
else:
pointsr += points[c[0]]
return winner, pointsr
def legal_play(played, atout, card, main):
if card not in cards:
return False
if card not in main:
return False
if played is None :
playedl = []
playeds = ''
elif isinstance(played, str):
playedl = played.split(',')
playeds = played
else:
playeds = ''.join(played)
playedl = played
if main is None or main=='':
main = []
if isinstance(main, str):
mains = main
main = main.split(',')
else:
mains = ','.join(main)
# Le premier a jouer fait ce qu'il veut
if len(playeds)==0:
return True
couldemandee =playeds[1]
# On regarde les atouts pour plus tard:
maxpose = -1
for c in playedl:
if c[1]==atout and points_atout_order[c[0]]>maxpose:
maxpose = points_atout_order[c[0]]
peutmonter = False
has_atout = False
for m in main: #Peut-on monter ?
if m[1] == atout:
has_atout = True
if points_atout_order[m[0]]>maxpose:
peutmonter = True
break
# Il faut jouer de la couleur demandée
if couldemandee != atout:
if couldemandee in mains:
if card[1]== couldemandee:
return True
else:
return False
if couldemandee == atout:
# Il faut jouer atout (si possible)
if has_atout:
if card[1] != atout:
return False
# Et monter ! (si possible)
if peutmonter:
if points_atout_order[card[0]]<=maxpose:
return False
# Mais si on a pas, tant pis !
return True
# Sinon, mon partenaire est-il maitre ?
if len(playedl)>=2:
maitre, xxx = winner(playedl, atout)
if maitre == len(playedl)-2 :
# Si oui, jeu libre
return True
# Sinon, ai-je de l'atout ?
if has_atout:
if card[1] != atout:
return False
# Si oui, je dois monter si possible
if peutmonter:
if points_atout_order[card[0]] < maxpose:
return False
return True
# Sinon, jeu libre !
return True