Mercurial > libervia-backend
comparison libervia/frontends/tools/games.py @ 4074:26b7ed2817da
refactoring: rename `sat_frontends` to `libervia.frontends`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 14:12:38 +0200 |
parents | sat_frontends/tools/games.py@be6d91572633 |
children |
comparison
equal
deleted
inserted
replaced
4073:7c5654c54fed | 4074:26b7ed2817da |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009-2021 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 = [ | |
23 "pique", | |
24 "coeur", | |
25 "trefle", | |
26 "carreau", | |
27 "atout", | |
28 ] # I have switched the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red) | |
29 VALUES_ORDER = [str(i) for i in range(1, 11)] + ["valet", "cavalier", "dame", "roi"] | |
30 | |
31 | |
32 class TarotCard(object): | |
33 """This class is used to represent a car logically""" | |
34 | |
35 def __init__(self, tuple_card): | |
36 """@param tuple_card: tuple (suit, value)""" | |
37 self.suit, self.value = tuple_card | |
38 self.bout = self.suit == "atout" and self.value in ["1", "21", "excuse"] | |
39 if self.bout or self.value == "roi": | |
40 self.points = 4.5 | |
41 elif self.value == "dame": | |
42 self.points = 3.5 | |
43 elif self.value == "cavalier": | |
44 self.points = 2.5 | |
45 elif self.value == "valet": | |
46 self.points = 1.5 | |
47 else: | |
48 self.points = 0.5 | |
49 | |
50 def get_tuple(self): | |
51 return (self.suit, self.value) | |
52 | |
53 @staticmethod | |
54 def from_tuples(tuple_list): | |
55 result = [] | |
56 for card_tuple in tuple_list: | |
57 result.append(TarotCard(card_tuple)) | |
58 return result | |
59 | |
60 def __cmp__(self, other): | |
61 if other is None: | |
62 return 1 | |
63 if self.suit != other.suit: | |
64 idx1 = SUITS_ORDER.index(self.suit) | |
65 idx2 = SUITS_ORDER.index(other.suit) | |
66 return idx1.__cmp__(idx2) | |
67 if self.suit == "atout": | |
68 if self.value == other.value == "excuse": | |
69 return 0 | |
70 if self.value == "excuse": | |
71 return -1 | |
72 if other.value == "excuse": | |
73 return 1 | |
74 return int(self.value).__cmp__(int(other.value)) | |
75 # at this point we have the same suit which is not 'atout' | |
76 idx1 = VALUES_ORDER.index(self.value) | |
77 idx2 = VALUES_ORDER.index(other.value) | |
78 return idx1.__cmp__(idx2) | |
79 | |
80 def __str__(self): | |
81 return "[%s,%s]" % (self.suit, self.value) | |
82 | |
83 | |
84 # These symbols are diplayed by Libervia next to the player's nicknames | |
85 SYMBOLS = {"Radiocol": ["♬"], "Tarot": ["♠", "♣", "♥", "♦"]} |