-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.rb
66 lines (59 loc) · 1.44 KB
/
robot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require './table.rb'
#needed for deg to rad conversion
class Numeric
def to_rad
self * Math::PI / 180
end
end
#Robot is an implementation of the generic Piece.
class Robot < Piece
def initialize
@piece_type = 'Robot'
end
def move
check_on_table_exception
new_x = @x + Math.cos(facing_degrees.to_rad).round(0)
new_y = @y + Math.sin(facing_degrees.to_rad).round(0)
@table.admissible_place_for(new_x,new_y)
@x = new_x
@y = new_y
end
def right
check_on_table_exception
new_degree = (@facing_degrees+90)%360
set_facing(new_degree)
end
def left
check_on_table_exception
new_degree = (@facing_degrees-90)%360
set_facing(new_degree)
end
#set facing of the robot. Input can be a direction(string) or an angle(integer)
private
def set_facing(par)
if par.is_a? Integer
@facing_degrees = par
case par
when 0 then @facing = "East"
when 90 then @facing = "North"
when 180 then @facing = "West"
when 270 then @facing = "South"
else
raise ArgumentError, "Wrong facing parameter. Be sure to use a valid angle"
end
elsif par.is_a? String
@facing = par
case par.downcase
when /^e/ then @facing_degree = 0
when /^w/ then @facing_degree = 180
when /^n/ then @facing_degree = 90
when /^s/ then @facing_degree = 270
else
raise ArgumentError, "Wrong facing parameter. Be sure to use a valid direction"
end
end
end
def to_s
"#{@x},#{@y},#{@facing}"
end
end