Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- import sys
- if sys.version_info[0] != 3:
- sys.exit("Please run this with python3")
- from urllib.request import urlopen
- from lg_fetch import fetch
- import numpy
- from subprocess import Popen, PIPE
- from string import Template
- sgf_url_template = Template('http://www.littlegolem.net/servlet/sgf/$gid/game.txt')
- send_url_template = Template('http://www.littlegolem.net/jsp/game/game.jsp?sendgame=$gid&sendmove=$move')
- bot_path = 'bot/build/salazar'
- bot_wd = '..'
- def ask_bot(pos):
- bot = Popen(bot_path, stdin=PIPE,
- stdout=PIPE,
- cwd=bot_wd,
- universal_newlines=True)
- output = bot.communicate(bytes('setup\n{}think\nquit\n'.format(pos), 'utf-8'))
- return output[0].strip()
- def part_to_lg_notation(part):
- return part[0].lower() + chr(int(part[1:]) - 1 + ord('a'))
- def move_to_lg_notation(move):
- parts = move.replace('-', ' ').replace(',', ' ').replace(';', ' ').split(' ')
- return ''.join([part_to_lg_notation(part) for part in parts if part != ''])
- def make_board(size, sgf_moves):
- board = numpy.zeros((size, size), dtype='int8')
- to_move = 1
- for move in sgf_moves:
- move = move[move.index('[') + 1 : move.index(']')]
- if move == 'swap' or move == 'resign':
- continue
- move = [ord(m) - ord('a') for m in move]
- if (len(move) == 6):
- board[move[3], move[2]] = board[move[1], move[0]]
- board[move[1], move[0]] = 0
- board[move[-1], move[-2]] = to_move
- to_move = -to_move
- return board, to_move
- def row_to_string(row):
- return ''.join(['x' if i > 0 else 'o' if i < 0 else '.' for i in row])
- def board_to_string(board):
- return ''.join([row_to_string(row) + '\n' for row in board[::-1]])
- def get_move(sgf_nodes):
- if len(sgf_nodes) == 1:
- return 'aa'
- if len(sgf_nodes) == 2:
- return 'swap'
- meta = dict([x.split('[') for x in sgf_nodes[0].split(']') if x != ''])
- size = int(meta['SZ'])
- board, to_move = make_board(size, sgf_nodes[1:])
- board = '{0} {0} {1}\n{2}'.format(size, 'b' if to_move > 0 else 'w', board_to_string(board))
- return move_to_lg_notation(ask_bot(board))
- def make_move(game_id):
- sgf = urlopen(sgf_url_template.substitute(gid=game_id)).read()
- sgf = str(sgf, 'utf-8').strip()
- if sgf.startswith('('):
- sgf = sgf[1:]
- if sgf.endswith(')'):
- sgf = sgf[:-1]
- nodes = [node for node in sgf.split(';') if node != '']
- move = get_move(nodes)
- fetch(send_url_template.substitute(gid=game_id, move=move))
- print("game #{}: move {} ".format(game_id, move))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement