-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.asv
More file actions
59 lines (45 loc) · 1.53 KB
/
agent.asv
File metadata and controls
59 lines (45 loc) · 1.53 KB
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
function [best_pos best_rot best_score,best_keystrokes] = agent(state, pos, rot,keystrokes, depth)
best_score = state.CURSCR; % Higher score = better
POS_RANGE = 9;
ROT_RANGE = 4;
MAX_DEPTH = 1;
if depth > MAX_DEPTH
best_pos = pos;
best_rot = rot;
% best_score = costfn(state, pos, rot);
best_keystrokes = keystrokes;
return
end
best_keystrokes = [];
if isequal(depth,2)
state = add_piece(state, state.PRVNUM);
elseif depth > 2
state = add_piece(state,randi(7,1,1));
end
MAX_ROT = [2, 4, 4, 4, 2, 2, 1] - 1;
best_pos = pos;
best_rot = rot;
for r = 0:MAX_ROT(state.PNM)
for p = 1:10
try_state = state; % Make copy of the state
[try_state valid k] = move_piece(try_state, p, r);
%figure(1); imagesc(imresize(try_state.BRDMAT,[40 400])); drawnow
if ~valid, continue; end
[~, new_state] = costfn(try_state, pos, rot);
%cost = costfn_holes(new_state);
%new_state.CURSCR = new_state.CURSCR + cost;
h = find(sum(new_state.BRDMAT),1,'last')
new_state.CURSCR = new_state.CURSCR + -10*find(sum(new_state.BRDMAT),1,'last')
%%% Add shape to cstfn state
% disp(size(new_state.BRDMAT));
figure(2); sc(new_state.BRDMAT); drawnow
pause(0.0000000000000001)
[pp, rr, s, kk] = agent(new_state, p, r,k, depth+1 );
if best_score <= s
best_score = s;
best_pos = pp;
best_rot = rr;
best_keystrokes = kk;
end
end
end