Exemple #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)
Exemple #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)