-
Notifications
You must be signed in to change notification settings - Fork 17
/
GreedySavingAgent.m
74 lines (65 loc) · 2.48 KB
/
GreedySavingAgent.m
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
classdef GreedySavingAgent < handle
%GreedyAgent Plays 2048 by picking move that results in most points.
properties (Constant)
rows = 4;
cols = 4;
actions = 4;
action_labels = {'UP', 'RIGHT', 'DOWN', 'LEFT'};
end
properties
game;
end
methods
function this = GreedySavingAgent()
this.game = Game(this.rows, this.cols);
end
function results = play(this, nr_games)
% PLAY Play 2048 game by picking move that results in most points.
% Parameters:
% nr_games - number of games to play. If 1, then also displays debug info.
% Returns scores in those games as a vector.
% play nr_games
for i = 1:nr_games
% initialize game field
this.game.new();
results(i) = 0;
if (nr_games == 1)
disp(this.game.state);
end
% play till end
while (~this.game.end())
best_points = -Inf;
best_action = -1;
best_game = this.game;
action_points = [];
for action = 1:this.actions
% make copy of the game
gamecopy = this.game.copy();
% make a move and observe reward
[points, changed] = gamecopy.move(action);
action_points(action) = points;
if points > best_points & changed
best_points = points;
best_action = action;
best_game = gamecopy;
end
end
global global_states;
global global_points;
global_states(:, :, end+1) = this.game.state;
global_points(end+1, :) = action_points;
% continue from the game with the best reward
points = best_points;
action = best_action;
this.game = best_game;
results(i) = results(i) + points;
if (nr_games == 1)
disp(this.action_labels{action});
disp(this.game.state);
end
end
disp([i results(i)]);
end
end
end
end