1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#Copyright (c) 2011 Walter Bender
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
import gtk
from utils import json_dump, json_load
ROW = 8
COL = 8
TILES = 3
class Hand:
''' Class for managing COL matrix of tiles '''
def __init__(self, tile_width, tile_height, remote=False):
# The tiles in your hand
self.hand = []
self.remote = remote # Does this hand belong to someone remote?
for i in range(COL):
self.hand.append(None)
# Tile spacing
self.xinc = int(tile_width)
if self.remote:
self.left = -self.xinc
else:
self.left = int(tile_width / 2)
self.top = 0
self.yinc = int(tile_height)
def clear(self):
for i in range(COL):
self.hand[i] = None
def deal(self, deck, number=COL):
''' Deal an initial set of tiles to the hand '''
for i in range(number):
self.hand[i] = deck.deal_next_tile()
if self.hand[i] is not None:
self.hand[i].spr.move(self.hand_to_xy(i))
self.hand[i].spr.set_layer(TILES)
return True
def find_empty_slot(self):
''' Is there an empty slot in the hand? '''
for i in range(COL):
if self.hand[i] == None:
return i
return None
def tiles_in_hand(self):
''' How many tiles are in the hand? '''
return COL - self.hand.count(None)
def serialize(self, buddy=None):
''' Serialize the hand for passing to share and saving '''
if buddy == None:
hand = []
else:
hand = [buddy]
for i in range(COL):
if self.hand[i] is not None:
hand.append(self.hand[i].number)
else:
hand.append(None)
return json_dump(hand)
def restore(self, hand_as_text, deck, buddy=False):
''' Restore tiles to hand upon resume or share. '''
hand = json_load(hand_as_text)
if buddy:
offset = 1 # skip the buddy
else:
offset = 0
for tile in range(COL):
i = tile + offset
if hand[i] is None:
self.hand[i] = None
else:
for k in range(ROW * COL):
if deck.tiles[k].number == hand[i]:
self.hand[tile] = deck.tiles[k]
self.hand[tile].spr.move(self.hand_to_xy(tile))
self.hand[tile].spr.set_layer(TILES)
break
def xy_to_hand(self, x, y):
''' Convert from sprite x,y to hand index. '''
if x < self.left + self.xinc:
return int((y - self.top) / self.yinc)
else:
return None
def hand_to_xy(self, i):
''' Convert from hand index to sprite x,y. '''
return ((self.left, (self.top + i * self.yinc)))
def hand_to_spr(self, i):
''' Return the sprite in hand-position i. '''
return self.hand[i].spr
def spr_to_hand(self, spr):
''' Return the index of a sprite in hand. '''
for i in range(COL):
if self.hand[i] is not None and self.hand[i].spr == spr:
return(i)
return None
|