<?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>Learn C++ &#187; Game Programming</title>
	<atom:link href="http://www.learncpp.com/category/computer-game-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com</link>
	<description></description>
	<lastBuildDate>Thu, 01 Dec 2011 07:21:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>5.9 &#8212; Random number generation</title>
		<link>http://www.learncpp.com/cpp-tutorial/59-random-number-generation/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/59-random-number-generation/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 22:43:10 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[prng]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rand]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[rng]]></category>
		<category><![CDATA[srand]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=218</guid>
		<description><![CDATA[<p>The ability to generate random numbers can be useful in certain kinds of programs, particularly in games, statistics modeling programs, and scientific simulations that need to model random events. Take games for example &#8212; Without random events, monsters would always attack you the same way, you&#8217;d always find the same treasure, the dungeon layout <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/cpp-tutorial/59-random-number-generation/">5.9 &#8212; Random number generation</a></span>]]></description>
			<content:encoded><![CDATA[<p>The ability to generate random numbers can be useful in certain kinds of programs, particularly in games, statistics modeling programs, and scientific simulations that need to model random events.  Take games for example &#8212; Without random events, monsters would always attack you the same way, you&#8217;d always find the same treasure, the dungeon layout would never change, etc&#8230; and that would not make for a very good game.</p>
<p>So how do we generate random numbers?  In real life, we often generate random results by doing things like flipping a coin, rolling a dice, or shuffling a deck of cards.  These events involve so many physical variables (eg. gravity, friction, air resistance, momentum, etc&#8230;) that they become almost impossible to predict or control, and produce results that are for all intents and purposes random.</p>
<p>However, computers aren&#8217;t designed to take advantage of physical variables &#8212; your computer can&#8217;t toss a coin, throw a dice, or shuffle real cards.  Computers live in a very controlled electrical world where everything is binary (false or true) and there is no in-between.  By their very nature, computers are designed to produce results that are as predictable as possible.  When you tell the computer to calculate 2 + 2, you ALWAYS want the answer to be 4.  Not 3 or 5 on occasion.</p>
<p>Consequently, computers are generally incapable of generating random numbers.  Instead, they must simulate randomness, which is most often done using pseudo-random number generators.</p>
<p>A <strong>pseudo-random number generator (PRNG)</strong> is a program that takes a starting number (called a <strong>seed</strong>), and performs mathematical operations on it to transform it into some other number that appears to be unrelated to the seed.  It then takes that generated number and performs the same mathematical operation on it to transform it into a new number that appears unrelated to the number it was generated from.  By continually applying the algorithm to the last generated number, it can generate a series of new numbers that will appear to be random if the algorithm is complex enough.</p>
<p>It&#8217;s actually fairly easy to write a PRNG.  Here&#8217;s a short program that generates 100 pseudo-random numbers:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;stdafx.h&gt;
#include &lt;iostream&gt;
using namespace std;

unsigned int PRNG()
{
    // our initial starting seed is 5323
    static unsigned int nSeed = 5323;

    // Take the current seed and generate a new value from it
    // Due to our use of large constants and overflow, it would be
    // very hard for someone to predict what the next number is
    // going to be from the previous one.
    nSeed = (8253729 * nSeed + 2396403);

    // Take the seed and return a value between 0 and 32767
    return nSeed  % 32767;
}

int main()
{
    // Print 100 random numbers
    for (int nCount=0; nCount &lt; 100; ++nCount)
    {
        cout &lt;&lt; PRNG() &lt;&lt; &quot;\t&quot;;

        // If we've printed 5 numbers, start a new column
        if ((nCount+1) % 5 == 0)
            cout &lt;&lt; endl;
	}
}
</pre>
<p>The result of this program is:</p>
<pre>
20433	22044	9937	30185	29341
14783	29730	8430	3076	28768
18053	16066	26537	100	30493
4943	19511	19251	6669	32117
31575	3373	32383	30496	12710
23999	11929	5425	9938	12107
28541	1938	3450	20283	16726
6440	4938	26094	24391	12248
24803	30416	16244	19590	6644
24646	4873	2841	23831	23476
17958	8827	17400	32129	32760
25744	25405	13591	8859	15932
19086	19666	19265	14179	1165
27168	20996	29427	5857	3434
18964	11980	564	4620	400
17362	16934	11889	419	9714
19808	29699	3694	25612	5512
20256	10009	10247	1860	1846
1487	14030	2615	16035	8107
28736	267	29395	9438	20294
</pre>
<p>Each number appears to be pretty random with respect to the previous one.  As it turns out, our algorithm actually isn&#8217;t very good, for reasons we will discuss later.  But it does effectively illustrate the principle of PRNG number generation.</p>
<p><strong>Generating random numbers in C++</strong></p>
<p>C (and by extension C++) comes with a built-in pseudo-random number generator.  It is implemented as two separate functions that live in the cstdlib header:</p>
<p><strong>srand()</strong> sets the initial seed value.  srand() should only be called once.</p>
<p><strong>rand()</strong> generates the next random number in the sequence (starting from the seed set by srand()).</p>
<p>Here&#8217;s a sample program using these functions:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;stdafx.h&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt; // for rand() and srand()
using namespace std;

int main()
{
    srand(5323); // set initial seed value to 5323

    // Print 100 random numbers
    for (int nCount=0; nCount &lt; 100; ++nCount)
    {
        cout &lt;&lt; rand() &lt;&lt; &quot;\t&quot;;

        // If we've printed 5 numbers, start a new column
        if ((nCount+1) % 5 == 0)
            cout &lt;&lt; endl;
	}
}
</pre>
<p>Here&#8217;s the output of this program:</p>
<pre>
17421	8558	19487	1344	26934
7796	28102	15201	17869	6911
4981	417	12650	28759	20778
31890	23714	29127	15819	29971
1069	25403	24427	9087	24392
15886	11466	15140	19801	14365
18458	18935	1746	16672	22281
16517	21847	27194	7163	13869
5923	27598	13463	15757	4520
15765	8582	23866	22389	29933
31607	180	17757	23924	31079
30105	23254	32726	11295	18712
29087	2787	4862	6569	6310
21221	28152	12539	5672	23344
28895	31278	21786	7674	15329
10307	16840	1645	15699	8401
22972	20731	24749	32505	29409
17906	11989	17051	32232	592
17312	32714	18411	17112	15510
8830	32592	25957	1269	6793
</pre>
<p><strong>The range of rand()</strong></p>
<p>rand() generates pseudo-random integers between 0 and RAND_MAX, a constant in cstdlib that is typically set to 32767.</p>
<p>Generally, we do not want random numbers between 0 and RAND_MAX &#8212; we want numbers between two other values, which we&#8217;ll call nLow and nHigh.  For example, if we&#8217;re trying to simulate the user rolling a dice, we want random numbers between 1 and 6.</p>
<p>It turns out it&#8217;s quite easy to take the result of rand() can convert it into whatever range we want:</p>
<pre class="brush: cpp; title: ; notranslate">
// Generate a random number between nLow and nHigh (inclusive)
unsigned int GetRandomNumber(int nLow, int nHigh)
{
    return (rand() % (nHigh - nLow + 1)) + nLow;
}
</pre>
<p><strong>PRNG sequences</strong></p>
<p>If you run the rand() sample program above multiple times, you will note that it prints the same result every time!  This means that while each number in the sequence is seemingly random with regards to the previous ones, the entire sequence is not random at all!  And that means our program ends up totally predictable (the same inputs lead to the same outputs every time).  There are cases where this can be useful or even desired (eg. you want a scientific simulation to be repeatable, or you&#8217;re trying to debug why your random dungeon generator crashes).</p>
<p>But often, this is not what is desired.  If you&#8217;re writing a game of hi-lo (where the user has 10 tries to guess a number, and the computer tells them whether their guess is too high or to low), you don&#8217;t want the program picking the same numbers each time.  So let&#8217;s take a deeper look at why this is happening, and how we can fix it.</p>
<p>Remember that each number in a PRNG sequence is generated from the previous number, in a deterministic way.  Thus, given any starting seed number, PRNGs will always generate the same sequence of numbers from that seed as a result!  We are getting the same sequence because our starting seed number is always 5323.</p>
<p>In order to make our entire sequence randomized, we need some way to pick a seed that&#8217;s not a fixed number.  The first answer that probably comes to mind is that we need a random number!  That&#8217;s a good thought, but if we need a random number to generate random numbers, then we&#8217;re in a catch-22.  It turns out, we really don&#8217;t need our seed to be a random number &#8212; we just need to pick something that changes each time the program is run.  Then we can use our PRNG to generate a unique sequence of pseudo-random numbers from that seed.</p>
<p>The commonly accepted method for doing this is to enlist the system clock.  Each time the user runs the program, the time will be different.  If we use this time value as our seed, then our program will generate a different sequence of numbers each time it is run!</p>
<p>C comes with a function called time() that returns the number of seconds since midnight on Jan 1, 1970.  To use it, we merely need to include the ctime header, and then initialize srand() with a call to time(0):</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;stdafx.h&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt; // for rand() and srand()
#include &lt;ctime&gt; // for time()
using namespace std;

int main()
{

    srand(time(0)); // set initial seed value to system clock
    for (int nCount=0; nCount &lt; 100; ++nCount)
    {
        cout &lt;&lt; rand() &lt;&lt; &quot;\t&quot;;

        if ((nCount+1) % 5 == 0)
            cout &lt;&lt; endl;
	}
}
</pre>
<p>Now our program will generate a different sequence of random numbers every time!</p>
<p><strong>What is a good PRNG?</strong></p>
<p>As I mentioned above, the PRNG we wrote isn&#8217;t a very good one.  This section will discuss the reasons why.  It is optional reading because it&#8217;s not strictly related to C or C++, but if you like programming you will probably find it interesting anyway.</p>
<p>In order to be a good PRNG, the PRNG needs to exhibit a number of properties:</p>
<p>First, the PRNG should generate each number with approximately the same probability.  This is called distribution uniformity.  If some numbers are generated more often than others, the result of the program that uses the PRNG will be biased!</p>
<p>For example, let&#8217;s say you&#8217;re trying to write a random item generator for a game.  You&#8217;ll pick a random number between 1 and 10, and if the result is a 10, the monster will drop a powerful item instead of a common one.  You would expect a 1 in 10 chance of this happening.  But if the underlying PRNG is not uniform, and generates a lot more 10s than it should, your players will end up getting more rare items than you&#8217;d intended, possibly trivializing the difficulty of your game.</p>
<p>Generating PRNGs that produce uniform results is difficult, and it&#8217;s one of the main reasons the PRNG we wrote at the top of this lesson isn&#8217;t a very good PRNG.</p>
<p>Second, the method by which the next number in the sequence shouldn&#8217;t be obvious or predictable.  For example, consider the following PRNG algorithm: <code>nNum = nNum + 1</code>.  This PRNG is perfectly uniform, but it&#8217;s not very useful as a sequence of random numbers!</p>
<p>Third, the PRNG should have a good dimensional distribution of numbers.  This means it should return low numbers, middle numbers, and high numbers seemingly at random.  A PRNG that returned all low numbers, then all high numbers may be uniform and non-predictable, but it&#8217;s still going to lead to biased results, particularly if the number of random numbers you actually use is small.</p>
<p>Fourth, all PRNGs are periodic, which means that at some point the sequence of numbers generated will eventually begin to repeat itself.  As mentioned before, PRNGs are deterministic, and given an input number, a PRNG will produce the same output number every time.  Consider what happens when a PRNG generates a number it has previously generated.  From that point forward, it will begin to duplicate the sequence between the first occurrence of that number and the next occurrence of that number over and over.  The length of this sequence is known as the <strong>period</strong></p>
<p>For example, here are the first 100 numbers generated from a PRNG with poor periodicity:</p>
<pre>
112	9	130	97	64
31	152	119	86	53
20	141	108	75	42
9	130	97	64	31
152	119	86	53	20
141	108	75	42	9
130	97	64	31	152
119	86	53	20	141
108	75	42	9	130
97	64	31	152	119
86	53	20	141	108
75	42	9	130	97
64	31	152	119	86
53	20	141	108	75
42	9	130	97	64
31	152	119	86	53
20	141	108	75	42
9	130	97	64	31
152	119	86	53	20
141	108	75	42	9
</pre>
<p>You will note that it generated 9 as the second number, and 9 again as the 16th number.  The PRNG gets stuck generating the sequence in-between these two 9&#8242;s repeatedly:  9-130-97-64-31-152-119-86-53-20-141-108-75-42-(repeat).</p>
<p>A good PRNG should have a long period for all seed numbers.  Designing an algorithm that meets this property can be extremely difficult &#8212; most PRNGs will have long periods for some seeds and short periods for others.  If the user happens to pick that seed, then the PRNG won&#8217;t be doing a good job.</p>
<p>Despite the difficulty in designing algorithms that meet all of these criteria, a lot of research has been done in this area because of it&#8217;s importance to scientific computing.</p>
<p><strong>rand() is a mediocre PRNG</strong></p>
<p>The algorithm used to implement rand() can vary from compiler to compiler, leading to results that may not be consistent across compilers.  Most implementations of rand() use a method called a <a href="http://en.wikipedia.org/wiki/Linear_congruential_generator">Linear Congruential Generator (LCG)</a>.  If you have a look at the first example in this lesson, you&#8217;ll note that it&#8217;s actually a LCG, though one with intentionally poorly picked bad constants.  LCGs tend to have shortcomings that make them not good choices for certain kinds of problems.</p>
<p>One of the main shortcomings of rand() is that RAND_MAX is usually set to 32767 (essentially 16-bits).  This means if you want to generate numbers over a larger range (eg. 32-bit integers), the algorithm is not suitable.  Also, rand() isn&#8217;t good if you want to generate random floating point numbers (eg. between 0.0 and 1.0), which is often useful when doing statistical modelling.  Finally, rand() tends to have a relatively short period compared to other algorithms.</p>
<p>That said, rand() is entirely suitable for learning how to program, and for programs in which a high-quality PRNG is not a necessity.  For such applications, I would highly recommend <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">Mersenne Twister</a>, which produces great results and is relatively easy to use.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/61-arrays-part-i/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> 6.1 &#8212; Arrays (Part I)</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter5" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/58-break-and-continue/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 5.8 &#8212; Break and Continue</a>
</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/59-random-number-generation/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Roguelike dungeon generation programming contest!</title>
		<link>http://www.learncpp.com/computer-game-programming/roguelike-dungeon-generation-programming-contest/</link>
		<comments>http://www.learncpp.com/computer-game-programming/roguelike-dungeon-generation-programming-contest/#comments</comments>
		<pubDate>Thu, 05 Jul 2007 00:32:46 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Game Programming]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/computer-game-programming/roguelike-dungeon-generation-programming-contest/</guid>
		<description><![CDATA[<p>To inaugurate the opening of the new LearnCpp.com Forums, I am announcing a programming contest!</p> <p>The object of this contest is to write a program that generates a random ASCII dungeon, and output it in HTML format. You can use any language you want, and be as creative or uncreative as you like. This <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/computer-game-programming/roguelike-dungeon-generation-programming-contest/">Roguelike dungeon generation programming contest!</a></span>]]></description>
			<content:encoded><![CDATA[<p>To inaugurate the opening of the new <a href="http://www.learncpp.com/forums/">LearnCpp.com Forums</a>, I am announcing a programming contest!</p>
<p>The object of this contest is to write a program that generates a random ASCII dungeon, and output it in HTML format.  You can use any language you want, and be as creative or uncreative as you like.  This isn&#8217;t meant to be a difficult assignment &#8212; in fact, it&#8217;s possible to generate and output a random dungeon in less than a hundred lines of code!  What it is meant to be is a fun way to stretch your brain, flex your programming skills, try out a new language you&#8217;ve always been wanting to learn, or try a new algorithm that&#8217;s been floating around in the back of your head.  Devise your own challenge: see if you can write your generator in under 100 lines of code, or in under 50.  Or if you&#8217;re a new programmer, see if you can do it at all!</p>
<p>Details can be found <a href="http://www.learncpp.com/forums/index.php?topic=4.msg10#new">here</a>.  Everyone who enters will receive a &#8220;dungeoneer&#8221; forum title, and a .gif medal to commemorate their entry.</p>
<p>The contest will run until midnight on August 5th.  Hope to see your entry there!</p>
<p><p><hr>
Want to get your <a
 href="http://www.earnmydegree.com/online-education/technology/programming.html">computer
 programming degree</a> to design your own <a
 href="http://psych.hanover.edu/handbook/bachpsy2.html">programs</a>?  Check
 out some <a href="http://www.earnmydegree.com/">online universities</a>
 to see who offers the online <a
 href="http://distancelearn.about.com/">classes</a> you'd like to take.  You can get your <a
 href="http://www.earnmydegree.com/online-education/bachelor/">online bachelor
 degree</a> in no time!
<br><hr></p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/computer-game-programming/roguelike-dungeon-generation-programming-contest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Three questions that need to be answered before you start writing your game</title>
		<link>http://www.learncpp.com/computer-game-programming/three-questions-that-need-to-be-answered-before-you-start-writing-your-game/</link>
		<comments>http://www.learncpp.com/computer-game-programming/three-questions-that-need-to-be-answered-before-you-start-writing-your-game/#comments</comments>
		<pubDate>Wed, 13 Jun 2007 18:50:19 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Game Programming]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/computer-game-programming/three-questions-that-need-to-be-answered-before-you-start-writing-your-game/</guid>
		<description><![CDATA[<p>When deciding to write a new game, there are 3 primary decisions that will drive forward your entire development process:</p> <p>1) Figure out what you want to do.</p> <p>First, you have to come up with a good game idea. This can be really difficult, or it can be really easy, depending on how creative <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/computer-game-programming/three-questions-that-need-to-be-answered-before-you-start-writing-your-game/">Three questions that need to be answered before you start writing your game</a></span>]]></description>
			<content:encoded><![CDATA[<p>When deciding to write a new game, there are 3 primary decisions that will drive forward your entire development process:</p>
<p><strong>1) Figure out what you want to do.</strong></p>
<p>First, you have to come up with a good game idea.  This can be really difficult, or it can be really easy, depending on how creative you are and whether inspiration hits.  An typical answer is, &#8220;I want to write a game like &lt;Halo/Starcraft/insert favorite game here&gt;&#8221;.  Unfortunately, this is not a good answer.  That game already exists, and the people who are interested in that type of game are already playing it.  You&#8217;re probably not going to do a better job than a company that can throw millions of dollars at development.  Even if your only competition is other hobbyist programmers, does the world really need another game where you can click on blocks and make them disappear if there are at least 3 of the same color touching?  (unless you&#8217;re making a <a href="http://en.wikipedia.org/wiki/Tetris_Attack">Tetris Attack</a> clone, then the answer is yes).  In order to attract attention, your game needs to offer something different &#8212; something novel.  What are you going to bring to the table that hasn&#8217;t been seen/done before?  People are resistant to change, so unless you can offer them something that makes them want to spend the time to figure out your game, your game will go sadly (but not unexpectedly) unappreciated.</p>
<p>Second, you have to decide upon the scope of your project.  It&#8217;s extremely common to think big and shoot for the moon.  However, in programming, this is often exactly the wrong idea, because odds are that six months into development you will get bored and frustrated with your lack of progress and decide to call it quits.  Shoot for something expremely simple and modular, and then add the complexity in layers.  Your goal should be to get something that is minimally playable within a couple of weeks time, even if it doesn&#8217;t have most of the features you want, and even if it&#8217;s totally unbalanced.</p>
<p><strong>2) Choose your language and platform.</strong></p>
<p>Is C++ really the right language for your project?  If you&#8217;re interested in developing a lightweight game that can be net accessible, perhaps flash or Java would be more appropriate choices.  C++ is good for games that will be downloaded to the user&#8217;s machine, games that need to be fast, and games that need to do complex things, like render 3d scenes.</p>
<p>Assuming that C++ is indeed the right language for you, what platform are you targeting?  Is your game going to be Windows only?  Linux only?  Mac only?  If you&#8217;re developing for a single platform, you make your development job easier because you can use platform specific tools and libraries, and don&#8217;t have to worry about cross-platform issues.  However, you&#8217;re also restricting yourself to a limited audience.  Many Windows programmers dismiss Linux and Mac users because they aren&#8217;t as numerous &#8212; however, users on these platforms also have a smaller selection of software to choose from, which means your game ported to those platforms may get more attention there.  If you&#8217;re writing a roguelike game, will you support old console-only workstations that don&#8217;t have GUIs?  Fortunately, many popular libraries these days are cross-platform, making your development job easier if you write the rest of your code so that it is cross-platform. </p>
<p><strong>3) What libraries are you going to use?</strong></p>
<p>The core C++ language was intentionally kept simplistic and minimal &#8212; consequently, it does not come with the tools required to create most games.  In order to do things like handle graphics, fonts, mouse actions, sounds, music, and other things that modern games need, you will have to use add-on libraries that have been developed for these purposes.</p>
<p>If your game is going to be a 3d graphics heavy game, the most popular choices are either <a href="http://en.wikipedia.org/wiki/Opengl/">OpenGL</a>, which is great for cross-platform use, or <a href="http://en.wikipedia.org/wiki/DirectX">DirectX</a>, which is Windows specific.  Or perhaps a layer on top of one of those, such as <a href="http://en.wikipedia.org/wiki/OGRE_3D">Ogre 3D</a>.</p>
<p>If you want to do a 2d game, <a href="http://en.wikipedia.org/wiki/Simple_DirectMedia_Layer">SDL</a> or <a href="http://en.wikipedia.org/wiki/Allegro_library">Allegro</a> will both fit the bill.  SDL can also utilized in conjunction with OpenGL, so you can mix 2d and 3d.  (Allegro might be able to as well, but I am not that familiar with Allegro).</p>
<p>If you&#8217;re interested in writing a console roguelike game, <a href="http://en.wikipedia.org/wiki/Ncurses">Ncurses</a> or <a href="http://pdcurses.sourceforge.net/">PDCurses</a> will allow you to do ASCII graphic games.  Many modern roguelikes emulate a console using SDL or Allegro &#8212; this gives you advantages not available on console machines, such as being able to access more colors, use different fonts, and swap in graphical tiles.</p>
<p>It&#8217;s worth noting that it may be possible use multiple libraries.  For example, if you write your code modularly enough, you could have it use Ncurses on old unix consoles, PDCurses on dos consoles, and an emulated console via SDL on Windows and X-Windows.  In this way, you can hit a huge range of machines with different capabilities.</p>
<p><strong>Now what?</strong></p>
<p>Once you have your idea, your platform target, and your target libraries, the easy part is done. ;)  From here, all you have to do is design your game, and learn how to use the tools at your disposal to implement it!</p>
<p>It is worth your while to do two things:</p>
<p>1) Design your game on paper as much as possible before trying to implement it.  The idea is that you want to minimize changes as much as possible while coding &#8212; so the more you can solidify before you start coding, the better.  The farther you are into the development process when a change is made, the larger ramifications that change will have, and the longer it will take to implement.  Make your major decisions as early as possible and try not to change them unless necessary. </p>
<p>2) When implementing a new technology for the first time, it&#8217;s often a good idea to test run your idea in a separate program (called a prototype).  The object of this prototype is to get something working fast, even if the coding is poor, and allow you to play with it without having to worry about messing up your game.  Once you are comfortable with how the feature works, you can then implement it correctly/modularly in your game.  Because you&#8217;ll get many learning curve mistakes out of the way when developing the prototype, you&#8217;ll have a better idea how to implement it successfully in your game without breaking anything else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/computer-game-programming/three-questions-that-need-to-be-answered-before-you-start-writing-your-game/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Introduction to Roguelike gaming</title>
		<link>http://www.learncpp.com/computer-game-programming/introduction-to-roguelike-gaming/</link>
		<comments>http://www.learncpp.com/computer-game-programming/introduction-to-roguelike-gaming/#comments</comments>
		<pubDate>Tue, 05 Jun 2007 18:41:39 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Game Programming]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=38</guid>
		<description><![CDATA[<p>I play many different kinds of video games, but Roguelike games are some of my favorites. Before I go into the why, let me introduce Roguelike games to those of you who are not familiar with them.</p> <p>The Roguelike game genre was created all the way back in 1980 when a game named Rogue <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.learncpp.com/computer-game-programming/introduction-to-roguelike-gaming/">Introduction to Roguelike gaming</a></span>]]></description>
			<content:encoded><![CDATA[<p>I play many different kinds of video games, but Roguelike games are some of my favorites.  Before I go into the why, let me introduce Roguelike games to those of you who are not familiar with them.</p>
<p>The Roguelike game genre was created all the way back in 1980 when a game named <a href="http://en.wikipedia.org/wiki/Rogue_%28computer_game%29">Rogue</a> was released.  In Rogue, your character walks around an two-dimensional ascii dungeon, fighting fantasy monsters (represented by ascii letters), finding items (represented by punctuation), and trying to survive.  Later, quite a few other now-well-known games were released &#8212; including the &#8220;Big 4&#8243;: <a href="http://en.wikipedia.org/wiki/Angband_%28computer_game%29">Angband</a>, <a href="http://en.wikipedia.org/wiki/NetHack">Nethack</a>, <a href="http://en.wikipedia.org/wiki/Ancient_Domains_of_Mystery">ADOM</a>, and <a href="http://en.wikipedia.org/wiki/Dungeon_Crawl">Crawl</a>.  Nethack, released in 1987, is probably the most well known of all the traditional Roguelike games, and it&#8217;s popularity has made it a part of computer culture and lore.</p>
<p>Even though many of you have never played a traditional Roguelike, it&#8217;s very possible you&#8217;ve played a non-traditional one.  <a href="http://en.wikipedia.org/wiki/Diablo_%28computer_game%29">Diablo</a> and <a href="http://en.wikipedia.org/wiki/Diablo_II">Diablo II</a>, the famous games by Blizzard, shares many features with typical Roguelike games, only with fancy graphics, a refined user interface, and in real-time.</p>
<p>There are several things that make Roguelike games really cool &#8212; first, unlike most traditional games, they are HIGHLY randomized.  Almost everything is different every time you play: the dungeons, the creatures you meet, the items you find.  This gives Roguelike games an immense amount of replay-ability, which is why they are still popular even after all these years.  Second, they are HARD, requiring the player to utilize any resources at hand in an attempt to survive.  And when your character dies, he or she dies forever.  Beating one of these games is a real challenge, and doing so is a real accomplishment.  Third, and most relevant to this blog and website, these games are often open-source.  This not only means they&#8217;re free (which is hard to beat), it also means we can take a look at their source code (many are written in C or C++).  While most of them have not been coded using modern coding methodologies (and hence are not suitable candidates for learning how to program), what they do offer is the chance to take a look at and discuss some very interesting algorithms in a simplified context.</p>
<p>Many young programmers decide to learn C++ because they ultimately want to write games (I did at one time).  Roguelike games are an ideal place to start, because they allow the programmer to concentrate on the structure of the code instead of having to spend all his or her time creating art assets and making things look pretty.  The innate simplicity of the user interface of Roguelike games (essentially being just ascii text) makes them an excellent testbed for learning and implementing new ideas and algorithms that can then be generalized to fully graphical games, such as Massively Multiplayer Online Role Playing Games.  And they run on almost any computer, even old monochrome HP workstations.</p>
<p>Despite the apparent simplicity of writing a roguelike game, many new programmers find themselves in over their heads once they get into the details of writing a roguelike game.  Roguelike games can be deceptively complex once you look under their hoods, requiring an intersection of artful decision making, algorithmical knowledge, good programming habits, and persistance.  And that is precisely what makes them so interesting. :)</p>
<p>How do you write a random dungeon generator?  Or a random item generator?  How do you do efficient field of view calculations, where a player can only see what is lit by his light source, and walls or other terrain obstacles block line of sight?</p>
<p>There are all questions that I would like to eventually explore on this blog.  I think it would be interesting to do a tutorial series on the subject, to show how a Roguelike game can be built from the ground up in C++.  In the meantime, while I am still working on the C++ Tutorial, I will occasionally discuss various general topics in Roguelike gaming on this blog.</p>
<p>Feel free to check out some of the major roguelike games in the genre and get a feel for them.  Just make sure you leave yourself enough time to get addicted.  Because they&#8217;re an awful lot of fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/computer-game-programming/introduction-to-roguelike-gaming/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

