Advertisement
Guest User

an interface between a Slither bot and LittleGolem

a guest
Apr 14th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. import sys
  4. if sys.version_info[0] != 3:
  5. sys.exit("Please run this with python3")
  6.  
  7. from urllib.request import urlopen
  8.  
  9. from lg_fetch import fetch
  10. import numpy
  11. from subprocess import Popen, PIPE
  12. from string import Template
  13.  
  14. sgf_url_template = Template('http://www.littlegolem.net/servlet/sgf/$gid/game.txt')
  15. send_url_template = Template('http://www.littlegolem.net/jsp/game/game.jsp?sendgame=$gid&sendmove=$move')
  16. bot_path = 'bot/build/salazar'
  17. bot_wd = '..'
  18.  
  19. def ask_bot(pos):
  20. bot = Popen(bot_path, stdin=PIPE,
  21. stdout=PIPE,
  22. cwd=bot_wd,
  23. universal_newlines=True)
  24. output = bot.communicate(bytes('setup\n{}think\nquit\n'.format(pos), 'utf-8'))
  25. return output[0].strip()
  26.  
  27. def part_to_lg_notation(part):
  28. return part[0].lower() + chr(int(part[1:]) - 1 + ord('a'))
  29.  
  30. def move_to_lg_notation(move):
  31. parts = move.replace('-', ' ').replace(',', ' ').replace(';', ' ').split(' ')
  32. return ''.join([part_to_lg_notation(part) for part in parts if part != ''])
  33.  
  34.  
  35. def make_board(size, sgf_moves):
  36. board = numpy.zeros((size, size), dtype='int8')
  37. to_move = 1
  38. for move in sgf_moves:
  39. move = move[move.index('[') + 1 : move.index(']')]
  40. if move == 'swap' or move == 'resign':
  41. continue
  42. move = [ord(m) - ord('a') for m in move]
  43. if (len(move) == 6):
  44. board[move[3], move[2]] = board[move[1], move[0]]
  45. board[move[1], move[0]] = 0
  46. board[move[-1], move[-2]] = to_move
  47. to_move = -to_move
  48. return board, to_move
  49.  
  50. def row_to_string(row):
  51. return ''.join(['x' if i > 0 else 'o' if i < 0 else '.' for i in row])
  52.  
  53. def board_to_string(board):
  54. return ''.join([row_to_string(row) + '\n' for row in board[::-1]])
  55.  
  56.  
  57. def get_move(sgf_nodes):
  58. if len(sgf_nodes) == 1:
  59. return 'aa'
  60. if len(sgf_nodes) == 2:
  61. return 'swap'
  62.  
  63. meta = dict([x.split('[') for x in sgf_nodes[0].split(']') if x != ''])
  64. size = int(meta['SZ'])
  65.  
  66. board, to_move = make_board(size, sgf_nodes[1:])
  67. board = '{0} {0} {1}\n{2}'.format(size, 'b' if to_move > 0 else 'w', board_to_string(board))
  68.  
  69. return move_to_lg_notation(ask_bot(board))
  70.  
  71.  
  72. def make_move(game_id):
  73. sgf = urlopen(sgf_url_template.substitute(gid=game_id)).read()
  74. sgf = str(sgf, 'utf-8').strip()
  75. if sgf.startswith('('):
  76. sgf = sgf[1:]
  77. if sgf.endswith(')'):
  78. sgf = sgf[:-1]
  79. nodes = [node for node in sgf.split(';') if node != '']
  80.  
  81. move = get_move(nodes)
  82.  
  83. fetch(send_url_template.substitute(gid=game_id, move=move))
  84. print("game #{}: move {} ".format(game_id, move))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement