示例#1
0
def play_auto(num_games):

    best_score = 0

    for _ in range(num_games):

        board = BOARD.SolitaireBoard()
        print(board)

        for m in range(5000):  # Limit to 100 moves
            moves = MOVES.find_moves(board)
            print(moves)
            if not moves:
                break
            m = random.choice(moves)
            print('Move:', m)
            MOVES.make_move(m, board)
            print(board)

        score = board.get_score()
        print('Score', score)

        if score > best_score:
            best_score = score

    print(best_score)
示例#2
0
def game():
    while True:
        moves.board_history.append(board.chessboard)
        board.printboard(board.chessboard)
        moves.make_move()
        moves.halfmove_checker += 1

        #Checks for threefold repition
        if moves.threefold_check():
            print("\nDraw by threefold repitition")
            break

        #Checks for 50 move rule
        if moves.halfmove_checker == 99:
            print("\nDraw by 50 move rule")
            break
示例#3
0
def human_turn(playing_board, my_pieces, computer_pieces):
    print_board(playing_board)

    # Get a list of all the pieces that can be moved
    edible = can_eat(playing_board, my_pieces, contents.WHITE, contents.BLACK)

    # Print the list of pieces
    print_pieces(edible[0])

    # Get which piece the user wants to move
    index = get_piece(edible[0])

    # Get where the user would like to move that piece
    print 'Please select where to move this piece to'
    moves = get_moves(playing_board, index, contents.WHITE, contents.BLACK)
    print_dct(moves)
    move = get_move(moves)

    # Make the move
    make_move(playing_board, index, move, edible[1])
示例#4
0
def get_non_checked_moves(board, current_turn, piece_moves):
    non_checked_moves = []

    for piece_move in piece_moves:
        potential_board = moves.make_move(board, piece_move, False)

        king_square = get_king_square(potential_board, current_turn)

        if not is_square_attacked(potential_board, king_square, current_turn):
            non_checked_moves.append(piece_move)
    
    return non_checked_moves
示例#5
0
def play_cli():
    deck = CARDS.make_deck()
    CARDS.shuffle_deck(deck)
    board = BOARD.new_board(deck)

    while True:
        moves = MOVES.find_moves(board)
        BOARD.show_board(board)
        print('------------')

        if not moves:
            print('No moves. Game over.')
            break

        for i in range(len(moves)):
            print('{}: {}'.format(i + 1, moves[i]))

        print()
        g = input('Move: ')

        MOVES.make_move(moves[int(g) - 1], board)
        print()
示例#6
0
    def apply_move(self, move):
        move = self.move_to_coordinates(move)
        legal_moves = self.get_legal_moves()

        if move not in legal_moves:
            raise IllegalMoveError

        moving_piece = self.board[move[0][0]][move[0][1]]

        if moving_piece.piece_type in ("K", "R"):
            moving_piece.has_moved = True

        self.board = moves.make_move(self.board, move)

        self.move_history.append(move)
        self.switch_turn()
示例#7
0
def computer_turn(playing_board, comp_pieces, human_pieces, difficulty):
    # Get globals
    global MAX_PRUNES
    global MIN_PRUNES
    global NODES_MADE

    # Set globals to 0
    MAX_PRUNES = 0
    MIN_PRUNES = 0
    NODES_MADE = 0

    # Call the alpha beta search to find best move
    result = alpha_beta_max(playing_board, comp_pieces, human_pieces,
                            difficulty, -1000, 1000)

    # Get the depth that the tree was searched at
    depth = result[2]

    # Output data
    print 'Depth searched: ', depth
    print 'Number of nodes: ', NODES_MADE
    print 'Number of MAX prunes: ', MAX_PRUNES
    print 'Number of MIN prunes: ', MIN_PRUNES

    # Find the new locations of computer's pieces
    new_spaces = result[0]

    # Find the new and old moves
    new_move = []
    old_move = []
    for move in new_spaces:
        if move not in comp_pieces:
            new_move = move
    for move in comp_pieces:
        if move not in new_spaces:
            old_move = move
    eaten = False

    # Find if a piece was eaten or not on computer's turn
    change_row = old_move[0] - new_move[0]
    change_col = old_move[1] - new_move[1]
    if abs(change_col) > 1 or abs(change_row) > 1:
        if change_row > 0:
            check_row = old_move[0] - 1
        elif change_row is 0:
            check_row = old_move[0]
        else:
            check_row = old_move[0] + 1
        if change_col > 0:
            check_col = old_move[1] - 1
        elif change_col is 0:
            check_col = old_move[1]
        else:
            check_col = old_move[1] + 1
        # If a piece was eaten
        if playing_board[check_row][check_col] is contents.WHITE:
            eaten = True

    # Print the computer's move
    print 'The computer moved from: ', old_move, ' to: ', new_move

    # Move the computer's piece
    make_move(playing_board, old_move, new_move, eaten)