Check if connect 4 occurs in matrix

조회 수: 19 (최근 30일)
The Legend
The Legend 2019년 12월 15일
편집: Stephen23 2019년 12월 16일
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
The Legend
The Legend 2019년 12월 15일
I have no clue how to do this myself at the moment.

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by