class TurtleGame::Game
Constants
- INDEX_MODIFIERS
- MOVESET
Attributes
current_move[RW]
matrix[RW]
move_history[RW]
output[RW]
Public Class Methods
new(width = nil, output)
click to toggle source
# File lib/turtle_game/game.rb, line 9 def initialize(width = nil, output) @width = width @output = output @move_history = [] @matrix = fetch_dimensions end
Public Instance Methods
move(direction)
click to toggle source
# File lib/turtle_game/game.rb, line 16 def move(direction) move_history << parse_direction(direction) flush build_matrix output.puts stringify_matrix end
Private Instance Methods
build_matrix()
click to toggle source
# File lib/turtle_game/game.rb, line 42 def build_matrix last_move = [0, 0] move_history.each_with_index do |move, i| index_modifiers = INDEX_MODIFIERS[move] last_move = last_move.zip(index_modifiers).map(&:sum) matrix[last_move[0]][last_move[1]] = last_move?(i) ? '🐢' : '#' end end
fetch_dimensions()
click to toggle source
# File lib/turtle_game/game.rb, line 60 def fetch_dimensions if @width Array.new(width) { ' ' }.map { Array.new(width) { ' ' } } else fetch_screen_size end end
fetch_screen_size()
click to toggle source
# File lib/turtle_game/game.rb, line 55 def fetch_screen_size dimensions = TTY::Screen.size Array.new(dimensions[0]) { ' ' }.map { Array.new(dimensions[1]) { ' ' } } end
flush()
click to toggle source
# File lib/turtle_game/game.rb, line 38 def flush $stdout.flush end
last_move?(i)
click to toggle source
# File lib/turtle_game/game.rb, line 51 def last_move?(i) move_history.length == i + 1 end
parse_direction(input)
click to toggle source
# File lib/turtle_game/game.rb, line 34 def parse_direction(input) MOVESET[input.strip.downcase] end
stringify_matrix()
click to toggle source
# File lib/turtle_game/game.rb, line 25 def stringify_matrix output = [] matrix.each_with_index do |y_axis, index| matrix[index].each { |x_value| output << x_value } output << "\n" end output.join end