Zura made like a life game

What is a life game?

Video *** Borrowed from youtube ***

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.

Let's try making it

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.

A little commentary

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 ^)

Output to image

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 ^)

I tried playing

movie.gif 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.

Impressions

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

Zura made like a life game
I made a life game with Numpy
〇✕ I made a game
I made a puzzle game (like) with Tkinter in Python
I made a roguelike game with Python
I made a bin picking game with Python
Make a squash game
I made a Christmas tree lighting game with Python
I made a school festival introduction game using Ren’py
I made a falling block game with Sense HAT
Make a Tetris-style game!
I made a simple typing game with tkinter in Python
Create a life game that is manually updated with tkinter
I made a Numer0n battle game in Java (I also made AI)
I made a python text
Made a command for FizzBuzz
I made a discord bot
Let's make a rock-paper-scissors game
Life game with Python [I made it] (on the terminal & Tkinter)
Save the result of the life game as a gif with python
I made a game called Battle Ship using pygame and tkinter
I made a client / server CLI tool for WebSocket (like Netcat for WebSocket)
I made a poker game server chat-holdem using websocket with python
I made a web application that graphs the life log recorded on Google Home like a Gantt chart.