DEATH

a multiplayer variant of conways game of LIFE
git clone https://git.ce9e.org/DEATH.git

commit
b3066cccb0ddbfd0088bc7c2cbb89b559259e5fa
parent
f42be35e4fd9e1421c1992aa1ffb2b36812cf166
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-05-25 20:20
refactor neighbors()

Diffstat

M DEATH/matrix.py 41 ++++++++++++++++++++++++-----------------

1 files changed, 24 insertions, 17 deletions


diff --git a/DEATH/matrix.py b/DEATH/matrix.py

@@ -66,26 +66,33 @@ class Map(Matrix):
   66    66 	def clear(self, i, j):
   67    67 		self.setitem(i, j, -1)
   68    68 
   -1    69 	def field_neighbors(self, i, j, players):
   -1    70 		coods = [
   -1    71 			(i + 1, j),
   -1    72 			(i, j + 1),
   -1    73 			(i - 1, j),
   -1    74 			(i, j - 1),
   -1    75 		]
   -1    76 		if self.diagonal:
   -1    77 			coods += [
   -1    78 				(i + 1, j + 1),
   -1    79 				(i + 1, j - 1),
   -1    80 				(i - 1, j + 1),
   -1    81 				(i - 1, j - 1),
   -1    82 			]
   -1    83 
   -1    84 		a = 0
   -1    85 		for _row, _col in coods:
   -1    86 			if _row in range(self.rows) and _col in range(self.cols):
   -1    87 				if self.getitem(_row, _col) in players:
   -1    88 					a += 1
   -1    89 		return a
   -1    90 
   69    91 	def neighbors(self, players):
   70    -1 		result = Map(self.rows, self.cols, self.diagonal)
   -1    92 		result = Matrix(self.rows, self.cols)
   71    93 		for i in range(self.rows):
   72    94 			for j in range(self.cols):
   73    -1 				a = 0
   74    -1 				for cood in [(i + 1, j), (i, j + 1), (i - 1, j), (i, j - 1)]:
   75    -1 					try:
   76    -1 						if self.getitem(cood[0], cood[1]) in players:
   77    -1 							a += 1
   78    -1 					except:
   79    -1 						pass
   80    -1 				if self.diagonal:
   81    -1 					for cood in [(i + 1, j + 1), (i + 1, j - 1), (i - 1, j + 1),
   82    -1 							(i - 1, j - 1)]:
   83    -1 						try:
   84    -1 							if self.getitem(cood[0], cood[1]) in players:
   85    -1 								a += 1
   86    -1 						except:
   87    -1 							pass
   88    -1 				result.setitem(i, j, a)
   -1    95 				result.setitem(i, j, self.field_neighbors(i, j,  players))
   89    96 		return result
   90    97 
   91    98 	def clone(self):