Skip to content

Instantly share code, notes, and snippets.

@jc00ke
Last active August 29, 2015 14:13
Show Gist options
  • Save jc00ke/553d15f92ad983e0d529 to your computer and use it in GitHub Desktop.
Save jc00ke/553d15f92ad983e0d529 to your computer and use it in GitHub Desktop.
Ruby Brightnight #1 - Sokoban
require "matrix"
require "pry"
class Warehouse
attr_reader :rows
attr_reader :columns
attr_reader :container
UP = "u"
DOWN = "d"
LEFT = "l"
RIGHT = "r"
MOVES = [UP, DOWN, LEFT, RIGHT]
CRATE = 9
PERSON = 2
WALL = 3
SPACE = 0
STORAGE = 8
CRATE_ON_STORAGE = 4
PERSON_ON_STORAGE = 5
MAPPING = {
PERSON => "@",
WALL => "#",
SPACE => " ",
CRATE => "o",
STORAGE => ".",
CRATE_ON_STORAGE => "*",
PERSON_ON_STORAGE => "+"
}
def initialize(rows=4, columns=4)
@rows = rows + 2
@columns = columns + 2
@container = build_container
@number_of_crates = 1
@number_of_storage_bins = 1
end
def print
0.upto(@rows - 1) do |row|
v = @container.row(row)
v.each do |col|
Kernel.print "#{MAPPING[col]} "
end
puts ""
end
puts ""
end
def move(direction)
if not valid_move?(direction)
puts "#{direction} is an invalid move, try again!"
puts ""
end
detect_wall(direction)
end
private
def detect_wall(direction)
end
def valid_move?(direction)
MOVES.include?(direction.downcase)
end
def build_container
Matrix.build(@rows, @columns) { |row, col|
if row == 0 or row == @rows - 1
WALL
elsif col == 0 or col == @columns - 1
WALL
elsif row == 1 and col == 1
PERSON
elsif row == 2 and col == 2
STORAGE
elsif row == 3 and col == 3
CRATE
else
SPACE
end
}
end
end
w = Warehouse.new(5,5)
w.print
puts "Your move: "
while input = gets.chomp
puts "Your move: "
exit if input == "exit"
w.move(input)
w.print
end
@spydermonkee
Copy link

PERFECT JOB!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment