% 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