]> git.scottworley.com Git - slidingtile/blob - sliding_tile.go
96e00dcc85645a6d997a878028adf06bf42e4af6
[slidingtile] / sliding_tile.go
1 package main
2
3 import "fmt"
4 import "os"
5 import "strconv"
6
7 const HOLE = 0
8 const BOARD_DIM = 4
9 const BOARD_SIZE = BOARD_DIM * BOARD_DIM
10
11 type Space int8
12 type Board [BOARD_SIZE]Space
13
14 const (
15 LEFT = -1
16 RIGHT = +1
17 UP = -BOARD_DIM
18 DOWN = +BOARD_DIM
19 )
20
21 func read_board_from_strings(in []string) (*Board, error) {
22 if len(in) != BOARD_SIZE {
23 return nil, fmt.Errorf("Please provide %d values", BOARD_SIZE)
24 }
25 var b Board
26 for i, s := range in {
27 num, err := strconv.Atoi(s)
28 b[i] = Space(num)
29 if err != nil {
30 return nil, err
31 }
32 }
33 return &b, nil
34 }
35
36 func adjacent_spaces(s Space) []Space {
37 if s < 0 || s >= BOARD_SIZE {
38 panic("Invalid space")
39 }
40 var adjacent []Space
41 if s >= BOARD_DIM {
42 adjacent = append(adjacent, s+UP)
43 }
44 if s < BOARD_SIZE-BOARD_DIM {
45 adjacent = append(adjacent, s+DOWN)
46 }
47 if s%BOARD_DIM != 0 {
48 adjacent = append(adjacent, s+LEFT)
49 }
50 if s%BOARD_DIM != BOARD_DIM-1 {
51 adjacent = append(adjacent, s+RIGHT)
52 }
53 return adjacent
54 }
55
56 func main() {
57 start, err := read_board_from_strings(os.Args[1:])
58 if err != nil {
59 panic(err)
60 }
61 fmt.Println(start)
62 }