Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. import os
  2. import sys
  3. from subprocess import Popen, PIPE, STDOUT
  4.  
  5.  
  6. def main():
  7. # Get robots who are fighting (player1, player2)
  8. bot1, bot2 = get_bots()
  9. # Simulate game init input
  10. send_init('1', bot1)
  11. send_init('2', bot2)
  12. round_num = 1
  13. move = 1
  14. field = ','.join(['0'] * 81)
  15. macroboard = ','.join(['-1'] * 9)
  16. print_board(field, macroboard, round_num, '')
  17. while True:
  18. for bot_id, bot in [('1', bot1), ('2', bot2)]:
  19. # Wait for any key
  20. raw_input()
  21. # Send inputs to bot
  22. move = send_update(bot, round_num, move, field, macroboard)
  23. # Update macroboard and game field
  24. field = update_field(field, move, str(bot_id))
  25. macroboard = update_macroboard(field, move)
  26. # Check for winner. If winner, exit.
  27. print_board(field, macroboard, round_num, move)
  28. if is_winner(macroboard):
  29. return
  30.  
  31. round_num += 1
  32.  
  33.  
  34. def get_bots():
  35. root = os.path.dirname(os.path.realpath(__file__))
  36. files = os.listdir(root)
  37. bots = [f for f in files
  38. if os.path.isdir(os.path.join(root, f)) and f != '.git']
  39.  
  40. bot_list = '\n'.join(
  41. ['{}. {}'.format(i, bot) for i, bot in enumerate(bots)])
  42.  
  43. bot1_name = bots[int(raw_input(
  44. 'Choose Player 1:\n' + bot_list + '\n\n> '))]
  45. bot2_name = bots[int(raw_input(
  46. 'Choose Player 2:\n' + bot_list + '\n\n> '))]
  47.  
  48. bot1 = Popen(['python', 'main.py'],
  49. cwd=os.path.join(root, bot1_name),
  50. stdout=PIPE,
  51. stdin=PIPE,
  52. stderr=STDOUT)
  53. bot2 = Popen(['python', 'main.py'],
  54. cwd=os.path.join(root, bot2_name),
  55. stdout=PIPE,
  56. stdin=PIPE,
  57. stderr=STDOUT)
  58.  
  59. return bot1, bot2
  60.  
  61.  
  62. def send_init(bot_id, bot):
  63. init_input = (
  64. 'settings timebank 10000\n'
  65. 'settings time_per_move 500\n'
  66. 'settings player_names player1,player2\n'
  67. 'settings your_bot player{bot_id}\n'
  68. 'settings your_botid {bot_id}\n'.format(bot_id=bot_id))
  69.  
  70. bot.stdin.write(init_input)
  71.  
  72.  
  73. def send_update(bot, round_num, move, field, macroboard):
  74. update_input = (
  75. 'update game round {round}\n'
  76. 'update game move {move}\n'
  77. 'update game field {field}\n'
  78. 'update game macroboard {macro}\n'
  79. 'action move 10000\n'.format(
  80. round=round_num,
  81. move=move,
  82. field=field,
  83. macro=macroboard))
  84.  
  85. bot.stdin.write(update_input)
  86. out = bot.stdout.readline().strip()
  87. print 'bot output: ' + repr(out)
  88. return out
  89.  
  90.  
  91. def update_field(field, move, bot_id):
  92. col, row = move.split(' ')[1:3]
  93. arr = field.split(',')
  94. index = int(row) * 9 + int(col)
  95. if arr[index] != '0':
  96. raise RuntimeError(
  97. 'Square {col} {row} already occupied by {occ}.'.format(
  98. col=col, row=row, occ=arr[index]))
  99.  
  100. arr[index] = bot_id
  101. return ','.join(arr)
  102.  
  103.  
  104. def update_macroboard(field, move):
  105. # break it up into small boards
  106. board = field.split(',')
  107. small_boards = []
  108. for r in range(0, 9, 3):
  109. for c in range(0, 9, 3):
  110. sb = []
  111. sb.extend(board[r * 9 + c:r * 9 + c + 3])
  112. sb.extend(board[(r + 1) * 9 + c:(r + 1) * 9 + c + 3])
  113. sb.extend(board[(r + 2) * 9 + c:(r + 2) * 9 + c + 3])
  114. small_boards.append(sb)
  115.  
  116. # determine macro board state
  117. def get_state(a):
  118. winopts = [
  119. [0, 1, 2],
  120. [3, 4, 5],
  121. [6, 7, 8],
  122. [0, 3, 6],
  123. [1, 4, 7],
  124. [2, 5, 8],
  125. [0, 4, 8],
  126. [6, 4, 2]]
  127.  
  128. winners = ('111', '222')
  129. for opt in winopts:
  130. val = a[opt[0]] + a[opt[1]] + a[opt[2]]
  131. if val in winners:
  132. return a[opt[0]]
  133.  
  134. if '0' not in a:
  135. return '3'
  136.  
  137. return '0'
  138.  
  139. macroboard = [get_state(b) for b in small_boards]
  140.  
  141. # modify macro board state based on availability of small board
  142. col, row = move.split(' ')[1:3]
  143. index = int(row) * 9 + int(col)
  144. boards = [
  145. [0, 3, 6, 27, 30, 33, 54, 57, 60], # top-left
  146. [1, 4, 7, 28, 31, 34, 55, 58, 61], # top-middle
  147. [2, 5, 8, 29, 32, 35, 56, 59, 62], # top-right
  148. [9, 12, 15, 36, 39, 42, 63, 66, 69], # middle-left
  149. [10, 13, 16, 37, 40, 43, 64, 67, 70], # middle-middle
  150. [11, 14, 17, 38, 41, 44, 65, 68, 71], # middle-right
  151. [18, 21, 24, 45, 48, 51, 72, 75, 78], # bottom-left
  152. [19, 22, 25, 46, 49, 52, 73, 76, 79], # bottom-middle
  153. [20, 23, 26, 47, 50, 53, 74, 77, 80]] # bottom-right
  154.  
  155. for i, b in enumerate(boards):
  156. if index in b:
  157. # If macro space available, update it to -1
  158. if macroboard[i] == '0':
  159. macroboard[i] = '-1'
  160. break
  161. else: # If macro space not available, update all 0 to -1
  162. macroboard = ['-1' if m == '0' else m for m in macroboard]
  163. break
  164.  
  165. return ','.join(macroboard)
  166.  
  167.  
  168. def print_board(field, macroboard, round_num, move):
  169. field = field.replace('0', ' ')
  170. a = field.split(',')
  171. msg = ''
  172. for i in range(0, 81, 9):
  173. if not i % 27 and i > 0:
  174. msg += '---+---+---\n'
  175.  
  176. msg += '|'.join([
  177. ''.join(a[i:i+3]),
  178. ''.join(a[i+3:i+6]),
  179. ''.join(a[i+6:i+9])]) + '\n'
  180.  
  181. sys.stderr.write("\x1b[2J\x1b[H") # clear screen
  182. msg += '\nRound {}\nmacroboard: {}\nfield: {}\nmove: {}\n'.format(
  183. round_num, macroboard, field, move)
  184.  
  185. sys.stdout.write(msg)
  186.  
  187.  
  188. def is_winner(macroboard):
  189. winopts = [
  190. [0, 1, 2],
  191. [3, 4, 5],
  192. [6, 7, 8],
  193. [0, 3, 6],
  194. [1, 4, 7],
  195. [2, 5, 8],
  196. [0, 4, 8],
  197. [6, 4, 2]]
  198.  
  199. m = macroboard.split(',')
  200. winners = ('111', '222')
  201. for opt in winopts:
  202. val = m[opt[0]] + m[opt[1]] + m[opt[2]]
  203. if val in winners:
  204. print 'WINNER! Player {}'.format(m[opt[0]])
  205. return True
  206.  
  207. return False
  208.  
  209.  
  210. if __name__ == '__main__':
  211. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement