- commit
- 63c8e9dc5ad344cf160aab10d79a9561b5e1ed3d
- parent
- 4a43a01c9389eb8a48708fcc73c87ff27b47f4f9
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-05-22 22:03
pep8
Diffstat
| M | DeathCli.py | 192 | +++++++++++++++++++++++++++++++------------------------------ |
| M | DeathCliMenu.py | 291 | +++++++++++++++++++++++++++++++------------------------------ |
| M | death.py | 86 | +++++++++++++++++++++++++++++++------------------------------ |
| M | death_extra.py | 26 | +++++++++++++------------- |
| M | death_extra_abk.py | 9 | ++++----- |
| M | death_extra_win.py | 52 | +++++++++++++++++++++++++++------------------------- |
| M | matrix.py | 173 | ++++++++++++++++++++++++++++++++----------------------------- |
7 files changed, 427 insertions, 402 deletions
diff --git a/DeathCli.py b/DeathCli.py
@@ -2,8 +2,6 @@ 2 2 # -*- coding: utf-8 -*- 3 3 4 4 import curses5 -1 import optparse6 -1 from death import Death7 5 import death_extra 8 6 9 7 help = """ @@ -19,110 +17,116 @@ the players take turns. 19 17 CONTROLS: 20 18 arrow-keys - move cursor 21 19 space - toggle field value22 -1 after that it is the next players' turn-1 20 after that it is the next players' turn 23 21 backspace - clear field value 24 22 return - perform a full step (one for every player) 25 23 tab - perform a single step (only for the active player)26 -1 after that it is the next players' turn-1 24 after that it is the next players' turn 27 25 h - display this help screen 28 26 escape - return to menu 29 27 30 28 return with any key 31 29 """ 32 30 -1 31 33 32 class DeathCli:34 -1 global help-1 33 global help 35 3436 -1 def __init__(self, death=death_extra.test(15,15,2), title='', screen=curses.initscr()):37 -1 self.screen = screen38 -1 self.death = death39 -1 self.title = title40 -1 self.screen.clear()41 -1 curses.curs_set(0)42 -1 self.screen.keypad(1)43 -1 curses.start_color()44 -1 curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_WHITE)45 -1 curses.init_pair(2,curses.COLOR_WHITE, curses.COLOR_BLACK)46 -1 self.row = 047 -1 self.col = 048 -1 self.draw()49 -1 while self.mainloop():50 -1 winner = self.death.win()51 -1 if winner:52 -1 print 'Player %s wins!' % (winner+1)53 -1 self.screen.getch()54 -1 break-1 35 def __init__(self, death=death_extra.test(15, 15, 2), title='', -1 36 screen=curses.initscr()): -1 37 self.screen = screen -1 38 self.death = death -1 39 self.title = title -1 40 self.screen.clear() -1 41 curses.curs_set(0) -1 42 self.screen.keypad(1) -1 43 curses.start_color() -1 44 curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) -1 45 curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK) -1 46 self.row = 0 -1 47 self.col = 0 -1 48 self.draw() -1 49 while self.mainloop(): -1 50 winner = self.death.win() -1 51 if winner: -1 52 print('Player %s wins!' % (winner + 1)) -1 53 self.screen.getch() -1 54 break 55 5556 -1 def draw(self):57 -1 w = 2*self.death.map.cols-158 -1 h = self.death.map.rows59 -1 x0 = (self.screen.getmaxyx()[1]-w)/260 -1 y0 = (self.screen.getmaxyx()[0]-h)/261 -1 # margin62 -1 for x in range(w):63 -1 self.screen.addstr(y0-1, x0+x, '-', curses.color_pair(2))64 -1 self.screen.addstr(y0+h, x0+x, '-', curses.color_pair(2))65 -1 for y in range(h):66 -1 self.screen.addstr(y0+y, x0-1, '|', curses.color_pair(2))67 -1 self.screen.addstr(y0+y, x0+w, '|', curses.color_pair(2))68 -1 self.screen.addstr(y0+h, x0+w, str(self.death.id+1), curses.color_pair(2))69 -1 # count70 -1 self.screen.addstr(y0-1, x0+2, '|'.join([str(a) for a in self.death.count()]), curses.color_pair(2))71 -1 # title72 -1 self.screen.addstr(y0-1, x0+w-len(self.title), self.title, curses.color_pair(2))73 -1 # map74 -1 for i in range(self.death.map.rows):75 -1 for j in range(self.death.map.cols):76 -1 field = self.death.map.getitem(i,j)77 -1 if field == 0:78 -1 s = ' '79 -1 else:80 -1 s = str(field)81 -1 if i == self.row and j == self.col:82 -1 self.screen.addstr(y0+i, x0+2*j, s, curses.color_pair(1))83 -1 else:84 -1 self.screen.addstr(y0+i, x0+2*j, s, curses.color_pair(2))-1 56 def draw(self): -1 57 w = 2 * self.death.map.cols - 1 -1 58 h = self.death.map.rows -1 59 x0 = (self.screen.getmaxyx()[1] - w) / 2 -1 60 y0 = (self.screen.getmaxyx()[0] - h) / 2 -1 61 # margin -1 62 for x in range(w): -1 63 self.screen.addstr(y0 - 1, x0 + x, '-', curses.color_pair(2)) -1 64 self.screen.addstr(y0 + h, x0 + x, '-', curses.color_pair(2)) -1 65 for y in range(h): -1 66 self.screen.addstr(y0 + y, x0 - 1, '|', curses.color_pair(2)) -1 67 self.screen.addstr(y0 + y, x0 + w, '|', curses.color_pair(2)) -1 68 self.screen.addstr(y0 + h, x0 + w, str(self.death.id + 1), -1 69 curses.color_pair(2)) -1 70 # count -1 71 self.screen.addstr(y0 - 1, x0 + 2, -1 72 '|'.join([str(a) for a in self.death.count()]), -1 73 curses.color_pair(2)) -1 74 # title -1 75 self.screen.addstr(y0 - 1, x0 + w - len(self.title), self.title, -1 76 curses.color_pair(2)) -1 77 # map -1 78 for i in range(self.death.map.rows): -1 79 for j in range(self.death.map.cols): -1 80 field = self.death.map.getitem(i, j) -1 81 if field == 0: -1 82 s = ' ' -1 83 else: -1 84 s = str(field) -1 85 if i == self.row and j == self.col: -1 86 self.screen.addstr(y0 + i, x0 + 2*j, s, curses.color_pair(1)) -1 87 else: -1 88 self.screen.addstr(y0 + i, x0 + 2*j, s, curses.color_pair(2)) 85 8986 -1 def mainloop(self):87 -1 key = self.screen.getch()88 -1 if key == curses.KEY_DOWN:89 -1 if self.row < self.death.map.rows-1:90 -1 self.row += 191 -1 elif key == curses.KEY_UP:92 -1 if self.row > 0:93 -1 self.row -= 194 -1 elif key == curses.KEY_RIGHT:95 -1 if self.col < self.death.map.cols-1:96 -1 self.col += 197 -1 elif key == curses.KEY_LEFT:98 -1 if self.col > 0:99 -1 self.col -= 1100 -1 elif key == ord(' '):101 -1 self.death.map.setitem(self.row, self.col, self.death.id+1)102 -1 self.death.next()103 -1 elif key == curses.KEY_BACKSPACE:104 -1 self.death.map.setitem(self.row, self.col, 0)105 -1 elif key == ord('\n'):106 -1 self.death.step()107 -1 elif key == ord(' '):108 -1 self.death.step_one(self.death.id)109 -1 self.death.next()110 -1 elif key == ord('h'):111 -1 self.screen.clear()112 -1 self.screen.addstr(0, 0, help, curses.color_pair(2))113 -1 self.screen.getch()114 -1 self.screen.clear()115 -1 draw()116 -1 elif key == 27:117 -1 self.screen.clear()118 -1 return False119 -1 self.draw()120 -1 return True-1 90 def mainloop(self): -1 91 key = self.screen.getch() -1 92 if key == curses.KEY_DOWN: -1 93 if self.row < self.death.map.rows - 1: -1 94 self.row += 1 -1 95 elif key == curses.KEY_UP: -1 96 if self.row > 0: -1 97 self.row -= 1 -1 98 elif key == curses.KEY_RIGHT: -1 99 if self.col < self.death.map.cols - 1: -1 100 self.col += 1 -1 101 elif key == curses.KEY_LEFT: -1 102 if self.col > 0: -1 103 self.col -= 1 -1 104 elif key == ord(' '): -1 105 self.death.map.setitem(self.row, self.col, self.death.id + 1) -1 106 self.death.next() -1 107 elif key == curses.KEY_BACKSPACE: -1 108 self.death.map.setitem(self.row, self.col, 0) -1 109 elif key == ord('\n'): -1 110 self.death.step() -1 111 elif key == ord('\t'): -1 112 self.death.step_one(self.death.id) -1 113 self.death.next() -1 114 elif key == ord('h'): -1 115 self.screen.clear() -1 116 self.screen.addstr(0, 0, help, curses.color_pair(2)) -1 117 self.screen.getch() -1 118 self.screen.clear() -1 119 self.draw() -1 120 elif key == 27: -1 121 self.screen.clear() -1 122 return False -1 123 self.draw() -1 124 return True 121 125122 -1123 -1 if __name__ == '__main__':124 -1 try: DeathCli()125 -1 except Exception as ex:126 -1 print ex127 -1 curses.endwin()128 126 -1 127 if __name__ == '__main__': -1 128 try: -1 129 DeathCli() -1 130 except Exception as ex: -1 131 print(ex) -1 132 curses.endwin()
diff --git a/DeathCliMenu.py b/DeathCliMenu.py
@@ -30,151 +30,158 @@ escape - quit 30 30 return with any key 31 31 """ 32 32 -1 33 33 34 class Crs:34 -1 def __init__(self, screen=curses.initscr()):35 -1 self.screen = screen36 -1 self.screen.clear()37 -1 curses.curs_set(0)38 -1 self.screen.keypad(1)39 -1 curses.start_color()40 -1 curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_WHITE)41 -1 curses.init_pair(2,curses.COLOR_WHITE, curses.COLOR_BLACK)42 -1 while self.main(): pass-1 35 def __init__(self, screen=curses.initscr()): -1 36 self.screen = screen -1 37 self.screen.clear() -1 38 curses.curs_set(0) -1 39 self.screen.keypad(1) -1 40 curses.start_color() -1 41 curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) -1 42 curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK) -1 43 while self.main(): -1 44 pass 43 4544 -1 def main(self):45 -1 x0 = (self.screen.getmaxyx()[1]-20)/246 -1 y0 = (self.screen.getmaxyx()[0]-2)/247 -1 # n48 -1 self.screen.clear()49 -1 n = 150 -1 while 1:51 -1 self.screen.addstr(y0, x0, 'number of players: %s' % n, curses.color_pair(2))52 -1 key = self.screen.getch()53 -1 if key == curses.KEY_UP:54 -1 n += 155 -1 elif key == curses.KEY_DOWN:56 -1 if n > 1:57 -1 n -= 158 -1 elif key == ord('h'):59 -1 self.screen.clear()60 -1 self.screen.addstr(0, 0, help, curses.color_pair(2))61 -1 self.screen.getch()62 -1 self.screen.clear()63 -1 elif key == ord('\n'):64 -1 break65 -1 elif key == 27:66 -1 return False67 -1 # map size68 -1 self.screen.clear()69 -1 rows = 1070 -1 while 1:71 -1 self.screen.addstr(y0, x0, 'number of rows: %s' % rows, curses.color_pair(2))72 -1 key = self.screen.getch()73 -1 if key == curses.KEY_UP:74 -1 if rows < self.screen.getmaxyx()[0]-2:75 -1 rows += 176 -1 elif key == curses.KEY_DOWN:77 -1 if rows > 1:78 -1 rows -= 179 -1 elif key == ord('h'):80 -1 self.screen.clear()81 -1 self.screen.addstr(0, 0, help, curses.color_pair(2))82 -1 self.screen.getch()83 -1 self.screen.clear()84 -1 elif key == ord('\n'):85 -1 break86 -1 elif key == 27:87 -1 return False88 -1 cols = 1089 -1 while 1:90 -1 self.screen.addstr(y0, x0, 'number of columns: %s' % cols, curses.color_pair(2))91 -1 key = self.screen.getch()92 -1 if key == curses.KEY_UP:93 -1 if cols < self.screen.getmaxyx()[1]/2-1:94 -1 cols += 195 -1 elif key == curses.KEY_DOWN:96 -1 if cols > 1:97 -1 cols -= 198 -1 elif key == ord('h'):99 -1 self.screen.clear()100 -1 self.screen.addstr(0, 0, help, curses.color_pair(2))101 -1 self.screen.getch()102 -1 self.screen.clear()103 -1 elif key == ord('\n'):104 -1 break105 -1 elif key == 27:106 -1 return False107 -1 # alive born kill108 -1 """109 -1 Diplay manu for every user110 -1 and append the results to the three lists afterwards111 -1 """112 -1 alive = []113 -1 born = []114 -1 kill = []115 -1 for player in range(n):116 -1 abk = [[], [], []]117 -1 _abk = 0118 -1 _i = 0119 -1 while 1:120 -1 self.screen.clear()121 -1 self.screen.addstr(y0-1, x0-10, 'Player %s' % str(player+1), curses.color_pair(2))122 -1 self.screen.addstr(y0, x0-10, 'alive', curses.color_pair(2))123 -1 self.screen.addstr(y0+1, x0-10, 'born', curses.color_pair(2))124 -1 self.screen.addstr(y0+2, x0-10, 'kill', curses.color_pair(2))125 -1 for i in range(9):126 -1 self.screen.addstr(y0-1, x0+2*i, str(i), curses.color_pair(2))127 -1 for __abk in range(3):128 -1 if _abk == __abk and _i == i:129 -1 color = curses.color_pair(1)130 -1 else:131 -1 color = curses.color_pair(2)132 -1 if i in abk[__abk]:133 -1 self.screen.addstr(y0+__abk, x0+2*i, 'X', color)134 -1 else:135 -1 self.screen.addstr(y0+__abk, x0+2*i, ' ', color)-1 46 def main(self): -1 47 x0 = (self.screen.getmaxyx()[1] - 20) / 2 -1 48 y0 = (self.screen.getmaxyx()[0] - 2) / 2 -1 49 # n -1 50 self.screen.clear() -1 51 n = 1 -1 52 while 1: -1 53 self.screen.addstr(y0, x0, 'number of players: %s' % n, -1 54 curses.color_pair(2)) -1 55 key = self.screen.getch() -1 56 if key == curses.KEY_UP: -1 57 n += 1 -1 58 elif key == curses.KEY_DOWN: -1 59 if n > 1: -1 60 n -= 1 -1 61 elif key == ord('h'): -1 62 self.screen.clear() -1 63 self.screen.addstr(0, 0, help, curses.color_pair(2)) -1 64 self.screen.getch() -1 65 self.screen.clear() -1 66 elif key == ord('\n'): -1 67 break -1 68 elif key == 27: -1 69 return False -1 70 # map size -1 71 self.screen.clear() -1 72 rows = 10 -1 73 while 1: -1 74 self.screen.addstr(y0, x0, 'number of rows: %s' % rows, -1 75 curses.color_pair(2)) -1 76 key = self.screen.getch() -1 77 if key == curses.KEY_UP: -1 78 if rows < self.screen.getmaxyx()[0] - 2: -1 79 rows += 1 -1 80 elif key == curses.KEY_DOWN: -1 81 if rows > 1: -1 82 rows -= 1 -1 83 elif key == ord('h'): -1 84 self.screen.clear() -1 85 self.screen.addstr(0, 0, help, curses.color_pair(2)) -1 86 self.screen.getch() -1 87 self.screen.clear() -1 88 elif key == ord('\n'): -1 89 break -1 90 elif key == 27: -1 91 return False -1 92 cols = 10 -1 93 while 1: -1 94 self.screen.addstr(y0, x0, 'number of columns: %s' % cols, -1 95 curses.color_pair(2)) -1 96 key = self.screen.getch() -1 97 if key == curses.KEY_UP: -1 98 if cols < self.screen.getmaxyx()[1] / 2 - 1: -1 99 cols += 1 -1 100 elif key == curses.KEY_DOWN: -1 101 if cols > 1: -1 102 cols -= 1 -1 103 elif key == ord('h'): -1 104 self.screen.clear() -1 105 self.screen.addstr(0, 0, help, curses.color_pair(2)) -1 106 self.screen.getch() -1 107 self.screen.clear() -1 108 elif key == ord('\n'): -1 109 break -1 110 elif key == 27: -1 111 return False -1 112 # alive born kill -1 113 """ -1 114 Diplay manu for every user -1 115 and append the results to the three lists afterwards -1 116 """ -1 117 alive = [] -1 118 born = [] -1 119 kill = [] -1 120 for player in range(n): -1 121 abk = [[], [], []] -1 122 _abk = 0 -1 123 _i = 0 -1 124 while 1: -1 125 self.screen.clear() -1 126 self.screen.addstr(y0 - 1, x0 - 10, 'Player %s' % str(player + 1), -1 127 curses.color_pair(2)) -1 128 self.screen.addstr(y0, x0 - 10, 'alive', curses.color_pair(2)) -1 129 self.screen.addstr(y0 + 1, x0 - 10, 'born', curses.color_pair(2)) -1 130 self.screen.addstr(y0 + 2, x0 - 10, 'kill', curses.color_pair(2)) -1 131 for i in range(9): -1 132 self.screen.addstr(y0 - 1, x0 + 2*i, str(i), curses.color_pair(2)) -1 133 for __abk in range(3): -1 134 if _abk == __abk and _i == i: -1 135 color = curses.color_pair(1) -1 136 else: -1 137 color = curses.color_pair(2) -1 138 if i in abk[__abk]: -1 139 self.screen.addstr(y0 + __abk, x0 + 2*i, 'X', color) -1 140 else: -1 141 self.screen.addstr(y0 + __abk, x0 + 2*i, ' ', color) 136 142137 -1 key = self.screen.getch()138 -1 if key == curses.KEY_UP:139 -1 if _abk > 0:140 -1 _abk -= 1141 -1 elif key == curses.KEY_DOWN:142 -1 if _abk < 2:143 -1 _abk += 1144 -1 if key == curses.KEY_LEFT:145 -1 if _i > 0:146 -1 _i -= 1147 -1 elif key == curses.KEY_RIGHT:148 -1 if _i < 10-1:149 -1 _i += 1150 -1 elif key == ord(' '):151 -1 if _i in abk[_abk]:152 -1 abk[_abk] = filter(lambda x: x != _i, abk[_abk])153 -1 else:154 -1 abk[_abk].append(_i)155 -1 elif key == curses.KEY_BACKSPACE:156 -1 abk[_abk] = filter(lambda x: x != _i, abk[_abk])157 -1 elif key == ord('h'):158 -1 self.screen.clear()159 -1 self.screen.addstr(0, 0, help, curses.color_pair(2))160 -1 self.screen.getch()161 -1 self.screen.clear()162 -1 elif key == ord('\n'):163 -1 break164 -1 elif key == 27:165 -1 return False166 -1 alive.append(abk[0])167 -1 born.append(abk[1])168 -1 kill.append(abk[2])-1 143 key = self.screen.getch() -1 144 if key == curses.KEY_UP: -1 145 if _abk > 0: -1 146 _abk -= 1 -1 147 elif key == curses.KEY_DOWN: -1 148 if _abk < 2: -1 149 _abk += 1 -1 150 if key == curses.KEY_LEFT: -1 151 if _i > 0: -1 152 _i -= 1 -1 153 elif key == curses.KEY_RIGHT: -1 154 if _i < 10 - 1: -1 155 _i += 1 -1 156 elif key == ord(' '): -1 157 if _i in abk[_abk]: -1 158 abk[_abk] = filter(lambda x: x != _i, abk[_abk]) -1 159 else: -1 160 abk[_abk].append(_i) -1 161 elif key == curses.KEY_BACKSPACE: -1 162 abk[_abk] = filter(lambda x: x != _i, abk[_abk]) -1 163 elif key == ord('h'): -1 164 self.screen.clear() -1 165 self.screen.addstr(0, 0, help, curses.color_pair(2)) -1 166 self.screen.getch() -1 167 self.screen.clear() -1 168 elif key == ord('\n'): -1 169 break -1 170 elif key == 27: -1 171 return False -1 172 alive.append(abk[0]) -1 173 born.append(abk[1]) -1 174 kill.append(abk[2]) 169 175170 -1 _map = Map(rows, cols)171 -1 d = Death(_map, n, alive, born, kill)172 -1 DeathCli(d, 'default', self.screen)173 -1 return True-1 176 _map = Map(rows, cols) -1 177 d = Death(_map, n, alive, born, kill) -1 178 DeathCli(d, 'default', self.screen) -1 179 return True 174 180175 -1 if __name__ == '__main__':176 -1 try: Crs()177 -1 except Exception as ex:178 -1 print ex179 -1 curses.endwin()180 181 -1 182 if __name__ == '__main__': -1 183 try: -1 184 Crs() -1 185 except Exception as ex: -1 186 print(ex) -1 187 curses.endwin()
diff --git a/death.py b/death.py
@@ -18,51 +18,53 @@ this happens in turns 18 18 19 19 from matrix import Map 20 20 -1 21 21 22 class Death:22 -1 def __init__(self, _map=Map(), n=1, alive=[[2,3]], born=[[3]], kill=[[]], win=lambda _map,n: None):23 -1 # the defaults make death the standart life24 -1 self.n = n25 -1 self.alive = alive26 -1 self.born = born27 -1 self.kill = kill28 -1 self.map = _map29 -1 self.id = 030 -1 self._win = win-1 23 def __init__(self, _map=Map(), n=1, alive=[[2, 3]], born=[[3]], kill=[[]], -1 24 win=lambda _map, n: None): -1 25 # the defaults make death the standart life -1 26 self.n = n -1 27 self.alive = alive -1 28 self.born = born -1 29 self.kill = kill -1 30 self.map = _map -1 31 self.id = 0 -1 32 self._win = win 31 3332 -1 def step_one(self,id):33 -1 def f(x):34 -1 if x[0] == 0:35 -1 if x[1] in self.born[id]:36 -1 return id+137 -1 else:38 -1 return x[0]39 -1 elif x[0] == id+1:40 -1 if x[1] in self.alive[id]:41 -1 return x[0]42 -1 else:43 -1 return 044 -1 else:45 -1 if x[1] in self.kill[id]:46 -1 return 047 -1 else:48 -1 return x[0]49 -1 self.map.join([self.map, self.map.neighbors([id+1])])50 -1 self.map.apply_f(f)-1 34 def step_one(self, id): -1 35 def f(x): -1 36 if x[0] == 0: -1 37 if x[1] in self.born[id]: -1 38 return id + 1 -1 39 else: -1 40 return x[0] -1 41 elif x[0] == id + 1: -1 42 if x[1] in self.alive[id]: -1 43 return x[0] -1 44 else: -1 45 return 0 -1 46 else: -1 47 if x[1] in self.kill[id]: -1 48 return 0 -1 49 else: -1 50 return x[0] -1 51 self.map.join([self.map, self.map.neighbors([id + 1])]) -1 52 self.map.apply_f(f) 51 5352 -1 def step(self):53 -1 for id in range(self.n):54 -1 self.step_one(id)-1 54 def step(self): -1 55 for id in range(self.n): -1 56 self.step_one(id) 55 5756 -1 def next(self):57 -1 self.id += 158 -1 if self.id == self.n:59 -1 self.id = 0-1 58 def next(self): -1 59 self.id += 1 -1 60 if self.id == self.n: -1 61 self.id = 0 60 6261 -1 def count(self):62 -1 c = []63 -1 for id in range(self.n):64 -1 c.append(self.map.count(id+1))65 -1 return c-1 63 def count(self): -1 64 c = [] -1 65 for id in range(self.n): -1 66 c.append(self.map.count(id + 1)) -1 67 return c 66 6867 -1 def win(self):68 -1 return self._win(self.map, self.n)-1 69 def win(self): -1 70 return self._win(self.map, self.n)
diff --git a/death_extra.py b/death_extra.py
@@ -6,17 +6,17 @@ from death import Death 6 6 import death_extra_win as win 7 7 import death_extra_abk as abk 8 89 -1 """10 -1 complete games11 -1 """-1 9 "complete games" -1 10 -1 11 12 12 def test(rows=15, cols=15, n=2):13 -1 map = Map(rows, cols)14 -1 alive = []15 -1 born = []16 -1 kill = []17 -1 for id in range(n):18 -1 alive.append(abk.conway[0])19 -1 born.append(abk.conway[1])20 -1 kill.append(abk.conway[2])21 -1 death = Death(map, n, alive, born, kill, win.economy)22 -1 return death-1 13 map = Map(rows, cols) -1 14 alive = [] -1 15 born = [] -1 16 kill = [] -1 17 for id in range(n): -1 18 alive.append(abk.conway[0]) -1 19 born.append(abk.conway[1]) -1 20 kill.append(abk.conway[2]) -1 21 death = Death(map, n, alive, born, kill, win.economy) -1 22 return death
diff --git a/death_extra_abk.py b/death_extra_abk.py
@@ -1,9 +1,8 @@ 1 1 #!/usr/bin/env python 2 2 # -*- coding: utf-8 -*- 3 34 -1 """5 -1 alive|born|kill6 -1 """7 -1 static_borders1 = [[0,1,2,3,4,5,6,7,8],[3,4],[4,5,6,7,8]]8 -1 conway = [[2,3],[3],[]]-1 4 "alive|born|kill" 9 5 -1 6 -1 7 static_borders1 = [[0, 1, 2, 3, 4, 5, 6, 7, 8], [3, 4], [4, 5, 6, 7, 8]] -1 8 conway = [[2, 3], [3], []]
diff --git a/death_extra_win.py b/death_extra_win.py
@@ -3,36 +3,38 @@ 3 3 4 4 """ 5 5 win-functions6 -1 * take a Map and number of players and-1 6 * take a Map and number of players and 7 7 * return the id of the winnig player or None 8 8 """ 9 9 -1 10 10 11 def death_match(_map, n):11 -1 # destroy every foe unit12 -1 winner = None13 -1 for id in range(n):14 -1 if _map.count(id+1) > 0:15 -1 if winner:16 -1 return None17 -1 else:18 -1 winner = id19 -1 return winner-1 12 # destroy every foe unit -1 13 winner = None -1 14 for id in range(n): -1 15 if _map.count(id + 1) > 0: -1 16 if winner: -1 17 return None -1 18 else: -1 19 winner = id -1 20 return winner -1 21 20 22 21 23 def capturetheflag(_map, n):22 -1 # capture the four corners23 -1 flag1 = _map.getitem(0,0)24 -1 flag2 = _map.getitem(0,_map.cols-1)25 -1 flag3 = _map.getitem(_map.rows-1,_map.cols-1)26 -1 flag4 = _map.getitem(_map.rows-1,0)27 -1 if flag1 == flag2 and flag2 == flag3 and flag3 == flag4:28 -1 return flag1-129 -1 else:30 -1 return None-1 24 # capture the four corners -1 25 flag1 = _map.getitem(0, 0) -1 26 flag2 = _map.getitem(0, _map.cols - 1) -1 27 flag3 = _map.getitem(_map.rows - 1, _map.cols - 1) -1 28 flag4 = _map.getitem(_map.rows - 1, 0) -1 29 if flag1 == flag2 and flag2 == flag3 and flag3 == flag4: -1 30 return flag1 - 1 -1 31 else: -1 32 return None 31 3332 -1 def economy(_map, n):33 -1 k = int(_map.rows * _map.cols / n * 0.2) # TODO ?34 -1 for id in range(n):35 -1 if _map.count(id+1) >= k:36 -1 return id37 -1 return None38 34 -1 35 def economy(_map, n): -1 36 k = int(_map.rows * _map.cols / n * 0.2) # TODO? -1 37 for id in range(n): -1 38 if _map.count(id + 1) >= k: -1 39 return id -1 40 return None
diff --git a/matrix.py b/matrix.py
@@ -1,96 +1,107 @@ 1 1 #!/usr/bin/env python 2 2 # -*- coding: utf-8 -*- 3 3 -1 4 4 5 class Matrix:5 -1 def __init__(self, rows, cols, value=0):6 -1 self.rows = rows7 -1 self.cols = cols8 -1 self.value = value9 -1 self.data=[]10 -1 for i in range(rows):11 -1 self.data.append([])12 -1 for j in range(cols):13 -1 self.data[i].append(value)-1 6 def __init__(self, rows, cols, value=0): -1 7 self.rows = rows -1 8 self.cols = cols -1 9 self.value = value -1 10 self.data = [] -1 11 for i in range(rows): -1 12 self.data.append([]) -1 13 for j in range(cols): -1 14 self.data[i].append(value) 14 1515 -1 def setitem(self, row, col, value):16 -1 self.data[row][col] = value-1 16 def setitem(self, row, col, value): -1 17 self.data[row][col] = value 17 1818 -1 def getitem(self, row, col):19 -1 if row<0 or col<0:20 -1 raise IndexError21 -1 return self.data[row][col]-1 19 def getitem(self, row, col): -1 20 if row < 0 or col < 0: -1 21 raise IndexError -1 22 return self.data[row][col] 22 2323 -1 def draw(self):24 -1 for row in self.data:25 -1 print row-1 24 def draw(self): -1 25 for row in self.data: -1 26 print(row) 26 2727 -1 def apply_f(self, f):28 -1 for i in range(self.rows):29 -1 for j in range(self.cols):30 -1 self.setitem(i, j, f(self.getitem(i,j)))31 -132 -1 def join(self, mats):33 -1 for mat in mats:34 -1 if mat.rows != self.rows or mat.cols != self.cols:35 -1 raise Exception36 -1 for i in range(self.rows):37 -1 for j in range(self.cols):38 -1 self.setitem(i, j, [mat.getitem(i,j) for mat in mats])-1 28 def apply_f(self, f): -1 29 for i in range(self.rows): -1 30 for j in range(self.cols): -1 31 self.setitem(i, j, f(self.getitem(i, j))) 39 3240 -1 def count(self, _item):41 -1 c = 042 -1 for row in self.data:43 -1 for item in row:44 -1 if item == _item:45 -1 c += 146 -1 return c-1 33 def join(self, mats): -1 34 for mat in mats: -1 35 if mat.rows != self.rows or mat.cols != self.cols: -1 36 raise Exception -1 37 for i in range(self.rows): -1 38 for j in range(self.cols): -1 39 self.setitem(i, j, [mat.getitem(i, j) for mat in mats]) 47 4048 -1 def save(filename, seperator=';'):49 -1 f = open(filename, 'w')50 -1 f.write('bla')51 -1 pass # TODO52 -1 f.close()-1 41 def count(self, _item): -1 42 c = 0 -1 43 for row in self.data: -1 44 for item in row: -1 45 if item == _item: -1 46 c += 1 -1 47 return c 53 4854 -1 def load(filename, seperator=';'):55 -1 f = open(filename, 'r')56 -1 pass # TODO57 -1 f.close()-1 49 def save(filename, seperator=';'): -1 50 f = open(filename, 'w') -1 51 f.write('bla') -1 52 pass # TODO -1 53 f.close() 58 5459 -1 class Map(Matrix):60 -1 def __init__(self, rows=15, cols=15, diagonal=True, value=0):61 -1 Matrix.__init__(self,rows, cols, value)62 -1 self.diagonal = diagonal-1 55 def load(filename, seperator=';'): -1 56 f = open(filename, 'r') -1 57 pass # TODO -1 58 f.close() 63 5964 -1 def neighbors(self, id, reverse=False):65 -1 try: 0 in id66 -1 except: id = [id]67 -1 if reverse: id.append(self.value)68 -1 result = Map(self.rows, self.cols, self.diagonal)69 -1 for i in range(self.rows):70 -1 for j in range(self.cols):71 -1 a = 072 -1 for cood in [(i+1,j), (i,j+1), (i-1,j), (i,j-1)]:73 -1 try:74 -1 if reverse:75 -1 if self.getitem(cood[0], cood[1]) not in id: a += 176 -1 else:77 -1 if self.getitem(cood[0], cood[1]) in id: a += 178 -1 except: pass79 -1 if self.diagonal:80 -1 for cood in [(i+1,j+1), (i+1,j-1), (i-1,j+1), (i-1,j-1)]:81 -1 try:82 -1 if reverse:83 -1 if self.getitem(cood[0], cood[1]) not in id: a += 184 -1 else:85 -1 if self.getitem(cood[0], cood[1]) in id: a += 186 -1 except: pass87 -1 result.setitem(i,j,a)88 -1 return result89 6090 -1 def clone(self):91 -1 result = Map(self.rows, self.cols, self.diagonal, self.value)92 -1 for i in range(self.rows):93 -1 for j in range(self.cols):94 -1 result.setitem(i, j, self.getitem(i,j))95 -1 return result-1 61 class Map(Matrix): -1 62 def __init__(self, rows=15, cols=15, diagonal=True, value=0): -1 63 Matrix.__init__(self, rows, cols, value) -1 64 self.diagonal = diagonal -1 65 -1 66 def neighbors(self, id, reverse=False): -1 67 try: -1 68 0 in id -1 69 except: -1 70 id = [id] -1 71 if reverse: -1 72 id.append(self.value) -1 73 result = Map(self.rows, self.cols, self.diagonal) -1 74 for i in range(self.rows): -1 75 for j in range(self.cols): -1 76 a = 0 -1 77 for cood in [(i + 1, j), (i, j + 1), (i - 1, j), (i, j - 1)]: -1 78 try: -1 79 if reverse: -1 80 if self.getitem(cood[0], cood[1]) not in id: -1 81 a += 1 -1 82 else: -1 83 if self.getitem(cood[0], cood[1]) in id: -1 84 a += 1 -1 85 except: -1 86 pass -1 87 if self.diagonal: -1 88 for cood in [(i + 1, j + 1), (i + 1, j - 1), (i - 1, j + 1), -1 89 (i - 1, j - 1)]: -1 90 try: -1 91 if reverse: -1 92 if self.getitem(cood[0], cood[1]) not in id: -1 93 a += 1 -1 94 else: -1 95 if self.getitem(cood[0], cood[1]) in id: -1 96 a += 1 -1 97 except: -1 98 pass -1 99 result.setitem(i, j, a) -1 100 return result 96 101 -1 102 def clone(self): -1 103 result = Map(self.rows, self.cols, self.diagonal, self.value) -1 104 for i in range(self.rows): -1 105 for j in range(self.cols): -1 106 result.setitem(i, j, self.getitem(i, j)) -1 107 return result