image thumbnail
from Cohesion Adhesion Balls by Krishna Lalith
Player having the last move wins the game.

Cohesion_Adhesion_Balls
function Cohesion_Adhesion_Balls
%--------------------------------------------------------------------------
% About the game

disp('           GAME->Cohesion Adhesion Balls')
disp('')
disp('DESCRIPTION:')
disp('')
disp('')
disp('1. The four colors from the weakest to the strongest are Yellow, Green, Magenta, Red.')
disp('2. Balls can move either vertically or horizontally not diagonally.')
disp('3. Cohesion: If two same colour balls come adjacent then both disappear.')
disp('3. Adhesion: If two differant colour balls come adjacent then the weakest disappears(based on direction choosen).')
disp('4. Ball will rest at boundary for each move.')
disp('5. Last person to move wins the game.')
disp('')
disp('')
disp('Press any key to continue...')
pause;
%--------------------------------------------------------------------------
% defining the board

C_A_B = 1+mod(fix(10*rand(3,3)),4);
Box=ones(3,3);
Board(C_A_B,Box);
%--------------------------------------------------------------------------
%game begins

clc
disp('           Press "0" for UUU to START');
disp('                          OR');
disp('           Press "1" for CPU to START');
turn=input('');

 step=1;
while step~=0
     
%--------------------------------------------------------------------------
%user move

if mod(turn,2)==0        
    disp('                PLAYERs TURN...');
    disp('Just Select the Colour and Click On the adjacent block for Direction...');
    
    valid=0;
    while valid==0   
%         clc
        if valid==0,   Board(C_A_B,Box);    end
        [Xnew Ynew Button] = ginput(1);        
        if Button~=1  break;  end        
        if Button==1
          Xnew=floor(Xnew);
          Ynew=floor(Ynew);
          if Box(Xnew,Ynew)==1              
            if (Xnew>0 & Xnew<4 & Ynew>0 & Ynew<4)                
                hold on
                line([Xnew Xnew+1 Xnew+1 Xnew Xnew],[Ynew Ynew Ynew+1 Ynew+1 Ynew],'linewidth',3,'color','r');
                subvalid=0;flag=0;
                while subvalid==0
                    [SXnew SYnew SButton] = ginput(1);
                    if SButton~=1  break;  end
                    if SButton==1
                        SXnew=floor(SXnew);
                        SYnew=floor(SYnew);
                        if xor(abs(Xnew-SXnew)==1,abs(Ynew-SYnew)==1)
                          if (SXnew>0 & SXnew<4 & SYnew>0 & SYnew<4)
                            if (Xnew==SXnew & sum(Box(Xnew,:))<2), SButton=0;  break;  end  
                            if (Ynew==SYnew & sum(Box(:,Ynew))<2), SButton=0;  break;  end                            
                            hold on
                            line([SXnew SXnew+1 SXnew+1 SXnew SXnew],[SYnew SYnew SYnew+1 SYnew+1 SYnew],'linewidth',3,'color','c');
                            pause(1);
                            if xor(SXnew==Xnew,SYnew==Ynew)
                                [subvalid,C_A_B,Box]=Update_Move(Xnew,Ynew,SXnew,SYnew,C_A_B,Box,turn);
                                if subvalid==1, valid=1;  break;  end
                            end
                          end
                        end
                    else
                        disp('           Please check ur move...');
                        continue;         
                    end
                end
                if (SButton~=1 | valid==0),  break;  end
            end
                           
          else
               disp('           Please check ur move...');
               continue;
          end          
                   
        end
        if valid==1, break;  end
    end 
    
    if valid==1,
        Board(C_A_B,Box);
        turn=turn+1;
    end
    
%Possiility for cpu???
step=Possibility(Box);
if step==0,
   hold on
   text(1.75,2.5,'U won the Game','color','c','fontsize',30); 
   text(1.75,1.5,'Thanx 4 Playing...','color','w','fontsize',20);
   break;
end
end

pause(1);
%--------------------------------------------------------------------------
%cpu move

if mod(turn,2)==1    
    disp('                CPUs TURN...');    
    set(gcf,'pointer','fleur');
    valid=0;
   %No Artificial Inteligence
   for Xnew=1:3
     valid=0;  
     for Ynew=1:3         
         valid=0;
         if Box(Xnew,Ynew)==1                
            for SXnew=1:3
                valid=0;
                for SYnew=1:3                  
                    valid=0;
                    if xor(SXnew==Xnew,SYnew==Ynew)
                      if xor(abs(Xnew-SXnew)==1,abs(Ynew-SYnew)==1) 
                         valid=0;
                         if (Xnew==SXnew & sum(Box(Xnew,:))<2), valid=0;  break;  end  
                         if (Ynew==SYnew & sum(Box(:,Ynew))<2), valid=0;  break;  end  
                         [valid,C_A_B,Box]=Update_Move(Xnew,Ynew,SXnew,SYnew,C_A_B,Box,turn);
                         if valid==1, break;  end
                      end
                    end
                end 
                if valid==1, break;  end
            end
          end
          if valid==1, break;  end         
     end
     if valid==1, break;  end
   end    
 
   if valid==1,
       hold on
       line([Xnew Xnew+1 Xnew+1 Xnew Xnew],[Ynew Ynew Ynew+1 Ynew+1 Ynew],'linewidth',3,'color','m');
       pause(1);
       hold on
       line([SXnew SXnew+1 SXnew+1 SXnew SXnew],[SYnew SYnew SYnew+1 SYnew+1 SYnew],'linewidth',3,'color','g');
       pause(1);   
       Board(C_A_B,Box);
       turn=turn+1;   
   else
       hold on
       text(1.75,2.5,'U won the Game','color','c','fontsize',30); 
       text(1.75,1.5,'Thanx 4 Playing...','color','w','fontsize',20);
       break;
   end

%Possiility for player???
step=Possibility(Box);
if (step==0),
   hold on
   text(1.75,2.5,'I won the Game','color','c','fontsize',30); 
   text(1.75,1.5,'Thanx for Playing...','color','w','fontsize',20);
   break;
end
end
end
pause(2);
close
%--------------------------------------------------------------------------

Contact us at files@mathworks.com