Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. import defs
  2. Tiles = defs.Tiles
  3. def addTuple(tu1, tu2):
  4.     return map(lambda x: x[0] + x[1], zip( tu1, tu2 )) # addTuple((a1, a2), (b1, b2)) == (a1+b1, b2+b2)
  5.    
  6. def subTuple(tu1, tu2):
  7.     return map(lambda x: x[0] - x[1], zip( tu1, tu2 )) # subTuple((a1, a2), (b1, b2)) == (a1-b1, b2-b2)
  8.  
  9. def findNeighbor(board, tile, row, col):
  10. # vert i hor to tablice zawierające informacje o tym, gdzie można szukać sąsiada tak, żeby nie wykroczyć poza tablicę.
  11. # vert w pionie, hor w poziomie
  12. # np. vert == [-1, 0] oznacza że można szukać w tym samym rzędzie pionowym (0) albo o jeden rząd niżej (-1).
  13.     vert = []
  14.     if row != 0:
  15.         vert.append(-1)
  16.     vert.append(0)
  17.     if row != len(board)-1:
  18.         vert.append(1)
  19.        
  20.     hor = []
  21.     if col != 0:
  22.         hor.append(-1)
  23.     hor.append(0)
  24.     if row != len(board)-1:
  25.         hor.append(1)
  26.        
  27.     i = 0
  28.     for x in vert:
  29.         ii = 0
  30.         for y in hor:
  31.             if board[row+i][col+ii] == tile:
  32.                 return row+i, col+ii
  33.             ii += 1
  34.         i += 1
  35.     return False
  36.    
  37. def checkLine(board, row, col, tile, neighbor, counter, towin):
  38.     offset = subTuple(neighbor, (row, col))
  39.     if counter == towin:
  40.         return tile
  41.     #jeżeli następne sprawdzane pole byłoby poza tablicą, to znaczy, że dotarliśmy do końca planszy i można zakończyć szukanie
  42.     elif row+offset[0] > len(board)-1 or col+offset[1] > len(board)-1:
  43.         return False
  44.     elif board[row+offset[0]][col+offset[1]] == tile:
  45.         counter += 1
  46.         return checkLine(board, neighbor[0], neighbor[1], tile, addTuple(neighbor, offset), counter, towin)
  47.     else:
  48.         return False
  49.  
  50. def checkBoard(board, players, towin):
  51.     i = 0
  52.     for row in board:
  53.         col = 0
  54.         for tile in row:
  55.             neighbor = False
  56.             if tile != Tiles.EMPTY:
  57.                 neighbor = findNeighbor(board, tile, i, col)
  58.             if neighbor:
  59.                 return checkLine(board, i, col, tile, neighbor, 1, towin)
  60.             col += 1
  61.         i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement