-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinatra_app.rb
65 lines (50 loc) · 2.01 KB
/
sinatra_app.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
require 'rubygems' if RUBY_VERSION < "1.9"
require 'sinatra/base'
require 'erb'
$:.unshift Dir.pwd
require 'node.rb'
require 'field.rb'
class MyApp < Sinatra::Base
post '/' do
size = params[:size].nil? ? '8,8'.split(',') : params[:size].split(',')
source = params[:source].nil? ? '0,3'.split(',') : params[:source].split(',')
target = params[:target].nil? ? '5,4'.split(',') : params[:target].split(',')
algo = params[:algo].nil? ? 'breadth_first' : params[:algo]
@board = Field.createboard(size[0].to_i,size[1].to_i)
start = Field.new source[0].to_i,source[1].to_i
target = Field.new target[0].to_i,target[1].to_i
@s = start
@t = target
start_time = Time.now
case algo
when 'breadth_first' then @result = start.breadth_first target
when 'breadth_first_minimal_cost' then @result = start.breadth_first_minimal_cost target
when 'depth_first' then @result = start.depth_first target
when 'depth_first_limit' then @result = start.depth_first_limit target
when 'hillclimbing' then @result = start.hillclimbing target
when 'a_algo' then @result = start.a_algo target
else @result = start.breadth_first target
end
@foo = @result.path[@result.path.size-1].path_cost.to_s
finish_time = Time.new
@time = (finish_time - start_time).to_s
erb :index
end
get '/' do
size = params[:size].nil? ? '8,8'.split(',') : params[:size].split(',')
source = params[:source].nil? ? '0,3'.split(',') : params[:source].split(',')
target = params[:target].nil? ? '5,4'.split(',') : params[:target].split(',')
@foo = "Algo"
@board = Field.createboard(size[0].to_i,size[1].to_i)
start = Field.new source[0].to_i,source[1].to_i
target = Field.new target[0].to_i,target[1].to_i
@s = start
@t = target
start_time = Time.now
@result = start.hillclimbing(target).path
finish_time = Time.new
@time = (finish_time - start_time).to_s
erb :index
end
end
MyApp.run!