Пример #1
0
def perft(board, depth):

    #Get all possible moves
    legal_moves = board.generate_moves()

    #Check if last move caused win
    lmv = board.last_move_won()

    #if last move won the game or no valid move available
    if lmv or len(legal_moves) <= 0:
        #Unmake last move made by calling function
        board.unmake_last_move()
        return 1

    #if last move did not win the game and valid moves are available,
    #but depth limit is reached
    if depth == 1:
        board.unmake_last_move()
        return len(legal_moves)

    #initialize variable to count leaf nodes visited so far
    count = 0

    #For all legal moves from current state, call perft recursively
    for i in legal_moves:
        board.make_move(i)
        count += perft(board, depth - 1)

    #Unmake last move made by calling function before returning count
    board.unmake_last_move()

    #return number of leaf nodes visited
    return count

    pass
Пример #2
0
	def is_terminal(self, board):
		
		legal_moves = board.generate_moves()
		lmw = board.last_move_won()
		
		#return true if no possible move or last move won
		if len(legal_moves) == 0 or lmw:
			return True
		
		#return false otherwise
		return False
Пример #3
0
	def utility(self, board, player):
		lmw = board.last_move_won()
		
		next_player = board.get_player()
		
		if lmw and player == next_player:
			return -1, -1, -1
		
		if lmw and player != next_player:
			return 1, 1, 1
		
		return 0, 0, 0
Пример #4
0
def is_terminal(board):

    #Get possible moves
    legal_moves = board.generate_moves()

    #Check if last move caused any player win the game
    lmw = board.last_move_won()

    #Return 'True' in either of the two conditions is true
    if len(legal_moves) == 0 or lmw:
        return True

    #Return 'False' otherwise
    return False
Пример #5
0
def utility(board, player):

    #check if last_move_won
    lmw = board.last_move_won()

    #get the player who has a turn
    next_player = board.get_player()

    #Return -1 if calling player loses
    if lmw and player == next_player:
        return -1, -1, -1

    #Return 1 if calling player wins
    if lmw and player != next_player:
        return 1, 1, 1

    #Return 0 otherwise
    return 0, 0, 0