Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. class TerritoryHandler:
  4. '''Handles the territory of the player in question.'''
  5.  
  6. def __init__(self, player, opponent):
  7. '''Initializes the player that possesses the terriory and their opponent.'''
  8.  
  9. self.player = player
  10. self.opponent = opponent
  11. self.territories = []
  12.  
  13. def drawLineOnBoard(self, a, b, c, mark1, mark2, board):
  14. '''Draws a horizontal and/or vertical line on the board.'''
  15.  
  16. for i in range(a, b):
  17. if mark1 and type(mark1[2]).__name__ == "int" and type(board[c][i][2]).__name__ == "int" and board[c][i][0] == self.player:
  18. board[c][i][2] += mark1[2]
  19. board[c][i][:1] = mark1[:1]
  20. elif mark1 and type(mark1[2]).__name__ == "int" and board[c][i][2] != "s":
  21. board[c][i] = mark1[:]
  22. elif mark1 and type(mark1[2]).__name__ != "int" and board[c][i][0] != self.player and board[c][i][2] != "s":
  23. board[c][i] = mark1[:]
  24.  
  25. if mark2 and type(mark2[2]).__name__ == "int" and type(board[i][c][2]).__name__ == "int" and board[i][c][0] == self.player:
  26. board[i][c][2] += mark2[2]
  27. board[i][c][:1] = mark2[:1]
  28. elif mark2 and type(mark2[2]).__name__ == "int" and board[i][c][2] != "s":
  29. board[i][c] = mark2[:]
  30. elif mark2 and type(mark2[2]).__name__ != "int" and board[i][c][0] != self.player and board[i][c][2] != "s":
  31. board[i][c] = mark2[:]
  32.  
  33. return 0
  34.  
  35. def checkLineOnBoard(self, a, b, c, opp_ver, opp_hor, board):
  36. '''Checks a horizontal and/or vertical line on the board for opponent stones.'''
  37.  
  38. for i in range(a, b):
  39. if opp_ver:
  40. if board[c][i][0] == opp_ver and board[c][i][2] == "s":
  41. return 1
  42. if opp_hor:
  43. if board[i][c][0] == opp_hor and board[i][c][2] == "s":
  44. return 1
  45.  
  46. return 0
  47.  
  48. def iterateRectangle(self, a, b, c, d, e, func, mark1, mark2, board):
  49. '''Iterates through rectangular areas on board calling function func and raising the flag if interruption in iteration is detected.'''
  50.  
  51. flag = 0
  52.  
  53. flag = func(a, b, c, None, mark1, board)
  54. for i in range(d, e):
  55. flag += func(a, b, i, None, mark2, board)
  56.  
  57. return flag
  58.  
  59. def drawTerritory(self, a, b, x, y, opponentList, territoryMark, territoryBorder, board):
  60. '''Draws the territory in where no opponent stones are found.'''
  61.  
  62. if not opponentList[0] and not opponentList[1] and y <= 4:
  63. draw_error = self.iterateRectangle(0, y, x, a, b, self.drawLineOnBoard, territoryBorder, territoryMark, board)
  64.  
  65. if not opponentList[0] and not opponentList[2] and y >= 4:
  66. draw_error = self.iterateRectangle(y + 1, 9, x, a, b, self.drawLineOnBoard, territoryBorder, territoryMark, board)
  67.  
  68. if not opponentList[0] and (not opponentList[1] or not opponentList[2]):
  69. draw_error = self.drawLineOnBoard(a, b, y, territoryBorder, None, board)
  70.  
  71. def iterateTerritory(self, a, b, x, y, territoryMark, board):
  72. '''Iterates through a territory on board checking it first for opponent stones and then drawing it.'''
  73.  
  74. opponent_found1 = self.checkLineOnBoard(a, b, y, self.opponent, None, board)
  75. opponent_found2 = 1
  76. opponent_found3 = 1
  77. territoryBorder = territoryMark[:]
  78. territoryBorder[2] = "x"
  79.  
  80. if y <= 4:
  81. # Iterates through the intersections when the move is on the lower side of the board or in the middle.
  82. opponent_found2 = self.iterateRectangle(0, y, x, a, b, self.checkLineOnBoard, self.opponent, self.opponent, board)
  83.  
  84. if y >= 4:
  85. # Iterates through the intersections when the move is on the upper side of the board or in the middle.
  86. opponent_found3 = self.iterateRectangle(y + 1, 9, x, a, b, self.checkLineOnBoard, self.opponent, self.opponent, board)
  87.  
  88. opponentList = [opponent_found1, opponent_found2, opponent_found3]
  89. self.drawTerritory(a, b, x, y, opponentList, territoryMark, territoryBorder, board)
  90.  
  91. def createNewTerritory(self, move, board):
  92. '''Draws new territory on the board determined by latest move with the value of the multiplier.'''
  93.  
  94. y = move[0][0]
  95. x = move[0][1]
  96. territoryMark = ["b", move[1], move[2]]
  97.  
  98. if x >= 4:
  99. # Iterates through the intersections when the move is on the right side of the board or in the middle.
  100. self.iterateTerritory(x + 1, 9, x, y, territoryMark, board)
  101.  
  102. if x <= 4:
  103. # Iterates through the intersections when the move is on the left side of the board or in the middle.
  104. self.iterateTerritory(0, x, x, y, territoryMark, board)
  105.  
  106. def searchForStones(self, move, stones):
  107. '''Searches for other stones of the player played nearby the last move.'''
  108.  
  109. nearbyStones = []
  110.  
  111. for stone in stones:
  112. y = abs(move[0] - stone[0][0])
  113. x = abs(move[1] - stone[0][1])
  114. if 0 < x + y <= 6:
  115. nearbyStones.append(stone)
  116.  
  117. return nearbyStones
  118.  
  119. def calculateNewTerritory(self, move, multiplier):
  120. '''Calculates multiplier for new territory determined by latest move.'''
  121.  
  122. x = 1
  123. coefficients = [0, 0]
  124.  
  125. for i, coor in enumerate(move):
  126. coefficients[i] = 4 - abs(4 - coor)
  127. if coefficients[i] == 1 or coefficients[i] == 4:
  128. coefficients[i] = 60
  129. x = 5
  130. elif coefficients[i] == 2:
  131. coefficients[i] = 900
  132. elif coefficients[i] == 3:
  133. coefficients[i] = 300
  134.  
  135. coefficients.sort()
  136. multiplier = (coefficients[0] * x + coefficients[1]) / (x + 1)
  137.  
  138. return multiplier
  139.  
  140. def territoryEstimator(self, move, stones, board):
  141. '''Estimates the territory created by latest move and stones on the board.'''
  142.  
  143. multiplier = 0
  144. multiplier = self.calculateNewTerritory(move[0], multiplier)
  145. nearbyStones = self.searchForStones(move[0], stones)
  146. print nearbyStones
  147.  
  148. move[1] = len(self.territories)
  149. move[2] = multiplier
  150. self.createNewTerritory(move, board)
  151.  
  152. return move[1]
  153.  
  154. def createBoard():
  155. '''Creates a new board.'''
  156.  
  157. board = []
  158. row = []
  159. for x in range(0, 9):
  160. for y in range(0, 9):
  161. row.append(["0", 0, 0])
  162. board.append(row)
  163. row = []
  164.  
  165. return board
  166.  
  167. def playMove():
  168. '''Takes a new move from the user as a keyboard input.'''
  169.  
  170. coordinateString = raw_input("Play a move ")
  171. coordinateList = coordinateString.split()
  172. for i, j in enumerate(coordinateList):
  173. coordinateList[i] = int(coordinateList[i])
  174. move = [coordinateList, "0", 0]
  175.  
  176. return move
  177.  
  178. def main():
  179. '''Controls the flow of the game.'''
  180.  
  181. board = createBoard()
  182. stones = []
  183. humanTerritory = TerritoryHandler("b", "w")
  184.  
  185. for i in range(0, 3):
  186. move = playMove()
  187. stones.append(move)
  188. board[6][7] = ["w", 0, "s"]
  189. index = humanTerritory.territoryEstimator(move, stones, board)
  190. board[move[0][0]][move[0][1]] = ["b", index, "s"]
  191.  
  192. for i, j in enumerate(board):
  193. print board[i]
  194.  
  195. if __name__ == "__main__":
  196. main()
Add Comment
Please, Sign In to add comment