5 Copyright (C) 2008 by Florian Hasheider
6 florian.hasheider@googlemail.com
8 Copyright (C) 2008 by Benjamin Kircher
9 benjamin.kircher@gmail.com
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the
23 Free Software Foundation, Inc.,
24 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 """Abstract base class for all units.
41 def __init__(self
, pos
):
42 if self
.__class
__ is Unit
:
43 raise NotImplementedError
44 self
.pos
= pos
or (0, 0)
45 self
.allowed_tiles
= (Plain
, Mountain
, Forest
, River
)
49 class MountedUnit(Unit
):
50 """Abstract base class for all mounted units.
53 def __init__(self
, pos
):
54 if self
.__class
__ is MountedUnit
:
55 raise NotImplementedError
56 Unit
.__init
__(self
, pos
)
57 self
.allowed_tiles
= (Plain
, Forest
, River
)
61 """Abstract base class for all naval units.
64 def __init__(self
, pos
):
65 if self
.__class
__ is SeaUnit
:
66 raise NotImplementedError
67 Unit
.__init
__(self
, pos
)
68 self
.allowed_tiles
= (Sea
)
72 def __init__(self
, pos
):
73 Unit
.__init
__(self
, pos
)
74 self
.strength
= (50, 0, 100)
77 class Swordsman(Unit
):
78 def __init__(self
, pos
):
79 Unit
.__init
__(self
, pos
)
80 self
.strength
= (100, 50, 0)
84 def __init__(self
, pos
):
85 Unit
.__init
__(self
, pos
)
86 self
.strength
= (0, 50, 0)
90 def __init__(self
, pos
):
91 Unit
.__init
__(self
, pos
)
92 self
.strength
= (500, 500, 500)
93 self
.allowed_tiles
= (Plain
, Mountain
, Forest
, River
, Sea
)
96 class Knight(MountedUnit
):
97 def __init__(self
, pos
):
98 MountedUnit
.__init
__(self
, pos
)
99 self
.strength
= (150, 50, 0)
102 class Sailboat(SeaUnit
):
103 def __init__(self
, pos
):
104 SeaUnit
.__init
__(self
, pos
)
105 self
.strength
= (50, 50, 50)
108 # ----------------------------------- TESTS ---------------------------------- #
112 class ModuleTest(unittest
.TestCase
):
113 """Test case for military units.
117 """Test correct instantiation of units.
122 self
.archer
= Archer((0, 0))
123 self
.swordsman
= Swordsman((0, 0))
126 self
.explorer
= Explorer((0, 0))
127 self
.superman
= Superman((0, 0))
130 self
.knight
= Knight((0, 0))
133 self
.sailboat
= Sailboat((0, 0))
136 def test_abstract(self
):
137 """Ensure that abstract units cannot be instantiated.
139 Abstract units are generic units, mounted units and sea units.
141 self
.failUnlessRaises(NotImplementedError, Unit
, (0, 0))
142 self
.failUnlessRaises(NotImplementedError, MountedUnit
, (0, 0))
143 self
.failUnlessRaises(NotImplementedError, SeaUnit
, (0, 0))
145 def test_position(self
):
146 """Test position attribute of units."""
149 def test_strength(self
):
150 """Ensure that each unit has appropriate strength tuple.
152 The strength of a unit consists usually of three values:
153 (attack, defense, bombing)
155 self
.failUnlessEqual((50, 0, 100), self
.archer
.strength
)
156 self
.failUnlessEqual((100, 50, 0), self
.swordsman
.strength
)
157 self
.failUnlessEqual((0, 50, 0), self
.explorer
.strength
)
158 self
.failUnlessEqual((500, 500, 500), self
.superman
.strength
)
159 self
.failUnlessEqual((150, 50, 0), self
.knight
.strength
)
160 self
.failUnlessEqual((50, 50, 50), self
.sailboat
.strength
)
162 def test_allowed_tiles(self
):
163 """Ensure that each unit has appropriate allowed_tiles tuple."""
164 self
.failUnlessEqual((Plain
, Mountain
, Forest
, River
),
165 self
.archer
.allowed_tiles
)
166 self
.failUnlessEqual((Plain
, Forest
, River
), self
.knight
.allowed_tiles
)
167 self
.failUnlessEqual((Sea
), self
.sailboat
.allowed_tiles
)
168 self
.failUnlessEqual((Plain
, Mountain
, Forest
, River
, Sea
),
169 self
.superman
.allowed_tiles
)
175 if __name__
== '__main__':