-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgame_map.py
75 lines (58 loc) · 1.97 KB
/
game_map.py
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
# -*- coding: utf-8 -*-
#
# @author YourName
import random
import sys
class GameMap(object):
MAX_MAP_SIZE = 100
def __init__(self, rows, cols):
"""Inits GameMap with row and column count."""
assert isinstance(rows, int)
assert isinstance(cols, int)
assert 0 < rows <= self.MAX_MAP_SIZE
assert 0 < cols <= self.MAX_MAP_SIZE
self.size = (rows, cols, )
self.cells = [[0 for col in range(cols)] for row in range(rows)]
@property
def rows(self):
pass
@property
def cols(self):
pass
def reset(self, possibility=0.5):
"""Reset the map with random data.
Args:
possibility: possibility of life cell
"""
pass
def set(self, row, col, val):
"""Set specific cell in the map."""
pass
def get_neighbor_count(self, row, col):
"""Get count of neighbors in specific cell.
Args:
row: row number
col: column number
Returns:
Count of live neighbor cells
"""
pass
def get_neighbor_count_map(self):
"""Get count of neighbors of every cell in the map.
Returns:
A grid contains every cell's neighbor count.
"""
return [[self.get_neighbor_count(row, col) for col in range(self.cols)] for row in range(self.rows)]
def print_map(self, cell_maps=None, sep=' ', fd=sys.stdout):
"""Print the map to target file descriptor
Args:
cell_maps: mapping from cell value to a string that will be displayed.
sep: separator between cells of the same row.
fd: file descriptor, default standard output
"""
if not cell_maps:
cell_maps = ['0', '1']
assert isinstance(cell_maps, list) or isinstance(cell_maps, dict)
assert isinstance(sep, str)
for row in self.cells:
print(sep.join(map(lambda cell: cell_maps[cell], row)), file=fd)