Class: RbSnake::Models::State
- Inherits:
-
Object
- Object
- RbSnake::Models::State
- Defined in:
- lib/rb_snake/models/state.rb
Instance Attribute Summary collapse
-
#current_direction ⇒ Object
readonly
Returns the value of attribute current_direction.
-
#food ⇒ Object
readonly
Returns the value of attribute food.
-
#game_finished ⇒ Object
readonly
Returns the value of attribute game_finished.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#snake ⇒ Object
readonly
Returns the value of attribute snake.
-
#speed_factor ⇒ Object
readonly
Returns the value of attribute speed_factor.
Class Method Summary collapse
Instance Method Summary collapse
- #finish_game! ⇒ Object
- #game_score ⇒ Object
- #increase_speed ⇒ Object
-
#initialize(snake:, food:, grid:, current_direction:, game_finished:, speed_factor:) ⇒ State
constructor
A new instance of State.
- #update_direction(new_direction) ⇒ Object
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_direction ⇒ Object (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 |
#food ⇒ Object (readonly)
Returns the value of attribute food.
12 13 14 |
# File 'lib/rb_snake/models/state.rb', line 12 def food @food end |
#game_finished ⇒ Object (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 |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
12 13 14 |
# File 'lib/rb_snake/models/state.rb', line 12 def grid @grid end |
#snake ⇒ Object (readonly)
Returns the value of attribute snake.
12 13 14 |
# File 'lib/rb_snake/models/state.rb', line 12 def snake @snake end |
#speed_factor ⇒ Object (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_state ⇒ Object
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_score ⇒ Object
40 41 42 |
# File 'lib/rb_snake/models/state.rb', line 40 def game_score snake.body.length end |
#increase_speed ⇒ Object
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 |