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

1 view (last 30 days)
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 Comment
Stephen23
Stephen23 on 16 Dec 2019
Edited: Stephen23 on 16 Dec 2019
"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?

Sign in to comment.

Answers (0)

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!