-
-
Save jeffpeterson/5295426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'turtle' | |
turtles = [Turtle.new, Turtle.new, Turtle.new] | |
while true | |
# get the command | |
print "Enter a command: " | |
s = gets.chomp | |
# exit? | |
if s == 'exit' then | |
break | |
end | |
# parse and route the command | |
turtle_num, t_cmd = s.split(" ", 2) | |
turtles[turtle_num.to_i].parse_command t_cmd | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Turtle | |
module Directions | |
UP = 0 | |
RIGHT = 1 | |
DOWN = 2 | |
LEFT = 3 | |
end | |
MAPSIZE = 20 | |
attr_accessor :x_pos, :y_pos, :is_pen_down, :direction, :the_map | |
def initialize | |
@x_pos = 0 | |
@y_pos = 0 | |
@the_map = Array.new(MAPSIZE) { Array.new(MAPSIZE) { 0 } } | |
@the_map[0][0] = 1 | |
@is_pen_down = false | |
@direction = Directions::RIGHT | |
end | |
def draw_map | |
i = 0 | |
while i < MAPSIZE | |
print "\n" | |
@the_map.each do |sub| | |
print "#{sub[i]} " | |
end | |
i += 1 | |
end | |
print "\n" | |
end | |
def move spaces | |
x_mod = 1 | |
y_mod = 1 | |
case @direction | |
when Directions::UP | |
if @y_pos - spaces < 0 then | |
return | |
else | |
x_mod = 0 | |
y_mod = -1 | |
end | |
when Directions::RIGHT | |
if @x_pos + spaces > MAPSIZE - 1 then | |
return | |
else | |
x_mod = 1 | |
y_mod = 0 | |
end | |
when Directions::DOWN | |
if @y_pos + spaces > MAPSIZE - 1 then | |
return | |
else | |
x_mod = 0 | |
y_mod = 1 | |
end | |
when Directions::LEFT | |
if @x_pos - spaces < 0 then | |
return | |
else | |
x_mod = -1 | |
y_mod = 0 | |
end | |
else | |
return | |
end | |
while spaces > 0 | |
@x_pos += x_mod | |
@y_pos += y_mod | |
if is_pen_down == true | |
@the_map[@x_pos][@y_pos] = 1 | |
end | |
spaces -= 1 | |
end | |
end | |
def put_pen_down | |
@is_pen_down = true | |
end | |
def pick_pen_up | |
@is_pen_down = false | |
end | |
def turn_left | |
@direction = @direction > 0 ? @direction - 1 : @direction = 3 | |
end | |
def turn_right | |
@direction = @direction < 3 ? @direction + 1 : @direction = 0 | |
end | |
def status | |
print "position: <#{@x_pos}, #{@y_pos}>, direction: #{@direction}, pen status: #{@is_pen_down}\n" | |
end | |
def parse_command command | |
commands = command.split | |
case commands[0] | |
when "1" | |
self.pick_pen_up | |
when "2" | |
self.put_pen_down | |
when "3" | |
self.turn_right | |
when "4" | |
self.turn_left | |
when "5" | |
spaces = commands[1].to_i | |
if spaces == 0 | |
print "Invalid command set: '#{command}'\n" | |
return | |
end | |
self.move spaces | |
when "6" | |
self.draw_map | |
when "9" | |
exit | |
else | |
print "Invalid command set: '#{command}'\n" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment