Skip to content

Instantly share code, notes, and snippets.

@toasterlovin
Created January 20, 2015 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save toasterlovin/ef192c9650c1cab2e2d4 to your computer and use it in GitHub Desktop.
Save toasterlovin/ef192c9650c1cab2e2d4 to your computer and use it in GitHub Desktop.
Our group's solution to the sokoban problem
@level1 = [
['#', '#', '#', '#', '#', '#', '#', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', '.', ' ', ' ', ' ', 'o', '@', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', '#', '#', '#', '#', '#', '#', '#']
]
def input_allowed?(input)
allowed = %w{w a s d q}
if allowed.include?(input)
return true
else
puts "Accepted keystrokes are 'w' (up), 'a' (left), 's' (down), 'd' (right), and 'q' (quit). Unaccepted input gets the rack >:D"
return false
end
end
def a
person_row = nil
person_index = nil
@level1.each do |row|
person_row = row
person_index = row.index("@")
break if person_index
end
if person_index <= 1
puts "Cannot move left"
return
end
object_at_left = person_row[person_index - 1]
case object_at_left
when '#'
return puts "Cannot move left; there's a wall there"
when 'o'
object_two_to_left = person_row[person_index - 2]
case object_two_to_left
when '#'
return puts "Cannot move left; there's a wall there"
when ' '
person_row[person_index - 2] = 'o'
person_row[person_index - 1] = "@"
person_row[person_index] = " "
when '.'
person_row[person_index - 2] = '*'
person_row[person_index - 1] = "@"
person_row[person_index] = " "
return :victory
end
end
end
# Start the game!
################
@level1.each do |row|
puts row.join('')
end
while input = gets.chomp
next unless input_allowed?(input)
break if input == "q"
victory = send(input)
@level1.each do |row|
puts row.join('')
end
if victory == :victory
puts "VICTORY!!!"
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment