<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="243142" data-post-id="243142">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I just thought about this unfinished business.</p>
<p>I think the last remaining problem is to find the clusters of adjacent empty fields. (reminder: when you click an empty field - no mine and no adjacent mine - all adjacent empty fields are also revealed.)</p>
<p>There must be some math for that. Any ideas?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243142" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-243142" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243142"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="243146" data-post-id="243146">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="03juan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/03juan/120/23111_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  03juan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Probably a breadth-first graph algorithm leveraging the neighbours function for guiding the recursion.</p>
<p>Here’s a non-Elixir maze-finding example implementation that could maybe help? Just have to gloss over all the Rust type system discussions <a href="https://www.youtube.com/watch?v=FoNJ5zhL6bQ&amp;t=3876s" rel="noopener nofollow ugc">https://www.youtube.com/watch?v=FoNJ5zhL6bQ&amp;t=3876s</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243146" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-243146" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243146"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="243570" data-post-id="243570">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is what I came up with.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@board %{
  0 =&gt; %{0 =&gt; 1, 1 =&gt; 1, 2 =&gt; 0, 3 =&gt; 0, 4 ...},
  1 =&gt; %{0 =&gt; 9, 1 =&gt; 2, 2 =&gt; 2, 3 =&gt; 1, 4 ...},
  ...
}
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">def find_cluster(start, board),
  do: _find_cluster([start], board, [start]) |&gt; Enum.uniq()

def _find_cluster([h | t], board, found) do
  found_ =
    Minesweeper.neighbours(h)
    |&gt; Enum.filter(fn coord -&gt; coord not in found end)
    |&gt; Enum.filter(fn {x, y} -&gt; get_in(board, [y, x]) == 0 end)

  _find_cluster(t, board, found) ++ _find_cluster(found_, board, found ++ found_)
end

def _find_cluster([], _board, found), do: found
</code></pre>
<p>I changed the <code>print_board</code> function so that it takes the cluster (found are marked a <code>8</code>) and the startpoint (<code>7</code>). On the left the initial board with no clusters, empty cells are marked with <code>0</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">start = {8, 0}
cluster = find_cluster(start, @board)
assert Minesweeper.print_board(Minesweeper.numbers(@mines), @mines, [], nil) ==
             Minesweeper.print_board(Minesweeper.numbers(@mines), @mines, cluster, start)
</code></pre>
<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/f/9/f9ba451af8f031264dc281f4265a5a5d1171b8b7.png" data-download-href="https://forum.elixirforum.com/uploads/default/f9ba451af8f031264dc281f4265a5a5d1171b8b7" title="image" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/original/3X/f/9/f9ba451af8f031264dc281f4265a5a5d1171b8b7.png" alt="image" data-base62-sha1="zDbYf2ilsYGNE1Ruj5aLdvNCLKn" width="479" height="571"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">479×571 28.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><p></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243570" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-243570" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243570"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="243572" data-post-id="243572">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="princemaple" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/princemaple/120/421_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  princemaple
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>FYI When dealing with multi dimensional arrays I normally just use a flat tuple key map. It saves you from a lot of head scratching issues.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243572" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-243572" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243572"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="243573" data-post-id="243573">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>indeed, would be simpler here. Getting rid of <code>get_in</code> for example. Also I don’t have the weird swap of <code>x,y-&gt;y,x</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243573" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-243573" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243573"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="243621" data-post-id="243621">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="03juan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/03juan/120/23111_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  03juan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So clear and clean in the end. <img src="https://forum.elixirforum.com/images/emoji/apple/+1.png?v=15" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"></p>
<p>I was also going to suggest a tuple key map for the <code>found</code> accumulator, or probably more ideal a MapSet.</p>
<p>Especially when dealing with a sparse, larger board the <code>Enum.filter(fn coord -&gt; coord not in found end)</code> grows the search time non-linearly.</p>
<p>You could also drop the <code>_find_cluster(t, board, found) ++ _find_cluster(found_, board, found ++ found_)</code> in favour of just returning <code>_find_cluster(found_, board, **updated found MapSet**)</code>, and set up the recursion with <code>do: ([start] ++ _find_cluster([start], board, MapSet.new([start]))) |&gt; Enum.uniq()</code>.</p>
<p><em>I probably missed some brackets</em></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243621" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-243621" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243621"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="243623" data-post-id="243623">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="princemaple" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/princemaple/120/421_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  princemaple
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>FYI My game initialization code is here <a href="https://github.com/princemaple/mine_sweeper/blob/bbaafba72905f1fffcd4dfc02432b07bffc0e549/lib/mine_sweeper/game_server.ex#L67" class="inline-onebox" rel="noopener nofollow ugc">mine_sweeper/lib/mine_sweeper/game_server.ex at bbaafba72905f1fffcd4dfc02432b07bffc0e549 · princemaple/mine_sweeper · GitHub</a></p>
<p>which I forgot to link to in the original post.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243623" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-243623" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243623"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="243631" data-post-id="243631">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="03juan" data-post="17" data-topic="45266">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/03juan/48/23111_2.png" class="avatar"> 03juan:</div>
<blockquote>
<p>Especially when dealing with a sparse, larger board the <code>Enum.filter(fn coord -&gt; coord not in found end)</code> grows the search time non-linearly.</p>
</blockquote>
</aside>
<p>You’re right. I’ll do that as soon as I have at least 1000 paying customers playing at a time and keep it as simple as possible for now. <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group quote-modified" data-username="03juan" data-post="17" data-topic="45266">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/03juan/48/23111_2.png" class="avatar"> 03juan:</div>
<blockquote>
<p>You could also drop the <code>_find_cluster(t, board, found) ++ _find_cluster(found_, board, found ++ found_)</code>  …</p>
</blockquote>
</aside>
<p>I do not understand this part …? What should I do?</p>
<aside class="quote no-group" data-username="princemaple" data-post="18" data-topic="45266">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/princemaple/48/421_2.png" class="avatar"> princemaple:</div>
<blockquote>
<p>FYI My game initialization code is here</p>
</blockquote>
</aside>
<p>sorry for littering your thread, maybe this could be split …?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243631" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-243631" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243631"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="243635" data-post-id="243635">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="princemaple" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/princemaple/120/421_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  princemaple
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Sebb" data-post="19" data-topic="45266">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sebb/48/21818_2.png" class="avatar"> Sebb:</div>
<blockquote>
<p>sorry for littering your thread</p>
</blockquote>
</aside>
<p>Not at all. Feel free to carry on.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243635" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-243635" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243635"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="243678" data-post-id="243678">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="stevensonmt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/120/20503_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  stevensonmt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>very very cool project and nice discussion. I’ll need to read up on Phoenix PubSub.broadcast to figure out what those calls are doing for you. Thanks for sharing this!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="243678" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/minesweeper-built-with-liveview-and-other-recent-tools/45266/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-243678" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="243678"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/45266/load_more?page=3">Load more posts (1 remaining)</a>
</div></template></turbo-stream>