-
Notifications
You must be signed in to change notification settings - Fork 0
/
specfuncs.jl
107 lines (82 loc) · 2.15 KB
/
specfuncs.jl
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
"""
directindex(direction)
Convert a String `direction` into its relative
index change.
Return: CartesianIndex
"""
function directindex(direction)
dirdict = Dict(
"UP" => (-1, 0), "DOWN" => (1, 0),
"LEFT" => (0, -1), "RIGHT" => (0, 1)
);
return CartesianIndex(dirdict[direction])
end
"""
swaptile!(board, tile1, tile2)
Swap the labels of `tile1` and `tile2` in-place.
"""
function swaptile!(board, tile1, tile2)
"""Swap the labels on the board for
elements at index t1 and t2.
"""
tmp = board[tile1];
board[tile1] = board[tile2];
board[tile2] = tmp;
end
"""
possibleactions(board)
Construct an array of valid actions for `board`
with respect to the tiles being moved (rather
than the blank space).
Return: Array of actions
"""
function possibleactions(board)
zeroindex = findfirst(iszero, board);
# Dict to change perspective of action
# to the tile being moved.
invmovedict = Dict(
"UP" => "DOWN", "DOWN" => "UP",
"LEFT" => "RIGHT", "RIGHT" => "LEFT"
);
possacts = [];
for direction in ("UP", "DOWN", "LEFT", "RIGHT")
modindex = zeroindex + directindex(direction)
if checkbounds(Bool, board, modindex)
act = (modindex, invmovedict[direction])
push!(possacts, act);
end
end
return possacts
end
"""
result(action, board)
Apply `action` to copy of `board` by swapping
tiles.
Return: Copy of modified board.
"""
function result(action, board)
"""Applies an action to the board.
The action should be a tuple in the form
(index, direction) where index is the index
of the piece to be moved, and direction is
one of "UP", "DOWN", "LEFT", "RIGHT"
"""
newboard = copy(board);
index1 = action[1];
index2 = index1 + directindex(action[2]);
swaptile!(newboard, index1, index2)
return newboard
end
"""
expand(board)
Calculate all possible next-move states.
Return: Array of puzzle states.
"""
function expand(board)
states = [];
possacts = possibleactions(board);
for act in possacts
push!(states, result(act, board));
end
return states
end