Added yada-yada stuff. Updated some tests
[empire.git] / map.py
blob81ff94c2287db7ef8676e8aebd3c3cf4c145a926
1 #!/usr/bin/env python
3 """
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.
26 """
28 import types
29 import csv
32 __doc__ = """
34 TODO: add description
35 """
37 class Map:
38 """
39 """
41 def __init__(self, width, height):
42 self.width = width
43 self.height = height
46 class Tile:
47 """Abstract base class for various tiles.
48 """
50 def __init__(self):
51 if self.__class__ is Tile:
52 raise NotImplementedError
53 self.fogofwar = True
54 self.units = []
56 # path to BMP file
57 name = str(self.__class__).split('.')[-1].lower()
58 self.image = "data/images/" + name + ".bmp"
61 class Sea(Tile):
62 def __init__(self):
63 Tile.__init__(self)
66 class Plain(Tile):
67 def __init__(self):
68 Tile.__init__(self)
69 self.city = None
72 class Mountain(Tile):
73 def __init__(self):
74 Tile.__init__(self)
75 self.mine = None
78 class Forest(Tile):
79 def __init__(self):
80 Tile.__init__(self)
81 self.watchtower = None
84 class River(Tile):
85 def __init__(self):
86 Tile.__init__(self)
87 self.bridge = None
90 MAPPING = {'.': Sea, '_': Plain, 'M': Mountain, '^': Forest, '~': River}
92 def load_map(filename):
93 """Load map from given filename. Returns a map object.
94 """
95 if type(filename) == types.ListType:
96 f = [line.strip().split(',') for line in filename]
97 else:
98 f = list(csv.reader(open(filename, 'r'), delimiter=','))
99 return Map(width=len(f), height=len(f[0]))
102 # ----------------------------------- TESTS ---------------------------------- #
104 import unittest
106 class ModuleTest(unittest.TestCase):
107 """Main test case for this module.
110 def test_mapping(self):
111 """Ensure correct mapping of tile symbols."""
112 expected = {'.': Sea, '_': Plain, 'M': Mountain, '^': Forest,
113 '~': River}
114 self.failUnlessEqual(expected, MAPPING)
116 def test_load_map_from_file(self):
117 """Test correct loading of maps from CSV files."""
118 map = load_map('data/maps/empty.map.csv')
119 self.failUnlessEqual((16, 16), (map.width, map.height))
121 def test_load_map_from_list(self):
122 """Test correct loading of maps from lists."""
123 # empty 16x16 map, all tiles are sea
124 empty_map = [
125 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
126 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
127 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
128 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
129 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
130 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
131 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
132 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
133 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
134 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
135 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
136 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
137 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
138 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
139 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
140 '.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.',
142 map = load_map(empty_map)
143 self.failUnlessEqual((16, 16), (map.width, map.height))
145 class MapTest(unittest.TestCase):
146 def test_init(self):
147 """Test correct instantiation of maps."""
148 map = Map(8, 8)
149 self.failUnlessEqual((8, 8), (map.width, map.height))
152 class TileTest(unittest.TestCase):
153 def test_abstract(self):
154 """Ensure that tile class is abstract."""
155 self.failUnlessRaises(NotImplementedError, Tile)
158 class RiverTest(unittest.TestCase):
159 def setUp(self):
160 self.river = River()
162 def test_init(self):
163 """Test correct instantiation of rivers."""
164 self.failUnlessEqual(self.river.fogofwar, True)
165 self.failUnlessEqual(self.river.bridge, None)
166 self.failUnlessEqual(self.river.units, [])
168 def test_image(self):
169 """Test if image string is correct."""
170 self.failUnlessEqual("data/images/river.bmp", self.river.image)
173 class SeaTest(unittest.TestCase):
174 def setUp(self):
175 self.sea = Sea()
177 def test_init(self):
178 """Test correct instantiation of sea tiles."""
179 self.failUnlessEqual(self.sea.fogofwar, True)
180 self.failUnlessEqual(self.sea.units, [])
182 def test_image(self):
183 """Test if image string is correct."""
184 self.failUnlessEqual("data/images/sea.bmp", self.sea.image)
187 class PlainTest(unittest.TestCase):
188 def setUp(self):
189 self.plain = Plain()
191 def test_init(self):
192 """Test correct instantiation of plains."""
193 self.failUnlessEqual(self.plain.fogofwar, True)
194 self.failUnlessEqual(self.plain.units, [])
195 self.failUnlessEqual(self.plain.city, None)
197 def test_image(self):
198 """Test if image string is correct."""
199 self.failUnlessEqual("data/images/plain.bmp", self.plain.image)
201 class MountainTest(unittest.TestCase):
202 def setUp(self):
203 self.mountain = Mountain()
205 def test_init(self):
206 """Test correct instantiation of mountains."""
207 self.failUnlessEqual(self.mountain.fogofwar, True)
208 self.failUnlessEqual(self.mountain.mine, None)
209 self.failUnlessEqual(self.mountain.units, [])
211 def test_image(self):
212 """Test if image string is correct."""
213 self.failUnlessEqual("data/images/mountain.bmp", self.mountain.image)
216 class ForestTest(unittest.TestCase):
217 def setUp(self):
218 self.forest = Forest()
220 def test_init(self):
221 """Test correct instantiation of forest tiles."""
222 self.failUnlessEqual(self.forest.fogofwar, True)
223 self.failUnlessEqual(self.forest.watchtower, None)
224 self.failUnlessEqual(self.forest.units, [])
226 def test_image(self):
227 """Test if image string is correct."""
228 self.failUnlessEqual("data/images/forest.bmp", self.forest.image)
231 def main():
232 unittest.main()
234 if __name__ == '__main__':
235 main()