Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from typing import Tuple
- import numpy as np
- def idebug(*args):
- # return
- print(*args, file=sys.stderr)
- def debug(*args):
- # return
- print(*args, file=sys.stderr)
- # def print_grid(bb: int, player_id: str = 'X', idebug: str = 'True'):
- # if idebug:
- # for i in range(6, -1, -1):
- # t0 = (bin((bb >> 9 * i) & 511)[2:]).rjust(9, ".")
- # t = [player_id if j == "1" else "." for j in t0]
- # debug("".join(t))
- """
- Creating the (nearly) perfect connect-four bot with limited move time and file size:
- https://towardsdatascience.com/creating-the-perfect-connect-four-ai-bot-c165115557b0
- """
- def get_position_mask_bitmap(board: np.array, player: int) -> Tuple[int, int]:
- """
- Calculate
- * one bitstring representing the positions of the tokens of a player (position)
- * one bitstring representing the positions of both players (mask)
- :param board: 2D numpy array game board (element is 0, 1 or 2)
- :param player:
- :return: position, mask
- """
- position, mask = '', ''
- # Start with right-most column
- for j in range(WIDTH - 1, -1, -1):
- # Add 0-bits to sentinel
- mask += '0'
- position += '0'
- # Start with bottom row
- for i in range(HEIGHT):
- mask += ['0', '1'][board[i, j] != 0]
- position += ['0', '1'][board[i, j] == player]
- return int(position, 2), int(mask, 2)
- # 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
- # 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 = np.arange(HEIGHT * WIDTH).reshape(HEIGHT, WIDTH)
- for i in range(HEIGHT):
- board_row = input() # one row of the board (from top to bottom)
- idebug(board_row)
- for j, element in enumerate(list(board_row)):
- board[i, j] = int(element) + 1 if element != '.' else 0
- 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)
- my_id += 1
- opp_id += 1
- me = np.arange(HEIGHT * WIDTH).reshape(HEIGHT, WIDTH)
- op = np.arange(HEIGHT * WIDTH).reshape(HEIGHT, WIDTH)
- for j in range(WIDTH):
- for i in range(HEIGHT):a
- me[i, j] = 1 if board[i, j] == my_id else 0
- op[i, j] = 1 if board[i, j] == opp_id else 0
- my_position, my_mask = get_position_mask_bitmap(board=board, player=my_id)
- op_position, op_mask = get_position_mask_bitmap(board=board, player=opp_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement