philRG

Connect4 with Buk. (Matrix reloaded :-))

Mar 4th, 2022 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.57 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def idebug(*args):
  5.     # return
  6.     print(*args, file=sys.stderr)
  7.  
  8.  
  9. def debug(*args):
  10.     # return
  11.     print(*args, file=sys.stderr)
  12.  
  13.  
  14. test = "6 13 20 27 34 41 48 55 62 \
  15.        5 12 19 26 33 40 47 54 61 \
  16.        4 11 18 25 32 39 46 53 60 \
  17.        3 10 17 24 31 38 45 52 59 \
  18.        2  9 16 23 30 37 44 51 58 \
  19.        1  8 15 22 29 36 43 50 57 \
  20.        0  7 14 21 28 35 42 49 56"
  21.  
  22.  
  23. def connected_four(position):
  24.     # Horizontal check
  25.     m = position & (position >> 7)
  26.     if m & (m >> 14):
  27.         return True
  28.     # Diagonal \
  29.     m = position & (position >> 6)
  30.     if m & (m >> 12):
  31.         return True
  32.     # Diagonal /
  33.     m = position & (position >> 8)
  34.     if m & (m >> 16):
  35.         return True
  36.     # Vertical
  37.     m = position & (position >> 1)
  38.     if m & (m >> 2):
  39.         return True
  40.     # Nothing found
  41.     return False
  42.  
  43.  
  44. def make_move(position, mask, col):
  45.     new_position = position ^ mask
  46.     new_mask = mask | (mask + (1 << (col * 7)))
  47.     return new_position, new_mask
  48.  
  49.  
  50. def print_board(bb: int, player: int = None, msg: str = None):
  51.     msg = f'player {player}' if not msg else msg
  52.     debug(f'plateau: {bin2(bb)}'.rjust(V_ALIGN, ' ') if not player else msg)
  53.     for j in range(6, -1, -1):
  54.         debug([1 & (bb >> 7 * i + j) for i in range(9)])
  55.  
  56.  
  57. # Drop chips in the columns.
  58. # Connect at least 4 of your chips in any direction to win.
  59.  
  60. # my_id: 0 or 1 (Player 0 plays first)
  61. # opp_id: if your index is 0, this will be 1, and vice versa
  62. my_id, opp_id = [int(i) for i in input().split()]
  63. idebug(my_id, opp_id)
  64.  
  65. WIDTH, HEIGHT = 9, 7
  66.  
  67. bin2 = lambda bb: bin(bb)[2:].rjust(63, "0")[:63]
  68.  
  69. V_ALIGN = 90
  70.  
  71. # game loop
  72. while True:
  73.     turn_index = int(input())  # starts from 0; As the game progresses, first player gets [0,2,4,...] and second player gets [1,3,5,...]
  74.     idebug(turn_index)
  75.     board = []
  76.     for i in range(HEIGHT):
  77.         board_row = input()  # one row of the board (from top to bottom)
  78.         idebug(board_row)
  79.         board.append(list(board_row))
  80.     num_valid_actions = int(input())  # number of unfilled columns in the board
  81.     # idebug(num_valid_actions)
  82.     for i in range(num_valid_actions):
  83.         action = int(input())  # a valid column index into which a chip can be dropped
  84.         # idebug(action)
  85.     opp_previous_action = int(input())  # opponent's previous chosen column index (will be -1 for first player in the first turn)
  86.     # idebug(opp_previous_action)
  87.  
  88.     debug(f'board: {board}')
  89.  
  90.     me = [[0] * WIDTH for _ in range(HEIGHT + 1)]
  91.     op = [[0] * WIDTH for _ in range(HEIGHT + 1)]
  92.     me_bin: str = '0b'
  93.     op_bin: str = '0b'
  94.     for j in range(WIDTH - 1, -1, -1):
  95.         for i in range(HEIGHT):
  96.             me[i][j] = 1 if board[i][j] == str(my_id) else 0
  97.             me_bin += '1' if board[i][j] == str(my_id) else '0'
  98.             op[i][j] = 1 if board[i][j] == str(opp_id) else 0
  99.             op_bin += '1' if board[i][j] == str(opp_id) else '0'
  100.  
  101.     # Write an action using print
  102.     # To debug: print("Debug messages...", file=sys.stderr, flush=True)
  103.     me_val: int = int(me_bin, 2)
  104.     op_val: int = int(op_bin, 2)
  105.  
  106.     # le plateau c est l'union des deux
  107.     # les coups possibles, c est le negation du plateau
  108.     plateau = me_val ^ op_val
  109.     debug(f'plateau: {plateau}')
  110.  
  111.     debug("_______________________________")
  112.     print_board(bb=plateau)
  113.     print_board(bb=me_val, player=my_id)
  114.     print_board(bb=op_val, player=opp_id)
  115.  
  116.     debug("_______________________________")
  117.     coups_possibles = [j for j in range(9) if 127 & (~plateau >> 7 * j)]
  118.     debug(f'coups_possibles: \n{coups_possibles}')
  119.  
  120.     debug("_______________________________")
  121.     col = 0
  122.     new_op, new_plateau = make_move(position=op_val, mask=plateau, col=col)
  123.     msg = f'play colon {col}: {bin2(new_op)}'.rjust(V_ALIGN, ' ')
  124.     print_board(bb=new_plateau, msg=msg)
  125.     if connected_four(new_plateau):
  126.         debug(f'player {opp_id} Wins after playing col #{col}!')
  127.  
  128.     # for col in coups_possibles:
  129.     #     new_me, new_plateau = make_move(position=me_val, mask=plateau, col=col)
  130.     #     if connected_four(new_plateau):
  131.     #         debug(f'player {my_id} Wins after playing col #{col}!')
  132.     #
  133.     # for col in coups_possibles:
  134.     #     new_op, new_plateau = make_move(position=op_val, mask=plateau, col=col)
  135.     #     if connected_four(new_plateau):
  136.     #         debug(f'player {opp_id} Wins after playing col #{col}!')
  137.  
  138.     # if connected_four(me_val):
  139.     #     debug(f'player {my_id} Wins!')
  140.     # if connected_four(op_val):
  141.     #     debug(f'player {opp_id} Wins!')
  142.  
Add Comment
Please, Sign In to add comment