Mercurial > libervia-backend
comparison frontends/quick_frontend/quick_card_game.py @ 144:80661755ea8d
Primitivus: Tarot card game implementation
- quick frontend: card_game added
- wix: card_game splitted with quick frontend
- tools: new game library
- primitivus: new card_game widget (not finished yet)
- primitivus: SàT XML UI management: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 26 Jul 2010 19:43:44 +0800 |
parents | |
children | 1d74c59a36a9 |
comparison
equal
deleted
inserted
replaced
143:119f45746fde | 144:80661755ea8d |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 helper class for making a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, error | |
23 from tools.jid import JID | |
24 | |
25 | |
26 | |
27 class QuickCardGame(): | |
28 | |
29 def __init__(self, parent, referee, players, player_nick): | |
30 self.parent = parent | |
31 self.referee = referee | |
32 self.players = players | |
33 self.played = {} | |
34 for player in players: | |
35 self.played[player] = None | |
36 self.player_nick = player_nick | |
37 self.bottom_nick = unicode(self.player_nick) | |
38 idx = self.players.index(self.player_nick) | |
39 idx = (idx + 1) % len(self.players) | |
40 self.right_nick = unicode(self.players[idx]) | |
41 idx = (idx + 1) % len(self.players) | |
42 self.top_nick = unicode(self.players[idx]) | |
43 idx = (idx + 1) % len(self.players) | |
44 self.left_nick = unicode(self.players[idx]) | |
45 self.bottom_nick = unicode(player_nick) | |
46 self.selected = [] #Card choosed by the player (e.g. during ecart) | |
47 self.hand_size = 13 #number of cards in a hand | |
48 self.hand = [] | |
49 self.to_show = [] | |
50 self.state = None | |
51 | |
52 def loadCards(self): | |
53 """Load all the cards in memory | |
54 @param dir: directory where the PNG files are""" | |
55 self.cards={} | |
56 self.deck=[] | |
57 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names | |
58 self.cards["pique"]={} #spade | |
59 self.cards["coeur"]={} #heart | |
60 self.cards["carreau"]={} #diamond | |
61 self.cards["trefle"]={} #club | |
62 | |
63 def newGame(self, hand): | |
64 """Start a new game, with given hand""" | |
65 assert (len(self.hand) == 0) | |
66 for suit, value in hand: | |
67 self.hand.append(self.cards[suit, value]) | |
68 self.hand.sort() | |
69 self.state = "init" | |
70 | |
71 def contratSelected(self, contrat): | |
72 """Called when the contrat has been choosed | |
73 @param data: form result""" | |
74 self.parent.host.bridge.tarotGameContratChoosed(self.player_nick, self.referee, contrat or 'Passe', self.parent.host.profile) | |
75 | |
76 def chooseContrat(self, xml_data): | |
77 """Called when the player as to select his contrat | |
78 @param xml_data: SàT xml representation of the form""" | |
79 raise NotImplementedError | |
80 | |
81 def showCards(self, game_stage, cards, data): | |
82 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" | |
83 self.to_show = [] | |
84 for suit, value in cards: | |
85 self.to_show.append(self.cards[suit, value]) | |
86 if game_stage == "chien" and data['attaquant'] == self.player_nick: | |
87 self.state = "wait_for_ecart" | |
88 else: | |
89 self.state = "chien" | |
90 | |
91 def MyTurn(self): | |
92 """Called when we have to play :)""" | |
93 if self.state == "chien": | |
94 self.to_show = [] | |
95 self.state = "play" | |
96 | |
97 def showScores(self, xml_data, winners, loosers): | |
98 """Called when the player as to select hist contrat | |
99 @param xml_data: SàT xml representation of the form""" | |
100 raise NotImplementedError | |
101 | |
102 def cardsPlayed(self, player, cards): | |
103 """A card has been played by player""" | |
104 if self.to_show: | |
105 self.to_show = [] | |
106 pl_cards = [] | |
107 if self.played[player] != None: #gof: à supprimer | |
108 for pl in self.played: | |
109 self.played[pl] = None | |
110 for suit, value in cards: | |
111 pl_cards.append(self.cards[suit, value]) | |
112 self.played[player] = pl_cards[0] | |
113 | |
114 def invalidCards(self, phase, played_cards, invalid_cards): | |
115 """Invalid cards have been played | |
116 @param phase: phase of the game | |
117 @param played_cards: all the cards played | |
118 @param invalid_cards: cards which are invalid""" | |
119 | |
120 if phase == "play": | |
121 self.state = "play" | |
122 elif phase == "ecart": | |
123 self.state = "ecart" | |
124 else: | |
125 error ('INTERNAL ERROR: unmanaged game phase') | |
126 | |
127 for suit, value in played_cards: | |
128 self.hand.append(self.cards[suit, value]) | |
129 | |
130 self.hand.sort() | |
131 |