Advertisement
philRG

Connect 4 with towardsdatascience's tutorial

Mar 3rd, 2022
1,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. import sys
  2. from typing import Tuple
  3.  
  4. import numpy as np
  5.  
  6.  
  7. def idebug(*args):
  8.     # return
  9.     print(*args, file=sys.stderr)
  10.  
  11.  
  12. def debug(*args):
  13.     # return
  14.     print(*args, file=sys.stderr)
  15.  
  16.  
  17. # def print_grid(bb: int, player_id: str = 'X', idebug: str = 'True'):
  18. #     if idebug:
  19. #         for i in range(6, -1, -1):
  20. #             t0 = (bin((bb >> 9 * i) & 511)[2:]).rjust(9, ".")
  21. #             t = [player_id if j == "1" else "." for j in t0]
  22. #             debug("".join(t))
  23.  
  24. """
  25.    Creating the (nearly) perfect connect-four bot with limited move time and file size:
  26.        https://towardsdatascience.com/creating-the-perfect-connect-four-ai-bot-c165115557b0
  27. """
  28.  
  29.  
  30. def get_position_mask_bitmap(board: np.array, player: int) -> Tuple[int, int]:
  31.     """
  32.    Calculate
  33.        * one bitstring representing the positions of the tokens of a player (position)
  34.        * one bitstring representing the positions of both players (mask)
  35.    :param board: 2D numpy array   game board (element is 0, 1 or 2)
  36.    :param player:
  37.    :return: position, mask
  38.    """
  39.     position, mask = '', ''
  40.     # Start with right-most column
  41.     for j in range(WIDTH - 1, -1, -1):
  42.         # Add 0-bits to sentinel
  43.         mask += '0'
  44.         position += '0'
  45.         # Start with bottom row
  46.         for i in range(HEIGHT):
  47.             mask += ['0', '1'][board[i, j] != 0]
  48.             position += ['0', '1'][board[i, j] == player]
  49.     return int(position, 2), int(mask, 2)
  50.  
  51.  
  52. # Drop chips in the columns.
  53. # Connect at least 4 of your chips in any direction to win.
  54.  
  55. # my_id: 0 or 1 (Player 0 plays first)
  56. # opp_id: if your index is 0, this will be 1, and vice versa
  57. my_id, opp_id = [int(i) for i in input().split()]
  58. idebug(my_id, opp_id)
  59.  
  60. WIDTH, HEIGHT = 9, 7
  61.  
  62. # game loop
  63. while True:
  64.     turn_index = int(input())  # starts from 0; As the game progresses, first player gets [0,2,4,...] and second player gets [1,3,5,...]
  65.     idebug(turn_index)
  66.     board = np.arange(HEIGHT * WIDTH).reshape(HEIGHT, WIDTH)
  67.     for i in range(HEIGHT):
  68.         board_row = input()  # one row of the board (from top to bottom)
  69.         idebug(board_row)
  70.         for j, element in enumerate(list(board_row)):
  71.             board[i, j] = int(element) + 1 if element != '.' else 0
  72.     num_valid_actions = int(input())  # number of unfilled columns in the board
  73.     idebug(num_valid_actions)
  74.     for i in range(num_valid_actions):
  75.         action = int(input())  # a valid column index into which a chip can be dropped
  76.         idebug(action)
  77.     opp_previous_action = int(input())  # opponent's previous chosen column index (will be -1 for first player in the first turn)
  78.     idebug(opp_previous_action)
  79.  
  80.     my_id += 1
  81.     opp_id += 1
  82.     me = np.arange(HEIGHT * WIDTH).reshape(HEIGHT, WIDTH)
  83.     op = np.arange(HEIGHT * WIDTH).reshape(HEIGHT, WIDTH)
  84.     for j in range(WIDTH):
  85.         for i in range(HEIGHT):a
  86.             me[i, j] = 1 if board[i, j] == my_id else 0
  87.             op[i, j] = 1 if board[i, j] == opp_id else 0
  88.  
  89.     my_position, my_mask = get_position_mask_bitmap(board=board, player=my_id)
  90.     op_position, op_mask = get_position_mask_bitmap(board=board, player=opp_id)
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement