-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrue.rb
More file actions
79 lines (66 loc) · 2.5 KB
/
Copy pathgrue.rb
File metadata and controls
79 lines (66 loc) · 2.5 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
require_relative 'vertex.rb'
require_relative 'shortestPathAlgorithm.rb'
require 'thread'
class Grue
attr_accessor :world,:path_finder,:current_room,:next_move
# Obtain a reference to the current world Grue lives in
# Create objects for the curent room, world, path_finder ( which supports graph algorithms ), and the next move grue will make
def initialize(world_reference)
@current_room =0
@world=world_reference
@path_finder = ShortestPathAlgorithm.new(world_reference)
@next_move = " "
end
# This method will set Grues location to the room farthest away from the users room.
def set_far_room(userPos)
@current_room = @path_finder.determine_farthest_room_from_user(userPos)
end
# Use some sort of shortest path algorithm to determine which move to make
# This will be the room that takes Grue closest to the user
def determine_next_move(userPos)
@next_move = @path_finder.path_weights[@current_room.title][userPos.title].path.split[0]
end
def move_grue_to_user(userPos)
grue_move(determine_next_move(userPos))
end
#If grue is attacked then move him randomly to adjacent room
def grue_attacked
puts "Grue is scared off, drops something shiny, and flees to an adjacent room!"
move_randomly
end
# Moves grue randomly to an adjacent room.
def move_randomly
paths = @path_finder.find_adjacent_rooms(@current_room)
grue_move(paths[rand(paths.length)])
end
def return_distance_to_user
return @world.grue.path_finder.path_weights[@world.grue.current_room.title][@world.user.current_room.title].distance
end
def grue_close_check
if(@world.grue.return_distance_to_user==1)
puts "You hear a loud growl from one of the adjacent rooms!"
end
end
# This method handles grue movement, he does not have to worry about blocked paths
def grue_move(direction)
case direction
when "North","n","north"
if(@current_room.north!= "0")
@current_room = @world.rooms[@current_room.north]
end
when "East","e","east"
if(@current_room.east!="0")
@current_room = @world.rooms[@current_room.east]
end
when "South","s","south"
if(@current_room.south!="0")
@current_room = @world.rooms[@current_room.south]
end
when "West","w","west"
if(@current_room.west!="0")
@current_room = @world.rooms[@current_room.west]
end
end
determine_next_move(@world.user.current_room)
end
end