-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
121 lines (106 loc) · 2.56 KB
/
app.go
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
package main
import (
"context"
"sort"
_ "net/http/pprof"
"time"
)
const DEFAULT_COMPLEXITY = 80
const DEFAULT_MAX_RUNTIME = 5000
// App struct
type App struct {
ctx context.Context
search *SearchState
complexity int
maxRuntime time.Duration
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (app *App) startup(ctx context.Context) {
app.ctx = ctx
app.complexity = DEFAULT_COMPLEXITY
app.maxRuntime = time.Duration(DEFAULT_MAX_RUNTIME)
st, err := GenerateState(app.complexity)
if err != nil {
// TODO ?
panic(err)
}
app.search = NewSearch(st)
}
// get current board in the app
func (app *App) GetBoard() [16]t_cell {
return app.search.state.board
}
// generate new board with the app complexity
func (app *App) GenerateBoard() [16]t_cell {
st, err := GenerateState(app.complexity)
if err != nil {
// TODO ?
panic(err)
}
app.search = NewSearch(st)
return app.GetBoard()
}
func (app *App) GetDefaultComplexity() int {
return DEFAULT_COMPLEXITY
}
func (app *App) GetDefaultMaxRuntime() int {
return DEFAULT_MAX_RUNTIME
}
func (app *App) SetComplexity(comp int) {
app.complexity = comp
}
func (app *App) SetMaxRuntime(milliseconds int) {
app.maxRuntime = time.Duration(milliseconds)
}
type IterationData struct {
Board [16]t_cell
}
type SolveResult struct {
Status STATUS
Iterations []IterationData
IterationCount int
//time elapsed in milliseconds
TimeElapsed time.Duration
}
// returns every iteration of the board while solving
func (app *App) Solve() SolveResult {
if app.search.successCode == codeUniq(app.search.state.board) {
return SolveResult{
Status: SUCCESS,
Iterations: []IterationData{{Board: app.search.state.board}},
IterationCount: 0,
TimeElapsed: time.Duration(0),
}
}
now := time.Now()
res, status := app.search.IDAStar(app.maxRuntime)
elapsed := time.Since(now)
if status == SUCCESS {
boards := make([]IterationData, len(res.hasSeen))
boards_slice := []*State{}
// TODO range hasSeen sort by complexity
for _, v := range res.hasSeen {
boards_slice = append(boards_slice, v)
}
sort.Slice(boards_slice, func(i, j int) bool {
return boards_slice[j].complexity > boards_slice[i].complexity
})
for i, v := range boards_slice {
boards[i] = IterationData{Board: v.board}
}
ms := elapsed / time.Millisecond
return SolveResult{
SUCCESS,
boards,
len(boards),
ms,
}
} else {
return SolveResult{Status: status}
}
}