Last active
July 3, 2019 14:33
-
-
Save jiro4989/b56e7420772691e40704a58209ab5e71 to your computer and use it in GitHub Desktop.
V言語でライフゲームしてみた (v0.1.11)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| const ( | |
| dead = 0 | |
| live = 1 | |
| ) | |
| fn get_neighbour_cells(board [][]int, x, y int) []int { | |
| mut result := []int | |
| for y2 := y-1; y2 <= y+1; y2++ { | |
| if y2 < 0 || board.len <= y2 { | |
| continue | |
| } | |
| for x2 := x-1; x2 <= x+1; x2++ { | |
| if x2 < 0 || board[y2].len <= x2 { | |
| continue | |
| } | |
| if x == x2 && y == y2 { | |
| continue | |
| } | |
| result << board[y2][x2] | |
| } | |
| } | |
| return result | |
| } | |
| fn is_reproduction(self, living_cell_count int) bool { | |
| if self != dead { | |
| return false | |
| } | |
| return living_cell_count == 3 | |
| } | |
| fn is_generation(self, living_cell_count int) bool { | |
| if self != live { | |
| return false | |
| } | |
| return living_cell_count == 2 || living_cell_count == 3 | |
| } | |
| fn is_underpopulation(self, living_cell_count int) bool { | |
| if self != live { | |
| return false | |
| } | |
| return living_cell_count <= 1 | |
| } | |
| fn is_overpopulation(self, living_cell_count int) bool { | |
| if self != live { | |
| return false | |
| } | |
| return 4 <= living_cell_count | |
| } | |
| fn next_step(board [][]int) [][]int { | |
| // v0.1.11 | |
| // この構文はまだ使えないみたい | |
| // mut new_board := [][]int | |
| // 今は多次元配列をこう定義する | |
| mut new_board := []array_int | |
| for y := 0; y < board.len; y++ { | |
| row := board[y] | |
| mut new_row := []int | |
| for x := 0; x < row.len; x++ { | |
| cell := row[x] | |
| nc := get_neighbour_cells(board, x, y) | |
| mut living_cell_count := 0 | |
| for c in nc { | |
| if c == live { | |
| living_cell_count++ | |
| } | |
| } | |
| if cell == dead { | |
| if is_reproduction(cell, living_cell_count) { | |
| new_row << live | |
| } else { | |
| new_row << dead | |
| } | |
| continue | |
| } | |
| if is_generation(cell, living_cell_count) { | |
| new_row << live | |
| } else if is_underpopulation(cell, living_cell_count) { | |
| new_row << dead | |
| } else if is_overpopulation(cell, living_cell_count) { | |
| new_row << dead | |
| } | |
| } | |
| new_board << new_row | |
| } | |
| return new_board | |
| } | |
| fn print_board(board [][]int) { | |
| for row in board { | |
| mut s := '|' | |
| for c in row { | |
| s += ' $c |' | |
| } | |
| println(s) | |
| } | |
| } | |
| ////////////////////////////////////////////////// | |
| // メイン処理 | |
| ////////////////////////////////////////////////// | |
| mut board := [ | |
| [dead, dead, dead, dead, dead], | |
| [dead, dead, dead, dead, dead], | |
| [dead, live, live, live, dead], | |
| [dead, dead, dead, dead, dead], | |
| [dead, dead, dead, dead, dead], | |
| ] | |
| for { | |
| board = next_step(board) | |
| print_board(board) | |
| println('------------------------------') | |
| time.sleep(1) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment