9 #include "patternplay/patternplay.h"
11 #include "patternsp.h"
12 #include "patternprob.h"
16 /* Internal engine state. */
20 struct pattern_setup pat
;
25 patternplay_genmove(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
27 struct patternplay
*pp
= e
->data
;
29 struct pattern pats
[b
->flen
];
30 floating_t probs
[b
->flen
];
31 pattern_rate_moves(&pp
->pat
, b
, color
, pats
, probs
);
34 for (int f
= 0; f
< b
->flen
; f
++) {
35 if (pp
->debug_level
>= 5 && probs
[f
] >= 0.001) {
36 char s
[256]; pattern2str(s
, &pats
[f
]);
37 fprintf(stderr
, "\t%s: %.3f %s\n", coord2sstr(b
->f
[f
], b
), probs
[f
], s
);
39 if (probs
[f
] > probs
[best
])
43 return coord_copy(b
->f
[best
]);
47 patternplay_evaluate(struct engine
*e
, struct board
*b
, struct time_info
*ti
, floating_t
*vals
, enum stone color
)
49 struct patternplay
*pp
= e
->data
;
51 struct pattern pats
[b
->flen
];
52 pattern_rate_moves(&pp
->pat
, b
, color
, pats
, vals
);
55 // unused variable 'total' in above call to pattern_rate_moves()
56 floating_t total
= pattern_rate_moves(&pp
->pat
, b
, color
, pats
, vals
);
57 /* Rescale properly. */
58 for (int f
= 0; f
< b
->flen
; f
++) {
63 if (pp
->debug_level
>= 4) {
64 for (int f
= 0; f
< b
->flen
; f
++) {
65 if (vals
[f
] >= 0.001) {
66 char s
[256]; pattern2str(s
, &pats
[f
]);
67 fprintf(stderr
, "\t%s: %.3f %s\n", coord2sstr(b
->f
[f
], b
), vals
[f
], s
);
75 patternplay_state_init(char *arg
)
77 struct patternplay
*pp
= calloc2(1, sizeof(struct patternplay
));
78 bool pat_setup
= false;
80 pp
->debug_level
= debug_level
;
83 char *optspec
, *next
= arg
;
86 next
+= strcspn(next
, ",");
87 if (*next
) { *next
++ = 0; } else { *next
= 0; }
89 char *optname
= optspec
;
90 char *optval
= strchr(optspec
, '=');
91 if (optval
) *optval
++ = 0;
93 if (!strcasecmp(optname
, "debug")) {
95 pp
->debug_level
= atoi(optval
);
99 } else if (!strcasecmp(optname
, "patterns") && optval
) {
100 patterns_init(&pp
->pat
, optval
, false, true);
104 fprintf(stderr
, "patternplay: Invalid engine argument %s or missing value\n", optname
);
111 patterns_init(&pp
->pat
, NULL
, false, true);
117 engine_patternplay_init(char *arg
, struct board
*b
)
119 struct patternplay
*pp
= patternplay_state_init(arg
);
120 struct engine
*e
= calloc2(1, sizeof(struct engine
));
121 e
->name
= "PatternPlay Engine";
122 e
->comment
= "I select moves blindly according to learned patterns. I won't pass as long as there is a place on the board where I can play. When we both pass, I will consider all the stones on the board alive.";
123 e
->genmove
= patternplay_genmove
;
124 e
->evaluate
= patternplay_evaluate
;