Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.78 KB | None | 0 0
  1. import copy
  2. import colorama
  3. from colorama import Back, Fore, Style
  4.  
  5. def is_valid(m):
  6.     i, j = m
  7.     return 0 <= i < 6 and 0 <= j < 6
  8.  
  9. def is_free(state, tgt):
  10.     if not is_valid(tgt):
  11.         return False
  12.     return state[tgt[0]][tgt[1]] == 0
  13.  
  14.  
  15. def get_moves(state, player, *, only_capture=False):
  16.     res = []
  17.     res2 = []
  18.     mpl = player
  19.     for i, row in enumerate(state):
  20.         for j, x in enumerate(row):
  21.             start = i, j
  22.             if x * mpl > 0:
  23.                 if not only_capture:
  24.                     len = 1
  25.                     while True:
  26.                         m = i + len * mpl, j + len
  27.                         if is_valid(m) and is_free(state, m):
  28.                             res.append([start, m])
  29.                         else:
  30.                             break
  31.                         if abs(x) == 1:
  32.                             break
  33.                         len += 1
  34.                     len = 1
  35.                     while True:
  36.                         m = i + len * mpl, j - len
  37.                         if is_valid(m) and is_free(state, m):
  38.                             res.append([start, m])
  39.                         else:
  40.                             break
  41.                         if abs(x) == 1:
  42.                             break
  43.                         len += 1
  44.                     if abs(x) > 1:
  45.                         len = 1
  46.                         while True:
  47.                             m = i - len * mpl, j + len
  48.                             if is_valid(m) and is_free(state, m):
  49.                                 res.append([start, m])
  50.                             else:
  51.                                 break
  52.                             len += 1
  53.                         len = 1
  54.                         while True:
  55.                             m = i - len * mpl, j - len
  56.                             if is_valid(m) and is_free(state, m):
  57.                                 res.append([start, m])
  58.                             else:
  59.                                 break
  60.                             len += 1
  61.  
  62.                 def try_capture(prefix, i0, j0):
  63.                     for dx, dy in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
  64.                         i1, j1 = i0, j0
  65.                         k = 0
  66.                         while is_valid((i1, j1)) and state[i1][j1] * x >= 0:
  67.                             i1 += dx
  68.                             j1 += dy
  69.                             k += 1
  70.                         if not is_valid((i1, j1)):
  71.                             continue
  72.                         if abs(x) == 1 and k > 1:
  73.                             continue
  74.                         ci, cj = i1, j1
  75.                         while True: # landing square
  76.                             i1 += dx
  77.                             j1 += dy
  78.                             if is_free(state, (i1, j1)):
  79.                                 res2.append(prefix + [(i1, j1)])
  80.                                 tmp = state[ci][cj]
  81.                                 state[ci][cj] = 0
  82.                                 try_capture(prefix + [(i1, j1)], i1, j1)
  83.                                 state[ci][cj] = tmp
  84.                             else:
  85.                                 break
  86.                             if abs(x) == 1:
  87.                                 break
  88.  
  89.                 try_capture([(i, j)], i, j)
  90.     if res2:
  91.         return res2
  92.     return res
  93.  
  94. eval_matrix2 = [
  95.     [None, 0, None, 0, None, 1],
  96.     [1, None, 1, None, 0, None],
  97.     [None, 7, None, 6, None, 4],
  98.     [13, None, 9, None, 11, None],
  99.     [None, 19, None, 12, None, 18],
  100.     [22, None, 15, None, 20, None]]
  101.  
  102. eval_matrix = [
  103.     [0, None, 0, None, 1, None],
  104.     [None, 1, None, 1, None, 0],
  105.     [7, None, 6, None, 4, None],
  106.     [None, 13, None, 9, None, 11],
  107.     [19, None, 12, None, 18, None],
  108.     [None, 22, None, 15, None, 20]]
  109.  
  110. eval_matrix_enemy = list(reversed(eval_matrix2))
  111.  
  112. def evaluate(state, player):
  113.     res = 0
  114.     for i, row in enumerate(state):
  115.         for j, x in enumerate(row):
  116.             res += x * 100
  117.             if x == 0:
  118.                 continue
  119.             t = 1 if x > 0 else -1
  120.             p1 = i - t, j + 1
  121.             p2 = i - t, j - 1
  122.             if not is_free(state, p1) and not is_free(state, p2):
  123.                 res += 20 * t
  124.             if x > 0:
  125.                 res += eval_matrix[i][j] * x * 3
  126.             if x < 0:
  127.                 res += eval_matrix_enemy[i][j] * x * 3
  128.     res += len(get_moves(state, False)) * 10 - len(get_moves(state, True)) * 10
  129.     # если нас могут срубить, то это плохо
  130.     #capture_penalty = 0
  131.     #for m in get_moves(state, not is_enemy, only_capture=True):
  132.     #    capture_penalty = max(capture_penalty, (len(m) - 1) * 100)
  133.     #res -= capture_penalty
  134.     res *= player
  135.     return res
  136.  
  137. def apply_move(state, move):
  138.     state = copy.deepcopy(state)
  139.     me = state[move[0][0]][move[0][1]]
  140.     # kings
  141.     rows = [x[0] for x in move]
  142.     if me == 1 and 5 in rows:
  143.         me = 2
  144.     if me == -1 and 0 in rows:
  145.         me = -2
  146.  
  147.     for (i1, j1), (i2, j2) in zip(move, move[1:]):
  148.         di = 1 if i2 > i1 else -1
  149.         dj = 1 if j2 > j1 else -1
  150.         i = i1
  151.         j = j1
  152.         while i != i2:
  153.             if state[i][j] != 0:
  154.                 state[i][j] = 0
  155.             i += di
  156.             j += dj
  157.             if state[i][j] != 0:
  158.                 state[i][j] = 0
  159.     state[move[-1][0]][move[-1][1]] = me
  160.     return state
  161.  
  162. def move_str(move):
  163.     if move == None:
  164.         return None
  165.     return '-'.join('%s%d' % (chr(ord('a') + j), i + 1) for i, j in move)
  166.  
  167. def get_move(state, player):
  168.     best = None
  169.     best_val = -1e30
  170.     for move in get_moves(state, player):
  171.         state2 = apply_move(state, move)
  172.         best_enemy = None
  173.         best_enemy_val = 1e30
  174.         for emove in get_moves(state2, -player):
  175.             state3 = apply_move(state2, emove)
  176.             v = evaluate(state3, player)
  177.             if v < best_enemy_val:
  178.                 best_enemy_val = v
  179.                 best_enemy = emove
  180.  
  181.         #value = evaluate(state2)
  182.         value = best_enemy_val
  183.         print("%s: %d" % (move_str(move), value))
  184.         #print_state(state2)
  185.         if value > best_val:
  186.             best_val = value
  187.             best = move
  188.     print("Best: %s: %d" % (move_str(best), best_val))
  189.     return best
  190.  
  191. init_state = [
  192.     [1, 0, 1, 0, 1, 0],
  193.     [0, 1, 0, 1, 0, 1],
  194.     [0, 0, 0, 0, 0, 0],
  195.     [0, 0, 0, 0, 0, 0],
  196.     [-1, 0, -1, 0, -1, 0],
  197.     [0, -1, 0, -1, 0, -1]]
  198.  
  199. def print_state(state):
  200.     for i, row in enumerate(reversed(state)):
  201.         s = str(6-i)
  202.         for j, x in enumerate(row):
  203.             s += Back.BLUE if (i + j) % 2 == 1 else Back.GREEN
  204.             if x == 0:
  205.                 s += ' '
  206.             else:
  207.                 s += Fore.WHITE if x > 0 else Fore.RED
  208.                 s += Style.BRIGHT
  209.                 if abs(x) == 1:
  210.                     s += 'o'
  211.                 elif abs(x) == 2:
  212.                     s += '@'
  213.                 else:
  214.                     s += '?'
  215.                 s += Fore.RESET + Style.RESET_ALL
  216.         s += Back.RESET
  217.         print(s)
  218.     print(' abcdef')
  219.  
  220. def play_with_user(user_player):
  221.     state = init_state
  222.     first_move = True
  223.     while True:
  224.         if user_player == -1 or not first_move:
  225.             move = get_move(state, -user_player)
  226.             if move == None:
  227.                 print("Game Over. You won! :c")
  228.                 return
  229.             state = apply_move(state, move)
  230.             print_state(state)
  231.  
  232.         first_move = False
  233.         while True:
  234.             e_moves = get_moves(state, user_player)
  235.             if not e_moves:
  236.                 print("Game Over. You lost! c:")
  237.                 return
  238.             print('Available moves:')
  239.             for i, m in enumerate(e_moves):
  240.                 print('%d %s' % (i+1, move_str(m)))
  241.  
  242.             s = input()
  243.             try:
  244.                 idx = int(s)
  245.                 m = e_moves[idx-1]
  246.                 break
  247.             except:
  248.                 pass
  249.  
  250.             s = s.split('-')
  251.             m = []
  252.             try:
  253.                 for x, y in s:
  254.                     m.append((ord(y) - ord('1'), ord(x) - ord('a')))
  255.             except:
  256.                 print("invalid move")
  257.                 print("expected: %s" % ', '.join(move_str(x) for x in e_moves))
  258.                 continue
  259.  
  260.             if not m in e_moves:
  261.                 print("invalid move")
  262.                 print("expected: %s" % ', '.join(move_str(x) for x in e_moves))
  263.             else:
  264.                 break
  265.  
  266.         state = apply_move(state, m)
  267.         print_state(state)
  268.  
  269.  
  270. def main():
  271.     colorama.init()
  272.     print("1 = white, -1 = black")
  273.     player = int(input())
  274.     assert(player in [1, -1])
  275.     play_with_user(player)
  276.  
  277. if __name__ == '__main__':
  278.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement