Created
April 9, 2017 06:36
-
-
Save anonymous/755b85536ac37734f2bc7fbb6954d548 to your computer and use it in GitHub Desktop.
Shuffle Matches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Create match templates | |
match3H = [1 0 1]; | |
match3V = [1;0;1]; | |
match4H1 = [1 0 1 1]; | |
match4H2 = [1 1 0 1]; | |
match4V1 = [1;0;1;1]; | |
match4V2 = [1;1;0;1]; | |
match5H = [1 1 0 1 1]; | |
match5V = [1;1;0;1;1]; | |
%Create match counts | |
match3Count = 0; | |
match4Count = 0; | |
match5Count = 0; | |
%Iterate for a large number of random board arrangements | |
for boardNumber = 1:100000 | |
%Create/reset match flags | |
match3Flag = false; | |
match4Flag = false; | |
match5Flag = false; | |
%Create a random 6x6 board with 4 types of icons | |
board = randi(4,6); | |
%Check to make sure that the generated board has no current matches | |
illegal = true; | |
while(illegal == true) | |
illegal = false; %reset illegal flag | |
%Check if there are any horizontal matches | |
for i = 1:6 | |
if(illegal == true) | |
break | |
end | |
for j = 1:4 | |
%If match, generate a new board and restart | |
if(board(i,j) == board(i,j+1) && board(i,j+1) == board (i,j+2)) | |
illegal = true; | |
board = randi(4,6); | |
break | |
end | |
end | |
end | |
%Check if there are any vertical matches | |
for i = 1:4 | |
if(illegal == true) | |
break | |
end | |
for j = 1:6 | |
%If match, generate a new board and restart | |
if(board(i,j) == board(i+1,j) && board(i+1,j) == board (i+2,j)) | |
illegal = true; | |
board = randi(4,6); | |
break | |
end | |
end | |
end | |
end | |
%********You now have a legitimate starting board******* | |
% board | |
%Filter board for one type of icon | |
typeOne = board == 1; | |
%Make sure you have enough icons for the size of match | |
if(nnz(typeOne) > 4) | |
%Cross correlate one type of icon on board with pattern you're looking for: | |
%normxcorr2(match5H,typeOne). Then check to see if it resulted in a value | |
%of 1, meaning there was a pattern match: find(abs(corr-1)<0.0001). If | |
%there was a match, then the result of find would not be empty: ~isempty. | |
if(~isempty(find(abs(normxcorr2(match5H,typeOne)-1)<0.00001,1)) || ~isempty(find(abs(normxcorr2(match5V,typeOne)-1)<0.00001,1))) | |
match5Flag = true; | |
match5Count = match5Count + 1; | |
end | |
end | |
%Make sure you have enough icons for the size of match and that no larger | |
%match is possible | |
if(nnz(typeOne) > 3 && match5Flag == false) | |
if(~isempty(find(abs(normxcorr2(match4H1,typeOne)-1)<0.00001,1)) || ~isempty(find(abs(normxcorr2(match4H2,typeOne)-1)<0.00001,1)) || ~isempty(find(abs(normxcorr2(match4V1,typeOne)-1)<0.00001,1)) || ~isempty(find(abs(normxcorr2(match4V2,typeOne)-1)<0.00001,1))) | |
match4Flag = true; | |
match4Count = match4Count + 1; | |
end | |
end | |
%Make sure you have enough icons for the size of match and that no larger | |
%match is possible | |
if(nnz(typeOne) > 2 && match5Flag == false && match4Flag == false) | |
if(~isempty(find(abs(normxcorr2(match3H,typeOne)-1)<0.00001,1)) || ~isempty(find(abs(normxcorr2(match3V,typeOne)-1)<0.00001,1))) | |
match3Flag = true; | |
match3Count = match3Count + 1; | |
end | |
end | |
end | |
match3Count | |
match4Count | |
match5Count | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment