Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Why does it only detect diagonal connect 4s (Ns) in this direction ↖, but not in this direction ↗ ?
    5 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Why does it only detect diagonal connect 4s (Ns) in this direction ↖, but not in this direction ↗ ?
My function
function victory = checkVictory(M, N);
    victory = 0;
    checkColor = 1;
    rowCheck = any(~cellfun(@isempty,regexp(cellstr(char(M)),char(checkColor*ones(1,N)))));
    colCheck = any(~cellfun(@isempty,regexp(cellstr(char(M).'),char(checkColor*ones(1,N)))));
    diaList = spdiags(M); 
    diaCheck = any(~cellfun(@isempty,regexp(cellstr(char(diaList).'),char(checkColor*ones(1,N)))));
    connectionFound = any([rowCheck,colCheck,diaCheck]);
    if connectionFound == true;
        victory = 1;
    else
        checkColor = 2;
        rowCheck = any(~cellfun(@isempty,regexp(cellstr(char(M)),char(checkColor*ones(1,N)))));
        colCheck = any(~cellfun(@isempty,regexp(cellstr(char(M).'),char(checkColor*ones(1,N)))));
        diaList = spdiags(M); 
        diaCheck = any(~cellfun(@isempty,regexp(cellstr(char(diaList).'),char(checkColor*ones(1,N)))));
        connectionFound = any([rowCheck,colCheck,diaCheck]);
        if connectionFound == true;
            victory = 2;
        end
    end
end
Output
Input piece in column [player 1]: 3
---------------
| | | | | | | |            % x = player 1
| | | | | | |x|            % o = player 2
| | | | | |x|o|
| | |x|o|x|x|x|         % <--- Here you can see that it doesn't the detect the diagonal connect 4 of 'x'
| |x|o|x|o|o|o|
|o|x|x|x|o|o|o|
---------------
Input piece in column [player 2]: 3
---------------
| | | | | | | |
| | | | | | |x|
| | |o| | |x|o|         % <--- Here you can see that it DOES the detect the diagonal connect 4 of 'o'
| | |x|o|x|x|x|
| |x|o|x|o|o|o|    
|o|x|x|x|o|o|o|
---------------
Player 2 has won the game!Play again? [yes/no]
0 Kommentare
Antworten (1)
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
