Created
June 27, 2018 11:11
-
-
Save jacoyutorius/9b2d16e0d01a8db2f0b56938de204fb8 to your computer and use it in GitHub Desktop.
Rubyでライフゲーム
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
| def main | |
| lifegame = Lifegame.new(width: 100) | |
| generation = 0 | |
| loop do | |
| # puts "Generation: #{generation} ---------------------------" | |
| lifegame.draw | |
| sleep 0.025 | |
| generation += 1 | |
| end | |
| end | |
| class Lifegame | |
| attr_reader :width, :height, :ecosystem | |
| LIVE_SPACE = "@" | |
| BLANK_SPACE = " " | |
| def initialize width: 50, height: 25 | |
| @width = width | |
| @height = height | |
| @ecosystem = Ecosystem.new(width, height) | |
| end | |
| def draw | |
| ecosystem.draw | |
| end | |
| class Ecosystem | |
| attr_reader :area | |
| attr_accessor :lives | |
| def initialize width, height | |
| @area = [] | |
| (0..height).each_with_index do |h, y| | |
| area << (0..width).map.with_index{|w, x| | |
| BLANK_SPACE | |
| } | |
| end | |
| # Paintmino | |
| # | |@|@| | |
| # |@|@| | | |
| # | |@| | | |
| center = [Random.rand(width), Random.rand(height)] | |
| @lives = [ | |
| [center.first, center.last-1], | |
| [center.first+1, center.last-1], | |
| [center.first-1, center.last], | |
| center, | |
| [center.first, center.last+1] | |
| ] | |
| end | |
| def draw | |
| area.each_with_index do |row, y| | |
| p row.map.with_index{|col, x| | |
| live?(x, y) ? LIVE_SPACE : BLANK_SPACE | |
| }.join | |
| end | |
| check | |
| end | |
| def check | |
| new_lives = [] | |
| area.each_with_index do |row, y| | |
| row.each_with_index do |col, x| | |
| center = [x, y] | |
| lives_count = count_around_lives(center) | |
| if live?(x, y) | |
| if (2..3).include? lives_count | |
| # born | |
| new_lives.push(center) | |
| end | |
| else | |
| if lives_count == 3 | |
| # born | |
| new_lives.push(center) | |
| end | |
| end | |
| end | |
| end | |
| lives.clear | |
| lives.concat(new_lives) | |
| end | |
| # |a|b|c| | |
| # |d|*|f| | |
| # |g|h|i| | |
| def count_around_lives center | |
| [ | |
| [center.first-1, center.last-1], | |
| [center.first, center.last-1], | |
| [center.first+1, center.last-1], | |
| [center.first-1, center.last], | |
| [center.first+1, center.last], | |
| [center.first-1, center.last+1], | |
| [center.first, center.last+1], | |
| [center.first+1, center.last+1], | |
| ].inject(0){|sum, col| | |
| sum += live?(col.first, col.last) ? 1 : 0 | |
| } | |
| end | |
| def live? x, y | |
| return false if x < 0 || y < 0 || x > width || y > height | |
| lives.include?([x, y]) | |
| end | |
| def width | |
| area.first.length | |
| end | |
| def height | |
| area.length | |
| end | |
| end | |
| end | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment