<?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>Web Cash &#187; Game Design</title>
	<atom:link href="http://www.earn-web-cash.com/category/game-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Combinations: General Function to Get All Possible Combinations</title>
		<link>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/</link>
		<comments>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 00:15:54 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[combinations]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/</guid>
		<description><![CDATA[Previously, we looked at how to calculate a binomial coefficient (to find the total number of possible combinations) and an example of how to generate a list of combinations with hard-coded loops. A function would be much more useful to us if it was flexible &#8211; if it could generate a list of combinations for [...]]]></description>
			<content:encoded><![CDATA[<p>Previously, we looked at <a href="http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/">how to calculate a binomial coefficient</a> (to find the total number of possible combinations) and an example of <a href="http://www.earn-web-cash.com/2008/02/26/get-all-combinations/">how to generate a list of combinations with hard-coded loops</a>.</p>
<p>A function would be much more useful to us if it was flexible &#8211; if it could generate a list of combinations for any size set of numbers.  We can&#8217;t do this with the hard coded loop solution, so we need to come up with a better method.<br />
<span id="more-163"></span></p>
<h4>The Function Concept</h4>
<p>Logically, the best way to do this is to create a function that takes a set of numbers (one combination), the size of the set being chosen from (x in [xCy]), and the size of the set being chosen (y in [xCy]).  The function would then use a sequential pattern to calculate the next possible combination and return it.</p>
<p>With this type of function, we could create a while loop that iterates through the whole set of possible combinations.  We initialize it with the first possible combination, continue to execute while the function returns a combination, and finish when the function returns 0 &#8211; in which case there are no more combinations.</p>
<p>If we&#8217;re finding the possible combinations for (11 Choose 4), the main loop would look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$choose</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Process the combo here</span>
  <span style="color: #666666; font-style: italic;">//  Store it in a file, an array, or something</span>
  <span style="color: #666666; font-style: italic;">//  Or just out put it</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This initializes an empty array &#8211; $combo.  Our function takes the array and populates it with the next possible combination.</p>
<p>If the array is empty, it&#8217;ll create the first combination &#8211; {1, 2, 3, 4}.  Otherwise, it will increment that set by one to get the next possible combination set &#8211; {1, 2, 3, 5}.  When the function receives a combination set it can&#8217;t possibly increment &#8211; {8, 9, 10, 11} &#8211; it returns false and the loop ends.</p>
<h4>Ok&#8230; How Do We Build the Function?</h4>
<p>Let&#8217;s start by creating the function, checking for an empty array, and returning the initial set.  Then we can dive into how we go about incrementing one combination set to get the next one.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Return the first set, i.e. {1, 2, 3, 4}</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$choose</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>    
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Here we'll calculate the next combination</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  If we didn't return before, no combination was found</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That should be pretty self-explanatory.  Now we can look at our pattern and develop a method of calculating the next possible combination.</p>
<h4>The Pattern</h4>
<p>In the last article on <a href="http://www.earn-web-cash.com/2008/02/26/get-all-combinations/">getting combinations</a>, we identified the basic pattern to follow.</p>
<ul>
<li>Find the &#8220;deepest&#8221; number that can be incremented</li>
<li>Increment it by one</li>
<li>Set any &#8220;deeper&#8221; numbers to one plus the previous number</li>
</ul>
<p>The &#8220;depeest&#8221; number would be the last digit.  In the set {1, 2, 3, 4}, the fourth position would be considered deepest.  Here&#8217;s the first step &#8211; and the most basic increment to do.</p>
<pre>1 2 3 4
1 2 3 5</pre>
<p>The fourth position, &#8217;4&#8242;, is the deepest.  It can be incremented.  We increment it by one.  There are no deeper numbers, so we&#8217;re done.</p>
<p>Let&#8217;s say the fourth position is maxed out.  What happens?</p>
<pre>1 2 3 11
1 2 4 5</pre>
<p>We move back on position to the third position.  This one, &#8217;3&#8242;, is the deepest number that can be incremented.  We increment it by one to 4.  Then we go down the line and set each number after it to one more than the previous.  In this case we only have to set the fourth digit to one more than the third, which is 5.</p>
<p>Let&#8217;s take a look at one more example.</p>
<pre>1 2 10 11
1 3 4 5</pre>
<p>Same thing here.  We move back to the second digit to find one we can increment.  We increment it.  We then drag the others back to be one greater than it.</p>
<h4>Writing This in Code</h4>
<p>The following code should go in the <code>else { }</code> area of the function above.</p>
<p>The first thing we need to do is find the deepest number that can be incremented.  The deepest digit can&#8217;t be incremented when it equals $total.  The second deepest digit can&#8217;t be incremented when it equals ($total &#8211; 1).  The next one at ($total &#8211; 2), then ($total &#8211; 3), etc.</p>
<p>This loop puts that pattern into practice.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#41;</span> 
             <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span><span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>At the end, $moveBack will hold the number of spaces from the end of the deepest digit.  If $moveBack == 0, then the deepest digit can be incremented.  If $moveBack == 1, we have to move up one digit to increment.  So on and so forth.</p>
<p>If $moveBack == $choose, we can stop counting.  That means that no more digits can be incremented &#8211; we&#8217;ve counted all the way back to digit #0.</p>
<h4>Incrementing to the Next Set</h4>
<p>Now that we know which digit can be incremented, we can increment the whole set and return the array.</p>
<p>First, we should check if $moveBack == $choose (to see if we&#8217;re done).  If it doesn&#8217;t, we start incrementing.</p>
<p>We start by incrementing $combo[$choose - $moveBack].  Then we loop through, decrementing $moveBack until it is 0 &#8211; and we update the deepest digit.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Increment the deepest digit that can be incremented</span>
  <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  Loop through and set all other numbers based on this</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span><span style="color: #339933;">--;</span>
    <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>			
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  $combo now holds the next combination</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  We couldn't increment.  The set passed</span>
  <span style="color: #666666; font-style: italic;">//    to the function was the last possible set.</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>And&#8230; We&#8217;re Done</h4>
<p>If you put all the pieces together correctly, you should now have a function that iterates through all of the possible combinations given a binomial coefficient.</p>
<p>If you were handling large sets (millions of possible combinations), you&#8217;d probably want to re-write this in a compiled language.  However, the logic is simple and based simply on arrays &#8211; so it should be easily portable to any language.</p>
<p>A word of warning about storing the information.  Don&#8217;t try to store all of the possible combinations in a giant array.  It&#8217;ll choke up all your RAM and eventually kill the script.  I forget how many combinations I tried to calculate that way, but PHP maxed out at 130mb of RAM and died.</p>
<p>Your best bet would be to write the combinations to a data file or a database as you go.  This dumps the storage requirements into a more capable format.</p>
<p>So what do you do with this?  I&#8217;m not really sure.  I wrote the script for a guy who was working on a statistical program he was using for his doctoral thesis.  I&#8217;m sure there are other specialized purposes for the script &#8211; statistics and probability.</p>
<p>If nothing else, it&#8217;s an interesting look at how to write a computer program to tackle a complex math problem.</p>
<p>Here&#8217;s the full source of the script, if you had trouble putting the pieces together.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Return the first set, i.e. {1, 2, 3, 4}</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$choose</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>    
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#41;</span> 
             <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$moveBack</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Increment the deepest digit that can be incremented</span>
    <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//  Loop through and set all other numbers based on this</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$moveBack</span><span style="color: #339933;">--;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>			
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//  $combo now holds the next combination</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  We couldn't increment.  The set passed</span>
    <span style="color: #666666; font-style: italic;">//    to the function was the last possible set.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">//  If we didn't return before, no combination was found</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$choose</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Process the combo here</span>
  <span style="color: #666666; font-style: italic;">//  Store it in a file, an array, or something</span>
  <span style="color: #666666; font-style: italic;">//  Or just out put it</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting Combinations: Calculating a Binomial Coefficient</title>
		<link>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/</link>
		<comments>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:52:50 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/</guid>
		<description><![CDATA[I came across this problem on the DigitalPoint forums the other day. How can we write a script to calculate the total number of random combinations in a set (i.e. the binomial coefficient), and how can we generate a list of these combinations? Today, we&#8217;ll tackle the first part. We&#8217;ll start with a quick primer [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem on the DigitalPoint forums the other day.  How can we write a script to calculate the total number of random combinations in a set (i.e. the binomial coefficient), and how can we generate a list of these combinations?</p>
<p>Today, we&#8217;ll tackle the first part.  We&#8217;ll start with a quick primer on math &#8211; what is a binomial coefficient?  Then, we&#8217;ll look into the most efficient way to calculate that and get the total number of possible combinations.<br />
<span id="more-159"></span><br />
<h4>What Is a Binomial Coefficient?</h4>
<p>If it&#8217;s been a while since you took statistics or calculus, you might be scratching your head thinking about what a binomial coefficient is and what it has to do with combinations.</p>
<p>If you&#8217;re looking at a set of random items &#8211; let&#8217;s say 52 playing cards &#8211; and you want to pick out a number of them &#8211; let&#8217;s say a 7 card poker hand &#8211; the binomial coefficient tells you how many possible combinations you can make.</p>
<p>The general formula for that is usually written:</p>
<p><code>(x Choose y) = (x!) / (y! * (x-y)!)</code></p>
<p>In our example above, the total number of combinations you can get in a 7-card poker game is (52! / (7! * 45!)) or somewhere around 130,000,000.</p>
<h4>How Do We Calculate That (in PHP)?</h4>
<p>These methods of calculation will work for any language, really.  We&#8217;ll just use PHP as an example.</p>
<p>The simplest way to calculate the binomial coefficient would be to loop through, calculate each factorial, and then evaluate the equation.  Notice I said simple, not efficient.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">52</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the numerator, 52!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denOne</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the first part of the denominator, 7!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denOne</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denTwo</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the second part of the denominator, 45!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denTwo</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$denOne</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>PHP will evaluate this properly because it&#8217;s nice and flexible with data types.  Do you know how large 52! is?  Try 8.06581751709E+67 large.</p>
<p>This is clearly too big to be an integer value, but PHP quietly converts the values to floats and keeps running.  There should be a better way &#8211; one where we don&#8217;t have to calculate all of the factorials.</p>
<h4>Method Two &#8211; Using array_diff</h4>
<p>This method is a bit more along the lines of human logic.  However, it&#8217;s still not going to be efficient.  Although it&#8217;ll work with smaller numbers than the first method, it&#8217;ll take more time and memory to compute.</p>
<p>Basically, we&#8217;ll create three arrays.  One for each set &#8211; {1, 2, 3, &#8230; 52}, {1, 2, 3, &#8230; 7}, {1, 2, 3, &#8230; 45}.</p>
<p>Since we know that the third set (45!) will be in the denominator and the first set (52!) will be in the numerator, we can use a nifty function called array_diff to cancel out all of the common values and shrink the numerator for us.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$newNumerator</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oldNumerator</span><span style="color: #339933;">,</span> <span style="color: #000088;">$oldDenominator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then we can calculate the factorials by multiplying all of the values in the array.  We can also make use of another PHP function here &#8211; array_product.  It does more or less what it sounds like &#8211; it finds the product of all the values in an array.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// the numerator array, 52!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denOne</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denOne</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  The first denominator array, 7!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denTwo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// The second denominator array, 45!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Use array_diff to cancel out the common values in 52! and 45!</span>
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #339933;">,</span> <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_product</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">array_product</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$denOne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This keeps the numbers much smaller.  Therefore we can work with bigger combinations before PHP putts out.  However, the arrays can eat up a lot of memory and processing power&#8230; so there still should be a better way.</p>
<h4>Combine the Two Concepts</h4>
<p>The more efficient way is to combine the two concepts.  We&#8217;ll use the first method of multiplying through a loop to generate the factorials.  This is far more efficient than iterating an array.</p>
<p>However, we&#8217;ll still cancel out the common values between 52! and 45!.  To do this, we modify the first loop so that it counts down from $x (52) to $x &#8211; $y (45).  Then we simply ignore the third loop &#8211; since it&#8217;s values were essentially counted when we calculated the first loop.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$den</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$den</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$den</span><span style="color: #339933;">;</span></pre></div></div>

<p>At this point, our script is pretty efficient.  It doesn&#8217;t use up a lot of memory, it executes quickly, and it&#8217;s not calculating very many redundant values.</p>
<p>However, there&#8217;s still a limit to what we can do.  The biggest number PHP seems to be able to calculate is 170!.  After PHP sees the value 7.25741561531E+306 it just seems to give up.  Geez, like 300 zeroes is a lot.</p>
<p>You could improve this a bit more, and increase the number of combinations it could compute.  Rather than calculating the $numerator and $denominator separate, as we did here, you&#8217;d want to calculate the $denominator first and then cancel out values as you multiply the numerator.</p>
<p>In other words, calculate the $denominator as normal.  Then, while you&#8217;re looping through to calculate the numerator, you factor out any common values in the $denominator and the number about to be multiplied into the numerator.</p>
<h4>Who Cares About How Many Combinations&#8230; I Want to See Them</h4>
<p>This is really the boring part.  Who cares how many combinations there are?  We can easily find that with a calculator.</p>
<p>The interesting part &#8211; where we definitely need a computer to help us &#8211; is to iterate through the set and find all of the possible combinations.  We&#8217;ll come back to this later in the week.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Javascript Function: Random Number Generator</title>
		<link>http://www.earn-web-cash.com/2008/02/24/random-number-function/</link>
		<comments>http://www.earn-web-cash.com/2008/02/24/random-number-function/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:07:28 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/24/random-number-function/</guid>
		<description><![CDATA[Yesterday, I wrote a short article on generating random numbers in Javascript. The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful. At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote a short <a href="http://www.earn-web-cash.com/2008/02/23/random-number-javascript/">article on generating random numbers in Javascript</a>.  The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful.</p>
<p>At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the general formula for this into a short helper function to generate a random number.<br />
<span id="more-158"></span></p>
<h4>Random Numbers: General Formula</h4>
<p>First, let&#8217;s take a minute and recap the general formula for finding a random number.</p>
<p>Javascript&#8217;s Math.random() method returns a random number between 0 and 1.  By multiplying this decimal value by a whole number, we can generate a desired range of values.  For example, by multiplying Math.random()&#8217;s result by 10, we get a range of 0 to 10.</p>
<p>Next, we take the Math.floor value of that random number.  This gives us just the integer portion &#8211; and it effectively cuts our range to 0 through 9.  Finally, we can modify this by adding an offset number.  So if we wanted a random number between 51 and 60, we&#8217;d add 51 to that result.</p>
<h4>Creating a Random Number Function</h4>
<p>The basic formula we end up with is&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> setSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> setOffset<span style="color: #339933;">;</span></pre></div></div>

<p>Our code would be much more readable if we had a helper function that could be called like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">randomNumber <span style="color: #339933;">=</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our function is going to take two parameters &#8211; min and max.  From these two parameters, we can calculate the offset and the set size and generate the proper random number.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> min<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> range <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>max <span style="color: #339933;">-</span> min<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> randomNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> range<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> offset<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> randomNumber<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All done.</p>
<p>Add that function to a utility file, and you&#8217;ve got a handy random number generator at your disposal.  It even uses the same format as PHP&#8217;s rand() function, to help increase consistency and readability in your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/24/random-number-function/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Create a Random Number in Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/</link>
		<comments>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 04:34:19 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/23/random-number-javascript/</guid>
		<description><![CDATA[I've been working with PHP for some time now, and I've become spoiled with the rand() function for generating random numbers.  It's so simple - pass a min, pass a max, and get the number you want.

While I was working on various game-related scripts this weekend, I was left wondering how to create a random number in Javascript.  It's not quite as simple as the rand() function - we'll have to go back to the old C-style random number generation.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with PHP for some time now, and I&#8217;ve become spoiled with the rand() function for generating random numbers.  It&#8217;s so simple &#8211; pass a min, pass a max, and get the number you want.</p>
<p>While I was working on various game-related scripts this weekend, I was left wondering how to create a random number in Javascript.  It&#8217;s not quite as simple as the rand() function &#8211; we&#8217;ll have to go back to the old C-style random number generation.<br />
<span id="more-156"></span></p>
<h4>The Math Object and Random Numbers</h4>
<p>Javascript comes with a built-in random number generator as part of the Math object.  This Math object does a number of calculations &#8211; trig functions (sine, cosine, and tangent), rounding, logarithms, exponents, etc.</p>
<p>To use the Math class to generate a random number, you begin with the random method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This method returns a random value &#8211; between 0 and 1.</p>
<h4>How Can I Make a Useful Random Number?</h4>
<p>If you&#8217;ve ever programmed in C/C++, you&#8217;ll know what to do.  If you&#8217;re used to working with something like PHP &#8211; which comes with an intelligent built-in random number generator &#8211; you may be left in the dark.</p>
<p>How can you take a random decimal &#8211; between 0 and 1 &#8211; and turn it into a random number between, say, 1 and 100?</p>
<p>The answer is simple &#8211; math.  You need to multiply the random() result to get a certain sized range and then use addition to start the range at the right number.</p>
<p>Let&#8217;s say you multiply the result of Math.random() by 10.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span></pre></div></div>

<p>Since the results of Math.random() are between 0 and 1, the results of this statement will be 0 to 10.  The minimum result is (0 * 10 = 0) and the highest result is (1 * 10 = 10).</p>
<p>If you were to only look at the whole number part of the result, you would get a range of 0 to 9.  In actuality, the result of Math.random() is never 0 or 1, so the above statement won&#8217;t give you a result of 10.</p>
<p>Therefore we can use the whole number portion of the result to get a random integer &#8211; 0 to 9.</p>
<h4>How Do We Get a Whole Number?</h4>
<p>Simple.  Use the floor method of the Math class.  Math.floor returns the whole number portion of a float/decimal.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1.45</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//  Result is 1</span></pre></div></div>

<p>Simply pass a float/decimal to the Math.floor method, and it will return the integer portion of that number.  If you&#8217;re working with negative numbers, it will always round down.  Keep that in mind.  For positive numbers, it&#8217;s simply a truncation function.</p>
<p>To get a random integer, we simply pass the result of Math.rand() to Math.floor().</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>What If My Random Set Doesn&#8217;t Start at 0?</h4>
<p>Here&#8217;s the last piece of the random puzzle.</p>
<p>You can determine the size of your random number set with multiplication.  The coefficient you use (like we used 10 before) determines the range your random number can be in.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 10 Numbers, 0-9</span>
Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 5 Numbers, 0-4</span>
Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 100 Numbers, 0-99</span></pre></div></div>

<p>If the set you want to create a random number from doesn&#8217;t start with 0, you simply use addition to get what you want.</p>
<p>Let&#8217;s say you want a random number from 101-150.  Your set contains 50 numbers, and it starts with 101.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">101</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 101 - 150</span></pre></div></div>

<p>The basic statement &#8211; Math.floor( Math.rand() * 50) &#8211; returns a value between 0 and 49.  We  want to offset that by 101 places and we&#8217;ll have our desired random set &#8211; 101 to 150.</p>
<p>At this point, you should have a general equation you can use for generating random numbers from any given set.  Take Math.rand(), apply a coefficient to determine the size of the random number set, and then use addition to offset that number to your desired starting point.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> setSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> setOffset<span style="color: #339933;">;</span></pre></div></div>

<p>Now go find something useful to use these random numbers for.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Javascript Game Spotlight: 3d Worm</title>
		<link>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/</link>
		<comments>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 15:09:07 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/</guid>
		<description><![CDATA[I was browsing the <a href="http://www.frihost.com" rel="nofollow">Frihost</a> forums this morning, and I stumbled on a cool Javascript game.

One of the Frihost users and his friend made a <a href="http://tekage.com.ar/uploaded/3Dworm/">3d Worm game</a>.  Although the directions are in Spanish (as is the source code), the game is pretty cool.  It's a good example of what you can accomplish with some Javascript/DOM trickery.]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/3dworm.png' title='3d Worm Screenshot'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/3dworm.thumbnail.png' alt='Screenshot of a 3d Worm game written in Javascript.' /></a>I was browsing the <a href="http://www.frihost.com" rel="nofollow">Frihost</a> forums this morning, and I stumbled on a cool Javascript game.</p>
<p>One of the Frihost users and his friend made a <a href="http://tekage.com.ar/uploaded/3Dworm/">3d Worm game</a>.  Although the directions are in Spanish (as is the source code), the game is pretty cool.  It&#8217;s a good example of what you can accomplish with some Javascript/DOM trickery.<br />
<span id="more-153"></span><br />
Playing the game is simple enough.  You use the four arrow keys and the Q/A key to pick one of six directions to go in.</p>
<p>At first, it can be very difficult to gauge the &#8220;3d&#8221; position of the apple.  Once you learn to use the shadows to your advantage, the game becomes pretty easy.  It&#8217;s still fun, though, and a great example of how Javascript can be used to make an interface for a game.</p>
<p>You see a few cool things here.</p>
<p>For one, the script captures keyboard input to drive the action.  Rather than clicking on buttons/boxes on the screen, the user can hit the appropriate keys and the game responds.  This is important for making a game feel &#8220;real&#8221; and not a kind of mash-up.</p>
<p>A lot of the positioning is done with &#8220;position: absolute&#8221; and the &#8220;top&#8221;/&#8221;left&#8221; styles.  This let&#8217;s you use the screen as a kind of canvas &#8211; drawing the images where-ever you need them.  It frees you from the constraints of normal HTML positioning, where you need to float/margin/padding stuff to get it to line up where you want.</p>
<p>It&#8217;s possible to read source code written in another language.  I just find this interesting.  The source is written in Spanish &#8211; so most of the variable/function names don&#8217;t mean a lot to me.  However, the code is pretty straightforward so it&#8217;s still easy enough to follow what&#8217;s going on.</p>
<p>Well go play some Worm.  This has inspired me to go do some coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JS Game: Making a Real Game Board from a Concept</title>
		<link>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/</link>
		<comments>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 04:08:57 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/</guid>
		<description><![CDATA[In a recent post, I sketched out how to use Javascript to build a gameboard for a browser-based game &#8211; specifically Tic Tac Toe. After a little tweaking, I&#8217;ve worked this into a functioning Tic Tac Toe board. In this post, we&#8217;ll explore the code used to set up this board. Once that&#8217;s done, the [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent post, I sketched out how to <a href="http://www.earn-web-cash.com/2008/02/19/js-online-game-board/">use Javascript to build a gameboard for a browser-based game</a> &#8211; specifically Tic Tac Toe.  After a little tweaking, I&#8217;ve worked this into a functioning Tic Tac Toe board.</p>
<p>In this post, we&#8217;ll explore the code used to set up this board.  Once that&#8217;s done, the next step will be to create a php script to evaluate some of the game&#8217;s logic.<br />
<span id="more-136"></span></p>
<h5>Recap: The Game Board Concept</h5>
<p>First, let&#8217;s take a step back and recap our Javascript game board concept.</p>
<p>The basic building block of our game board is a div element.  This div element is styled with a CSS stylesheet &#8211; 50px by 50px.  By using floats and clearss strategically, we can stack 9 of these div elements into a 3&#215;3 box.</p>
<p>Javascript is added to make these boxes functional.  An &#8220;onClick&#8221; handler is used to call a Javascript function that changes the background image of the div.  This effectively marks the square as either a &#8220;Circle&#8221; or &#8220;Cross&#8221; for Tic Tac Toe.</p>
<p>To alter the div &#8211; and also to add textual output &#8211; we&#8217;ll be using Javascript&#8217;s powerful DOM functions.  These allow us to grab a specific element by it&#8217;s ID and change it.</p>
<h5>Creating Graphics</h5>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' title='Tic Tac Toe Circle Image'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' alt='Tic Tac Toe Circle Image' /></a>I created two images in Inkscape to use for this board.  Each is a 50&#215;50 png file &#8211; with a gray background and a black border.  The actual token (the circle or cross) is red and laid on top of the gray background.</p>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' title='Tic Tac Toe Cross Image'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' alt='Tic Tac Toe Cross Image' /></a>I&#8217;m no great artist, but these should work for a functional demo.  We can worry about making nice circle and cross tokens later.  I think getting the game <strong>working</strong> is more important than making it pretty from the get-go.</p>
<h5>Turning Our .js Script into a Class</h5>
<p>In making this a functional board, I also converted the script into an actual class.  The script may end up being somewhat large and unwieldy &#8211; and an object oriented approach may help us keep it tidy and clean.  Or it may add a lot of overhead&#8230; but I like objects.</p>
<p>In our HTML file, we&#8217;ll create the object like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
var tictactoe = new game();
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>In the attached .js file, we actually define the <strong>game</strong> object.  Here&#8217;s part of the object definition.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> game<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//  Array to hold the bgImgs</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'tttcircle.png'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'tttcross.png'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//  Player information			</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Player One&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Player Two&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>This class constructor does some of the initialization for us.</p>
<p>First, it creates an array with background images.  At the moment we&#8217;re only using two images.  However, this technique would be useful if you had a more complex map &#8211; with 10-15 images you could lay over a div.</p>
<p>Second, we create some player information variables.  The &#8216;currentPlayer&#8217; property is going to track whether the &#8216;cross&#8217; or &#8216;circle&#8217; player is currently taking a turn.  The &#8216;players&#8217; array will just hold the names of those players for now.</p>
<h5>The Background Image Changing Function</h5>
<p>One of the major methods of this class will be changeBackground().  Just like in the previous example, this method will change the background style of a given div tag.  This way we can change it from an open square to a circle or cross token.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changeBackground</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'box-'</span> <span style="color: #339933;">+</span> boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'transparent url('</span> <span style="color: #339933;">+</span> 
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">') top left no-repeat'</span><span style="color: #339933;">;</span>
&nbsp;
    box.<span style="color: #660066;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changePlayer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This should be pretty straightforward.  We&#8217;re storing the &#8216;div&#8217; element in the &#8216;box&#8217; variable.  We&#8217;re then setting the &#8216;background&#8217; style as we would in a css style.  Remember that we stored the background images in an array (this.bgImgs) and this.currentPlayer corresponds to a key in the this.bgImgs array (either 0 or 1).</p>
<p>The &#8216;box.removeAttribute()&#8217; method is removing &#8216;onClick&#8217; from that div.  We can&#8217;t use a square a second time, so we might as well eliminate the onClick handler altogether.</p>
<p>Finally, this.changePlayer() is calling a new method.  This is going to help us switch from Player One&#8217;s turn to Player Two&#8217;s turn.</p>
<h5>One Turn to Another &#8211; this.changePlayer()</h5>
<p>The last method we need to declare for this class at the moment is changePlayer.</p>
<p>This will toggle the active player &#8211; which in turn affects whether a circle or cross is placed on the board.  For some added effect, we&#8217;ll also create a new html element to display a message that says who&#8217;s turn it is.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changePlayer</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//  Switch the active player</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Get a reference to our 'message' element and create the message</span>
    <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> msg <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;It is &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'s turn.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> txt <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Erase any existing text</span>
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>box.<span style="color: #660066;">hasChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      box.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>box.<span style="color: #660066;">lastChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Add the text node (our message) to our element</span>
    box.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, this is pretty straightforward.  The DOM functions are amazingly simple &#8211; once you see how they work.</p>
<p>&#8216;box&#8217; is a reference to our element (id = message).  The msg variable is a temp variable I created to hold the string.  The &#8216;createTextNode&#8217; method creates a new block of text (with our msg) that we can then insert into an HTML element.</p>
<p>The while() loop is simply there to erase any old text.  As long as our &#8216;box&#8217; element has any child nodes inside of it (text or other HTML tags), the loop will execute and delete one of those child nodes each time.  This way we have a clean slate on which to write down who&#8217;s turn it is.</p>
<p><script type="text/javascript">var tictactoe = new game();</script></p>
<p style="float: left; margin-right: 1em;" class="message break" id="message">
<div class="break">
<div class="box" id="box-1" onClick="tictactoe.changeBackground('1');">1</div>
<div class="box" id="box-2" onClick="tictactoe.changeBackground('2');">2</div>
<div class="box" id="box-3" onClick="tictactoe.changeBackground('3');" style="margin-right: 1em;">3</div>
</div>
<div class="break">
<div class="box" id="box-4" onClick="tictactoe.changeBackground('4');">4</div>
<div class="box" id="box-5" onClick="tictactoe.changeBackground('5');">5</div>
<div class="box" id="box-6" onClick="tictactoe.changeBackground('6');" style="margin-right: 1em;">6</div>
</div>
<div class="break">
<div class="box" id="box-7" onClick="tictactoe.changeBackground('7');">7</div>
<div class="box" id="box-8" onClick="tictactoe.changeBackground('8');">8</div>
<div class="box" id="box-9" onClick="tictactoe.changeBackground('9');" style="margin-right: 1em;">9</div>
</div>
<h5>Seeing It In Action</h5>
<p>Well, that&#8217;s really all there is to it.  Have a look at this functioning demo.  If you had trouble piecing together the example code, you can download the source files (.js, .css, and two .png) from the list below.</p>
<p>Good luck, and be sure to check back soon.  Our next task is going to be a <strong>real</strong> challenge.  We need to add some <strong>logic</strong> to this game &#8211; evaluating victoary conditions and creating AI!</p>
<p>Source code files:</p>
<ul>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tictactoe-basic.js' title='Basic Tic-Tac-Toe JS File'>Basic Tic-Tac-Toe JS File</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tictactoe1.css' title='Tic-Tac-Toe CSS File (#2)'>Tic-Tac-Toe CSS File (#2)</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' title='Tic Tac Toe Cross Image'>Tic Tac Toe Cross Image</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' title='Tic Tac Toe Circle Image'>Tic Tac Toe Circle Image</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JS Game: Creating a Simple Game Board in Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/</link>
		<comments>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 23:46:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/19/js-online-game-board/</guid>
		<description><![CDATA[A thorough knowledge of Javascript can give you tremendous control over what happens on a website. Combined with PHP through an AJAX request, you can make some pretty nifty browser-based games. This tutorial will focus on the first part of making a basic board game &#8211; creating a board. The end result will be a [...]]]></description>
			<content:encoded><![CDATA[<p>A thorough knowledge of Javascript can give you tremendous control over what happens on a website.  Combined with PHP through an AJAX request, you can make some pretty nifty browser-based games.</p>
<p>This tutorial will focus on the first part of making a basic board game &#8211; creating a board.  The end result will be a three by three game board suitable for use in developing a tic-tac-toe game.<br />
<span id="more-132"></span></p>
<h5>Game Development with JS, AJAX, and PHP</h5>
<p>How can we use JS and PHP &#8211; connected through AJAX &#8211; to develop an online game?</p>
<p>Javascript&#8217;s main use for us is to create an interface for the game.  Through Javascript we can capture user input &#8211; i.e. mouse clicks and text.  We can also alter the game&#8217;s output &#8211; changing the HTML of the page, adding images, and moving things around.  Javascript will provide a lot of the front end work.</p>
<p>PHP, on the other hand, is a more robust language for dealing with the logic of the game.  PHP could be useful for developing an AI and evaluating winning conditions.  It also offers a great way to store information for later use through flat files or database integration.</p>
<p>If we&#8217;re going to use both Javascript and PHP, we&#8217;ll need to use AJAX.  This is the glue that holds the whole thing together.  It will send input from the main Javascript to the PHP processing scripts.  The PHP script will then send info back to the Javascript and it will alter the page&#8217;s layout accordingly.</p>
<h5>Step One: Building a Game Board</h5>
<p>The first step in creating a graphical online game is to build a game board.  The most basic board I could think of &#8211; and therefore a good starting point &#8211; is a tic-tac-toe board.</p>
<p>First, we should define and create our basic building block.  A square.</p>
<p>In terms of HTML/CSS, our square will be a div tag.  We&#8217;ll be able to position and style it to our liking, and we can add javascript triggers like &#8220;onClick&#8221; to help run our Javascript.</p>
<div class="basicbox" id="box" onClick="makeBlack('box');"></div>
<p>Our first box is going to be a simple 50px x 50px square.  When you click it, it will change background colors to black.</p>
<p>Here&#8217;s the CSS we&#8217;ll use for the square.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">thin</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Inside the html body we can create our box like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack();&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Finally, we need to define our Javascript function makeBlack().  We&#8217;ll access the div element through it&#8217;s ID (&#8216;box&#8217;) and change it&#8217;s background style to be black.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> makeBlack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'black'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we&#8217;ve got a building block!  We just successfully used Javascript and DOM to create a click-able box.  With some tweaking, we can turn this into a nifty 3&#215;3 grid for Tic Tac Toe.</p>
<h5>Step Two: Build a Grid</h5>
<p>For Tic Tac Toe, we&#8217;re going to need a 3&#215;3 grid.  We can modify our CSS a bit and create 9 separate divs to accomplish this.  Add the following lines to the CSS for .box, and create a new class .break.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.break</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> bloth<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This way our boxes line up in a row.  If we include a &#8220;break&#8221; class in our div, we can start a new line.  Therefore we should update the html like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('one');&quot;</span>&gt;</span>1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;two&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('two');&quot;</span>&gt;</span>2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('three');&quot;</span>&gt;</span>3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;four&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('four');&quot;</span>&gt;</span>4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;five&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('five');&quot;</span>&gt;</span>5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;six&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('six');&quot;</span>&gt;</span>6<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;seven&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('seven');&quot;</span>&gt;</span>7<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;eight&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('eight');&quot;</span>&gt;</span>8<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nine&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('nine');&quot;</span>&gt;</span>9<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Notice that each box has it&#8217;s own unique ID &#8211; one to nine.  I also changed the makeBlack() function to include a parameter &#8211; an ID.  Let&#8217;s update the makeBlack() function to utilize this to change the specific box that we click on.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> makeBlack <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'black'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="break">
<div class="box" id="one" onClick="makeBlack('one');">1</div>
<div class="box" id="two" onClick="makeBlack('two');">2</div>
<div class="box" id="three" style="margin-right: .5em;" onClick="makeBlack('three');">3</div>
</div>
<div class="break">
<div class="box" id="four" onClick="makeBlack('four');">4</div>
<div class="box" id="five" onClick="makeBlack('five');">5</div>
<div class="box" id="six" style="margin-right: .5em;" onClick="makeBlack('six');">6</div>
</div>
<div class="break">
<div class="box" id="seven" onClick="makeBlack('seven');">7</div>
<div class="box" id="eight" onClick="makeBlack('eight');">8</div>
<div class="box" id="nine" style="margin-right: .5em;" onClick="makeBlack('nine');">9</div>
</div>
<h5>Step Three: Use a Background Image Instead of a Background Color</h5>
<p>Turning a box to a black background color isn&#8217;t that impressive.  But it&#8217;s easy to use as a demonstration.</p>
<p>For a game, it would be much more useful to make an image appear in our div.  We can do that by editing the &#8220;background&#8221; style of the div in the makeBlack() function.  Note: You might also want to change the name of the function to something more descriptive, like changeBackground().</p>
<p>Here&#8217;s how to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> changeBackground <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'transparent url(image.png) top left no-repeat'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The property &#8220;box.style.background&#8221; accesses the &#8220;background&#8221; css style for the &#8220;box&#8221; id.  Give it a value just like you would in a css stylesheet.  In this case, we&#8217;re giving it a background of &#8220;image.png.&#8221;  You might want to change this to a &#8220;circle.png&#8221; or a &#8220;cross.png&#8221; that you create for your game.</p>
<h5>Step Four: Changing the onClick Event</h5>
<p>The last thing we want to look at in this introduction is how to change the onClick event for our div.  You might want to get rid of it altogether &#8211; or maybe change it to a new function.  For example, after turning the box black, you might want to make the onClick call a new function &#8211; makeWhite().</p>
<p>We can do this through another DOM method, similar to the way we accessed the styles before.  Assuming we have the same variable box that refers to our div, here&#8217;s what we need to do in our function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//  Get rid of onClick altogether</span>
box.<span style="color: #660066;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  Change onClick to a new function</span>
box.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'makeWhite();'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Where To Go From Here?</h5>
<p>Now that you&#8217;ve got a basic 3&#215;3 grid and you know how to make it respond to user input, you can get started on making a game.</p>
<p>Here are a few of the things you&#8217;ll want to think about next.</p>
<ul>
<li>I need some images to place in my boxes.</li>
<li>How Can I Alternate Players (and images)?</li>
<li>How Can I Determine Who Wins?</li>
<li>How Can I Make the Computer Play (AI!)?</li>
</ul>
<p>You can go ahead and get started on your own &#8211; but I&#8217;ll certainly return to this topic within the next week or two with some more examples.  After the next tutorial, we&#8217;ll have a working <a href="http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/">Tic-Tac-Toe board driven solely by Javascript</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>

