Jeu de belote en ligne.

belote_jeu.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. N_PLAYERS = 4
  3. N_TURN = int(32/4)
  4. # P : pique, C : Coeur, F : Carreau, T : Trèfle
  5. # 7..9, 0 puis VDRA
  6. cards =['AP', 'RP', 'DP', 'VP', '0P', '9P', '8P', '7P',
  7. 'AC', 'RC', 'DC', 'VC', '0C', '9C', '8C', '7C',
  8. 'AF', 'RF', 'DF', 'VF', '0F', '9F', '8F', '7F',
  9. 'AT', 'RT', 'DT', 'VT', '0T', '9T', '8T', '7T'
  10. ]
  11. points_atout = {'V' : 20, '9' : 14, 'A': 11, '0': 10, 'R': 4, 'D': 3, '8':0, '7':0}
  12. points_atout_order = {'V' : 20, '9' : 14, 'A': 11, '0': 10, 'R': 4, 'D': 3, '8':1, '7':0}
  13. points_order = {'A': 11, '0': 10, 'R': 4, 'D': 3, 'V': 2, '9': 1, '8':0.5, '7':0}
  14. points = {'A': 11, '0': 10, 'R': 4, 'D': 3, 'V': 2, '9': 0, '8':0, '7':0}
  15. translate = {'A': 'As', 'R' : 'Roi', 'D': 'Dame', 'V': 'Valet', '0': '10', 'P': 'Pique', 'C': 'Coeur', 'T':'Trèfle', 'F':'Carreau'}
  16. colors = ['P', 'C', 'F', 'T']
  17. def translator(card):
  18. s = []
  19. for c in card:
  20. if c in translate:
  21. s.append(translate[c])
  22. else:
  23. s.append(c)
  24. return ' '.join(s)
  25. def winner(cards, atout):
  26. """
  27. Returns id winner (0-3), number of points
  28. """
  29. if isinstance(cards, str):
  30. cardsl = cards.split(',')
  31. cardss = cards
  32. else:
  33. cardss = ''.join(cards)
  34. cardsl = cards
  35. if len(cardss)==0:
  36. return 0, 0
  37. winner = None
  38. if atout in cardss:
  39. for i in range(len(cardsl)):
  40. if cardsl[i][1] == atout:
  41. if winner is None or points_atout_order[cardsl[i][0]] > points_atout_order[cardsl[winner][0]]:
  42. winner = i
  43. if winner is None:
  44. couldemandee = cardss[1]
  45. for i in range(len(cardsl)):
  46. if cardsl[i][1] == couldemandee:
  47. if winner is None or points_order[cardsl[i][0]] > points_order[cardsl[winner][0]]:
  48. winner = i
  49. pointsr = 0
  50. for c in cardsl:
  51. if c[1] == atout:
  52. pointsr += points_atout[c[0]]
  53. else:
  54. pointsr += points[c[0]]
  55. return winner, pointsr
  56. def legal_play(played, atout, card, main):
  57. if card not in cards:
  58. return False
  59. if card not in main:
  60. return False
  61. if played is None :
  62. playedl = []
  63. playeds = ''
  64. elif isinstance(played, str):
  65. playedl = played.split(',')
  66. playeds = played
  67. else:
  68. playeds = ''.join(played)
  69. playedl = played
  70. if main is None or main=='':
  71. main = []
  72. if isinstance(main, str):
  73. mains = main
  74. main = main.split(',')
  75. else:
  76. mains = ','.join(main)
  77. # Le premier a jouer fait ce qu'il veut
  78. if len(playeds)==0:
  79. return True
  80. couldemandee =playeds[1]
  81. # On regarde les atouts pour plus tard:
  82. maxpose = -1
  83. for c in playedl:
  84. if c[1]==atout and points_atout_order[c[0]]>maxpose:
  85. maxpose = points_atout_order[c[0]]
  86. peutmonter = False
  87. has_atout = False
  88. for m in main: #Peut-on monter ?
  89. if m[1] == atout:
  90. has_atout = True
  91. if points_atout_order[m[0]]>maxpose:
  92. peutmonter = True
  93. break
  94. # Il faut jouer de la couleur demandée
  95. if couldemandee != atout:
  96. if couldemandee in mains:
  97. if card[1]== couldemandee:
  98. return True
  99. else:
  100. return False
  101. if couldemandee == atout:
  102. # Il faut jouer atout (si possible)
  103. if has_atout:
  104. if card[1] != atout:
  105. return False
  106. # Et monter ! (si possible)
  107. if peutmonter:
  108. if points_atout_order[card[0]]<=maxpose:
  109. return False
  110. # Mais si on a pas, tant pis !
  111. return True
  112. # Sinon, mon partenaire est-il maitre ?
  113. if len(playedl)>=2:
  114. maitre, xxx = winner(playedl, atout)
  115. if maitre == len(playedl)-2 :
  116. # Si oui, jeu libre
  117. return True
  118. # Sinon, ai-je de l'atout ?
  119. if has_atout:
  120. if card[1] != atout:
  121. return False
  122. # Si oui, je dois monter si possible
  123. if peutmonter:
  124. if points_atout_order[card[0]] < maxpose:
  125. return False
  126. return True
  127. # Sinon, jeu libre !
  128. return True