-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkangaroo.rb
62 lines (54 loc) · 1.11 KB
/
kangaroo.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
require_relative 'Die'
require_relative 'Point'
require_relative 'Grid'
class Kangaroo
attr_accessor :position
attr_accessor :die
attr_accessor :grid
attr_accessor :hopCount
def initialize(grid)
@position=Point.new(0, 0)
@die=Die.new
@grid=grid
@hopCount=0
end
public
def hop!
direction = @die.throw
@nextPosition = Point.new(@position.x,@position.y)
case direction
when :north
@nextPosition.y+=1
when :south
@nextPosition.y-=1
when :east
@nextPosition.x+=1
when :west
@nextPosition.x-=1
end
if (grid.lies_outside?(@nextPosition))
puts "Oops, hit the boundary (#{@nextPosition.x},#{@nextPosition.y})"
else
@position=@nextPosition
@hopCount+=1
puts "Hoped to (#{position.x},#{position.y})"
end
end
public
def at_home?
return @position==@grid.homePoint
end
private
def move! (direction)
case direction
when :north
position.y+=1
when :south
position.y-=1
when :east
position.x+=1
else
position.x-=1
end
end
end