<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>recursion | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/recursion/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Wed, 10 Mar 2021 12:07:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>recursion | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>Interesting programming exercise- no loops</title>
		<link>https://learncgames.com/interesting-programming-exercise-no-loops/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=interesting-programming-exercise-no-loops</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 12 Mar 2021 00:00:35 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=2862</guid>

					<description><![CDATA[<p>I read about a university course where the students had to write C code without using For, While or Goto for loops. Instead they had to use recursion. Yes it&#8217;s bound to be inefficient compared to the looping structure but that&#8217;s not the point. For instance for a loop like this: for (int i=0;i&#60;10;i++) { [&#8230;]</p>
The post <a href="https://learncgames.com/interesting-programming-exercise-no-loops/">Interesting programming exercise- no loops</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:107,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/thedigitalartist-202249\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1119594&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260202125618\/https:\/\/pixabay.com\/users\/thedigitalartist-202249\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 11:58:07&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-10 20:06:45&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-12 08:08:51&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-03-12 08:08:51&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:108,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1119594&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 11:58:08&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-10 20:06:45&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-12 08:08:51&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-03-12 08:08:51&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_2865" aria-describedby="caption-attachment-2865" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-2865" src="https://learncgames.com/wp-content/uploads/2021/03/fractal-1119594_640-300x168.jpg" alt="Recursive fractal" width="300" height="168" srcset="https://learncgames.com/wp-content/uploads/2021/03/fractal-1119594_640-300x168.jpg 300w, https://learncgames.com/wp-content/uploads/2021/03/fractal-1119594_640.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-2865" class="wp-caption-text">Image by <a href="https://pixabay.com/users/thedigitalartist-202249/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1119594">Pete Linforth</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1119594">Pixabay</a></figcaption></figure>
<p>I read about a university course where the students had to write C code without using For, While or Goto for loops. Instead they had to use recursion.</p>
<p>Yes it&#8217;s bound to be inefficient compared to the looping structure but that&#8217;s not the point. For instance for a loop like this:</p>
<pre><code class="language-c" data-line="">for (int i=0;i&lt;10;i++) {
   DoSomething(i);
}</code></pre>
<p>It could be done as</p>
<pre><code class="language-c" data-line="">void DoSomething(int i) {
   if (i==10) return;
  // Do whatever here
   DoSomething(i+1);
}

DoSomething(0);
</code></pre>
<p>The point isn&#8217;t to teach you to write inefficient code but to think a bit differently. I&#8217;ve used recursion quite extensively in the <a title="Link to Slay Tutorial four - adding forts and stuff" href="https://learncgames.com/tutorials/slay-tutorial-four-adding-forts-etc/" target="_blank" rel="nofollow noopener">Slay tutorials</a> code. Here&#8217;s an example. It sets a field <strong>continent</strong> in all adjacent land squares where <strong>continent</strong> == -1.</p>
<pre><code class="language-c" data-line="">void FillIn(int x, int y, int island) {
	if (onMap(x, y)) {
		if (island == map[x][y].island &amp;&amp; map[x][y].continent == -1) {
			map[x][y].continent = numContinents;
			allContinents[numContinents].count++;
			FillIn(x - 1, y, island);
			FillIn(x + 1, y, island);
			FillIn(x, y - 1, island);
			FillIn(x, y + 1, island);
			FillIn(x - 1, y - 1, island);
			FillIn(x + 1, y - 1, island);
			FillIn(x + 1, y + 1, island);
			FillIn(x - 1, y + 1, island);
		}
	}
}</code></pre>The post <a href="https://learncgames.com/interesting-programming-exercise-no-loops/">Interesting programming exercise- no loops</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2862</post-id>	</item>
		<item>
		<title>Using recursive fill to count maps</title>
		<link>https://learncgames.com/using-recursive-fill-to-count-maps/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-recursive-fill-to-count-maps</link>
					<comments>https://learncgames.com/using-recursive-fill-to-count-maps/#comments</comments>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Thu, 19 Mar 2020 00:00:36 +0000</pubDate>
				<category><![CDATA[Game]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[fill]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=166</guid>

					<description><![CDATA[<p>In the Empire game (yes, just one last mention!) during the map generation the program counts up the size of individual islands and sea areas. This is don by recursion and quite useful. Otherwise, the usual place you see recursion mentioned is in calculating factorials and Fibonacci sequences. The map struct holds not only the [&#8230;]</p>
The post <a href="https://learncgames.com/using-recursive-fill-to-count-maps/">Using recursive fill to count maps</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img decoding="async" class="size-full wp-image-167 alignleft" src="https://learncgames.com/wp-content/uploads/2020/03/terrainhexes.gif" alt="Terrain hexagons" width="224" height="204" />In the Empire game (<em>yes, just one last mention!</em>) during the map generation the program counts up the size of individual islands and sea areas. This is don by recursion and quite useful. Otherwise, the usual place you see recursion mentioned is in calculating factorials and Fibonacci sequences.</p>
<p>The map struct holds not only the terrain type and other information but a continent number. This is set initially to 0.  After the land and seas have been generated, the program picks a random land point on the map (just keep picking a random location until it has land with a continent number of 0).</p>
<p>It then counts by calling itself 8 times. It calls itself recursively at location y,x with directions (y-1,x-1) that&#8217;s NorthWest, (y-1,x)  and so on round to West (y,x-1).  But it only does it as long as each of those locations is land type with a continent number of 0. And as it does it, it sets the continent numbers in each location to 1 for the first continent and so on.</p>
<p>This recursive algorithm means that every contiguous land location gets reached. A similar thing is done with seas and lakes but you have to be careful that the stack is large enough.</p>
<pre><code class="language-c" data-line="">void FillIn(int x,int y,int locis) {
  if onMap(x,y) {
    if (locis==map[x][y].locis &amp;&amp; map[x] 
 [y].continent==UNALLOCATED) { 
      map[x][y].continent=numconts;
      allconts[numconts].count++;
      FillIn(x-1,y,locis);
      FillIn(x+1,y,locis);
      FillIn(x,y-1,locis);
      FillIn(x,y+1,locis);
      FillIn(x-1,y-1,locis);
      FillIn(x+1,y-1,locis);
      FillIn(x+1,y+1,locis);
      FillIn(x-1,y+1,locis);
    }
  }
}</code></pre>
<p>This is used for both land and sea, so locis can be either. It&#8217;s an efficient algorithm and quite quick. </p>
<p><em><strong>Those graphics? They were the initial hexagon graphics&#8230;</strong></em></p>The post <a href="https://learncgames.com/using-recursive-fill-to-count-maps/">Using recursive fill to count maps</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://learncgames.com/using-recursive-fill-to-count-maps/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">166</post-id>	</item>
	</channel>
</rss>
