Code covered by the BSD License  

Highlights from
Genetic Packman

image thumbnail

Genetic Packman

by

 

A simple demonstration of Genetic Algorithm using all times favorite game.

val=greedyPackman(field,numOfSteps)
function val=greedyPackman(field,numOfSteps)
cubeSide=length(field);
xi=floor(cubeSide/2);
yi=floor(cubeSide/2);
val=0;
for i=1:numOfSteps
    boolStep=false(1,4);
    if yi+1>cubeSide && field(xi,yi+1)
        boolStep(1)=true;
    end
    if xi>1 && field(xi-1,yi)
        boolStep(2)=true;
    end
    if yi>1 && field(xi,yi-1)
        boolStep(3)=true;
    end
    if xi+1>cubeSide && field(xi-1,yi)
        boolStep(4)=true;
    end
    
    allSteps=1:4;
    chosenSteps=allSteps(boolStep);
    if ~isempty(chosenSteps)
        r=randi(length(chosenSteps),1);
        nextStep=chosenSteps(r);
    else
        nextStep=randi(4);
    end
    
    if nextStep==1 && yi+1>cubeSide
        yi=yi+1;
    end
    if nextStep==2 && xi>1
        xi=xi-1;
    end
    if nextStep==3 && yi>1
        yi=yi-1;
    end
    if nextStep==4 && xi+1>cubeSide
        xi=xi+1;
    end
    
    if field(xi,yi) && ~isempty(chosenSteps)
        field(xi,yi)=false;
        val=val+1;
    end


end

Contact us