Why is is the following function only detecting diagonal functions that that start top left and end bottom right?

조회 수: 1 (최근 30일)
Why is is the following function only detecting diagonal functions that that start top left and end bottom right ( \ ) and doesn't it detect the mirrored version ( / )
Function checking for connect N
function victory = checkVictory(M, N);
victory = 0;
checkWin = @(x)conv2(x,eye(N),'same')>=N | conv2(x,ones(N,1),'same')>=N | conv2(x,ones(1,N),'same')>=N;
teamOne = checkWin(M == 1);
teamTwo = checkWin(M == 2);
if any(teamOne(:)) == 1
victory = 1;
elseif any(teamTwo(:)) == 1
victory = 2;
end
end
Output
---------------
| |x| | | | | |
| |x| | | | |o|
| |o|o| | |o|x|
|o|x|o|x|o|x|x| % <---- Here it doesn't detect the diagonal connect N (4 in this case)
|o|o|x|o|o|o|x|
|o|x|o|x|x|o|o|
---------------
Input piece in column [player 2]: 4
---------------
| |x| | | | | |
| |x| | | | |o|
| |o|o|o| |o|x| % <---- Here it does detect the diagonal connect N (4 in this case)
|o|x|o|x|o|x|x|
|o|o|x|o|o|o|x|
|o|x|o|x|x|o|o|
---------------
Player 2 has won the game!Play again? [yes/no]
Please help me, I'm clueless. All help is greatly appreciated.
  댓글 수: 1
Stephen23
Stephen23 2019년 12월 16일
편집: Stephen23 2019년 12월 16일
"Please help me, I'm clueless."
Did you try reading my comment which I wrote an hour ago?:
"Why is is the following function only detecting diagonal functions that that start top left and end bottom right ( \ ) and doesn't it detect the mirrored version ( / )"
Not only did I explain this in my comment I also updated my answer with the very simple fix (based on flipud): is there a particular reason why you are continuing to use the old version?

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

답변 (0개)

카테고리

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