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

1 Ansicht (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 16 Dez. 2019
Bearbeitet: Stephen23 am 16 Dez. 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?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by