From 64d70e1169c42156f5c180492be2592445e2903a Mon Sep 17 00:00:00 2001 From: Guido Schweizer Date: Tue, 31 May 2022 11:37:31 +1200 Subject: [PATCH] added dragonruby part for improved version --- main.rb | 121 +++++++++----------------------------------------------- 1 file changed, 18 insertions(+), 103 deletions(-) diff --git a/main.rb b/main.rb index 711b27b..3eee502 100644 --- a/main.rb +++ b/main.rb @@ -1,87 +1,4 @@ -class Game - attr_reader :map, :round - - def initialize(max_x,max_y,input) - @map = [] - @max_x = max_x - @max_y = max_y - @round = 0 - - max_x.times do |i| - col = [] - max_y.times do |j| - if input.include? [i,j] - col << 1 - else - col << 0 - end - end - map << col - end - end - - def tick - @round += 1 - calculate - - return map - end - - private - - attr_reader :max_x, :max_y - - def calculate - new_map = [] - max_x.times do |x| - col = [] - max_y.times do |y| - new_cell_value = check_cell(x,y,map) - col << new_cell_value - end - new_map << col - end - @map = new_map - end - - def check_cell(x,y,map) - adjacent_count = cell_value(map,x+1,y+1) + - cell_value(map,x+1,y) + - cell_value(map,x+1,y-1) + - cell_value(map,x,y+1) + - cell_value(map,x,y-1) + - cell_value(map,x-1,y+1) + - cell_value(map,x-1,y) + - cell_value(map,x-1,y-1) - - is_live_cell = map[x][y] == 1 - - if is_live_cell - return adjacent_count == 2 || adjacent_count == 3 ? 1 : 0 - else # dead_cell - return adjacent_count == 3 ? 1 : 0 - end - end - - def cell_value(map,x,y) - return 0 if x > max_x - 1 || x < 0 - return 0 if y > max_y - 1 || y < 0 - - map[x][y] - end - - def print_map - puts "\n--- the map ---" - max_y.times do |y| - max_x.times do |x| - char = map[x][y] == 1 ? "#" : "." - print char - end - print "\n" - end - end - -end +require 'app/game.rb' MAX_X = 40 MAX_Y = 24 @@ -104,18 +21,18 @@ INPUT = [ [17,17], ] -SPEED = 0.3 # every second +SPEED = 0.05 # every second TICK_FOR_STEP = (SPEED * 60).round -def tick args +def tick args args.outputs.labels << [640, 500, 'Conways game of life', 5, 1] # args.outputs.labels << [640, 460, 'Go to docs/docs.html and read it!', 5, 1] # args.outputs.labels << [640, 420, 'Join the Discord! https://discord.dragonruby.org', 5, 1] # args.outputs.sprites << [576, 280, 128, 101, 'dragonruby.png'] game = args.state.game if game.nil? - game = Game.new(MAX_X,MAX_Y,INPUT) + game = Game.new(INPUT) args.state.game = game end @@ -133,24 +50,22 @@ def tick args game.tick end - MAX_X.times do |x| - MAX_Y.times do |y| - live_cell = game.map[x][y] == 1 ? true : false - args.outputs.solids << [ - x*BLOCK_SIZE, - y*BLOCK_SIZE, - BLOCK_SIZE, - BLOCK_SIZE, - 219, - 163, - 211, - 255] if live_cell - end + live_cells = game.live_cells + live_cells.each do |x,y| + args.outputs.solids << [ + x*BLOCK_SIZE, + y*BLOCK_SIZE, + BLOCK_SIZE, + BLOCK_SIZE, + 219, + 163, + 211, + 255] end - args.outputs.labels << [640, 600, "Round ##{game.round}", 5, 1] - # args.outputs.labels << [640, 700, "Tick count ##{args.state.tick_count}", 5, 1] - # args.outputs.labels << [10, 710, "framerate: #{args.gtk.current_framerate.round}"] + args.outputs.labels << [640, 600, "Round ##{game.tick_count}", 5, 1] + args.outputs.labels << [640, 700, "Tick count ##{args.state.tick_count}", 5, 1] + args.outputs.labels << [10, 710, "framerate: #{args.gtk.current_framerate.round}"] # render