~ubuntu-branches/ubuntu/raring/kajongg/raring-proposed

« back to all changes in this revision

Viewing changes to src/wall.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-12-18 23:15:58 UTC
  • Revision ID: package-import@ubuntu.com-20121218231558-tgyjxp592ulaou1r
Tags: upstream-4.9.95
ImportĀ upstreamĀ versionĀ 4.9.95

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
"""
 
4
Copyright (C) 2008,2009,2010 Wolfgang Rohdewald <wolfgang@rohdewald.de>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
"""
 
20
 
 
21
from common import elements
 
22
from tile import Tile
 
23
 
 
24
class WallEmpty(Exception):
 
25
    """exception when trying to get a tile off the empty wall"""
 
26
    pass
 
27
 
 
28
class Wall(object):
 
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.
 
35
        self.game = game
 
36
        tileCount = elements.count(game.ruleset)
 
37
        self.tiles = [Tile('Xy') for _ in range(tileCount)]
 
38
        self.living = None
 
39
        self.kongBox = None
 
40
        assert len(self.tiles) % 8 == 0
 
41
 
 
42
    def deal(self, tileNames=None, deadEnd=False):
 
43
        """deal tiles. May raise WallEmpty.
 
44
        Returns a list of tiles"""
 
45
        if tileNames is None:
 
46
            tileNames = [None]
 
47
        count = len(tileNames)
 
48
        if deadEnd:
 
49
            if len(self.kongBox) < count:
 
50
                raise WallEmpty
 
51
            tiles = self.kongBox[-count:]
 
52
            self.kongBox = self.kongBox[:-count]
 
53
            if len(self.kongBox) % 2 == 0:
 
54
                self.placeLooseTiles()
 
55
        else:
 
56
            if len(self.living) < count:
 
57
                raise WallEmpty
 
58
            tiles = self.living[:count]
 
59
            self.living = self.living[count:]
 
60
        for tile, name in zip(tiles, tileNames):
 
61
            if name is not None:
 
62
                tile.element = name
 
63
        return tiles
 
64
 
 
65
    def build(self):
 
66
        """virtual: build visible wall"""
 
67
 
 
68
    def placeLooseTiles(self):
 
69
        """virtual: place two loose tiles on the dead wall"""
 
70
 
 
71
    def decorate(self):
 
72
        """virtual: show player info on the wall"""
 
73
 
 
74
    def hide(self):
 
75
        """virtual: hide all four walls and their decorators"""
 
76
 
 
77
    def divide(self):
 
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