]> git.scottworley.com Git - slidingtile/blame - sliding_tile.go
Adjacent spaces
[slidingtile] / sliding_tile.go
CommitLineData
0e27f67c
SW
1package main
2
3import "fmt"
4import "os"
5import "strconv"
6
7const HOLE = 0
2c35054f
SW
8const BOARD_DIM = 4
9const BOARD_SIZE = BOARD_DIM * BOARD_DIM
0e27f67c
SW
10
11type Space int8
12type Board [BOARD_SIZE]Space
13
2c35054f
SW
14const (
15 LEFT = -1
16 RIGHT = +1
17 UP = -BOARD_DIM
18 DOWN = +BOARD_DIM
19)
20
0e27f67c 21func read_board_from_strings(in []string) (*Board, error) {
212a5629
SW
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
0e27f67c
SW
34}
35
2c35054f
SW
36func 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
0e27f67c 56func main() {
212a5629
SW
57 start, err := read_board_from_strings(os.Args[1:])
58 if err != nil {
59 panic(err)
60 }
61 fmt.Println(start)
0e27f67c 62}