comparison tools/games.py @ 141:8c80d4dec7a8

mover Card class to tools/games and renamed it in TarotCard
author Goffi <goffi@goffi.org>
date Thu, 22 Jul 2010 15:49:16 +0800
parents
children
comparison
equal deleted inserted replaced
140:e496134ed679 141:8c80d4dec7a8
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT: a jabber client
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU 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 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, error
23
24 """This library help manage general games (e.g. card games)"""
25
26
27 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)
28 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
29
30 class TarotCard():
31 """This class is used to represent a car logically"""
32 #TODO: move this in a library in tools, and share this with frontends (e.g. card_game in wix use the same class)
33
34 def __init__(self, tuple_card):
35 """@param tuple_card: tuple (suit, value)"""
36 self.suit, self.value = tuple_card
37 self.bout = True if self.suit=="atout" and self.value in ["1","21","excuse"] else False
38 if self.bout or self.value == "roi":
39 self.points = 4.5
40 elif self.value == "dame":
41 self.points = 3.5
42 elif self.value == "cavalier":
43 self.points = 2.5
44 elif self.value == "valet":
45 self.points = 1.5
46 else:
47 self.points = 0.5
48
49 def get_tuple(self):
50 return (self.suit,self.value)
51
52 @staticmethod
53 def from_tuples(tuple_list):
54 result = []
55 for card_tuple in tuple_list:
56 result.append(TarotCard(card_tuple))
57 return result
58
59 def __cmp__(self, other):
60 if other == None:
61 return 1
62 if self.suit != other.suit:
63 idx1 = suits_order.index(self.suit)
64 idx2 = suits_order.index(other.suit)
65 return idx1.__cmp__(idx2)
66 if self.suit == 'atout':
67 if self.value == other.value == 'excuse':
68 return 0
69 if self.value == 'excuse':
70 return -1
71 if other.value == 'excuse':
72 return 1
73 return int(self.value).__cmp__(int(other.value))
74 #at this point we have the same suit which is not 'atout'
75 idx1 = values_order.index(self.value)
76 idx2 = values_order.index(other.value)
77 return idx1.__cmp__(idx2)
78
79 def __str__(self):
80 return "[%s,%s]" % (self.suit, self.value)