Note that there are some explanatory texts on larger screens.

plurals
  1. POBreadth-first search to solve puzzle
    primarykey
    data
    text
    <p>I made a <a href="https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&amp;Itemid=8&amp;page=show_problem&amp;problem=217" rel="nofollow">flip-game</a> and I'm trying to make a method to give the player a hint about their next move. Basically, I'm trying to use a breadth-first search to solve the game and then show the player the next move. I'm having some problems with my algorithm, however. The <code>Exit()</code>s are just to help me debug. Anyway, the algorithm successfully adds the current game state, <code>isWhite</code>, to <code>gameStates</code> and checks if it is a solution. The problem comes when I try to add to the queue. My code never gets to the second <code>Exit()</code> and I can't figure out why. Any help would be appreciated. Thanks.</p> <p>EDIT: Problem fixed. Now I just need to figure out what to do with the solution once I find it...</p> <pre class="lang-cs prettyprint-override"><code> private void hint() { Queue&lt;bool[,]&gt; statesToCheck = new Queue&lt;bool[,]&gt;(); List&lt;bool[,]&gt; closedStates = new List&lt;bool[,]&gt;(); bool[,] rootState = new bool[4, 4]; rootState = (bool[,])isWhite.Clone(); statesToCheck.Enqueue((bool[,])rootState.Clone()); //closedStates.Add((bool[,])rootState.Clone()); while (statesToCheck.Count() &gt; 0) { bool solved = true; //checks for solution state rootState = statesToCheck.Dequeue(); for (int row = 0; row &lt; GAME_SIZE; row++) for (int col = 0; col &lt; GAME_SIZE; col++) { if (!rootState[row, col]) solved = false; } if (solved) { //add something to do System.Diagnostics.Debug.Write("It worked"); return; } else { closedStates.Add((bool[,])rootState.Clone()); } //simulates clicking each space and adds each one to statesToCheck for (int row = 0; row &lt; GAME_SIZE; row++) for (int col = 0; col &lt; GAME_SIZE; col++) { bool[,] stateToChange = new bool[4, 4]; stateToChange = (bool[,])rootState.Clone(); flip(stateToChange, row, col);//gets next gameState (next node in a typical breadth-first search) if (!closedStates.Contains(stateToChange)) { //closedStates.Add((bool[,])rootState.Clone()); statesToCheck.Enqueue((bool[,])stateToChange.Clone()); } } } </code></pre>
    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.
 

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