Class: RbSnake::Models::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rb_snake/models/state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snake:, food:, grid:, current_direction:, game_finished:, speed_factor:) ⇒ State

Returns a new instance of State.



14
15
16
17
18
19
20
21
# File 'lib/rb_snake/models/state.rb', line 14

def initialize(snake:, food:, grid:, current_direction:, game_finished:, speed_factor:)
  @snake = snake
  @food = food
  @grid = grid
  @current_direction = current_direction
  @game_finished = game_finished
  @speed_factor = speed_factor
end

Instance Attribute Details

#current_directionObject (readonly)

Returns the value of attribute current_direction.



12
13
14
# File 'lib/rb_snake/models/state.rb', line 12

def current_direction
  @current_direction
end

#foodObject (readonly)

Returns the value of attribute food.



12
13
14
# File 'lib/rb_snake/models/state.rb', line 12

def food
  @food
end

#game_finishedObject (readonly)

Returns the value of attribute game_finished.



12
13
14
# File 'lib/rb_snake/models/state.rb', line 12

def game_finished
  @game_finished
end

#gridObject (readonly)

Returns the value of attribute grid.



12
13
14
# File 'lib/rb_snake/models/state.rb', line 12

def grid
  @grid
end

#snakeObject (readonly)

Returns the value of attribute snake.



12
13
14
# File 'lib/rb_snake/models/state.rb', line 12

def snake
  @snake
end

#speed_factorObject (readonly)

Returns the value of attribute speed_factor.



12
13
14
# File 'lib/rb_snake/models/state.rb', line 12

def speed_factor
  @speed_factor
end

Class Method Details

.initial_stateObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rb_snake/models/state.rb', line 23

def self.initial_state
  new(
    snake: Snake.new(
      body: [Coordinate.new(row: 1, col: 1), Coordinate.new(row: 0, col: 1)]
    ),
    food: Food.new(row: 10, col: 1),
    grid: Grid.new(rows: 12, cols: 24),
    current_direction: Direction::DOWN,
    game_finished: false,
    speed_factor: 6
  )
end

Instance Method Details

#finish_game!Object



36
37
38
# File 'lib/rb_snake/models/state.rb', line 36

def finish_game!
  @game_finished = true
end

#game_scoreObject



40
41
42
# File 'lib/rb_snake/models/state.rb', line 40

def game_score
  snake.body.length
end

#increase_speedObject



48
49
50
# File 'lib/rb_snake/models/state.rb', line 48

def increase_speed
  @speed_factor = [speed_factor - 1, 1].max # the lower, the faster
end

#update_direction(new_direction) ⇒ Object



44
45
46
# File 'lib/rb_snake/models/state.rb', line 44

def update_direction(new_direction)
  @current_direction = new_direction
end