annotate browser_side/games.py @ 39:305e81c7a32c

Tarot game: a game can now be finished
author Goffi <goffi@goffi.org>
date Sun, 22 May 2011 00:15:01 +0200
parents 1d406077b49b
children 9d8e79ac4c9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 """This library help manage general games (e.g. card games)"""
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 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)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 class TarotCard():
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 """This class is used to represent a car logically"""
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 #TODO: move this in a library in tools, and share this with frontends (e.g. card_game in wix use the same class)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 def __init__(self, tuple_card):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 """@param tuple_card: tuple (suit, value)"""
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 self.suit, self.value = tuple_card
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self.bout = True if self.suit=="atout" and self.value in ["1","21","excuse"] else False
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 if self.bout or self.value == "roi":
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.points = 4.5
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 elif self.value == "dame":
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 self.points = 3.5
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 elif self.value == "cavalier":
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.points = 2.5
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 elif self.value == "valet":
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.points = 1.5
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 else:
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.points = 0.5
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def get_tuple(self):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return (self.suit,self.value)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @staticmethod
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def from_tuples(tuple_list):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 result = []
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 for card_tuple in tuple_list:
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 result.append(TarotCard(card_tuple))
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 return result
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def __cmp__(self, other):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 if other == None:
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 return 1
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 if self.suit != other.suit:
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 idx1 = suits_order.index(self.suit)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 idx2 = suits_order.index(other.suit)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 return idx1.__cmp__(idx2)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 if self.suit == 'atout':
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if self.value == other.value == 'excuse':
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 return 0
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 if self.value == 'excuse':
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 return -1
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 if other.value == 'excuse':
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 return 1
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return int(self.value).__cmp__(int(other.value))
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 #at this point we have the same suit which is not 'atout'
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 idx1 = values_order.index(self.value)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 idx2 = values_order.index(other.value)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return idx1.__cmp__(idx2)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def __str__(self):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 return "[%s,%s]" % (self.suit, self.value)