Paste: game tree maker

Author: Rex
Mode: ml
Date: Fri, 12 Jun 2009 17:27:07
Plain Text |
%tchukarumaRealTREE.m
%state is the vector with all possible game states
%temp is the character string that you need to paste into maple
%in order to run this program type something like
%[a,b]=tchukarumaRealTREE
% a will then contain all of the states and b the stuff to paste into maple
%I think the main command you need to change below is play 
function [state,terminal,temp,temp2]=tchukarumaRealTREE()
bins = input('Please enter the number of bins, excluding the ruma.');
stones = input('Please enter the number of stones in each bin.');
%alph=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
%alph=['a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a'];
alph=char(97*ones(10000,1)); %works for all a's


fid=fopen('output.m','w');
state(1,:)=[stones*ones(1,bins) 0];
terminal(1)=1;
binnum=1;
nummoves(1)=1;
gamelevel = 1;
veclevel=2;
chain(1)=0;
chaining=0;
fprintf(fid,'b:=Graph(undirected,{');
temp2='c:=Graph(undirected,{';
stopping=1;

while(stopping && veclevel<2000)
    if(chain(gamelevel)~=0)
        chaining=1;
        binnum=chain(gamelevel);
    end
    
    if(state(gamelevel,binnum)~=0 && terminal(gamelevel)~=0)
        [state(veclevel,:),terminal(veclevel),chain(veclevel)]=play(binnum,state(gamelevel,:),bins);
        nummoves(veclevel)=nummoves(gamelevel)+1;
        binvec(veclevel)=binnum;
        if(veclevel==2)
            	temp2=strcat(temp2,'[',alph(nummoves(gamelevel)),vec2str(state(gamelevel,:)),',',alph(nummoves(veclevel)),vec2str(state(veclevel,:)),']');
        else
            temp2=strcat(temp2,',[',alph(nummoves(gamelevel)),vec2str(state(gamelevel,:)),',',alph(nummoves(veclevel)),vec2str(state(veclevel,:)),']');
        end
       if(veclevel~=2)
           fprintf(fid,',');
       end
      	fprintf(fid,strcat(',{',num2str(gamelevel),',',num2str(veclevel),'}'));
        veclevel=veclevel+1;
    end
    if(chaining==1)
        gamelevel=gamelevel+1;
        chaining=0;
        binnum=1;
    else
        if(binnum==bins)
            binnum=1;
            gamelevel=gamelevel+1;
        else
            binnum=binnum+1;
        end
    end

    if(gamelevel+1>veclevel)
        stopping=0;
    end
end

fprintf(fid,'});');
fprintf(fid,strcat(temp2,'});'));


function str=vec2str(v)
    str='';
    for i=1:length(v)
        str=strcat(str,num2str(v(i)));
    end
end

function [s,t,c]=play(move,state,bins)
    c=0;
    t=1;
    moving = state(move);
    state(move)=0;
    for i=1:moving
        if(move==bins+1)
            move=1;
        else
            move=move+1;
        end
        if((i==moving && state(move)==0) && move~=bins+1)
            t=0;
        end
        state(move)=state(move)+1;
    end
    s=state;
    if(move~=bins+1)
        c=move;
    end
    if(min(state(1:bins)==zeros(1,bins))==1)
        t=2;
    end

end
end

New Annotation

Summary:
Author:
Mode:
Body: