This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.rb
262 lines (227 loc) · 4.66 KB
/
game.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
MAP = ["LLLLLLLC~~",
"LLLLLLLC~~",
"LLLCLLLC~~",
"LLC~CHC~~~",
"LLC~B~~~B~",
"LLP~~~~~~~",
"LC~~~~~~~~",
"LC~~~~~~CC",
"CC~B~~CCLC",
"~~~~~B~CC~"]
class Feature
attr_reader :name
def initialize(name, x, y, volume)
@name = name
@x = x
@y = y
@volume = volume
end
def distance_from(x, y)
Math.hypot(x - @x, y - @y)
end
def volume_from(x, y)
distance = distance_from(x, y) + 1
@volume / (distance * distance)
end
end
class Map
def initialize(tiles)
@tiles = tiles
@features = parse_features
end
def each_tile
@tiles.map.with_index do |tile_row, y|
tile_row.chars.map.with_index do |tile_col, x|
yield [tile_col, x, flip(y)]
end
end
end
def parse_features
features = []
each_tile do |tile, x, y|
feature_map = {
"B" => [["Buoy", 0.5]],
"C" => [["Coastline", 0.5]],
"P" => [["Coastline", 0.5],["Port", 0.4]],
"H" => [["Lighthouse", 1.0],["Coastline", 0.5]]
}
if fvs = feature_map[tile]
fvs.each do |(feature, volume)|
features << Feature.new(feature, x, y, volume)
end
end
end
features
end
def height
@tiles.length
end
def width
@tiles.first.length
end
def port
@features.find {|f|
f.name == "Port"
}
end
def flip(y)
(height - 1) - y
end
def tile_category(x,y)
case @tiles[flip(y)][x]
when "P"
:in_port
when "C", "L", "H"
:aground
when "~", "B"
:at_sea
else
:in_proper_trouble
end
end
def valid_start_position?(x,y)
tile_category(x, y) == :at_sea &&
port.distance_from(x, y) > 3
end
def get_start_position
candidate_x = 0
candidate_y = 0
begin
candidate_x = rand(width)
candidate_y = rand(height)
end while !valid_start_position?(candidate_x, candidate_y)
[candidate_x, candidate_y]
end
def draw_boat_at(bx, by)
each_tile do |tile, x, y|
if x == 0
puts
end
if bx == x && by == y
print "^"
else
print tile
end
end
puts
end
def features_audible_from(x, y)
@features.map {|f| [f, f.volume_from(x, y)] }
end
end
class State
def initialize(x, y)
@x = x
@y = y
end
def move(dx, dy)
State.new(@x + dx, @y + dy)
end
def same_position?(x, y)
x == @x && y == @y
end
def ==(another_state)
another_state.same_position?(@x, @y)
end
def audible_features(map)
map.features_audible_from(@x, @y)
end
def status(map)
map.tile_category(@x, @y)
end
def draw_on_map(map)
map.draw_boat_at(@x, @y)
end
def to_json(*args)
{'x' => @x, 'y' => @y}.to_json
end
end
class TextResponder
ACK = "Aye aye, Captain"
def success(state)
[ACK, state]
end
def listen(features_and_volume, state)
above_hearing_threshold = features_and_volume.select {|(_, fv)| fv >0.05 }
response = "You can hear: "+
above_hearing_threshold.map do |(f,fv)|
f.name + " (" + adverb(fv) + ")"
end.join(",")
[response, state]
end
def adverb(volume)
case volume
when 1
"ear-splittingly loud"
when 0.5..1
"very loudly"
when 0.25...0.5
"loudly"
when 0.11...0.25
"in the distance"
else
"barely"
end
end
end
class Game
def initialize(map, responder)
@map = map
@responder = responder
end
def step(state, action)
case action
when :go_north
@responder.success(state.move(0, 1))
when :go_south
@responder.success(state.move(0, -1))
when :go_west
@responder.success(state.move(-1, 0))
when :go_east
@responder.success(state.move(1, 0))
when :listen
@responder.listen(state.audible_features(@map), state)
when :noop
@responder.success(state)
end
end
def status(state)
state.status(@map)
end
end
if __FILE__ == $0
map = Map.new(MAP)
game = Game.new(map, TextResponder.new)
start_x, start_y = map.get_start_position
state = State.new(start_x, start_y)
while true do
print "> "
command = gets.chomp
ack, state = case command
when "north"
game.step(state, :go_north)
when "south"
game.step(state, :go_south)
when "east"
game.step(state, :go_east)
when "west"
game.step(state, :go_west)
when "listen"
game.step(state, :listen)
when "cheat"
state.draw_on_map(map)
game.step(state, :noop)
else
["Huh?", state ]
end
case game.status(state)
when :in_port
puts "You're home!!"
exit 0
when :aground
puts "You crashed!!"
exit 1
end
puts ack
end
end