Paste: unplayo

Author: rex
Mode: ml
Date: Tue, 9 Jun 2009 13:16:37
Plain Text |
% unplay 2
% A single-turn unplay program for Tchukaruma.
% Tells all of the possible previous board states
% 	and also tells a list of the correlating choices
% 	that would have been made.
% By Rex Ford

% Collecting information
% bins = input('Please enter the number of bins.');
function unplay_2

bins=4;
state=zeros(1,bins);
for i=1:bins
    i
    state(i)=input('how many are in this hole?');
end
state


    function skip=find_skip(state)
        % finding the number we are skipping
        skip=1;
        while(state(skip)~=0 && skip<3)
            skip=skip+1;
        end
    end


    function [fin_state,choice] = unplay_wrapping(state,start_pos,wraps)
        pos=start_pos;
        collection=0;
        % finding the lowest value that isn't the skip bin
        skip=find_skip(state);
        while(state(pos)~=0 || pos~=skip || wraps~=0)
            if(pos==skip)
                wraps=wraps-1;
                if(pos>1)
                    pos=pos-1;
                else
                    pos=length(state);
                end
            end
            state(pos)=state(pos)-1;
            collection=collection+1;
            if(pos>1)
                pos=pos-1;
            else
                pos=length(state);
            end
        end
        state(pos)=collection;
        fin_state=state;
        choice=pos;
    end


    function [fin_states,choices] = unplay(state,start_pos)
        fin_states=zeros(1,length(state));
        choices=zeros(1,1);
        skip=find_skip(state);
        
        lowest=1;
        while(state(lowest)==0)
            lowest=lowest+1;
        end
        
        for i=1:length(state)
            if(state(i)~=0 && state(i) < state(lowest) && i~=skip)
                lowest=i;
            end
        end
        for wraps=0:state(lowest)
            [fin_states(wraps+1,:),choices(wraps+1,1)] = unplay_wrapping(state,start_pos,wraps);
        end
    end

% Here is the meat:
% This is the unplay for tchukaruma
answers=zeros(1,length(state));
choices=zeros(1,1);
k=1;
for i=1:length(state)
    if(state(i)==0 && my_side(i)==false)
        % we try two things from here:
        % the zero could have been 2
        state(i)=2;
        [new_answers,new_choices]=unplay(state,i);
        answers=[answers;new_answers];
        choices=[choices;new_choices];
        k=k+1;
        % the zero could have been 3
        state(i)=3;
        [new_answers,new_choices]=unplay(state,i);
        answers=[answers;new_answers];
        choices=[choices;new_choices];
        state(i)=0;
        k=k+1;
    elseif(state(i)~=0)
        [new_answers,new_choices]=unplay(state,i);
        answers=[answers;new_answers];
        choices=[choices;new_choices];
        k=k+1;
    end
end

answers
choices


% tells if a position is on 'my' side
    function is_it = my_side(x)
        is_it=same_side(1,x);
    end

% are these two positions on the same side?
    function is_it =same_side(x,y)
        is_it=false;
        n=length(state)/2;
        if(x>n && y>n)
            is_it=true;
        end
        if(x<=n && y<=n)
            is_it=true;
        end
    end

end

Annotation: unplayo, better

Author: Rex
Mode: ml
Date: Tue, 9 Jun 2009 15:06:03
Plain Text |
% unplay 2
% A single-turn unplay program for Tchukaruma.
% Tells all of the possible previous board states
% 	and also tells a list of the correlating choices
% 	that would have been made.
% By Rex Ford

% Collecting information
% bins = input('Please enter the number of bins.');
function unplay_2

bins=4;
state=zeros(1,bins);
for i=1:bins
    fprintf('bin %i: ',i);
    state(i)=input('how many? ');
end
state

% Here is the meat:
% This is the unplay for ayo
answers=zeros(1,length(state));
choices=zeros(1,1);
k=1;
for i=1:length(state)
    if(state(i)==0 && my_side(state,i)==false)
        % we try two things from here:
        % the zero could have been 2
        state(i)=2;
        [new_answers,new_choices]=unplay(state,i);
        answers=[answers;new_answers];
        choices=[choices;new_choices];
        k=k+1;
        % the zero could have been 3
        state(i)=3;
        [new_answers,new_choices]=unplay(state,i);
        answers=[answers;new_answers];
        choices=[choices;new_choices];
        state(i)=0;
        k=k+1;
    elseif(state(i)~=0)
        [new_answers,new_choices]=unplay(state,i);
        answers=[answers;new_answers];
        choices=[choices;new_choices];
        k=k+1;
    end
end

answers(1,:)=[];
choices(1,:)=[];
answers
choices


    function skip=find_skip(state)
        % finding the number we are skipping
        skip=1;
        while(state(skip)~=0 && skip<3)
            skip=skip+1;
        end
    end


    function [fin_state,choice] = unplay_wrapping(state,start_pos,wraps)
        pos=start_pos;
        collection=0;
        % finding the lowest value that isn't the skip bin
        skip=find_skip(state);
        while(state(pos)~=0 || pos~=skip || wraps~=0)
            if(pos==skip)
                wraps=wraps-1;
                if(pos>1)
                    pos=pos-1;
                else
                    pos=length(state);
                end
            end
            state(pos)=state(pos)-1;
            collection=collection+1;
            if(pos>1)
                pos=pos-1;
            else
                pos=length(state);
            end
        end
        state(pos)=collection;
        fin_state=state;
        choice=pos;
    end


    function [fin_states,choices] = unplay(state,start_pos)
        fin_states=zeros(1,length(state));
        choices=zeros(1,1);
        skip=find_skip(state);
        
        lowest=1;
        while(state(lowest)==0)
            lowest=lowest+1;
        end
        
        for i=1:length(state)
            if(state(i)~=0 && state(i) < state(lowest) && i~=skip)
                lowest=i;
            end
        end
        for wraps=0:state(lowest)
            [fin_states(wraps+1,:),choices(wraps+1,1)] = unplay_wrapping(state,start_pos,wraps);
        end
    end

% tells if a position is on 'my' side
    function is_it = my_side(state,x)
        is_it=same_side(state,1,x);
    end

% are these two positions on the same side?
    function is_it =same_side(state,x,y)
        is_it=false;
        n=length(state)/2;
        if(x>n && y>n)
            is_it=true;
        end
        if(x<=n && y<=n)
            is_it=true;
        end
    end
    
    function [good_answers,good_chioces]=filter_results(answers,choices)
    	x=1;
    	while(x<=size(answers))
    		if(min(answers(x,:)<0)
    			answers(x,:)=[];
    			choices(x,:)=[];
    			x=x-1;
    		end
    		x=x+1;
    	end
    	answers=good_answers;
    	choices=good_choices;
    end

end

Annotation: generalized

Author: Rex
Mode: ml
Date: Tue, 9 Jun 2009 20:39:03
Plain Text |
% unplay 2
% A single-turn unplay program for Tchukaruma.
% Tells all of the possible previous board states
% 	and also tells a list of the correlating choices
% 	that would have been made.
% By Rex Ford

% Collecting information
% bins = input('Please enter the number of bins.');
function unplayo

    function skip=find_skip(state)
        % finding the number we are skipping
        skip=1;
        while(state(skip)~=0 && skip<3)
            skip=skip+1;
        end
    end
    function [fin_state,choice] = unplay_wrapping(state,start_pos,wraps)
        pos=start_pos;
        collection=0;
        % finding the lowest value that isn't the skip bin
        skip=find_skip(state);
        while(state(pos)~=0 || pos~=skip || wraps~=0)
            if(pos==skip)
                wraps=wraps-1;
                if(pos>1)
                    pos=pos-1;
                else
                    pos=length(state);
                end
            end
            state(pos)=state(pos)-1;
            collection=collection+1;
            if(pos>1)
                pos=pos-1;
            else
                pos=length(state);
            end
        end
        state(pos)=collection;
        fin_state=state;
        choice=pos;
    end


    function [fin_states,choices] = unplay(state,start_pos)
        fin_states=zeros(1,length(state));
        choices=zeros(1,1);
        skip=find_skip(state);

        lowest=1;
        while(state(lowest)==0)
            lowest=lowest+1;
        end

        for i=1:length(state)
            if(state(i)~=0 && state(i) < state(lowest) && i~=skip)
                lowest=i;
            end
        end
        for wraps=0:state(lowest)
            [fin_states(wraps+1,:),choices(wraps+1,1)] = unplay_wrapping(state,start_pos,wraps);
        end
    end

% tells if a position is on 'my' side
    function is_it = my_side(state,x)
        is_it=same_side(state,1,x);
    end

% are these two positions on the same side?
    function is_it =same_side(state,x,y)
        is_it=false;
        n=length(state)/2;
        if(x>n && y>n)
            is_it=true;
        end
        if(x<=n && y<=n)
            is_it=true;
        end
    end

    function [good_answers,good_choices]=filter_results(answers,choices)
        x=1;
        [the_length,the_width]=size(answers);
        while(x<=the_length)
            the_min = min(answers(x,:));
            if(the_min<0)
                answers(x,:)=[];
                choices(x,:)=[];
                x=x-1;
                [the_length,the_width]=size(answers);
            end
            x=x+1;
        end
        x=1;
        [the_length,the_width]=size(answers);
        while(x<=the_length)
            the_min = min(answers(x,floor(the_width/2)+1:the_width));
            if(the_min>0)
                answers(x,:)=[];
                choices(x,:)=[];
                x=x-1;
                [the_length,the_width]=size(answers);
            end
            x=x+1;
        end
        good_answers=answers;
        good_choices=choices;
    end


    function amount=zero_length(state,x)
        k=0;
        while(x+k<=length(state) && state(x+k)==0)
            k=k+1;
        end
        amount=k;
    end


    function the_array=to_bin_array(x,width)
        bin=dec2bin(x);
        [bin_length,bin_width]=size(bin);
        the_array=zeros(1,width);
        for idx=1:bin_width
            the_array(idx)=bin(idx)-48;
        end
    end



%% Here is the beginning of the actual program:

bins=input('how many bins? ');
while(mod(bins,2)~=0)
   bins=input('you must enter an even number. ');
end
state=zeros(1,bins);
for i=1:bins
    fprintf('bin %i: ',i);
    state(i)=input('how many? ');
end
state

% Here is the meat:
answers=zeros(1,length(state));
choices=zeros(1,1);
original_state=state;
for j=1:length(state)
    if(state(j)==0 && same_side(state,1,j)==false)
        num_zeros=zero_length(state,j);
        for b=0:2^(num_zeros)-1
            my_array=to_bin_array(b,num_zeros);
            my_array=my_array+ones(size(my_array))*2;
            state(j:j+num_zeros-1)=my_array(1,1:num_zeros)
            [new_answers,new_choices]=unplay(state,j);
            answers=[answers;new_answers];
            choices=[choices;new_choices];
            state=original_state;
        end
        j=j+num_zeros;
    elseif(state(j)~=0)
        [new_answers,new_choices]=unplay(state,j);
        answers=[answers;new_answers];
        choices=[choices;new_choices];
        state=original_state;
    end
end

answers(1,:)=[];
choices(1,:)=[];
[answers,choices]=filter_results(answers,choices);
answers
choices
end

New Annotation

Summary:
Author:
Mode:
Body: