1
# -*- coding: utf-8 -*-
4
Copyright (C) 2008,2009,2010 Wolfgang Rohdewald <wolfgang@rohdewald.de>
6
kajongg is free software you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
from common import elements
24
class WallEmpty(Exception):
25
"""exception when trying to get a tile off the empty wall"""
29
"""represents the wall with four sides. self.wall[] indexes them counter clockwise, 0..3. 0 is bottom.
30
Wall.tiles always holds references to all tiles in the game even when they are used"""
31
def __init__(self, game):
32
"""init and position the wall"""
33
# we use only white dragons for building the wall. We could actually
34
# use any tile because the face is never shown anyway.
36
tileCount = elements.count(game.ruleset)
37
self.tiles = [Tile('Xy') for _ in range(tileCount)]
40
assert len(self.tiles) % 8 == 0
42
def deal(self, tileNames=None, deadEnd=False):
43
"""deal tiles. May raise WallEmpty.
44
Returns a list of tiles"""
47
count = len(tileNames)
49
if len(self.kongBox) < count:
51
tiles = self.kongBox[-count:]
52
self.kongBox = self.kongBox[:-count]
53
if len(self.kongBox) % 2 == 0:
54
self.placeLooseTiles()
56
if len(self.living) < count:
58
tiles = self.living[:count]
59
self.living = self.living[count:]
60
for tile, name in zip(tiles, tileNames):
66
"""virtual: build visible wall"""
68
def placeLooseTiles(self):
69
"""virtual: place two loose tiles on the dead wall"""
72
"""virtual: show player info on the wall"""
75
"""virtual: hide all four walls and their decorators"""
78
"""divides a wall, building a living and and a dead end"""
79
# neutralise the different directions of winds and removal of wall tiles
80
assert self.game.divideAt is not None
81
# shift tiles: tile[0] becomes living end
82
self.tiles[:] = self.tiles[self.game.divideAt:] + self.tiles[0:self.game.divideAt]
83
kongBoxSize = self.game.ruleset.kongBoxSize
84
self.living = self.tiles[:-kongBoxSize]
85
boxTiles = self.tiles[-kongBoxSize:]
86
for pair in range(kongBoxSize // 2):
87
boxTiles = boxTiles[:pair*2] + [boxTiles[pair*2+1], boxTiles[pair*2]] + boxTiles[pair*2+2:]
88
self.kongBox = boxTiles