Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, start out by being <em>explicit</em> about what your function does.</p> <pre><code>/** * Checks to see if the grid point passed in is captured. * @param...(you should say what your params are here **/ public static boolean checkCapture(int x, int y) { //some code } </code></pre> <p>This is important: what if this function checks to see if the gird point is capturing some other, arbitrary point? Further, we immediately see a problem... captured <em>by who</em>? Whenever solving a recursive problem you need to understand the base case: here it is that there is no vertical or horizontal adjacent area that isn't occupied by an enemy. </p> <p>Therefore, we must check to see in regards to a particular color:</p> <pre><code>/** * Checks to see if the grid point passed in is captured. * @param...(you should say what your params are here **/ public static boolean checkCapture(int x, int y) { if (!isOccupied(x,y)) {//writing this function should be trivial return false;//can't be captured; no one is there! } Color color = getColor(x,y);//similarly, get the color of whoever is there. Status status = checkFlanked(x, y, color); if (status = Status.FLANKED) { return true; } } private static Status checkFlanked(int x, int y, Color color) { //check to see that this location is valid for the board //check to see if this square is occupied at all //if it is not, return LIBERTY (an empty space means no capture, right?) //if it is, is it occupied by the opposite color? --&gt; Return a FLANKED result! //if it is, is it occupied by the same color? --&gt; recurse! } </code></pre> <p>Now we've broken down our problem a bit! And it's easy to see how the base case is resolved: if the square is unoccupied, it can't be flanking... so it returns a <code>LIBERTY</code> result. If it's occupied by the opposite color, then this square is flanking whomever you were originally checking. The only difficult part is then checking to see whether, in the case of this being occupied by the original color, any other locations have liberty or not.</p> <pre><code> //get all valid adjacent locations //call checkFlanked on those locations. //If any return LIBERTY, return LIBERTY. Otherwise return FLANKED. </code></pre> <p>(Note: I'm assuming <code>LIBERTY</code> and <code>FLANKED</code> have been defined as an enum for clarity's sake.)</p> <p>I hope this helps you break down your problem in a more sensible way. Remember: when you're using recursion, you care about two cases: the base case, and the '+1 iteration' case. Note that even with the above you have to solve some problems:</p> <ul> <li>You need to intelligently not recurse back to squares you've already visited. (Investigate tail recursion, but you can also just pass in additional state indicating squares that are checked already.)</li> <li>You need to make sure you don't fall off the board and return an appropriate result if you do. Basically, you need to solve the 'what is a valid location?' problem.</li> </ul> <p>Some other interesting questions to ask are:</p> <ul> <li>Do you search by-breadth or by-depth? </li> <li>Is this appropriate as a static method, or should it be captured in a class?</li> </ul>
    singulars
    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. VO
      singulars
      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