Is it like this? Is it a little different from the actual life game? It seems that it is also called an aggression game.
[Wikipedia Life Game](https://ja.wikipedia.org/wiki/%E3%83%A9%E3%82%A4%E3%83%95%E3%82%B2%E3%83%BC%E3 % 83% A0) Click here for the actual definition. Maybe what I want to make is different from life games.
I usually write python, so I adopted python this time as well python is easy (^ q ^)
Click here for code We have confirmed the operation with Ubuntu 14.04 + Python 3.5.
Python is functional and easy to write, so for statements and if statements can be reduced and it is really easy to write At first I thought I would write it object-oriented, It seemed pretty cool just to combine the list comprehension and the function, so I didn't make a class this time.
main.py
def next_state(f, i, j):
s = f[i][j]
e = environment(f, i, j)
if all(map(lambda x: x == s, e)):
return s
else:
num_friend = len(list(filter(lambda x: x == s, e)))
num_enemy = len(list(filter(lambda x: x != s, e)))
prob = 50 + (num_friend - num_enemy) * 6
alive = random.randint(0, 100) < prob
if alive:
return FRIEND if s == FRIEND else ENEMY
else:
return ENEMY if s == FRIEND else FRIEND
Click here for the main logic The current environment for 8 cells around one cell is acquired, and the state of the next step is determined. The more allies around you, the easier it is to survive, and the more enemies you have, the easier it is to die. Use lambda and get rid of it (^ q ^)
Finally, the state of the battle will be output as a GIF video.
main.py
def output_field(f, step):
f = list(filter(lambda x: not all(list(map(lambda e: e == '*', x))) , f))
f = list(map(lambda x: list(filter(lambda e: e != '*', x)), f))
f = list(map(lambda x: list(map(object2color ,x)) ,f))
arr = np.array(f, dtype=np.int8)
image = Image.fromarray(arr.astype('uint8'))
image.save('./tmp/' + '%07d.png' % step)
This code converts the 2D array to RGB values and outputs it to the tmp directory. Finally, it is converted to GIF with ImageMagick and output to the output directory. Since we are dealing with a 2D array, the lambda is duplicated. If you have lambda, it's easy (^ q ^)
Oh, it feels good. If you do not devise parameters, it will converge in a certain state. It may be interesting to make a commander cell and correct the winning percentage of the surrounding ○ squares. It's an original implementation that is a little different from the rules of Wikipedia, so it may not be a life game, but I'm personally satisfied.
I've been playing with Lisp lately, which makes lambda easier to use (or more, I'm trying to use it) If you have lambda, it's easy (^ q ^)
Recommended Posts