def playBestMove(): global gamestate global depth_to_consider global move fuseki_match, fuseki_move = op.make_move(gamestate) if fuseki_match: print('Found a cool fuseki move!') move = fuseki_move[:] gamestate = do_move(gamestate, fuseki_move, True) return print('Thinking hard about this one...') p_predicts = sess1.run(res, feed_dict={ pre_x: np.copy(gamestate), keep_prob: 1.0 })[0] printDistribution(p_predicts) p_indices = [[ i, j ] for i, j in itertools.product(*[range(19), range(19)]) if not np.any(gamestate[i][j])] p_indices.sort(key=lambda x: p_predicts[x[0]][x[1]], reverse=True) i = 0 while bc.suicide(np.copy(gamestate), p_indices[i]): i += 1 bestMove = p_indices[i] move = bestMove[:] gamestate = do_move(gamestate, bestMove, True)
def playMove(): global gamestate global depth_to_consider fuseki_match, fuseki_move = op.make_move(gamestate) if fuseki_match: print('Found a cool fuseki move!') gamestate = do_move(gamestate, fuseki_move, True) return print('Thinking hard about this one...') gametree = growTree(GameTree(name=gamestate), num_moves_considered, depth_to_consider) print('I\'ve made a game tree!') direction = tMiniMax(gametree) gamestate = gametree.children[direction].name
def playBestMove(): global gamestate global depth_to_consider fuseki_match, fuseki_move = op.make_move(gamestate) if fuseki_match: print('Found a cool fuseki move!') gamestate = do_move(gamestate, fuseki_move, True) return print('Thinking hard about this one...') p_predicts = sess1.run(res, feed_dict={pre_x: np.copy(gamestate), keep_prob: 1.0})[0] p_indices = [[i, j] for i, j in itertools.product(*[range(19), range(19)]) if not np.any(gamestate[i][j])] p_indices.sort(key=lambda x: p_predicts[x[0]][x[1]], reverse=True) bestMove = p_indices[0] gamestate = do_move(gamestate, bestMove, True)
def playBestMove(): global gamestate fuseki_match, fuseki_move = op.make_move(gamestate) if fuseki_match: gamestate = do_move(gamestate, fuseki_move) # print('Found a cool fuseki move!') return True p_predicts = sess.run(res, feed_dict={pre_x: np.copy(gamestate), keep_prob: 1.0})[0] p_indices = [[i, j] for i, j in itertools.product(*[range(19), range(19)]) if not np.any(gamestate[i][j])] p_indices.sort(key=lambda x: p_predicts[x[0]][x[1]], reverse=True) i = 0 while i < len(p_indices) and bc.suicide(np.copy(gamestate), p_indices[i]): i += 1 if i == len(p_indices): gamestate = flip(gamestate) return False bestMove = p_indices[i] gamestate = do_move(gamestate, bestMove) return True
def showBoard(): result = " a b c d e f g h i j k l m n o p q r s \n" for i in range(19): result += str(i).zfill(2) + ' ' for j in range(19): if gamestate[i][j][0] == 1: result += '0 ' enumerate( else: result += '+ ' result += '\n' return result def do_move(gs, pos, isBlackMove): if not isBlackMove: gs = flip(gs) gs = bc.boardchange(np.copy(gs), pos) if isBlackMove: gs = flip(gs) return gs def updateTree(root, width, depth): if depth == 0: root.name[1] = sess2.run(res_v, feed_dict={pre_x_v: root.name[0], keep_prob_v: 1.0})[0][0] root.children = None return root if root.children is not None and root.children != []: root.children = [updateTree(c,width,depth-1) for c in root.children] return root p_predicts = sess1.run(res, feed_dict={pre_x: root.name[0], keep_prob: 1.0})[0] p_indices = [[i, j] for i, j in itertools.product(*[range(19), range(19)]) if not np.any(root.name[0][i][j])] p_indices.sort(key=lambda x: p_predicts[x[0]][x[1]], reverse=True) root.children = [updateTree(GameTree(name=do_move(np.copy(root.name[0]), x, True)), width, depth-1) for x in p_indices[:width]] return root def moveTree(direction): global gametree gametree = gametree.children[direction] def matchTreeToState(): global gamestate global gametree if gametree.children is None or gametree.children == []: gametree = GameTree(name=[gamestate, None]) return for c in gametree.children: if gamestate == c.name[0]: gametree = c return gametree = GameTree(name=[gamestate, None]) def playMove(): global gamestate global depth_to_consider global num_moves_considered global gametree fuseki_match, fuseki_move = op.make_move(gamestate) if fuseki_match: print('Found a cool fuseki move!') gamestate = do_move(gamestate, fuseki_move, True) matchTreeToState() return print('Thinking hard about this one...') gametree = updateTree(gametree, num_moves_considered, depth_to_consider) print('I\'ve made a game tree!') moveTree(tMiniMax(gametree)) gamestate = np.copy(gametree.name[0]) def tMin(tree): if tree.children is None or tree.children == []]: return tree.name[1]
import numpy as np import opening as op pos = np.zeros((19, 19, 3), dtype=np.int) pos[3][3][1] = 1 print(op.make_move(pos))