import random import board version = 5 def init(): pass def generate(c): name = 'generate_%s' % (version,) func = globals()[name] return func(c) def generate_1(c): d = board.nextcolor(c) moves = [] for p in range(board.max): if board.legal(c, p): if board.legal(d, p): moves.append(p) if moves: return random.choice(moves) def generate_2(c): d = board.nextcolor(c) moves = [] captures = [] for p in range(board.max): if board.legal(c, p): if board.legal(d, p): moves.append(p) if board.checkcapture(c, p): captures.append(p) if captures: return random.choice(captures) if moves: return random.choice(moves) def generate_3(c): d = board.nextcolor(c) moves = [] captures = [] rescues = [] for p in range(board.max): if not board.legal(c, p): continue if board.legal(d, p): moves.append(p) if board.checkcapture(c, p): captures.append(p) if board.checkcapture(d, p): rescues.append(p) if captures: return random.choice(captures) if rescues: return random.choice(rescues) if moves: return random.choice(moves) def generate_4(c): d = board.nextcolor(c) moves = [] captures = [] rescues = [] for p in range(board.max): if board.isedge(p): if not board.isnear(p, d): continue if not board.legal(c, p): continue if board.legal(d, p): moves.append(p) if board.checkcapture(c, p): captures.append(p) if board.checkcapture(d, p): rescues.append(p) if captures: return random.choice(captures) if rescues: return random.choice(rescues) if moves: return random.choice(moves) def evaluate_5(c, cbs): value = 0 for b in board.iterblock(c): lib = len(board.getlib(b)) size = board.blocksize(b) value += (lib - 1) + (size - 1) * 2 capture = len(cbs) value += capture * 3 return value def generate_5(c): max = 0 d = board.nextcolor(c) best = [] for p in range(board.max): if not board.isempty(p): continue if board.isedge(p): if not board.isnear(p, d): continue if board.countnear(p, c) >= 3: continue try: cbs = board.put(c, p) value = evaluate_5(c, cbs) if value < max: continue elif value > max: max = value best = [p] else: best.append(p) finally: board.unput(c, p, cbs) while best: move = random.choice(best) if board.legal(c, move): return move else: best.remove(move)