1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

mojoの勉強のためにライフゲームのコードを書く

Last updated at Posted at 2023-10-10

pythonの云千倍速いという触れ込みのmojoの勉強を始めたかったので、定番のライフゲームの実装を行う。
簡単のため、ターミナル上で勝手に表示されるだけのものにする。
使用したのバージョン「mojo: 0.3.0」

実装したコード

from memory import memcpy
from random import randint, seed
from time import sleep

struct Lifegame:
    var board: DTypePointer[DType.int8]
    var next_board: DTypePointer[DType.int8]
    var rows: Int
    var cols: Int

    fn __init__(inout self, rows: Int, cols: Int):
        self.board = DTypePointer[DType.int8].alloc(rows * cols)
        self.next_board = DTypePointer[DType.int8].alloc(rows * cols)
        randint(self.board, rows * cols, 0, 1) #0/1 random initialization
        self.rows = rows
        self.cols = cols

    fn exec_game(self):
        while(True):
            print("\033[;H\033[2J")
            self.draw_board()
            sleep(0.1)
            self.next_step()

    fn next_step(self):
        for y in range(self.rows):
            for x in range(self.cols):
                let alive_around: Int = self.num_alive_around(x,y)
                if alive_around == 2:
                    self.next_board.store(self.cols * y + x, self.board.load(self.cols * y + x))
                elif alive_around == 3:
                    self.next_board.store(self.cols * y + x, 1)
                else:
                    self.next_board.store(self.cols * y + x, 0)

        memcpy(self.board, self.next_board, self.rows * self.cols)
        

    fn num_alive_around(self, x: Int, y: Int) -> Int:
        var num_alive: Int = 0
        for dy in range(-1,2,1):
            for dx in range(-1,2,1):
                if dx==0 and dy==0: continue
                if x + dx < 0 or self.cols <= x + dx: continue
                if y + dy < 0 or self.rows <= y + dy: continue
                let idx: Int = self.cols * (y + dy) + x + dx
                if self.board.load(idx)==1:
                    num_alive += 1
        return num_alive 

    fn draw_board(self):
        for y in range(self.rows):
            var one_line: String = ''
            for x in range(self.cols):
                one_line += '@' if self.board.load(self.cols * y + x)==1 else '-'
            print(one_line)


fn main():
    seed()
    var new_game = Lifegame(15,30) # size of game screen
    new_game.exec_game()

実行結果(途中)

$ mojo lifegame.mojo
------------------------@-----
--@--------------------@@-----
-@-@--------------------------
-@@---------------------------
------------------------------
---------------@@-------------
--@@-----------@@-------------
--@@--------------------------
------------------------------
-------------------@@---------
-------------------@@---------
------------------------------
---@@-------------------------
--@-@---@@--------------------
---@----@@--------------------

感想

  • ほぼpythonを書いているような感じだった。
  • 所有権の話をしっかり理解できていない
  • memory系の処理に明るくなく、load, storeの使い方が不明
  • 見てて楽しい

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?