示例#1
0
def plaverVSPlayer():
    run = True
    finished = False
    game = Game(WINDOW, 1)
    time_elapsed = None
    firstMove = True

    while run:
        
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN and finished:
                    run = False
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN and not finished:
                pos = pygame.mouse.get_pos()
                x, y = pos
                if x <= 800:
                    row, col = get_row_col_from_mouse(pos)
                    game.select(row,col)
                    if game.checkWin() != -1:
                        finished = True
                elif game.button.isOver(pos):
                    #Hint for Player vs Player
                    if not firstMove:
                        row, col = game.getLastMove()
                    else:
                        row = 0
                        col = 0
                        firstMove = False
                    game.nodes = 0
                    start_time = time.perf_counter()
                    (m, oldRow , oldCol , finalRow, finalCol) = game.max_with_alpha_beta_cuts(row, col, 6, -2000, 2000, game.getPlayer(), True, 6)
                    end_time = time.perf_counter()
                    
                    time_elapsed = end_time-start_time

                    game.hintSquarePiece = (oldRow, oldCol)
                    game.hintSquareToMove = (finalRow, finalCol)
                
    
        game.update(time_elapsed)
示例#2
0
def playerVSPc():
    global diff_pvc
    global order
    run = True

    game = Game(WINDOW, 2)
    finished = False
    firstMove = True
    time_elapsed = None
    
    while run:
        
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN and finished:
                    run = False
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN and not finished:
                pos = pygame.mouse.get_pos()
                x, y = pos
                if x <= 800 and game.getPlayer() == 1 and not finished:
                    row, col = get_row_col_from_mouse(pos)
                    game.select(row,col)
                    if game.checkWin() != -1:
                        finished = True
                elif game.button.isOver(pos) and game.getPlayer() == 1:
                    #Hint for Player vs PC
                    if not firstMove:
                        row, col = game.getLastMove()
                    else:
                        row = 0
                        col = 0
                        firstMove = False

                    game.nodes = 0
                    start_time = time.perf_counter()
                    (m, oldRow , oldCol , finalRow, finalCol) = game.max_with_alpha_beta_cuts(row, col, 6, -2000, 2000, game.getPlayer(), True, 6)
                    end_time = time.perf_counter()

                    time_elapsed = end_time-start_time
                    game.hintSquarePiece = (oldRow, oldCol)
                    game.hintSquareToMove = (finalRow, finalCol)
                
                elif game.button.isOver(pos) and game.getPlayer() == 2:
                    row, col = game.getLastMove()
                    
                    if algo == 1:
                        if diff_pvc == 6:
                            depth = 5
                        else:
                            depth = diff_pvc
                        game.nodes = 0
                        start_time = time.perf_counter()
                        (m, oldRow , oldCol , finalRow, finalCol) = game.max(row, col, depth, game.getPlayer(), order, diff_pvc)
                        end_time = time.perf_counter()

                    elif algo == 2:
                        game.nodes = 0
                        start_time = time.perf_counter()
                        (m, oldRow , oldCol , finalRow, finalCol) = game.max_with_alpha_beta_cuts(row, col, diff_pvc, -2000, 2000, game.getPlayer(), order, diff_pvc)
                        end_time = time.perf_counter()

                    time_elapsed = end_time-start_time
                    
                    game.selected = oldRow, oldCol
                    game.ai_move(finalRow, finalCol)
                    if game.checkWin() != -1:
                        finished = True
                
                

        game.update(time_elapsed)
示例#3
0
def pcVSPc():
    global diff_cvc_1
    global diff_cvc_2
    global algo_PC1
    global algo_PC2
    global order_PC1
    global order_PC2

    run = True
    game = Game(WINDOW, 3)
    firstMove = True
    finished = False
    time_elapsed = None
    
    while run:
        
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN and finished:
                    run = False
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN and not finished:
                pos = pygame.mouse.get_pos()
                x, y = pos
                if game.button.isOver(pos) and game.getPlayer() == 1:   
                    if not firstMove:
                        row, col = game.getLastMove()
                    else:
                        row = 0
                        col = 0
                        firstMove = False

                    if algo_PC1 == 1:

                        if diff_cvc_1 == 6:
                            depth = 5
                        else:
                            depth = diff_cvc_1

                        game.nodes = 0
                        start_time = time.perf_counter()
                        (m, oldRow , oldCol , finalRow, finalCol) = game.max(row, col, depth, game.getPlayer(), order_PC1, diff_cvc_1)
                        end_time = time.perf_counter()

                    elif algo_PC1 == 2:
                        game.nodes = 0
                        start_time = time.perf_counter()
                        (m, oldRow , oldCol , finalRow, finalCol) = game.max_with_alpha_beta_cuts(row, col, diff_cvc_1, -2000, 2000, game.getPlayer(), order_PC1,diff_cvc_1)
                        end_time = time.perf_counter()

                    time_elapsed = end_time-start_time

                    game.selected = oldRow, oldCol
                    game.ai_move(finalRow, finalCol)
                    if game.checkWin() != -1:
                        finished = True
                
                elif game.button.isOver(pos) and game.getPlayer() == 2:
                    row, col = game.getLastMove()
                    
                    if algo_PC2 == 1:

                        if diff_cvc_2 == 6:
                            depth = 5
                        else:
                            depth = diff_cvc_2
                        
                        game.nodes = 0
                        start_time = time.perf_counter()
                        (m, oldRow , oldCol , finalRow, finalCol) = game.max(row, col, depth, game.getPlayer(), order_PC2, diff_cvc_2)
                        end_time = time.perf_counter()

                    elif algo_PC2 == 2:
                        game.nodes = 0
                        start_time = time.perf_counter()
                        (m, oldRow , oldCol , finalRow, finalCol) = game.max_with_alpha_beta_cuts(row, col, diff_cvc_2, -2000, 2000, game.getPlayer(), order_PC2, diff_cvc_2)
                        end_time = time.perf_counter()


                    time_elapsed = end_time-start_time
                    
                    game.selected = oldRow, oldCol
                    game.ai_move(finalRow, finalCol)
                    if game.checkWin() != -1:
                        finished = True
                
                
    
        game.update(time_elapsed)