add folders for dragonruby structure
This commit is contained in:
148
sguis-conways/app/game.rb
Normal file
148
sguis-conways/app/game.rb
Normal file
@@ -0,0 +1,148 @@
|
||||
class Game
|
||||
attr_reader :live_cells, :tick_count
|
||||
|
||||
def initialize(live_cells)
|
||||
@tick_count = 0
|
||||
@live_cells = live_cells # Arr[ [x,y] ]
|
||||
end
|
||||
|
||||
def tick(args=nil)
|
||||
calculate_next_gen
|
||||
render(args)
|
||||
@tick_count += 1
|
||||
end
|
||||
|
||||
def trigger_cell(x,y)
|
||||
cell = [x,y]
|
||||
if live_cells.include? cell
|
||||
live_cells.delete(cell)
|
||||
else
|
||||
live_cells << cell
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_writer :live_cells, :tick_count
|
||||
|
||||
def calculate_next_gen
|
||||
next_gen_living_cells = []
|
||||
live_cell_map = convert_cells_to_map
|
||||
|
||||
cells_to_check = calculate_cells_to_check
|
||||
|
||||
cells_to_check.each do |x,y|
|
||||
is_next_gen_living = check_cell(x,y,live_cell_map)
|
||||
next_gen_living_cells << [x,y] if is_next_gen_living
|
||||
end
|
||||
@live_cells = next_gen_living_cells
|
||||
end
|
||||
|
||||
def check_cell(x,y,live_cell_map)
|
||||
adjacent_living_cell_count = adjacent_living_cell_count(x,y,live_cell_map)
|
||||
|
||||
return true if adjacent_living_cell_count == 3
|
||||
return true if adjacent_living_cell_count == 2 && is_living_cell?(x,y,live_cell_map)
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
def is_living_cell?(x,y,live_cell_map)
|
||||
live_cell_map.dig(x)&.dig(y) == true
|
||||
end
|
||||
|
||||
def convert_cells_to_map
|
||||
map = {}
|
||||
live_cells.each do |x,y|
|
||||
map[x] ||= {}
|
||||
map[x][y] = true
|
||||
end
|
||||
|
||||
map
|
||||
end
|
||||
|
||||
def calculate_cells_to_check
|
||||
cells_to_check = live_cells.dup
|
||||
|
||||
live_cells.each do |x,y|
|
||||
cells_to_check += adjacent_cell_coordinates(x,y)
|
||||
end
|
||||
|
||||
cells_to_check.uniq
|
||||
end
|
||||
|
||||
def adjacent_cell_coordinates(x,y)
|
||||
adjacent_cells = []
|
||||
|
||||
-1.upto(1).each do |i|
|
||||
-1.upto(1).each do |j|
|
||||
adjacent_cells << [x+i,y+j]
|
||||
end
|
||||
end
|
||||
adjacent_cells -= [[x,y]]
|
||||
|
||||
adjacent_cells
|
||||
end
|
||||
|
||||
def adjacent_living_cell_count(x,y,live_cell_map)
|
||||
map = live_cell_map
|
||||
count = 0
|
||||
-1.upto(1).each do |i|
|
||||
-1.upto(1).each do |j|
|
||||
next if i==0 && j==0
|
||||
|
||||
count += 1 if map.dig(x+i)&.dig(y+j)
|
||||
end
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
def render(args)
|
||||
# print_console
|
||||
end
|
||||
|
||||
def print_console
|
||||
puts "--- Round #{tick_count} ---"
|
||||
|
||||
max_x = live_cells.map{|x,y| x}.max + 5
|
||||
max_y = live_cells.map{|x,y| y}.max + 5
|
||||
|
||||
max_y.times do |y|
|
||||
max_x.times do |x|
|
||||
if live_cells.include? [x,y]
|
||||
print "*"
|
||||
else
|
||||
print "."
|
||||
end
|
||||
end
|
||||
print "\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# console test
|
||||
|
||||
# INPUT = [
|
||||
# [9,9],
|
||||
# [10,10],
|
||||
# [9,11],
|
||||
# [11,11],
|
||||
# [9,12],
|
||||
# [11,12],
|
||||
# [13,13],
|
||||
# [14,13],
|
||||
# [15,13],
|
||||
# [16,13],
|
||||
# [16,15],
|
||||
# [16,16],
|
||||
# [17,17],
|
||||
# ]
|
||||
|
||||
# game = Game.new(INPUT)
|
||||
|
||||
# 100.times do |i|
|
||||
# game.tick
|
||||
|
||||
# sleep 0.2
|
||||
# end
|
||||
81
sguis-conways/app/main.rb
Normal file
81
sguis-conways/app/main.rb
Normal file
@@ -0,0 +1,81 @@
|
||||
require 'app/game.rb'
|
||||
|
||||
MAX_X = 64
|
||||
MAX_Y = 39
|
||||
|
||||
BLOCK_SIZE = (1280/MAX_X).round
|
||||
|
||||
INPUT = [
|
||||
[31,23],
|
||||
[31,24],
|
||||
[32,23],
|
||||
[31,25],
|
||||
[33,24]
|
||||
]
|
||||
|
||||
SPEED = 0.05 # every second
|
||||
TICK_FOR_STEP = (SPEED * 60).round
|
||||
|
||||
|
||||
def tick args
|
||||
# INIT
|
||||
args.state.paused = true if args.state.paused.nil?
|
||||
|
||||
game = args.state.game
|
||||
if game.nil?
|
||||
game = Game.new(INPUT)
|
||||
args.state.game = game
|
||||
end
|
||||
|
||||
# HANDLE Inputs
|
||||
if args.inputs.keyboard.key_up.space
|
||||
args.state.paused = !args.state.paused
|
||||
end
|
||||
|
||||
# GAME LOOP
|
||||
if !args.state.paused
|
||||
# continue ticks if unpaused
|
||||
if args.state.tick_count % TICK_FOR_STEP == 0
|
||||
game.tick
|
||||
end
|
||||
else
|
||||
#paused
|
||||
args.outputs.labels << [640, 40, "Game is paused. Press <space> to continue", 4, 1]
|
||||
|
||||
# allows adding new cells
|
||||
if args.inputs.mouse.click
|
||||
x = (args.inputs.mouse.click.point.x / BLOCK_SIZE).floor
|
||||
y = (args.inputs.mouse.click.point.y / BLOCK_SIZE).floor
|
||||
game.trigger_cell(x,y)
|
||||
end
|
||||
end
|
||||
|
||||
# RENDER
|
||||
args.outputs.labels << [1160, 710, "Round ##{game.tick_count}"]
|
||||
#args.outputs.labels << [10, 710, "framerate: #{args.gtk.current_framerate.round}"]
|
||||
args.outputs.labels << [640, 710, 'Conways game of life', 5, 1]
|
||||
|
||||
# draw BG
|
||||
args.outputs.solids << [
|
||||
0,
|
||||
0,
|
||||
MAX_X*BLOCK_SIZE,
|
||||
MAX_Y*BLOCK_SIZE,
|
||||
164,
|
||||
212,
|
||||
220,
|
||||
128]
|
||||
|
||||
# draw the cells
|
||||
live_cells = game.live_cells
|
||||
args.outputs.solids << live_cells.map do |x,y|
|
||||
[x*BLOCK_SIZE,
|
||||
y*BLOCK_SIZE,
|
||||
BLOCK_SIZE,
|
||||
BLOCK_SIZE,
|
||||
219,
|
||||
163,
|
||||
211,
|
||||
255]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user