Skip to content

Commit

Permalink
created movement test
Browse files Browse the repository at this point in the history
  • Loading branch information
mapra99 committed Nov 2, 2019
1 parent 46adfc4 commit def217c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'byebug'
gem 'minitest'
gem 'ruby2d', '~> 0.9.2'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
GEM
remote: https://rubygems.org/
specs:
byebug (11.0.1)
minitest (5.13.0)
ruby2d (0.9.2)

PLATFORMS
ruby
x64-mingw32

DEPENDENCIES
byebug
minitest
ruby2d (~> 0.9.2)

BUNDLED WITH
Expand Down
18 changes: 10 additions & 8 deletions src/actions/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ def self.move_snake(state)

private

def calc_next_position(state)
def self.calc_next_position(state)
curr_position = state.snake.positions.first
case state.next_direction
when UP
when Model::Direction::UP
Model::Coord.new(curr_position.row - 1,
curr_position.col)
when RIGHT
when Model::Direction::RIGHT
Model::Coord.new(curr_position.row,
curr_position.col + 1)
when DOWN
when Model::Direction::DOWN
Model::Coord.new(curr_position.row + 1,
curr_position.col)
when LEFT
when Model::Direction::LEFT
Model::Coord.new(curr_position.row,
curr_position.col - 1)
end
end

def position_is_valid?(state, position)
def self.position_is_valid?(state, position)
# verificar que esté en la grilla
is_invalid = ((position.row >= state.grid.rows) || (position.row < 0)) ||
((position.col >= state.grid.cols) || (position.col < 0))
Expand All @@ -43,12 +43,14 @@ def position_is_valid?(state, position)
!(state.snake.positions.include? position)
end

def move_snake_to(state, next_position)
def self.move_snake_to(state, next_position)
new_positions = [next_position] + state.snake.positions[0...-1]
state.snake.positions = new_positions

state
end

def end_game(state)
def self.end_game(state)
state.game_finished = true
state
end
Expand Down
2 changes: 1 addition & 1 deletion src/model/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Snake < Struct.new(:positions)
class Grid < Struct.new(:rows, :cols)
end

class State < Struct.new(:snake, :food, :grid, :next_direction, game_finished)
class State < Struct.new(:snake, :food, :grid, :next_direction, :game_finished)
end

def self.initial_state
Expand Down

0 comments on commit def217c

Please sign in to comment.