Middle part of a fence using terrain representation
<p>I'm making a fence using terrain representation where 1's is fenced land and 0's are empty spaces. Here would be a map for example:</p>

<p>0110</p>

<p>1111</p>

<p>1111</p>

<p>1101</p>

<p>How the code works is it scans the corners and the sides of 1's and counts how many meters of fence you would need, where every side of a 1 is 2.5m. The j variable is like the x value and the i is the y value.I have the working code for the sides and the corners, the only thing I need help with is how i would go about setting a for loop for the center of the fences. Here is my working code:</p>

<pre><code>package workspace;

public class Fencing {

public static void main(String[] args) {
    boolean b = true;
    double corner = 0;
    double center = 0;
    double side = 0;
    int i = 0;
    int j = 0;
    int[][] map = { 
        { 0, 1, 1, 0 }, 
        { 1, 1, 1, 1 }, 
        { 1, 1, 1, 1 }, 
        { 1, 1, 0, 1 } 
    };
    for (i = 0; i &lt; 3; i++) {
        for (j = 0; j &lt; 3; j++) {
            if (map[i][j] != 1 &amp;&amp; map[i][j] != 0) {
                b = false;
                System.out.println("--&gt; A value of " + map[i][j] + " was found at " + i + "," + j);
            }
        }
    }
    if (b == false &amp;&amp; i == 3) {
        System.out.println("The map is invalid");
        return;
    } else {
        System.out.println("Map is valid");
    }
    if (map[0][0] == 1) {
        corner += 5;
        if (map[1][0] == 1 &amp;&amp; map[0][1] == 1) {
            corner += 0;
        }
        if (map[1][0] != 1 &amp;&amp; map[0][1] != 1) {
            corner += 5;
        }
        if (map[1][0] != 1 &amp;&amp; map[0][1] == 1 || map[1][0] == 1 &amp;&amp; map[0][1] != 1) {
            corner += 2.5;
        }
    } else {
        corner += 0;
    }
    if (map[0][3] == 1) {
        corner += 5;
        if (map[1][3] == 1 &amp;&amp; map[0][2] == 1) {
            corner += 0;
        }
        if (map[1][3] != 1 &amp;&amp; map[0][2] != 1) {
            corner += 5;
        }
        if (map[1][3] != 1 &amp;&amp; map[0][2] == 1 || map[1][3] == 1 &amp;&amp; map[0][2] != 1) {
            corner += 2.5;
        }
    } else {
        corner += 0;
    }
    if (map[3][0] == 1) {
        corner += 5;
        if (map[3][1] == 1 &amp;&amp; map[2][0] == 1) {
            corner += 0;
        }
        if (map[3][1] != 1 &amp;&amp; map[02][0] != 1) {
            corner += 5;
        }
        if (map[3][1] != 1 &amp;&amp; map[2][0] == 1 || map[3][1] == 1 &amp;&amp; map[2][0] != 1) {
            corner += 2.5;
        }
    } else {
        corner += 0;
    }
    if (map[3][3] == 1) {
        corner += 5;
        if (map[3][2] == 1 &amp;&amp; map[2][3] == 1) {
            corner += 0;
        }
        if (map[3][2] != 1 &amp;&amp; map[2][3] != 1) {
            corner += 5;
        }
        if (map[3][2] != 1 &amp;&amp; map[2][3] == 1 || map[3][2] == 1 &amp;&amp; map[2][3] != 1) {
            corner += 2.5;
        }
    } else {
        corner += 0;
    }
    System.out.println("Corner fencing = " + corner);

    i = 0;
    j = 0;
    for (j = 1; j &lt; 3; j++) { // Top
        if (map[0][j] == 1) {
            side += 2.5;
            if (map[0][j + 1] == 1 &amp;&amp; map[0][j - 1] == 1) {
                side += 0;
            }
            if (map[0][j + 1] != 1 || map[0][j - 1] != 1) {
                side += 2.5;
            }
            if (map[0][j + 1] != 1 &amp;&amp; map[0][j - 1] != 1) {
                side += 5;
            }
        } else {
            side += 0;
        }
    }
    System.out.println("After the first row, fencing = " + side);

    i = 0;
    j = 0;
    for (j = 1; j &lt; 3; j++) { // Bottom
        if (map[3][j] == 1) {
            side += 2.5;
            if (map[3][j + 1] == 1 &amp;&amp; map[3][j - 1] == 1) {
                side += 0;
            }
            if (map[3][j + 1] != 1 || map[3][j - 1] != 1) {
                side += 2.5;
            }
            if (map[3][j + 1] != 1 &amp;&amp; map[3][j - 1] != 1) {
                side += 5;
            }
        } else {
            side += 0;
        }
    }
    System.out.println("After the bottom row, fencing = " + side);

    i = 0;
    j = 0;
    for (i = 1; i &lt; 3; i++) {// Right
        if (map[i][3] == 1) {
            side += 2.5;
            if (map[i + 1][3] == 1 &amp;&amp; map[i - 1][3] == 1) {
                side += 0;
            }
            if (map[i + 1][3] != 1 || map[i - 1][3] != 1) {
                side += 2.5;
            }
            if (map[i + 1][3] != 1 &amp;&amp; map[i - 1][3] != 1) {
                side += 5;
            }
        } else {
            side += 0;
        }
    }
    System.out.println("After the first column, fencing = " + side);

    i = 0;
    j = 0;
    for (i = 1; i &lt; 3; i++) {// Left
        if (map[i][0] == 1) {
            side += 2.5;
            if (map[i + 1][0] == 1 &amp;&amp; map[i - 1][0] == 1) {
                side += 0;
            }
            if (map[i + 1][0] != 1 || map[i - 1][0] != 1) {
                side += 2.5;
            }
            if (map[i + 1][0] != 1 &amp;&amp; map[i - 1][0] != 1) {
                side += 5;
            }
        } else {
            side += 0;
        }
    }
    System.out.println("After the last column, fencing = " + side);

}
}
</code></pre>

<p>I'm unsure if i would even use a for loop to scan the core, but I imagine it would be something like the side loops. Anyone have an idea on what the code would look like?</p>