def handleMove(self, move): if move == "pass": self.updateHistory(self.boardHistory[0]) self.switch_player() is_valid = GoRuleChecker(self.boardHistory).sixth_resolve_history( self.currentStone) if not is_valid: return self.decide_winner(self.boardHistory[0]) else: row, col = Go_Board().point_parser(move) madeMove = GoRuleChecker(self.boardHistory).sixth_resolve_history( self.currentStone, row, col) opponentStone = self.playerTwoStone if self.currentStone == self.playerOneStone else self.playerOneStone opponentName = self.get_player_name(opponentStone) if madeMove: whatIfBoard = Go_Board(self.boardHistory[0]).place( self.currentStone, row, col) try: self.updateHistory( copy.deepcopy( GoRuleChecker().board_after_remove_captured_stone( whatIfBoard, self.currentStone, row, col))) except TypeError: return opponentName self.switch_player() else: return opponentName
def n_depth_capture(self,boards,n = 1): opponent = "B" if self.player_stone == "W" else "W" ref = GoRuleChecker(boards) latest_board = self.determine_latest_board(ref) set_of_liberties = ref.check_liberties(latest_board,opponent,"coord") if n == 1: liberties_one = [] for i,liberties in enumerate(set_of_liberties): if len(liberties) == 1: liberties_one.append(liberties[0]) if liberties_one: liberties_one = sorted(liberties_one, key=lambda x: x[1]) if ref.sixth_resolve_history(self.player_stone, liberties_one[0][0], liberties_one[0][1]): return str(liberties_one[0][1] + 1) + "-" + str(liberties_one[0][0] + 1)
def driver(self): result = [] f = FrontEnd() json_string = f.input_receiver() json_list = list(f.parser(json_string)) for i,read in enumerate(json_list): if len(read) == 19: board = read result.append(self.check_the_score(board)) elif len(read) == 2: stone, move = read[0], read[1] self.stone_checker(stone) if move == "pass": result.append(True) elif isinstance(move,list): point, boards = move[0], move[1] row, col = self.point_parser(point) self.coord_checker(row,col) latest_board = boards[0] occupied = Go_Board(latest_board).is_occupied(row,col) if occupied is True: result.append(False) else: result.append(GoRuleChecker(boards).sixth_resolve_history(stone, row, col)) return json.dumps(result)
def decide_winner(self, board): score = GoRuleChecker().check_the_score(board) black_score, white_score = score["B"], score["W"] if black_score > white_score: return [self.playerOneName] elif white_score > black_score: return [self.playerTwoName] else: return sorted([self.playerOneName, self.playerTwoName])
def decide_winner(self, board): score = GoRuleChecker().check_the_score(board) black_score, white_score = score["B"], score["W"] if black_score > white_score: self.results['winner'].append(self.playerOneName) self.results['loser'].append(self.playerTwoName) elif white_score > black_score: self.results['winner'].append(self.playerTwoName) self.results['loser'].append(self.playerOneName) else: self.results['winner'].append(self.playerOneName) self.results['winner'].append(self.playerTwoName) return self.results
def make_move(self, boards): ref = GoRuleChecker(boards) boards_correct = ref.sixth_resolve_history(self.player_stone) if boards_correct: print("correct boards") if random.random() <= 0.2: return "pass" else: row, col = self.random_coord() if boards[0][row][col] == " ": print("sending", row, col) return str(col + 1) + "-" + str(row + 1) while boards[0][row][col] != " ": if ref.sixth_resolve_history(self.player_stone, row, col) or True: print("sending", row, col) return str(col + 1) + "-" + str(row + 1) else: row, col = self.random_coord() continue print("bleh bleh blej") else: raise Exception("boards history not valid")
def make_a_move(self, boards): ref = GoRuleChecker(boards) recent_board = self.determine_latest_board(ref) boards_correct = ref.sixth_resolve_history(self.stone) if boards_correct: empty_coord = ref.get_coord(recent_board, " ") empty_coord = sorted(empty_coord, key=lambda x: x[1]) while empty_coord: current_coord = empty_coord.pop(0) row, col = current_coord[0], current_coord[1] if ref.sixth_resolve_history(self.stone, row, col): return str(col + 1) + "-" + str(row + 1) try: ref.check_suicide(self.stone, row, col) ref.check_ko(self.stone, row, col) except Exception: continue return "pass" else: return "This history makes no sense!"
def make_a_move(self,boards): ref = GoRuleChecker(boards) recent_board = self.determine_latest_board(ref) boards_correct = ref.sixth_resolve_history(self.player_stone) if boards_correct: rand = math.floor(random.random() * 100) if rand < 25: list_empty_coord = self.GoBoard.get_coord(recent_board," ") a,b = random.randrange(2*GoBoard.Board_Size),\ random.randrange(2*GoBoard.Board_Size) return str(a+1)+"-"+str(b+1) capture = self.n_depth_capture(boards,1) if capture: return capture else: empty_coord = GoBoard.get_coord(recent_board, " ") empty_coord = sorted(empty_coord, key=lambda x: x[1]) while empty_coord: current_coord = empty_coord.pop(0) row, col = current_coord[0], current_coord[1] if ref.sixth_resolve_history(self.player_stone, row, col): return str(col+1)+"-"+str(row+1) try: ref.check_suicide(self.player_stone,row,col) ref.check_ko(self.player_stone,row,col) except Exception: continue return "pass" else: return "This history makes no sense!"