Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- class TerritoryHandler:
- '''Handles the territory of the player in question.'''
- def __init__(self, player, opponent):
- '''Initializes the player that possesses the terriory and their opponent.'''
- self.player = player
- self.opponent = opponent
- self.territories = []
- def drawLineOnBoard(self, a, b, c, mark1, mark2, board):
- '''Draws a horizontal and/or vertical line on the board.'''
- for i in range(a, b):
- if mark1 and type(mark1[2]).__name__ == "int" and type(board[c][i][2]).__name__ == "int" and board[c][i][0] == self.player:
- board[c][i][2] += mark1[2]
- board[c][i][:1] = mark1[:1]
- elif mark1 and type(mark1[2]).__name__ == "int" and board[c][i][2] != "s":
- board[c][i] = mark1[:]
- elif mark1 and type(mark1[2]).__name__ != "int" and board[c][i][0] != self.player and board[c][i][2] != "s":
- board[c][i] = mark1[:]
- if mark2 and type(mark2[2]).__name__ == "int" and type(board[i][c][2]).__name__ == "int" and board[i][c][0] == self.player:
- board[i][c][2] += mark2[2]
- board[i][c][:1] = mark2[:1]
- elif mark2 and type(mark2[2]).__name__ == "int" and board[i][c][2] != "s":
- board[i][c] = mark2[:]
- elif mark2 and type(mark2[2]).__name__ != "int" and board[i][c][0] != self.player and board[i][c][2] != "s":
- board[i][c] = mark2[:]
- return 0
- def checkLineOnBoard(self, a, b, c, opp_ver, opp_hor, board):
- '''Checks a horizontal and/or vertical line on the board for opponent stones.'''
- for i in range(a, b):
- if opp_ver:
- if board[c][i][0] == opp_ver and board[c][i][2] == "s":
- return 1
- if opp_hor:
- if board[i][c][0] == opp_hor and board[i][c][2] == "s":
- return 1
- return 0
- def iterateRectangle(self, a, b, c, d, e, func, mark1, mark2, board):
- '''Iterates through rectangular areas on board calling function func and raising the flag if interruption in iteration is detected.'''
- flag = 0
- flag = func(a, b, c, None, mark1, board)
- for i in range(d, e):
- flag += func(a, b, i, None, mark2, board)
- return flag
- def drawTerritory(self, a, b, x, y, opponentList, territoryMark, territoryBorder, board):
- '''Draws the territory in where no opponent stones are found.'''
- if not opponentList[0] and not opponentList[1] and y <= 4:
- draw_error = self.iterateRectangle(0, y, x, a, b, self.drawLineOnBoard, territoryBorder, territoryMark, board)
- if not opponentList[0] and not opponentList[2] and y >= 4:
- draw_error = self.iterateRectangle(y + 1, 9, x, a, b, self.drawLineOnBoard, territoryBorder, territoryMark, board)
- if not opponentList[0] and (not opponentList[1] or not opponentList[2]):
- draw_error = self.drawLineOnBoard(a, b, y, territoryBorder, None, board)
- def iterateTerritory(self, a, b, x, y, territoryMark, board):
- '''Iterates through a territory on board checking it first for opponent stones and then drawing it.'''
- opponent_found1 = self.checkLineOnBoard(a, b, y, self.opponent, None, board)
- opponent_found2 = 1
- opponent_found3 = 1
- territoryBorder = territoryMark[:]
- territoryBorder[2] = "x"
- if y <= 4:
- # Iterates through the intersections when the move is on the lower side of the board or in the middle.
- opponent_found2 = self.iterateRectangle(0, y, x, a, b, self.checkLineOnBoard, self.opponent, self.opponent, board)
- if y >= 4:
- # Iterates through the intersections when the move is on the upper side of the board or in the middle.
- opponent_found3 = self.iterateRectangle(y + 1, 9, x, a, b, self.checkLineOnBoard, self.opponent, self.opponent, board)
- opponentList = [opponent_found1, opponent_found2, opponent_found3]
- self.drawTerritory(a, b, x, y, opponentList, territoryMark, territoryBorder, board)
- def createNewTerritory(self, move, board):
- '''Draws new territory on the board determined by latest move with the value of the multiplier.'''
- y = move[0][0]
- x = move[0][1]
- territoryMark = ["b", move[1], move[2]]
- if x >= 4:
- # Iterates through the intersections when the move is on the right side of the board or in the middle.
- self.iterateTerritory(x + 1, 9, x, y, territoryMark, board)
- if x <= 4:
- # Iterates through the intersections when the move is on the left side of the board or in the middle.
- self.iterateTerritory(0, x, x, y, territoryMark, board)
- def searchForStones(self, move, stones):
- '''Searches for other stones of the player played nearby the last move.'''
- nearbyStones = []
- for stone in stones:
- y = abs(move[0] - stone[0][0])
- x = abs(move[1] - stone[0][1])
- if 0 < x + y <= 6:
- nearbyStones.append(stone)
- return nearbyStones
- def calculateNewTerritory(self, move, multiplier):
- '''Calculates multiplier for new territory determined by latest move.'''
- x = 1
- coefficients = [0, 0]
- for i, coor in enumerate(move):
- coefficients[i] = 4 - abs(4 - coor)
- if coefficients[i] == 1 or coefficients[i] == 4:
- coefficients[i] = 60
- x = 5
- elif coefficients[i] == 2:
- coefficients[i] = 900
- elif coefficients[i] == 3:
- coefficients[i] = 300
- coefficients.sort()
- multiplier = (coefficients[0] * x + coefficients[1]) / (x + 1)
- return multiplier
- def territoryEstimator(self, move, stones, board):
- '''Estimates the territory created by latest move and stones on the board.'''
- multiplier = 0
- multiplier = self.calculateNewTerritory(move[0], multiplier)
- nearbyStones = self.searchForStones(move[0], stones)
- print nearbyStones
- move[1] = len(self.territories)
- move[2] = multiplier
- self.createNewTerritory(move, board)
- return move[1]
- def createBoard():
- '''Creates a new board.'''
- board = []
- row = []
- for x in range(0, 9):
- for y in range(0, 9):
- row.append(["0", 0, 0])
- board.append(row)
- row = []
- return board
- def playMove():
- '''Takes a new move from the user as a keyboard input.'''
- coordinateString = raw_input("Play a move ")
- coordinateList = coordinateString.split()
- for i, j in enumerate(coordinateList):
- coordinateList[i] = int(coordinateList[i])
- move = [coordinateList, "0", 0]
- return move
- def main():
- '''Controls the flow of the game.'''
- board = createBoard()
- stones = []
- humanTerritory = TerritoryHandler("b", "w")
- for i in range(0, 3):
- move = playMove()
- stones.append(move)
- board[6][7] = ["w", 0, "s"]
- index = humanTerritory.territoryEstimator(move, stones, board)
- board[move[0][0]][move[0][1]] = ["b", index, "s"]
- for i, j in enumerate(board):
- print board[i]
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment