Mercurial > libervia-backend
comparison src/tools/frontends/games.py @ 682:2805fa3f4bdf
tools: moved src/tools/games.py to src/tools/frontends/
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 18 Oct 2013 11:58:42 +0200 |
parents | src/tools/games.py@84a6e83157c2 |
children | 30eb49e4e05d |
comparison
equal
deleted
inserted
replaced
681:66633443bcd7 | 682:2805fa3f4bdf |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """This library help manage general games (e.g. card games) and it is shared by the frontends""" | |
21 | |
22 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) | |
23 VALUES_ORDER = [str(i) for i in xrange(1, 11)] + ["valet", "cavalier", "dame", "roi"] | |
24 | |
25 | |
26 class TarotCard(object): | |
27 """This class is used to represent a car logically""" | |
28 | |
29 def __init__(self, tuple_card): | |
30 """@param tuple_card: tuple (suit, value)""" | |
31 self.suit, self.value = tuple_card | |
32 self.bout = self.suit == "atout" and self.value in ["1", "21", "excuse"] | |
33 if self.bout or self.value == "roi": | |
34 self.points = 4.5 | |
35 elif self.value == "dame": | |
36 self.points = 3.5 | |
37 elif self.value == "cavalier": | |
38 self.points = 2.5 | |
39 elif self.value == "valet": | |
40 self.points = 1.5 | |
41 else: | |
42 self.points = 0.5 | |
43 | |
44 def get_tuple(self): | |
45 return (self.suit, self.value) | |
46 | |
47 @staticmethod | |
48 def from_tuples(tuple_list): | |
49 result = [] | |
50 for card_tuple in tuple_list: | |
51 result.append(TarotCard(card_tuple)) | |
52 return result | |
53 | |
54 def __cmp__(self, other): | |
55 if other is None: | |
56 return 1 | |
57 if self.suit != other.suit: | |
58 idx1 = SUITS_ORDER.index(self.suit) | |
59 idx2 = SUITS_ORDER.index(other.suit) | |
60 return idx1.__cmp__(idx2) | |
61 if self.suit == 'atout': | |
62 if self.value == other.value == 'excuse': | |
63 return 0 | |
64 if self.value == 'excuse': | |
65 return -1 | |
66 if other.value == 'excuse': | |
67 return 1 | |
68 return int(self.value).__cmp__(int(other.value)) | |
69 # at this point we have the same suit which is not 'atout' | |
70 idx1 = VALUES_ORDER.index(self.value) | |
71 idx2 = VALUES_ORDER.index(other.value) | |
72 return idx1.__cmp__(idx2) | |
73 | |
74 def __str__(self): | |
75 return "[%s,%s]" % (self.suit, self.value) |