Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def idebug(*args):
- # return
- print(*args, file=sys.stderr)
- def debug(*args):
- # return
- print(*args, file=sys.stderr)
- test = "6 13 20 27 34 41 48 55 62 \
- 5 12 19 26 33 40 47 54 61 \
- 4 11 18 25 32 39 46 53 60 \
- 3 10 17 24 31 38 45 52 59 \
- 2 9 16 23 30 37 44 51 58 \
- 1 8 15 22 29 36 43 50 57 \
- 0 7 14 21 28 35 42 49 56"
- def connected_four(position):
- # Horizontal check
- m = position & (position >> 7)
- if m & (m >> 14):
- return True
- # Diagonal \
- m = position & (position >> 6)
- if m & (m >> 12):
- return True
- # Diagonal /
- m = position & (position >> 8)
- if m & (m >> 16):
- return True
- # Vertical
- m = position & (position >> 1)
- if m & (m >> 2):
- return True
- # Nothing found
- return False
- def make_move(position, mask, col):
- new_position = position ^ mask
- new_mask = mask | (mask + (1 << (col * 7)))
- return new_position, new_mask
- def print_board(bb: int, player: int = None, msg: str = None):
- msg = f'player {player}' if not msg else msg
- debug(f'plateau: {bin2(bb)}'.rjust(V_ALIGN, ' ') if not player else msg)
- for j in range(6, -1, -1):
- debug([1 & (bb >> 7 * i + j) for i in range(9)])
- # Drop chips in the columns.
- # Connect at least 4 of your chips in any direction to win.
- # my_id: 0 or 1 (Player 0 plays first)
- # opp_id: if your index is 0, this will be 1, and vice versa
- my_id, opp_id = [int(i) for i in input().split()]
- idebug(my_id, opp_id)
- WIDTH, HEIGHT = 9, 7
- bin2 = lambda bb: bin(bb)[2:].rjust(63, "0")[:63]
- V_ALIGN = 90
- # game loop
- while True:
- turn_index = int(input()) # starts from 0; As the game progresses, first player gets [0,2,4,...] and second player gets [1,3,5,...]
- idebug(turn_index)
- board = []
- for i in range(HEIGHT):
- board_row = input() # one row of the board (from top to bottom)
- idebug(board_row)
- board.append(list(board_row))
- num_valid_actions = int(input()) # number of unfilled columns in the board
- # idebug(num_valid_actions)
- for i in range(num_valid_actions):
- action = int(input()) # a valid column index into which a chip can be dropped
- # idebug(action)
- opp_previous_action = int(input()) # opponent's previous chosen column index (will be -1 for first player in the first turn)
- # idebug(opp_previous_action)
- debug(f'board: {board}')
- me = [[0] * WIDTH for _ in range(HEIGHT + 1)]
- op = [[0] * WIDTH for _ in range(HEIGHT + 1)]
- me_bin: str = '0b'
- op_bin: str = '0b'
- for j in range(WIDTH - 1, -1, -1):
- for i in range(HEIGHT):
- me[i][j] = 1 if board[i][j] == str(my_id) else 0
- me_bin += '1' if board[i][j] == str(my_id) else '0'
- op[i][j] = 1 if board[i][j] == str(opp_id) else 0
- op_bin += '1' if board[i][j] == str(opp_id) else '0'
- # Write an action using print
- # To debug: print("Debug messages...", file=sys.stderr, flush=True)
- me_val: int = int(me_bin, 2)
- op_val: int = int(op_bin, 2)
- # le plateau c est l'union des deux
- # les coups possibles, c est le negation du plateau
- plateau = me_val ^ op_val
- debug(f'plateau: {plateau}')
- debug("_______________________________")
- print_board(bb=plateau)
- print_board(bb=me_val, player=my_id)
- print_board(bb=op_val, player=opp_id)
- debug("_______________________________")
- coups_possibles = [j for j in range(9) if 127 & (~plateau >> 7 * j)]
- debug(f'coups_possibles: \n{coups_possibles}')
- debug("_______________________________")
- col = 0
- new_op, new_plateau = make_move(position=op_val, mask=plateau, col=col)
- msg = f'play colon {col}: {bin2(new_op)}'.rjust(V_ALIGN, ' ')
- print_board(bb=new_plateau, msg=msg)
- if connected_four(new_plateau):
- debug(f'player {opp_id} Wins after playing col #{col}!')
- # for col in coups_possibles:
- # new_me, new_plateau = make_move(position=me_val, mask=plateau, col=col)
- # if connected_four(new_plateau):
- # debug(f'player {my_id} Wins after playing col #{col}!')
- #
- # for col in coups_possibles:
- # new_op, new_plateau = make_move(position=op_val, mask=plateau, col=col)
- # if connected_four(new_plateau):
- # debug(f'player {opp_id} Wins after playing col #{col}!')
- # if connected_four(me_val):
- # debug(f'player {my_id} Wins!')
- # if connected_four(op_val):
- # debug(f'player {opp_id} Wins!')
Add Comment
Please, Sign In to add comment