Mercurial > libervia-backend
annotate src/tools/games.py @ 664:cac98ca76479
install: added lxml to setup.py dependencies
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 31 Oct 2013 17:18:32 +0100 |
parents | 84a6e83157c2 |
children |
rev | line source |
---|---|
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
4 # SAT: a jabber client |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
10 # (at your option) any later version. |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
15 # GNU Affero General Public License for more details. |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
590
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from logging import debug, info, error |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 """This library help manage general games (e.g. card games)""" |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
23 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) |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
24 VALUES_ORDER = [str(i) for i in xrange(1, 11)] + ["valet", "cavalier", "dame", "roi"] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
25 |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
27 class TarotCard(object): |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 """This class is used to represent a car logically""" |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 #TODO: move this in a library in tools, and share this with frontends (e.g. card_game in wix use the same class) |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 def __init__(self, tuple_card): |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 """@param tuple_card: tuple (suit, value)""" |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 self.suit, self.value = tuple_card |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
34 self.bout = self.suit == "atout" and self.value in ["1", "21", "excuse"] |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 if self.bout or self.value == "roi": |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 self.points = 4.5 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 elif self.value == "dame": |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 self.points = 3.5 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 elif self.value == "cavalier": |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 self.points = 2.5 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 elif self.value == "valet": |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 self.points = 1.5 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 else: |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 self.points = 0.5 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 def get_tuple(self): |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
47 return (self.suit, self.value) |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 @staticmethod |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 def from_tuples(tuple_list): |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 result = [] |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 for card_tuple in tuple_list: |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 result.append(TarotCard(card_tuple)) |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 return result |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def __cmp__(self, other): |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
57 if other is None: |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 return 1 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 if self.suit != other.suit: |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
60 idx1 = SUITS_ORDER.index(self.suit) |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
61 idx2 = SUITS_ORDER.index(other.suit) |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 return idx1.__cmp__(idx2) |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 if self.suit == 'atout': |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 if self.value == other.value == 'excuse': |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 return 0 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 if self.value == 'excuse': |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 return -1 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 if other.value == 'excuse': |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 return 1 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 return int(self.value).__cmp__(int(other.value)) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
71 # at this point we have the same suit which is not 'atout' |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
72 idx1 = VALUES_ORDER.index(self.value) |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
73 idx2 = VALUES_ORDER.index(other.value) |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 return idx1.__cmp__(idx2) |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 def __str__(self): |
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 return "[%s,%s]" % (self.suit, self.value) |