Beispiel #1
0
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal
        """
        assert is_black_white(color)
        # Special cases
        if self.board[point] != EMPTY:
            raise ValueError("occupied")

        # General case: deal with captures, suicide
        opp_color = GoBoardUtil.opponent(color)
        self.board[point] = color
        neighbors = self.neighbors[point]
        for nb in neighbors:
            if self.board[nb] == opp_color:
                if self._detect_capture(nb):
                    self.board[point] = EMPTY
                    raise ValueError("capture")
        if not self._stone_has_liberty(point):
            # check suicide of whole block
            block = self._block_of(point)
            if not self._has_liberty(block):  # undo suicide move
                self.board[point] = EMPTY
                raise ValueError("suicide")
        self.current_player = GoBoardUtil.opponent(color)
        return True
Beispiel #2
0
    def is_legal(self, point, color):
        """
        Check if the move is legal
        """
        assert is_black_white(color)
        # Special cases
        if self.board[point] != EMPTY:
            return False

        # General case: deal with captures, suicide
        opp_color = GoBoardUtil.opponent(color)
        self.board[point] = color
        neighbors = self.neighbors[point]
        # Captur
        for nb in neighbors:
            if self.board[nb] == opp_color:
                if self._detect_capture(nb):
                    self.board[point] = EMPTY
                    return False
        # Sucide
        if not self._stone_has_liberty(point):
            # check suicide of whole block
            block = self._block_of(point)
            if not self._has_liberty(block):  # undo suicide move
                self.board[point] = EMPTY
                return False
        # Undo
        self.board[point] = EMPTY
        return True
Beispiel #3
0
 def _block_of(self, stone):
     """
     Find the block of given stone
     Returns a board of boolean markers which are set for
     all the points in the block 
     """
     color = self.get_color(stone)
     assert is_black_white(color)
     return self.connected_component(stone)
Beispiel #4
0
 def solveForColor(self, color, timelimit):
     self.current_winning_move = None
     assert is_black_white(color)
     self.time = time.time() + timelimit
     timeOut = False
     winForToPlay = self.negamaxBoolean()
     #if time.time() > self.time:
     #    timeOut = True
     winForColor = winForToPlay == (color == self.current_player)
     return winForColor, timeOut, self.current_winning_move
Beispiel #5
0
 def play_move_gomoku(self, point, color):
     assert is_black_white(color)
     if point == PASS: 
         return False
     if self.board[point] != EMPTY:
         return False
     self.board[point] = color
     self.current_player = GoBoardUtil.opponent(color)
     self.lastPoint = point
     self.lastColor = color
     return True
Beispiel #6
0
 def play_move_gomoku(self, point, color):
     """
         Play a move of color on point, for the game of gomoku
         Returns boolean: whether move was legal
         """
     assert is_black_white(color)
     assert point != PASS
     if self.board[point] != EMPTY:
         return False
     self.board[point] = color
     self.current_player = GoBoardUtil.opponent(color)
     return True
Beispiel #7
0
 def play_move(self, point, color):
     """
     Play a move of color on point
     Returns boolean: whether move was legal
     """
     assert is_black_white(color)
     if point == PASS:
         return
     if self.board[point] != EMPTY:
         return False
     self.board[point] = color
     self.check_win(point, color)
     self.current_player = GoBoardUtil.opponent(color)
     return True
Beispiel #8
0
	def __init__(self, master=None):
		choice = input("Choose player by entering 1 for Black or 2 for White (default player is Black): ")
		turn = BLACK
		try:
			turn = int(choice)
		except:
			pass
		
		if not is_black_white(turn):
			turn = BLACK

		tk.Frame.__init__(self, master)
		self.create_widgets(turn)
		self.master.title("GOMOKU")	
Beispiel #9
0
 def play_move_gomoku(self, point, color):
     """
     Play a move of color on point
     Returns boolean: whether move was legal
     """
     assert is_black_white(color), "wc"
     assert self.board[point] == EMPTY, "occupied"
     # Special cases
     if point == PASS:
         self.ko_recapture = None
         self.current_player = GoBoardUtil.opponent(color)
         return True
     self.board[point] = color
     return True
Beispiel #10
0
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal
        """
        assert is_black_white(color)
        # Special cases
        if point == PASS:
            return True
        elif (self.get_empty_points().size == 0) or (self.board[point] != EMPTY):
            return False  

        self.board[point] = color
        return True
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal
        """
        assert is_black_white(color)
        # Special cases
        '''
        if point == PASS:
            self.ko_recapture = None
            self.current_player = GoBoardUtil.opponent(color)
            return True
        '''
        if self.board[point] != EMPTY:
            return False
        '''
        if point == self.ko_recapture:
            return False
        '''

        # General case: deal with captures, suicide, and next ko point
        '''
        oppColor = GoBoardUtil.opponent(color)
        in_enemy_eye = self._is_surrounded(point, oppColor)
        '''
        self.board[point] = color
        '''
        single_captures = []
        
        neighbors = self._neighbors(point)
        for nb in neighbors:
            if self.board[nb] == oppColor:
                single_capture = self._detect_and_process_capture(nb)
                if single_capture != None:
                    single_captures.append(single_capture)
        block = self._block_of(point)
        if not self._has_liberty(block): # undo suicide move
            self.board[point] = EMPTY
            return False
        self.ko_recapture = None
        if in_enemy_eye and len(single_captures) == 1:
            self.ko_recapture = single_captures[0]
        '''

        self.current_player = GoBoardUtil.opponent(color)
        return True
 def play_move_gomoku(self, point, color):
     """
         Play a move of color on point, for the game of gomoku
         Returns boolean: whether move was legal
         """
     assert is_black_white(color)
     assert point != PASS
     if self.board[point] != EMPTY:
         return False
     self.board[point] = color
     row, col = self._point_to_2d_coord(point)
     # print("play_move_gomoku: row = {}, col = {}".format(row, col))
     self.twoDBoard[row][col] = color
     # print("twoDBoard = {}".format(self.twoDBoard))
     self.moves.append((point, color))
     self.current_player = GoBoardUtil.opponent(color)
     return True
Beispiel #13
0
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal, string: reason for illegal move
        """
        assert is_black_white(color)
        # Special cases
        if point == PASS:
            return False, ""
            #self.ko_recapture = None
            #self.current_player = GoBoardUtil.opponent(color)

        elif self.board[point] != EMPTY:
            return False, "occupied"

        if point == self.ko_recapture:
            return False

        # General case: deal with captures, suicide, and next ko point
        opp_color = GoBoardUtil.opponent(color)
        in_enemy_eye = self._is_surrounded(point, opp_color)
        self.board[point] = color
        single_captures = []
        neighbors = self._neighbors(point)
        for nb in neighbors:  # captures
            if self.board[nb] == opp_color:
                if self._detect_capture(nb):
                    self.board[point] = EMPTY
                    return False, "capture"
                """
                single_capture = self._detect_and_process_capture(nb)   # call _detect_capture, if yes, undo move and raise an error.
                if single_capture != None:                              
                    single_captures.append(single_capture)
                """
        block = self._block_of(point)
        if not self._has_liberty(block):  # undo suicide move
            self.board[point] = EMPTY
            return False, "suicide"
        """
        self.ko_recapture = None
        if in_enemy_eye and len(single_captures) == 1:
            self.ko_recapture = single_captures[0]
        #self.current_player = GoBoardUtil.opponent(color)
        """
        return True, ""
Beispiel #14
0
 def play_move(self, point, color):
     """
     Play a move of color on point
     Returns boolean: whether move was legal
     """
     assert is_black_white(color)
     if point == PASS:
         self.ko_recapture = None
         self.current_player = GoBoardUtil.opponent(color)
         self.last2_move = self.last_move
         self.last_move = point
         return True
     elif self.board[point] != EMPTY:
         return False
     self.board[point] = color
     self.current_player = GoBoardUtil.opponent(color)
     self.last2_move = self.last_move
     self.last_move = point
     return True
Beispiel #15
0
 def _block_of(self, stone):
     """
     Find the block of given stone
     Returns a board of boolean markers which are set for
     all the points in the block 
     """
     marker = np.full(self.maxpoint, False, dtype = bool)
     pointstack = [stone]
     color = self.get_color(stone)
     assert is_black_white(color)
     marker[stone] = True
     while pointstack:
         p = pointstack.pop()
         neighbors = self.neighbors_of_color(p, color)
         for nb in neighbors:
             if not marker[nb]:
                 marker[nb] = True
                 pointstack.append(nb)
     return marker
Beispiel #16
0
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal
        """
        assert is_black_white(color)
        # Special cases
        if point == PASS:
            self.ko_recapture = None
            self.current_player = GoBoardUtil.opponent(color)
            self.last2_move = self.last_move
            self.last_move = point
            return True
        elif self.board[point] != EMPTY:
            return False
        # if point == self.ko_recapture:
        #     return False

        # General case: deal with captures, suicide, and next ko point
        # opp_color = GoBoardUtil.opponent(color)
        # in_enemy_eye = self._is_surrounded(point, opp_color)
        self.board[point] = color
        self.moves.append(point)
        # single_captures = []
        # neighbors = self._neighbors(point)
        # for nb in neighbors:
        #     if self.board[nb] == opp_color:
        #         single_capture = self._detect_and_process_capture(nb)
        #         if single_capture != None:
        #             single_captures.append(single_capture)
        # block = self._block_of(point)
        # if not self._has_liberty(block):  # undo suicide move
        #     self.board[point] = EMPTY
        #     return False
        # self.ko_recapture = None
        # if in_enemy_eye and len(single_captures) == 1:
        #     self.ko_recapture = single_captures[0]
        self.current_player = GoBoardUtil.opponent(color)
        self.last2_move = self.last_move
        self.last_move = point
        return True
Beispiel #17
0
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal
        """
        assert is_black_white(color)
        # Special cases
        if point == PASS:
            return False
        elif self.board[point] != EMPTY:
            raise ValueError("occupied")
        if point == self.ko_recapture:
            return False

        # General case: deal with captures, suicide, and next ko point
        opp_color = GoBoardUtil.opponent(color)
        in_enemy_eye = self._is_surrounded(point, opp_color)
        if in_enemy_eye:
            raise ValueError("suicide")

        self.board[point] = color
        single_captures = []
        neighbors = self.neighbors[point]
        for nb in neighbors:
            if self.board[nb] == opp_color:
                single_capture = self._detect_and_process_capture(nb)
                if single_capture == True:
                    raise ValueError("capture")

        if not self._stone_has_liberty(point):
            # check suicide of whole block
            block = self._block_of(point)
            if not self._has_liberty(block):  # undo suicide move
                self.board[point] = EMPTY
                raise ValueError("suicide")
        self.ko_recapture = None
        # if in_enemy_eye and len(single_captures) == 1:
        #     self.ko_recapture = single_captures[0]
        self.current_player = GoBoardUtil.opponent(color)
        return True
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal
        """
        assert is_black_white(color)
        # Special cases
        if point == PASS:
            self.ko_recapture = None
            self.current_player = GoBoardUtil.opponent(color)
            self.last2_move = self.last_move
            self.last_move = point
            return True
        elif self.board[point] != EMPTY:
            return False
        if point == self.ko_recapture:
            return False

        # General case: deal with captures, suicide, and next ko point
        self.board[point] = color

        self.current_player = GoBoardUtil.opponent(color)
        return True
Beispiel #19
0
 def is_legal(self, point, color):
     """
     Check whether it is legal for color to play on point
     """
     assert is_black_white(color)
     # Special cases
     if point == PASS:
         return True
     elif self.board[point] != EMPTY:
         return False
     if point == self.ko_recapture:
         return False
         
     # General case: detect captures, suicide
     opp_color = GoBoardUtil.opponent(color)
     self.board[point] = color
     legal = True
     has_capture = self._detect_captures(point, opp_color)
     if not has_capture and not self._stone_has_liberty(point):
         block = self._block_of(point)
         if not self._has_liberty(block): # suicide
             legal = False
     self.board[point] = EMPTY
     return legal
Beispiel #20
0
    def is_capture(self, point, color):

        if is_black_white(color):

            board_copy = self.copy()

            nb = board_copy._neighbors(point)
            opp_color = GoBoardUtil.opponent(color)
            board_copy.board[point] = color
            for opp_point in nb:

                if board_copy.board[opp_point] == opp_color:
                    opp_block = board_copy._block_of(opp_point)
                    if not board_copy._has_liberty(opp_block):

                        board_copy.board[point] = EMPTY
                        del board_copy
                        return True

            board_copy.board[point] = EMPTY
            del board_copy
            return False
        else:
            return True
 def is_legal(self, point, color):
     """
     Check whether it is legal for color to play on point
     """
     assert is_black_white(color)
     # Special cases
     if point == PASS:
         return False
     elif self.board[point] != EMPTY:
         return False
     if point == self.ko_recapture:
         return False
         
     # General case: deal with captures, suicide, and next ko point
     opp_color = GoBoardUtil.opponent(color)
     in_enemy_eye = self._is_surrounded(point, opp_color)
     self.board[point] = color
     single_captures = []
     neighbors = self.neighbors[point]
     for nb in neighbors:
         if self.board[nb] == opp_color:
             single_capture = self._detect_and_process_capture(nb)
             if single_capture == True:
                 self.board[point] = EMPTY
                 return False
     if not self._stone_has_liberty(point):
         # check suicide of whole block
         block = self._block_of(point)
         if not self._has_liberty(block): # undo suicide move
             self.board[point] = EMPTY
             return False
     self.ko_recapture = None
     if in_enemy_eye and len(single_captures) == 1:
         self.ko_recapture = single_captures[0]
     self.board[point] = EMPTY
     return True
Beispiel #22
0
    def play_move(self, point, color):
        """
        Play a move of color on point
        Returns boolean: whether move was legal
        """
        assert is_black_white(color)
        # Special cases
        if point == PASS:
            #self.ko_recapture = None
            self.current_player = GoBoardUtil.opponent(color)
            return True
        elif self.board[point] != EMPTY:
            return False
        #if point == self.ko_recapture:
        #return False

        # General case: deal with captures, suicide, and next ko point
        #oppColor = GoBoardUtil.opponent(color)
        #in_enemy_eye = self._is_surrounded(point, oppColor)
        self.board[point] = color
        #single_captures = []
        #neighbors = self._neighbors(point)
        #for nb in neighbors:
        #if self.board[nb] == oppColor:
        #single_capture = self._detect_and_process_capture(nb)
        #if single_capture != None:
        #single_captures.append(single_capture)
        #block = self._block_of(point)
        #if not self._has_liberty(block): # undo suicide move
        #self.board[point] = EMPTY
        #return False
        #self.ko_recapture = None
        #if in_enemy_eye and len(single_captures) == 1:
        #self.ko_recapture = single_captures[0]

        #check vertical winner
        count = -1
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location += 1
                else:
                    break
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location -= 1
                else:
                    break
        if count >= 5:
            self.winner = color

        #check horizental winner
        count = -1
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location += (self.size + 1)
                else:
                    break
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location -= (self.size + 1)
                else:
                    break
        if count >= 5:
            self.winner = color
        #check diag from top left to down right
        count = -1
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location += (self.size + 2)
                else:
                    break
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location -= (self.size + 2)
                else:
                    break
        if count >= 5:
            self.winner = color

        #check diag from top right to down left
        count = -1
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location += self.size
                else:
                    break
        location = point
        for i in range(5):
            if location > 0 and location < len(self.board):
                if self.board[location] == color:
                    count += 1
                    location -= self.size
                else:
                    break
        if count >= 5:
            self.winner = color

        self.current_player = GoBoardUtil.opponent(color)
        return True