0

there is a 4 connect game using a minimax algorithm I'm trying to improving it by turning it to alpha-beta i have this :

getResult([H,T|_],H,T).

doBest(Color, Depth, BestScore, BestMove):-
    board(C,_),
    setof(X,between(1,C,X),MoveList),
    minimax(MoveList,Depth, Color, Res), 
    sort(Res,SortRes),
    (level(N),Depth == N;true),
    (   Color == r ,!,
              nth0(0,SortRes,Move),
              getResult(Move,BestScore,BestMove)
        ;
        Color == y ,!,
                      length(SortRes,Len),
              nth1(Len,SortRes,Move),
                      getResult(Move,BestScore,BestMove)
    ).



minimax([],_,_,[]).
minimax([H|T],Depth, Color, L):-
    minimax(T,Depth,Color,L1),
    (
        full(H),!, L = L1,!; 
        ( 
        insert(H,Color), write('__here__'),
            top(H,Hight),
            checkStatus(H,Hight,Color), write('_'),write(H),write('-'),write(Hight),write('_'),
            (    win(r) ,!,(V0 is -1000000000  * (Depth + 1),append([[V0,H]],L1,L));
             win(y) ,!,(V0 is 1000000000   * (Depth + 1),append([[V0,H]],L1,L));
                     draw() ,!, append([[0,H]],L1,L);
                 not(win(r);win(y);draw()),!,
                 (
                Depth == 0 ,!,(score(r,Vr),score(y,Vy),V0 is Vy - Vr,
                    write('_'),write(Vr),write('|'),write(Vy),write('|'),write(V0),write('_'),
                    append([[V0,H]],L1,L));
                Depth > 0  ,!,( NewDep is Depth - 1,
                        (Color == r,!,doBest(y, NewDep, BestScore,_),(write('{'),write(BestScore),write('}'));
                         Color == y,!,doBest(r, NewDep, BestScore,_),(write('{'),write(BestScore),write('}')) ),
                    append([[BestScore,H]],L1,L)
                      )  
             )
            )
        ),
        resetStatus(),
        remove(H), write('__out__')
    ).

what its doing is for the current move get all the other player's moves , "sons of the vertex in the tree" , and for each- check if its a legal move (the column isn't full) , if yes, then check outliners ( a win to either or draw),
if not, check if its a leave vertex in the tree, and get the values if not, go for another round in the next depth for every vertex create more sons, every move that is calculated is inserted to the "game state" until the value goes up the tree and then its removed for alphabeta pruning i know you need too add an A, B, parameters to both functions and send the boundaries at every calculation , and every vertex is checked against those to prone the next vertexes

i have this algorithem but i cant implement it to the original above , how shpuld i do it?

 evaluate_and_choose([ Move | Moves ], Position, D, Alpha, Beta, Move1, BestMove ) :-
    move( Move, Position, Positionl ),
    alpha_beta( D, Positionl, Alpha, Beta, MoveX, Value ),
    Value1 is -Value,
    cutoff( Move, Value1, D, Alpha, Beta, Moves, Position, Move1, BestMove ).

evaluate_and_choose( [], Position, D, Alpha, Beta, Move, ( Move, Alpha )).

alpha_beta( 0, Position, Alpha, Beta, Move, Value ) :- 
    value( Position, Value ).
    
alpha_beta( D, Position, Alpha, Beta, Move, Value ) :- 
    findall( M, move( Position, M ), Moves ),
    Alphal is -Beta,
    Betal is -Alpha,
    D1 is D-l,
    evaluate_and_choose( Moves, Position, D1, Alphal, Betal, nil, ( Move, Value )).

    
cutoff( Move, Value, D, Alpha, Beta, Moves, Position, Movel, ( Move,Value )) :- 
    Value > Beta.
cutoff(Move, Value, D, Alpha, Beta, Moves, Position, Movel, BestMove ) :- 
    Alpha < Value, Value < Beta,
    evaluate_and_choose( Moves, Position, D, Value, Beta, Move, BestMove ).

cutoff( Move, Value, D, Alpha, Beta, Moves, Position, Movel, BestMove ) :- 
    Value < Alpha,
    evaluate_and_choose( Moves, Position, D, Alpha, Beta, Move1, BestMove ).
  • I have no idea how your code works and I won't try to either. But why don't you use the standard alpha-beta pruning structure, found as pseudo code e.g. here: https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning? – eligolf Oct 12 '22 at 06:07

0 Answers0