SHARE
TWEET

Untitled

a guest Jun 27th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. RED = 'R'
  2. BLACK = 'B'
  3. EMPTY = 'X'
  4.  
  5. COLUMN = 7
  6. ROW = 6
  7. COLUMN = 4
  8. ROW = 4
  9.  
  10. def flip(color):
  11.     if color == RED:
  12.         return BLACK
  13.     return RED
  14.  
  15. class Board:
  16.     def __init__(self, s = None, turn = RED, depth = 0, parent = None):
  17.         self._board = s or EMPTY * COLUMN * ROW
  18.         self._turn = turn
  19.         self._depth = depth
  20.         self._parent = parent
  21.  
  22.     def __repr__(self):
  23.         return self._board
  24.  
  25.     def __eq__(self, other):
  26.         if isinstance(other, Board):
  27.             return self._board == other._board
  28.         return False
  29.  
  30.     def __hash__(self):
  31.         return hash(self._board)
  32.  
  33.     def copy(self):
  34.         return Board(s = self._board, turn = self._turn, depth = self._depth, parent = self._parent)
  35.  
  36.     def get(self, x, y):
  37.         if 0 <= x and x < COLUMN and 0 <= y and y < ROW:
  38.             return self._board[COLUMN * y + x]
  39.  
  40.     def get_column(self, x):
  41.         if 0 <= x and x < COLUMN:
  42.             values = ''.join([self.get(x, i) for i in range(ROW)])
  43.             return values
  44.  
  45.     def set(self, x, y, v):
  46.         if 0 <= x and x < COLUMN and 0 <= y and y < ROW:
  47.             new_board = self._board[:COLUMN * y + x] + v + self._board[COLUMN * y + x + 1:]
  48.             self._board = new_board
  49.  
  50.     def draw(self):
  51.         chopped = reversed([self._board[i:i+COLUMN]
  52.                 for i in range(0, COLUMN * ROW, COLUMN)])
  53.         print('\n'.join(chopped))
  54.  
  55.     def valid_moves(self):
  56.         columns = [self.get_column(i) for i in range(COLUMN)]
  57.         moves = []
  58.         for i, column in enumerate(columns):
  59.             j = 0
  60.             while j < ROW:
  61.                 if column[j] == EMPTY:
  62.                     moves.append((i, j))
  63.                     break
  64.                 j += 1
  65.         return moves
  66.  
  67.     def children(self):
  68.         moves = self.valid_moves()
  69.         children = []
  70.         for move in moves:
  71.             child = self.copy()
  72.             child._turn = flip(self._turn)
  73.             child._depth = self._depth + 1
  74.             child._parent = self
  75.             child.set(move[0], move[1], self._turn)
  76.             children.append(child)
  77.         return children
  78.  
  79.     def check_victory(self):
  80.         # vertical
  81.         for i in range(COLUMN):
  82.             n = 0
  83.             for j in range(ROW):
  84.                 if self.get(i, j) == self._turn:
  85.                     n += 1
  86.                 else:
  87.                     n = 0
  88.                 if n == 4:
  89.                     return True
  90.  
  91.         # horizontal
  92.         for j in range(ROW):
  93.             n = 0
  94.             for i in range(COLUMN):
  95.                 if self.get(i, j) == self._turn:
  96.                     n += 1
  97.                 else:
  98.                     n = 0
  99.                 if n == 4:
  100.                     return True
  101.  
  102.         # diagonal
  103.         right = [(0,2), (0,1), (0,0), (1,0), (2,0), (3,0)]
  104.         left =  [(6,2), (6,1), (6,0), (5,0), (4,0), (3,0)]
  105.  
  106.         for start in right:
  107.             n = 0
  108.             i, j = start
  109.             while i < ROW and j < COLUMN:
  110.                 if self.get(i, j) == self._turn:
  111.                     n += 1
  112.                 else:
  113.                     n = 0
  114.                 if n == 4:
  115.                     return True
  116.                 i += 1
  117.                 j += 1
  118.  
  119.         for start in left:
  120.             n = 0
  121.             i, j = start
  122.             while 0 <= i and j < COLUMN:
  123.                 if self.get(i, j) == self._turn:
  124.                     n += 1
  125.                 else:
  126.                     n = 0
  127.                 if n == 4:
  128.                     return True
  129.                 i -= 1
  130.                 j += 1
  131.  
  132.         return False
  133.  
  134.  
  135. board = Board() # s = 'RXXXXXRXRXXXXXXXRXXXXXXXXXXXXXXXXXXXXXXXXX', turn = RED)
  136. from collections import deque
  137. children = deque([board])
  138.  
  139. last = 0
  140. while True:
  141.     if len(children) == 0:
  142.         break
  143.     child = children.popleft()
  144.     if child._depth > 8:
  145.         last = child
  146.         break
  147.     for child in child.children():
  148.         children.append(child)
  149.  
  150. last.draw()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top