]> git.scottworley.com Git - slidingtile/blob - sliding_tile.go
68cd4b1292f49b8a8c8f5a1c6bd7439bf5951f28
[slidingtile] / sliding_tile.go
1 package main
2
3 import "flag"
4 import "fmt"
5 import "os"
6 import "reflect"
7 import "strconv"
8
9 import "github.com/petar/GoLLRB/llrb"
10 import "github.com/willf/bloom"
11
12 var bloomsize = flag.Int("bloomsize", 2000000, "Size bloomfilter for this many states.")
13
14 const HOLE = 0
15 const BOARD_DIM = 4
16 const BOARD_SIZE = BOARD_DIM * BOARD_DIM
17
18 type Space int8
19 type Board [BOARD_SIZE]Space
20 type Step struct {
21 board Board
22 prev *Step
23 length int
24 }
25
26 const (
27 LEFT = -1
28 RIGHT = +1
29 UP = -BOARD_DIM
30 DOWN = +BOARD_DIM
31 )
32
33 func read_board_from_strings(in []string) (*Board, error) {
34 if len(in) != BOARD_SIZE {
35 return nil, fmt.Errorf("Please provide %d values", BOARD_SIZE)
36 }
37 var b Board
38 for i, s := range in {
39 num, err := strconv.Atoi(s)
40 b[i] = Space(num)
41 if err != nil {
42 return nil, err
43 }
44 }
45 return &b, nil
46 }
47
48 func adjacent_spaces(s Space) []Space {
49 if s < 0 || s >= BOARD_SIZE {
50 panic("Invalid space")
51 }
52 var adjacent []Space
53 if s >= BOARD_DIM {
54 adjacent = append(adjacent, s+UP)
55 }
56 if s < BOARD_SIZE-BOARD_DIM {
57 adjacent = append(adjacent, s+DOWN)
58 }
59 if s%BOARD_DIM != 0 {
60 adjacent = append(adjacent, s+LEFT)
61 }
62 if s%BOARD_DIM != BOARD_DIM-1 {
63 adjacent = append(adjacent, s+RIGHT)
64 }
65 return adjacent
66 }
67
68 func create_successors(start *Step) {
69 for _, adj := adjacent_spaces(start.
70 }
71
72 func sliding_tile(start *Board) error {
73 seen := bloom.NewWithEstimates(uint(*bloomsize), 0.0001)
74 seen.Add(reflect.ValueOf(*start).Bytes())
75 return nil
76 }
77
78 func main() {
79 flag.Parse()
80 start, err := read_board_from_strings(flag.Args())
81 if err != nil {
82 fmt.Println(err)
83 os.Exit(1)
84 }
85 err = sliding_tile(start)
86 if err != nil {
87 fmt.Println(err)
88 os.Exit(1)
89 }
90 }