|
| 1 | +"""A example of variant-board elucidator |
| 2 | +""" |
| 3 | + |
| 4 | +import os |
| 5 | +import re |
| 6 | +import json |
| 7 | + |
| 8 | +from reversi import BitBoard, Elucidator |
| 9 | +from reversi import C as c |
| 10 | + |
| 11 | + |
| 12 | +DO_RANDOM_MOVE_MATCHES = True |
| 13 | +DO_BEST = True |
| 14 | +DO_MAX = True |
| 15 | +DO_SHORTEST = True |
| 16 | +VERIFY_RECORD = True |
| 17 | + |
| 18 | +RANDOM_MATCH = 10000 |
| 19 | +CONTROLL = { |
| 20 | + # name : [random, best, max, shortest] |
| 21 | + 'X' : [False, False, False, False], # noqa: E203 |
| 22 | + 'x' : [False, False, False, False], # noqa: E203 |
| 23 | + 'Cross' : [False, False, False, False], # noqa: E203 |
| 24 | + 'Plus' : [False, False, False, False], # noqa: E203 |
| 25 | + 'Drone-8' : [False, False, False, False], # noqa: E203 |
| 26 | + 'Drone-6' : [False, False, False, False], # noqa: E203 |
| 27 | + 'Kazaguruma-8' : [False, False, False, False], # noqa: E203 |
| 28 | + 'Kazaguruma-6' : [False, False, False, False], # noqa: E203 |
| 29 | + 'Manji-8' : [False, False, False, False], # noqa: E203 |
| 30 | + 'Manji-6' : [False, False, False, False], # noqa: E203 |
| 31 | + 'S' : [False, False, False, False], # noqa: E203 |
| 32 | + 'Random' : [False, False, False, False], # noqa: E203 |
| 33 | + 'Square-8' : [False, False, False, False], # noqa: E203 |
| 34 | + 'Square-6' : [False, False, False, False], # noqa: E203 |
| 35 | + 'Square-4' : [True, True, True, True], # noqa: E203 |
| 36 | + 'Rectangle' : [False, False, False, False], # noqa: E203 |
| 37 | + 'Octagon' : [False, False, False, False], # noqa: E203 |
| 38 | + 'Diamond' : [False, False, False, False], # noqa: E203 |
| 39 | + 'T' : [False, False, False, False], # noqa: E203 |
| 40 | + 'Torus' : [False, False, False, False], # noqa: E203 |
| 41 | + 'Two' : [False, False, False, False], # noqa: E203 |
| 42 | + 'Equal' : [False, False, False, False], # noqa: E203 |
| 43 | + 'Xhole' : [False, False, False, False], # noqa: E203 |
| 44 | + 'C' : [False, False, False, False], # noqa: E203 |
| 45 | + 'Rainbow' : [False, False, False, False], # noqa: E203 |
| 46 | + 'Pylamid' : [False, False, False, False], # noqa: E203 |
| 47 | + 'Heart' : [False, False, False, False], # noqa: E203 |
| 48 | + 'Waffle' : [False, False, False, False], # noqa: E203 |
| 49 | + 'Bonsai' : [False, False, False, False], # noqa: E203 |
| 50 | + 'Satellite' : [False, False, False, False], # noqa: E203 |
| 51 | + 'Peach' : [False, False, False, False], # noqa: E203 |
| 52 | + 'Pumpkin' : [False, False, False, False], # noqa: E203 |
| 53 | + 'Scarab' : [False, False, False, False], # noqa: E203 |
| 54 | + 'Globe' : [False, False, False, False], # noqa: E203 |
| 55 | + 'E' : [False, False, False, False], # noqa: E203 |
| 56 | + 'Ring' : [False, False, False, False], # noqa: E203 |
| 57 | + 'Inside' : [False, False, False, False], # noqa: E203 |
| 58 | + 'Outside' : [False, False, False, False], # noqa: E203 |
| 59 | + 'Skull' : [False, False, False, False], # noqa: E203 |
| 60 | + 'Hourglass' : [False, False, False, False], # noqa: E203 |
| 61 | + 'Treasure' : [False, False, False, False], # noqa: E203 |
| 62 | + 'Rosetta' : [False, False, False, False], # noqa: E203 |
| 63 | + 'Chaos' : [False, False, False, False], # noqa: E203 |
| 64 | + 'B' : [False, False, False, False], # noqa: E203 |
| 65 | + 'Blackhole' : [False, False, False, False], # noqa: E203 |
| 66 | + 'W' : [False, False, False, False], # noqa: E203 |
| 67 | + 'Whitehole' : [False, False, False, False], # noqa: E203 |
| 68 | + 'Reunion' : [False, False, False, False], # noqa: E203 |
| 69 | + 'Universe' : [False, False, False, False], # noqa: E203 |
| 70 | + 'Pioneer' : [False, False, False, False], # noqa: E203 |
| 71 | + 'Chair' : [False, False, False, False], # noqa: E203 |
| 72 | + 'Coffeecup' : [False, False, False, False], # noqa: E203 |
| 73 | + 'House' : [False, False, False, False], # noqa: E203 |
| 74 | + 'Alien' : [False, False, False, False], # noqa: E203 |
| 75 | + 'Cyborg' : [False, False, False, False], # noqa: E203 |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +def get_board_conf_properties(conf): |
| 80 | + no = conf['no'] |
| 81 | + continent = conf['continent'] |
| 82 | + first = c.black if not int(conf['first'], 16) else c.white |
| 83 | + size = 8 if not int(conf['size'], 16) else 10 |
| 84 | + hole = int('0x' + ''.join([i.replace('0x', '') for i in conf['hole']]), 16) |
| 85 | + ini_black = int('0x' + ''.join([i.replace('0x', '') for i in conf['init_black']]), 16) |
| 86 | + ini_white = int('0x' + ''.join([i.replace('0x', '') for i in conf['init_white']]), 16) |
| 87 | + return no, continent, first, size, hole, ini_black, ini_white |
| 88 | + |
| 89 | + |
| 90 | +def output_file(board_conf, ex='json'): |
| 91 | + outfile = 'board_conf.' + ex |
| 92 | + deco = str |
| 93 | + if ex == 'json': |
| 94 | + outfile = 'board_conf.' + ex |
| 95 | + deco = decostr |
| 96 | + with open(outfile, "w", encoding='utf8', newline="\n") as f: |
| 97 | + if ex == 'json': |
| 98 | + f.write('{\n') |
| 99 | + else: |
| 100 | + f.write('export const boardConf = {\n') |
| 101 | + |
| 102 | + last = len(board_conf.keys()) |
| 103 | + cnt = 0 |
| 104 | + for name in board_conf.keys(): |
| 105 | + f.write(' "' + name + '": {\n') |
| 106 | + |
| 107 | + # propaty |
| 108 | + conf = board_conf[name] |
| 109 | + f.write(' "no" : ' + str(conf['no']) + ',\n') # noqa: E221 |
| 110 | + f.write(' "continent" : "' + conf['continent'] + '",\n') # noqa: E221 |
| 111 | + f.write(' "type" : "' + conf['type'] + '",\n') # noqa: E221 |
| 112 | + f.write(' "negative" : ' + deco(conf['negative']) + ',\n') # noqa: E221 |
| 113 | + f.write(' "first" : ' + deco(conf['first']) + ',\n') # noqa: E221 |
| 114 | + f.write(' "size" : ' + deco(conf['size']) + ',\n') # noqa: E221 |
| 115 | + f.write(' "hole" : [' + ", ".join([deco(h) for h in conf['hole']]) + '],\n') # noqa: E221 |
| 116 | + f.write(' "color_code" : "' + conf['color_code'] + '",\n') # noqa: E221 |
| 117 | + f.write(' "init_black" : [' + ", ".join([deco(h) for h in conf['init_black']]) + '],\n') # noqa: E221 |
| 118 | + f.write(' "init_white" : [' + ", ".join([deco(h) for h in conf['init_white']]) + '],\n') # noqa: E221 |
| 119 | + f.write(' "init_green" : [' + ", ".join([deco(h) for h in conf['init_green']]) + '],\n') # noqa: E221 |
| 120 | + f.write(' "init_ash" : [' + ", ".join([deco(h) for h in conf['init_ash']]) + '],\n') # noqa: E221 |
| 121 | + f.write(' "black" : ' + '[]' + ',\n') # noqa: E221 |
| 122 | + f.write(' "white" : ' + '[]' + ',\n') # noqa: E221 |
| 123 | + f.write(' "squares" : "' + conf['squares'] + '",\n') # noqa: E221 |
| 124 | + f.write(' "blanks" : ' + str(conf['blanks']) + ',\n') # noqa: E221 |
| 125 | + f.write(' "random_10000_matches" : "' + conf['random_10000_matches'] + '",\n') # noqa: E221 |
| 126 | + f.write(' "best_match_winner" : "' + conf['best_match_winner'] + '",\n') # noqa: E221 |
| 127 | + f.write(' "best_match_score" : "' + conf['best_match_score'] + '",\n') # noqa: E221 |
| 128 | + f.write(' "best_match_record" : "' + conf['best_match_record'] + '",\n') # noqa: E221 |
| 129 | + f.write(' "black_max_score" : "' + conf['black_max_score'] + '",\n') # noqa: E221 |
| 130 | + f.write(' "black_max_record" : "' + conf['black_max_record'] + '",\n') # noqa: E221 |
| 131 | + f.write(' "white_max_score" : "' + conf['white_max_score'] + '",\n') # noqa: E221 |
| 132 | + f.write(' "white_max_record" : "' + conf['white_max_record'] + '",\n') # noqa: E221 |
| 133 | + f.write(' "black_shortest_move_count": ' + str(conf['black_shortest_move_count']) + ',\n') # noqa: E221 |
| 134 | + f.write(' "black_shortest_record" : "' + conf['black_shortest_record'] + '",\n') # noqa: E221 |
| 135 | + f.write(' "white_shortest_move_count": ' + str(conf['white_shortest_move_count']) + ',\n') # noqa: E221 |
| 136 | + f.write(' "white_shortest_record" : "' + conf['white_shortest_record'] + '",\n') # noqa: E221 |
| 137 | + f.write(' "note" : "' + conf['note'] + '"\n') # noqa: E221 |
| 138 | + |
| 139 | + if cnt == last - 1: |
| 140 | + f.write(' }\n') |
| 141 | + else: |
| 142 | + f.write(' },\n') |
| 143 | + cnt += 1 |
| 144 | + f.write('}\n') |
| 145 | + |
| 146 | + |
| 147 | +def decostr(string): |
| 148 | + return '"' + str(string) + '"' |
| 149 | + |
| 150 | + |
| 151 | +def get_scores(socre): |
| 152 | + match = re.search(r'\(black\) (\d+) -', score) |
| 153 | + black_score = match.group(1) |
| 154 | + match = re.search(r'- (\d+) \(white\)', score) |
| 155 | + white_score = match.group(1) |
| 156 | + return int(black_score), int(white_score) |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == '__main__': |
| 160 | + # board conf |
| 161 | + board_conf_json = 'board_conf.json' |
| 162 | + board_conf = {} |
| 163 | + if os.path.isfile(board_conf_json): |
| 164 | + with open(board_conf_json) as f: |
| 165 | + board_conf = json.load(f) |
| 166 | + |
| 167 | + # elucidate |
| 168 | + for name in board_conf.keys(): |
| 169 | + conf = board_conf[name] |
| 170 | + no, continent, first, size, hole, ini_black, ini_white = get_board_conf_properties(conf) |
| 171 | + |
| 172 | + print('-------------------------------') |
| 173 | + board = BitBoard(size=size, hole=hole, ini_black=ini_black, ini_white=ini_white) |
| 174 | + print(board) |
| 175 | + print('No. :', no) |
| 176 | + print('cotinent :', continent) |
| 177 | + print('name :', name) |
| 178 | + print('size :', size) |
| 179 | + |
| 180 | + conf['squares'] = "?" |
| 181 | + conf['blanks'] = '"?"' |
| 182 | + |
| 183 | + if 'random_10000_matches' not in conf: |
| 184 | + conf['random_10000_matches'] = "?" |
| 185 | + |
| 186 | + if 'best_match_winner' not in conf: |
| 187 | + conf['best_match_winner'] = "?" |
| 188 | + if 'best_match_score' not in conf: |
| 189 | + conf['best_match_score'] = "?" |
| 190 | + if 'best_match_record' not in conf: |
| 191 | + conf['best_match_record'] = "?" |
| 192 | + |
| 193 | + if 'black_max_record' not in conf: |
| 194 | + conf['black_max_record'] = "?" |
| 195 | + if 'black_max_score' not in conf: |
| 196 | + conf['black_max_score'] = "?" |
| 197 | + if 'white_max_record' not in conf: |
| 198 | + conf['white_max_record'] = "?" |
| 199 | + if 'white_max_score' not in conf: |
| 200 | + conf['white_max_score'] = "?" |
| 201 | + |
| 202 | + if 'black_shortest_move_count' not in conf or conf['black_shortest_move_count'] == '?': |
| 203 | + conf['black_shortest_move_count'] = '"?"' |
| 204 | + if 'black_shortest_record' not in conf: |
| 205 | + conf['black_shortest_record'] = "?" |
| 206 | + if 'white_shortest_move_count' not in conf or conf['white_shortest_move_count'] == '?': |
| 207 | + conf['white_shortest_move_count'] = '"?"' |
| 208 | + if 'white_shortest_record' not in conf: |
| 209 | + conf['white_shortest_record'] = "?" |
| 210 | + |
| 211 | + if name == 'Random': |
| 212 | + print('skip Random') |
| 213 | + continue |
| 214 | + if name == 'Chaos': |
| 215 | + print('skip Chaos') |
| 216 | + continue |
| 217 | + if name == 'Pioneer': |
| 218 | + print('skip Pioneer') |
| 219 | + continue |
| 220 | + |
| 221 | + elucidator = Elucidator(name, size, first, hole, ini_black, ini_white) |
| 222 | + |
| 223 | + squares = elucidator.squares |
| 224 | + blanks = elucidator.blanks |
| 225 | + conf['squares'] = squares |
| 226 | + conf['blanks'] = blanks |
| 227 | + print('squares :', squares) |
| 228 | + print('blanks :', blanks) |
| 229 | + |
| 230 | + if CONTROLL[name][0] and DO_RANDOM_MOVE_MATCHES: |
| 231 | + conf['random_10000_matches'] = elucidator.get_random_match_result(RANDOM_MATCH) |
| 232 | + |
| 233 | + if CONTROLL[name][1] and DO_BEST: |
| 234 | + conf["best_match_winner"], conf["best_match_score"], conf["best_match_record"] = elucidator.get_best_match_winner() |
| 235 | + |
| 236 | + if CONTROLL[name][2] and DO_MAX: |
| 237 | + conf["black_max_score"], conf["black_max_record"], conf["white_max_score"], conf["white_max_record"] = elucidator.get_max_winner() |
| 238 | + |
| 239 | + if CONTROLL[name][3] and DO_SHORTEST: |
| 240 | + conf["black_shortest_move_count"], conf["black_shortest_record"], conf["white_shortest_move_count"], conf["white_shortest_record"] = elucidator.get_shortest_winner() # noqa: E501 |
| 241 | + |
| 242 | + if VERIFY_RECORD: |
| 243 | + if conf["best_match_record"] != "?": |
| 244 | + score = conf["best_match_score"] |
| 245 | + black_score, white_score = get_scores(score) |
| 246 | + print('\n>>> verify best_match_record :', conf["best_match_record"]) |
| 247 | + elucidator.verify_record(conf["best_match_record"], black_score=black_score, white_score=white_score) |
| 248 | + if conf["black_max_record"] != "?": |
| 249 | + score = conf["black_max_score"] |
| 250 | + black_score, white_score = get_scores(score) |
| 251 | + print('\n>>> verify black_max_record :', conf["black_max_record"]) |
| 252 | + elucidator.verify_record(conf["black_max_record"], black_score=black_score, white_score=white_score) |
| 253 | + if conf["white_max_record"] != "?": |
| 254 | + score = conf["white_max_score"] |
| 255 | + black_score, white_score = get_scores(score) |
| 256 | + print('\n>>> verify white_max_record :', conf["white_max_record"]) |
| 257 | + elucidator.verify_record(conf["white_max_record"], black_score=black_score, white_score=white_score) |
| 258 | + if conf["black_shortest_record"] != "?": |
| 259 | + move_count = conf["black_shortest_move_count"] |
| 260 | + print('\n>>> verify black_shortest_record :', conf["black_shortest_record"]) |
| 261 | + elucidator.verify_record(conf["black_shortest_record"], move_count=move_count) |
| 262 | + if conf["white_shortest_record"] != "?": |
| 263 | + move_count = conf["white_shortest_move_count"] |
| 264 | + print('\n>>> verify white_shortest_record :', conf["white_shortest_record"]) |
| 265 | + elucidator.verify_record(conf["white_shortest_record"], move_count=move_count) |
| 266 | + |
| 267 | + print('-------------------------------') |
| 268 | + |
| 269 | + # save conf |
| 270 | + output_file(board_conf, 'json') |
| 271 | + output_file(board_conf, 'js') |
0 commit comments