function Nim3PilesFun(N1,N2,N3) % This is a function for 3 piles Nim game. % N1, N2, N3 are original numbers of objects in each pile. % Player can take any number of objects from one pile. % The player who takes the last object lose. disp (' '); disp('************ Game Start ***********'); disp(' '); fprintf('***** You can remove any number of objects from one pile *****'); disp(' '); disp('***** The player who takes the last object win. *****'); disp('***** Enter -1 to end the game. *****'); fprintf('The original piles are [%i,%i,%i]', N1,N2,N3); disp (' '); % set current number of objects in the pile C1 = N1; C2 = N2; C3 = N3; while (C1>0 || C2>0 || C3>0 ) % player move P = input('Please enter your move (for example use [3,5,6] to indicate your move)? '); % player's move if(P == -1)%quit the game by player C1=0; C2=0; C3=0; else if (P(1)> C1 || P(2)> C2 || P(3)> C3) %enter any number more than existing objects in the pile disp('Illegal move! You can not remove more objects than what in the pile'); else C1 = P(1); C2 = P(2); C3 = P(3); if( C1==0 && C2==0 && C3==0) % player made the last move disp('You made the last move. You won!'); else % computer need to make a move if(C1==0 && C2==0) % if only one pile left, take all objects from the pile C3=0; elseif(C2==0 && C3==0) C1=0; elseif(C1==0 && C3==0) C2=0; else % if there is more than one piles left, [R1,R2,R3] = NimSum(C1,C2,C3); C1= bin2dec(R1); C2= bin2dec(R2); C3= bin2dec(R3); end %after computer move if(C1==0 && C2==0 && C3==0) disp('Computer made the last move. Computer won!'); else fprintf('The computer made a move and the remaining piles are [%i,%i,%i]', C1,C2,C3); disp(' '); end end end end end function [r1,r2,r3] = NimSum(c1,c2,c3) b1 = dec2bin(c1); b2 = dec2bin(c2); b3 = dec2bin(c3); a = fliplr(b1); b = fliplr(b2); c = fliplr(b3); La = length(a); Lb = length(b); Lc = length(c); L = max(La,max(Lb,Lc)); Lmin = min(La,min(Lb,Lc)); S = size(L); M = size(L); for i=L:-1:1 Ea = GetElement(a,La,i); Eb = GetElement(b,Lb,i); Ec = GetElement(c,Lc,i); S(i) = Ea+ Eb+Ec; if ( mod(S(i),2) ~= 0) M(i)=1; else M(i)=0; end end Msum=0; for i=L:-1:1 Msum=Msum+ M(i); end changed=0; if (Msum ~= 0) %N Position, can force to P position for l=L:-1:Lmin % start with number with largest number if(La == l && changed==0) [a,changed] = RemoveObjects(a,l,M); end if(changed==0 && Lb == l) [b,changed] = RemoveObjects(b,l,M); end if(changed==0 && Lc == l ) [c,changed] = RemoveObjects(c,l,M); end end r1 = fliplr(a); r2 = fliplr(b); r3 = fliplr(c); else % at P postion and make any move if(c1~=0 && changed ==0) if(c1 > 1) r1= dec2bin(c1-randi(c1-1)); else r1= '0'; end r2=b2; r3=b3 elseif(c2~=0 && changed ==0) if(c2>1) r2= dec2bin(c2-randi(c2-1)); else r2='0'; end r1=b1; r3=b3; elseif(c3~=0 && changed ==0) if(c3>1) r3= dec2bin(c3-randi(c3-1)); else r3='0'; end r1=b1; r2=b2; end end %nested function function e= GetElement(x,L,i) if(i<=L) e = str2double(x(i)); else e =str2double('0'); end end function [r, changed]= RemoveObjects(x,L,m) r=size(L); removed=0; changed=0; for j=L:-1:1 y= str2double(x(j)); if (m(j)==1 && y==1) x(j)= '0'; removed=1; changed=1; elseif( j