Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to assume you have your board as a <code>char[][]</code> or <code>int[][]</code> (matrix), where you might have:</p> <p><code>if (char[][]) -&gt; 'B' for black, 'R' for red</code></p> <p>or</p> <p><code>if (int[][]) -&gt; 1 for black, 0 for red</code></p> <p>In my mind it would only make sense to have a 2-d array (matrix) for this kind of problem. Anyway, the algorithm to check the winner not just <em>should</em>, but <strong>must</strong> loop over the board, as others have said. The reason for this is because it <strong>is</strong> the most elegant solution to this type of problem.</p> <p>You should basically do the following:</p> <p>A nested <em>for</em> loop: one to iterate rows, one to iterate columns.</p> <pre><code>for (int i = 0; i &lt; matrix.length; i ++) { for (int j = 0; j &lt; matrix.length; j ++) { // Check for stuff in here } } </code></pre> <p>You can check <strong>vertical</strong>, <strong>horizontal</strong> and diagonal through something like the following:</p> <p><strong>vertical (down)</strong>:</p> <pre><code>if (colorOfPieceFound) { // check j-1 (move down one row, same column); // check j-2, etc. } </code></pre> <p><strong>horizontal (left)</strong>:</p> <pre><code>if (colorOfPieceFound) { // check i-1 (move left one column, same row); // check i-2, etc. } </code></pre> <p><strong>diagonal (up-left)</strong>:</p> <pre><code>if (colorOfPieceFound) { // check [i-1][j+1] (move down one row, same column); // repeat with +/- 2 } </code></pre> <p>Basically you have 8 directions you need to check when you find a piece. You do this for each element of the matrix (i.e. <code>checkAllDirections(matrix[i][j])</code>) or starting at the place where the piece was '<em>dropped</em>'.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload