forked from CoderAcademyEdu/morning-challenges-term1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze2.rb
38 lines (37 loc) · 989 Bytes
/
maze2.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
class Maze
def find_position(num)
@player_row = @maze.index { | row | row.index(num)}
@player_col = @maze[@player_row].index(num)
end
def initialize(maze)
@maze = maze
find_position
#set player start location
end
def walk(moves)
# Do each move in turn
for move in moves
#Change player position
case move
when 'N'
@player_row -= 1
when 'S'
@player_row += 1
when 'E'
@player_col += 1
when 'W'
@player_col -= 1
end
#Test the new location
value =(@player_row >= 0 && @player_col >=0) ? @maze[@player_row][@player_col] : nil
case value
when 3
return 'Finish'
when 1, nil
return 'Dead'
end
return 'Lost'
rescue
'Dead'
end
end