Skip to content

Commit 2e0833c

Browse files
authored
Merge pull request #1 from algorithmsbooks/tawheeler-patch-1
Update 2048
2 parents bb6e09f + 3498742 commit 2e0833c

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/mdp/2048.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,44 +50,46 @@ const TWENTY_FORTY_EIGHT_MOVE_STRINGS = ["LEFT", "DOWN", "RIGHT", "UP"]
5050
γ::Float64 = 1.0
5151
end
5252

53-
function transition(s::Board, a::TwentyFortyEightAction)
53+
function transition(mdp::TwentyFortyEight, s::Board, a::TwentyFortyEightAction)
5454
s′ = move(s, a)
55-
if s′ == s
55+
if s′ == s # terminal state or illegal action
5656
return s′
5757
end
5858
s′ = insert_tile_rand(s′, draw_tile())
5959
return s′
6060
end
6161

62-
function reward(s::Board, a::TwentyFortyEightAction)
62+
function reward(mdp::TwentyFortyEight, s::Board, a::TwentyFortyEightAction)
6363
s′ = move(s, a)
64-
if s′ == s
64+
if s′ == s # terminal state or illegal action
6565
return -1.0
6666
end
6767
s′ = insert_tile_rand(s′, draw_tile())
6868
return score_board(s′) - score_board(s)
6969
end
7070

71+
function transition_and_reward(mdp::TwentyFortyEight, s::Board, a::TwentyFortyEightAction)
72+
s′ = move(s, a)
73+
if s′ == s # terminal state or illegal action
74+
return (s′, -1.0)
75+
end
76+
s′ = insert_tile_rand(s′, draw_tile())
77+
r = score_board(s′) - score_board(s)
78+
return (s′, r)
79+
end
80+
7181

7282
function MDP(mdp::TwentyFortyEight; γ::Float64=mdp.γ)
7383
return MDP(
7484
γ,
7585
nothing, # no ordered states
7686
DIRECTIONS,
7787
nothing, # no probabilistic transition function
78-
(s,a) -> reward(s, a),
79-
(s, a)->begin
80-
s′ = transition(s, a)
81-
r = reward(s, a)
82-
return (s′, r)
83-
end
88+
(s,a) -> reward(mdp, s, a),
89+
(s, a)-> transition_and_reward(mdp, s,a)
8490
)
8591
end
8692

87-
88-
89-
90-
9193
"""
9294
Print out a 2048 state.
9395
"""

0 commit comments

Comments
 (0)