Check if connect 4 occurs in matrix

10 次查看(过去 30 天)
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!

采纳的回答

Stephen23
Stephen23 2019-12-16
编辑:Stephen23 2019-12-16
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 个评论
The Legend
The Legend 2019-12-16
This function does not work for diagonal line ups I just noticed..
Stephen23
Stephen23 2019-12-16
编辑:Stephen23 2019-12-16
"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.

请先登录,再进行评论。

更多回答(1 个)

Adam Danz
Adam Danz 2019-12-15
编辑:Adam Danz 2019-12-15
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 个评论
The Legend
The Legend 2019-12-16
Do you know how to output the winning team?
Adam Danz
Adam Danz 2019-12-16
" 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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by