36
+ − 1 #!/usr/bin/python
+ − 2 # -*- coding: utf-8 -*-
+ − 3
+ − 4 """
+ − 5 Libervia: a Salut à Toi frontend
165
+ − 6 Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org>
36
+ − 7
+ − 8 This program is free software: you can redistribute it and/or modify
+ − 9 it under the terms of the GNU Affero General Public License as published by
+ − 10 the Free Software Foundation, either version 3 of the License, or
+ − 11 (at your option) any later version.
+ − 12
+ − 13 This program is distributed in the hope that it will be useful,
+ − 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
+ − 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ − 16 GNU Affero General Public License for more details.
+ − 17
+ − 18 You should have received a copy of the GNU Affero General Public License
+ − 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
+ − 20 """
+ − 21
+ − 22 """This library help manage general games (e.g. card games)"""
+ − 23
+ − 24
+ − 25 suits_order = [ 'pique' , 'coeur' , 'trefle' , 'carreau' , 'atout' ] #I have switched the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
+ − 26 values_order = map ( str , range ( 1 , 11 )) + [ "valet" , "cavalier" , "dame" , "roi" ]
+ − 27
+ − 28 class TarotCard ():
+ − 29 """This class is used to represent a car logically"""
+ − 30 #TODO: move this in a library in tools, and share this with frontends (e.g. card_game in wix use the same class)
+ − 31
+ − 32 def __init__ ( self , tuple_card ):
+ − 33 """@param tuple_card: tuple (suit, value)"""
+ − 34 self . suit , self . value = tuple_card
+ − 35 self . bout = True if self . suit == "atout" and self . value in [ "1" , "21" , "excuse" ] else False
+ − 36 if self . bout or self . value == "roi" :
+ − 37 self . points = 4.5
+ − 38 elif self . value == "dame" :
+ − 39 self . points = 3.5
+ − 40 elif self . value == "cavalier" :
+ − 41 self . points = 2.5
+ − 42 elif self . value == "valet" :
+ − 43 self . points = 1.5
+ − 44 else :
+ − 45 self . points = 0.5
+ − 46
+ − 47 def get_tuple ( self ):
+ − 48 return ( self . suit , self . value )
+ − 49
+ − 50 @staticmethod
+ − 51 def from_tuples ( tuple_list ):
+ − 52 result = []
+ − 53 for card_tuple in tuple_list :
+ − 54 result . append ( TarotCard ( card_tuple ))
+ − 55 return result
+ − 56
+ − 57 def __cmp__ ( self , other ):
+ − 58 if other == None :
+ − 59 return 1
+ − 60 if self . suit != other . suit :
+ − 61 idx1 = suits_order . index ( self . suit )
+ − 62 idx2 = suits_order . index ( other . suit )
+ − 63 return idx1 . __cmp__ ( idx2 )
+ − 64 if self . suit == 'atout' :
+ − 65 if self . value == other . value == 'excuse' :
+ − 66 return 0
+ − 67 if self . value == 'excuse' :
+ − 68 return - 1
+ − 69 if other . value == 'excuse' :
+ − 70 return 1
+ − 71 return int ( self . value ) . __cmp__ ( int ( other . value ))
+ − 72 #at this point we have the same suit which is not 'atout'
+ − 73 idx1 = values_order . index ( self . value )
+ − 74 idx2 = values_order . index ( other . value )
+ − 75 return idx1 . __cmp__ ( idx2 )
+ − 76
+ − 77 def __str__ ( self ):
+ − 78 return "[ %s , %s ]" % ( self . suit , self . value )