forked from ricktimmis/gobandit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorer.go
39 lines (34 loc) · 889 Bytes
/
scorer.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
package main
import (
"fmt"
)
/* Score provides an evaluation component for calculating rules
against the board. The simplest usage is to initialise a variable with
a function, and then call the evaluate method injecting the function via the variable.
*/
type Score struct {
bd *Board
}
type scorer interface {
init()
evaluate()
}
func (s *Score) init(p *Board) error {
if p == nil {
return fmt.Errorf("Failed passing board pointer")
}
s.bd = p
return nil
}
// FIXME Issue #3 - Recursive Scoring evaluation - It would be great to pass in a set of rules to evaluate, rather than
// making multiple calls.
func (s *Score) evaluate(f func(b *Board) int) (int, error) {
var err = error(nil)
defer func() {
if err := recover(); err != nil {
err = fmt.Errorf("Evaluation failed with error: %v", err)
}
}()
result := f(s.bd)
return result, err
}