Esempio n. 1
0
def main():
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Checkers")
    clock = pygame.time.Clock()

    moves = 0

    board = Board(size, constraints, board_colors, board_alternate,
                  board_highlight)

    # add red pieces to the board
    red_pieces = []
    for p in board.red_starting_positions:
        piece = Piece(p, 'Red', red, board_highlight, board.dim, -1, font)
        red_pieces.append(piece)

    # add black pieces to the board
    black_pieces = []
    for p in board.black_starting_positions:
        piece = Piece(p, 'Black', black, board_highlight, board.dim, 1, font)
        black_pieces.append(piece)

    all_pieces = red_pieces + black_pieces

    draw_board(board, 0, all_pieces, pygame.draw, screen)

    selected = False
    jumping = None
    jumps = []
    playing = True
    winner = ''
    players = ["Red", "Black"]

    ai = AI('Black')

    while playing:
        clock.tick(FPS)
        currentPlayer = players[moves % len(players)]

        if currentPlayer == "Black":
            pieces = black_pieces
            opponents = red_pieces
            (piece, jumps) = ai.choose_piece(board, pieces, opponents)
            while len(jumps):
                (piece, jumps, pieces,
                 opponents) = ai.move_piece(piece, board, pieces, opponents)
                for p in pieces:
                    p.check_king(rows)
            draw_board(board, moves % len(players), all_pieces, pygame.draw,
                       screen)
            moves += 1

        else:
            pieces = red_pieces
            opponents = black_pieces

            for event in pygame.event.get():
                if event.type == pygame.QUIT: sys.exit()
                # handle MOUSEBUTTONUP
                if event.type == pygame.MOUSEBUTTONUP:
                    pos = pygame.mouse.get_pos()
                    square = board.get_square(pos)
                    if selected:
                        (board, pieces, selected, jumping, jumps,
                         moves) = move_piece(square, board, pieces, opponents,
                                             all_pieces, pos, selected,
                                             jumping, jumps, moves)
                    else:
                        (board, pieces, selected,
                         jumps) = select_piece(square, board, pieces,
                                               all_pieces, pos, jumps)

                    draw_board(board, moves % len(players), all_pieces,
                               pygame.draw, screen)

                #check for winning condition
                red_count = 0
                black_count = 0

                for p in all_pieces:
                    if p.alive:
                        if p.player == 'Red':
                            red_count += 1
                        if p.player == 'Black':
                            black_count += 1

                if red_count == 0:
                    winner = 'Black'
                    playing = False

                if black_count == 0:
                    winner = 'Red'
                    playing = False

    print(winner + ' won in only ' + str(moves // 2) + ' turns! Good job!')