Skip to content

Instantly share code, notes, and snippets.

@swoopej
Created November 4, 2015 13:38
Show Gist options
  • Save swoopej/522ca7340b06bebb5973 to your computer and use it in GitHub Desktop.
Save swoopej/522ca7340b06bebb5973 to your computer and use it in GitHub Desktop.
Basic Impementation
class Player
MAX_X = 3
MAX_Y = 3
DIRECTIONS = []
def errors
@errors
end
def initialize
@x = 0
@y = 0
@errors = nil
end
def current_position
[@x, @y]
end
def move_left
if @x == -MAX_X
@errors = "eek too far left!"
else
@x -= 1
end
end
def move_right
if @x == MAX_X
@errors = "eek too far right!"
else
@x += 1
end
end
def move_up
if @y == MAX_Y
@errors = "eek too far up!"
else
@y += 1
end
end
def move_down
if @y == -MAX_Y
@errors = "eek too far up!"
else
@y -= 1
end
end
def reset
@x = 0
@y = 0
@errors = nil
end
end
player = Player.new
while true
puts "You're currently at #{player.current_position}."
puts "How would you like to move?"
move = gets.chomp
player.send(move)
puts player.errors if player.errors
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment