Check if connect 4 occurs in matrix

10 views (last 30 days)
The Legend
The Legend on 15 Dec 2019
Edited: Stephen23 on 16 Dec 2019
Could someone help me with a code to check if there is a Connect 4 in the following matrix:
0 0 0 0 0 0 0 0 = empty
0 0 2 1 2 2 0 1 = green
1 2 2 2 1 1 2 2 = blue
2 1 1 1 2 1 1
2 1 2 1 2 2 2
1 2 1 2 1 2 1
A Connect 4 occurs when (in this case) 4 of the same color (in this case 1 and 2) line up horizontally, vertically or diagonally.
All help is greatly appreciated!
  1 Comment
The Legend
The Legend on 15 Dec 2019
I have no clue how to do this myself at the moment.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Dec 2019
Edited: Stephen23 on 16 Dec 2019
Simple, efficient, and robust using conv2:
>> M = [0,0,0,0,0,0,0;0,0,2,1,2,2,0;1,2,2,2,1,1,2;2,1,1,1,2,1,1;2,1,2,1,2,2,2;1,2,1,2,1,2,1]
M =
0 0 0 0 0 0 0
0 0 2 1 2 2 0
1 2 2 2 1 1 2
2 1 1 1 2 1 1
2 1 2 1 2 2 2
1 2 1 2 1 2 1
>> F = @(x)conv2(x,eye(4),'same')>=4 | conv2(x,flipud(eye(4)),'same')>=4 | conv2(x,ones(4,1),'same')>=4 | conv2(x,ones(1,4),'same')>=4;
>> X1 = F(M==1)
X1 =
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
>> X2 = F(M==2)
X2 =
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
"How would I go about outputting which team won aswell?"
>> C = {'no team','team one','team two','both teams'};
>> X = 1+any(X1(:))+2*any(X2(:));
>> fprintf('%s won!\n',C{X})
team two won!
  2 Comments
The Legend
The Legend on 16 Dec 2019
This function does not work for diagonal line ups I just noticed..
Stephen23
Stephen23 on 16 Dec 2019
Edited: Stephen23 on 16 Dec 2019
"This function does not work for diagonal line ups I just noticed.."
It does work for diagonals (e.g. your original example matrix, which contains only one line-up (which is diagonal)), but I forgot the anti-diagonal direction. See my updated answer for the simple change to include the anti-diagonal direction.
If you have any examples that do not work with my updated answer then please show them, otherwise I do not have the chance to fix or improve my answer.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 15 Dec 2019
Edited: Adam Danz on 15 Dec 2019
It's a little tricky to find a vector within a matrix when the dimensions of the matrix are all larger than the vector. I'm converting the values to characters and using regular expressions to do the matching but there may be a better way.
In any case, here's a demo. See inline comments for details.
% The input matrix
data = [
0 0 0 0 0 0 0
0 0 2 1 2 2 0
1 2 2 2 1 1 2
2 1 1 1 2 1 1
2 1 2 1 2 2 2
1 2 1 2 1 2 1];
% defined minimum consecutive values (ie, cx=4 equals "connect 4")
cx = 4;
% Define the value to check
checkVal = 2;
% The following checkxxx are all logicals (1/0) where a 1 means a connection is found
checkRows = any(~cellfun(@isempty,regexp(cellstr(char(data)),char(checkVal*ones(1,cx)))));
checkCols = any(~cellfun(@isempty,regexp(cellstr(char(data).'),char(checkVal*ones(1,cx)))));
dataDiags = spdiags(data); % a list of all diagonals
checkDiags = any(~cellfun(@isempty,regexp(cellstr(char(dataDiags).'),char(checkVal*ones(1,cx)))));
% output a final logical 1/0 that indicates a connection was found.
connectionFound = any([checkRows,checkCols,checkDiags]);
This could easily be put into a loop to find connections for any non-0 element.
unqVals = unique(data(data>0));
for i = 1:numel(unqVals)
checkVal = unqVals(i);
. . .
end
  4 Comments
The Legend
The Legend on 16 Dec 2019
Do you know how to output the winning team?
Adam Danz
Adam Danz on 16 Dec 2019
" how to output the winning team"
"checkVal" determines which group to test, 1 or 2. You can run it on both groups and output the group that wins. But see Stephen's updated answer since it's simpler.

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!