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
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 Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!