Skip to content

Commit

Permalink
associated actions and view in APp
Browse files Browse the repository at this point in the history
  • Loading branch information
mapra99 committed Nov 2, 2019
1 parent def217c commit 4a35093
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exit
actual_state
n
expected_state.snake
19 changes: 14 additions & 5 deletions src/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

require_relative 'view/ruby2d'
require_relative 'model/state'
require_relative 'actions/actions'

class App
def initialize
@state = Model.initial_state
end

def start
view = View::Ruby2dView.new
initial_state = Model.initial_state
view.render(initial_state)
Thread.new { init_timer(view) }
view.start(@state)
end

def init_timer
def init_timer(view)
loop do
sleep 0.5
# trigger
@state = Actions::move_snake(@state)
view.render(@state)
sleep 0.1
end
end
end

app = App.new
app.start
7 changes: 5 additions & 2 deletions src/view/ruby2d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ def initialize
@pixel_size = 50
end

def render(state)
def start(state)
extend Ruby2D::DSL
set(title: 'Snake',
width: @pixel_size * state.grid.cols,
height: @pixel_size * state.grid.rows)
show
end

def render(state)
render_food(state)
render_snake(state)
show
end

private
Expand Down
30 changes: 30 additions & 0 deletions test/actions_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'minitest/autorun'
require_relative '../src/actions/actions'
require_relative '../src/model/state'
require 'byebug'

class ActionsTest < Minitest::Test
def test_move_snake
initial_state = Model::State.new(
Model::Snake.new([Model::Coord.new(1, 1),
Model::Coord.new(0, 1)]),
Model::Food.new(4, 4),
Model::Grid.new(8, 12),
Model::Direction::DOWN,
false
)

expected_state = Model::State.new(
Model::Snake.new([Model::Coord.new(2, 1),
Model::Coord.new(1, 1)]),
Model::Food.new(4, 4),
Model::Grid.new(8, 12),
Model::Direction::DOWN,
false
)
actual_state = Actions::move_snake(initial_state)
assert_equal actual_state, expected_state
end
end

0 comments on commit 4a35093

Please sign in to comment.