annotate src/tools/games.py @ 499:28f4ce741ae5

primitivus: fixed display for selected entity in contacts list + added indentation for status message
author Goffi <goffi@goffi.org>
date Tue, 04 Sep 2012 13:36:14 +0200
parents 2a072735e459
children ca13633d3b6b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT: a jabber client
459
cf005701624b copyleft date update
Goffi <goffi@goffi.org>
parents: 228
diff changeset
6 Copyright (C) 2009, 2010, 2011, 2012 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
7
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
12
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
16 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
17
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
18 You should have received a copy of the GNU Affero General Public License
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
21
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
22 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
23
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
24 """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
25
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
26
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
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)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
28 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
29
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class TarotCard():
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
31 """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
32 #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
33
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
34 def __init__(self, tuple_card):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
35 """@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
36 self.suit, self.value = tuple_card
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.bout = True if self.suit=="atout" and self.value in ["1","21","excuse"] else False
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
38 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
39 self.points = 4.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
40 elif self.value == "dame":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.points = 3.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
42 elif self.value == "cavalier":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.points = 2.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
44 elif self.value == "valet":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.points = 1.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
46 else:
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.points = 0.5
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 def get_tuple(self):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
50 return (self.suit,self.value)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
51
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
52 @staticmethod
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def from_tuples(tuple_list):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
54 result = []
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
55 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
56 result.append(TarotCard(card_tuple))
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
57 return result
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
58
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __cmp__(self, other):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
60 if other == None:
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
61 return 1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
62 if self.suit != other.suit:
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
63 idx1 = suits_order.index(self.suit)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
64 idx2 = suits_order.index(other.suit)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
65 return idx1.__cmp__(idx2)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
66 if self.suit == 'atout':
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
67 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
68 return 0
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
69 if self.value == 'excuse':
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
70 return -1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if other.value == 'excuse':
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return 1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return int(self.value).__cmp__(int(other.value))
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
74 #at this point we have the same suit which is not 'atout'
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
75 idx1 = values_order.index(self.value)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
76 idx2 = values_order.index(other.value)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
77 return idx1.__cmp__(idx2)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
78
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def __str__(self):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
80 return "[%s,%s]" % (self.suit, self.value)