<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3579312376304629754</id><updated>2012-02-16T14:42:15.140-08:00</updated><category term='Python'/><category term='PATH'/><category term='Microsoft'/><category term='Cygwin'/><category term='Game'/><category term='Image'/><category term='C'/><category term='Painter&apos;s Algorithm'/><category term='Write'/><category term='Windows'/><category term='Palette'/><category term='Unions'/><category term='Font'/><category term='C++'/><category term='Malloc'/><category term='MSDN'/><category term='GCC'/><category term='File'/><category term='Event Handling'/><category term='Function'/><category term='Console'/><category term='Call'/><category term='Batch'/><category term='Game Design'/><category term='Linear Alegreba'/><category term='Events'/><category term='Char'/><category term='Collision'/><category term='Color Palette'/><category term='Structures'/><category term='Custom'/><category term='Script'/><category term='Python Django'/><category term='Physics'/><category term='AS3'/><category term='2D Array'/><category term='Project'/><category term='Pointers'/><category term='Basics'/><category term='Reg'/><category term='Beginner'/><category term='Algorithm'/><category term='Color'/><category term='Linked Lists'/><category term='PRNG'/><category term='Django'/><category term='No Knowledge'/><category term='Resize'/><category term='Windows Console'/><category term='Handle'/><category term='Character'/><category term='DigiPen'/><category term='Enumerations'/><title type='text'>Randy "CecilSunkure" Gaul's Game Programming Blog</title><subtitle type='html'>INSIDE TAKE ON BECOMING A GAME PROGRAMMER</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>42</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-475107574719174105</id><published>2011-12-05T13:10:00.001-08:00</published><updated>2011-12-05T21:12:02.882-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Painter&apos;s Algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Console'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows'/><category scheme='http://www.blogger.com/atom/ns#' term='Image'/><title type='text'>Windows Console Game: Painter's Algorithm</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The previous post in this series was on a wonderful bag of tricks used to set a custom color palette (awesome!) and change the font and font size. In this post we'll be going over the &lt;a href="http://en.wikipedia.org/wiki/Painter's_algorithm"&gt;painter's algorithm&lt;/a&gt;, and how to actually make use of it in your game.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The painter's algorithm is a very simple priority paradigm which dictates when you place what image onto the screen.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;For example suppose you have three different images; the sky, a cloud, and &amp;nbsp;the sun. You want to display these images, but you don't want the sun overlapping a cloud (physically impossible), or even worse have the blue sky overlap everything. To solve this you simply draw images onto the screen starting with the one that is going to be furthest back in terms of depth, which would be the sky.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-2O52oGN6PQI/Tt001TZzybI/AAAAAAAAACo/otpQQ4LGzCA/s1600/a1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="198" src="http://2.bp.blogspot.com/-2O52oGN6PQI/Tt001TZzybI/AAAAAAAAACo/otpQQ4LGzCA/s320/a1.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;Example of drawing sky, then sun, then cloud. Correct.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-WIrpD3UZm_4/Tt001i_Ir7I/AAAAAAAAACw/gdabODTyH0E/s1600/a2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="198" src="http://3.bp.blogspot.com/-WIrpD3UZm_4/Tt001i_Ir7I/AAAAAAAAACw/gdabODTyH0E/s320/a2.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;Example of drawing sky, then cloud, then sun. Wrong.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;If we recall the &lt;a href="http://cecilsunkure.blogspot.com/2011/11/windows-console-game-event-handling.html"&gt;article on event handling&lt;/a&gt;, the article actually taught a method for producing and writing images to the screen by using header files to hold image data and a function to write images to an array (or buffer) of CHAR_INFO structures. Using the same system, you can create images in header files and then write them to the screen in the correct order so that images overlap each other at the proper times. The way you go about this is to have your array of CHAR_INFO structures for writing images to. Then once you've manipulated the array (or buffer) all you want, you then pass it to the WriteConsoleOutput function and place it onto the screen in one fell swoop.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-JhTvVg8kFFY/Tt1BaivtNhI/AAAAAAAAAC4/BK9XEz-jTsc/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="174" src="http://2.bp.blogspot.com/-JhTvVg8kFFY/Tt1BaivtNhI/AAAAAAAAAC4/BK9XEz-jTsc/s320/Capture.PNG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;Here's a screenshot of a demo program I wrote just for this specific purpose!&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;Rendering of images with transparency using painter's algorithm.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;Note: Read &lt;a href="http://cecilsunkure.blogspot.com/2011/12/windows-console-game-set-custom-color.html"&gt;this article&lt;/a&gt; for info on resizing font, this will allow&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;you to have square characters like in the image above (8x8).&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Building from our knowledge gained in the previous posts, I've put together a nice demo that displays the images above, and lets you draw on the screen with the left and right clicks of the mouse. Lets examine some of the code:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#ifndef FILESUNH&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#define FILESUNH&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;/* A SUN background! */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#define SUNW 15&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#define SUNH 15&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;typedef struct&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;{&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int width;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int height;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int chars[SUNW * SUNH];&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int colors[SUNW * SUNH];&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;} _SUN;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;_SUN SUN =&amp;nbsp;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;{&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; SUNW,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; SUNH,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,255,255,177,255,255,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,177,255,255,255,177,255,255,255,177,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,177,255,255,255,255,255,177,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,255,177,177,177,255,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,177,178,219,178,177,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,177,178,219,219,219,178,177,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 177,255,177,255,177,219, 94,219, 94,219,177,255,177,255,177,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,177,178,219,126,219,178,177,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,177,178,219,178,177,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,255,177,177,177,255,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,177,255,255,255,255,255,177,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,177,255,255,255,177,255,255,255,177,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 255,255,255,255,255,255,255,177,255,255,255,255,255,255,255,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; },&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, 62, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, 62, 14, 62, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, 62, 14, 14, 14, 62, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;62, &amp;nbsp;0, 62, &amp;nbsp;0, 62, 14,224, 14,224, 14, 62, &amp;nbsp;0, 62, &amp;nbsp;0, 62,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, 62, 14,224, 14, 62, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, 62, 14, 62, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, 62, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, 62, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0, &amp;nbsp;0,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;};&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#endif /* FILESUNH */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt; &lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code is the header file for the image of the sun. As shown in this article, you can easily create new images! The most interesting thing about this image is the 255 character code in the chars array. This code is actually used for transparency. I modified our old writeImageToBuffer code to account for transparency by adding in a single if statement. If the loop finds a character of 255, it does nothing and skips the character. This will leave whatever character was already written in that particular location alone, thus creating transparency! Take a look at the new writeImageToBuffer function:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;/* Writes an image to the buffer */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;void writeImageToBuffer(CHAR_INFO buffer[], int chars[], int colors[], int imageBUFFERWIDTH, int imageBUFFERHEIGHT, int xoffset, int yoffset)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;{&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int x, y;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; /* Keep xoffset and yoffset within the screen's boundaries */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; boundCheck(&amp;amp;xoffset, &amp;amp;yoffset);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; for (y = 0; y &amp;lt; imageBUFFERHEIGHT; ++y)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; for (x = 0; x &amp;lt; imageBUFFERWIDTH; ++x)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if (chars[x + imageBUFFERWIDTH * y] != (unsigned char)255)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; buffer[(x + xoffset) + BUFFERWIDTH * (y + yoffset)].Char.AsciiChar =&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;chars[x + imageBUFFERWIDTH * y];&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; buffer[(x + xoffset) + BUFFERWIDTH * (y + yoffset)].Attributes =&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;colors[x + imageBUFFERWIDTH * y];&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; return;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;}&lt;/code&gt;&lt;br /&gt;
&lt;code&gt; &lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;As I said, there's simply a new if statement to make sure that the character 255 is not written onto the screen. 255 in ASCII is a character you'll more than likely find pretty useless, as it just fills the whole space with foreground, though you have the exact same thing with 219 without any nasty side affects that 255 can ensue on your code. Basically 255 is the perfect ASCII index for transparency as it's pretty much useless as anything else.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The last thing to show in this demo is the order in which you actually write images onto the buffer, being sky sun and cloud. This is really simple as shown here:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;writeImageToBuffer(consoleBuffer, SKY.chars, SKY.colors, SKY.width, SKY.height, 0, 0);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;writeImageToBuffer(consoleBuffer, SUN.chars, SUN.colors, SUN.width, SUN.height, 10, 5);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;writeImageToBuffer(consoleBuffer, CLOUD.chars, CLOUD.colors, CLOUD.width, CLOUD.height, 15, 11);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt; &lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;And there you have it! By ensuring that you write images in the correct order in terms of depth, you overwrite images farther back with images closer up creating a proper sense of depth to the viewer.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-475107574719174105?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/475107574719174105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/12/windows-console-game-painters-algorithm.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/475107574719174105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/475107574719174105'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/12/windows-console-game-painters-algorithm.html' title='Windows Console Game: Painter&apos;s Algorithm'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-2O52oGN6PQI/Tt001TZzybI/AAAAAAAAACo/otpQQ4LGzCA/s72-c/a1.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3937674144016441898</id><published>2011-12-01T14:25:00.001-08:00</published><updated>2011-12-05T15:10:39.597-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Color'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Console'/><category scheme='http://www.blogger.com/atom/ns#' term='Font'/><category scheme='http://www.blogger.com/atom/ns#' term='Custom'/><category scheme='http://www.blogger.com/atom/ns#' term='Resize'/><category scheme='http://www.blogger.com/atom/ns#' term='Color Palette'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows'/><category scheme='http://www.blogger.com/atom/ns#' term='Palette'/><category scheme='http://www.blogger.com/atom/ns#' term='Console'/><title type='text'>Windows Console Game: Set Custom Color Palette and Font Size</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;Last post the topic was of &lt;a href="http://cecilsunkure.blogspot.com/2011/11/windows-console-game-event-handling.html"&gt;event handling&lt;/a&gt;, this article is going to cover a treat! I have recently been graced with a great sample source code of a program that can alter the windows console font, color palette, and font size. If you've ever searched for methods of changing these aspects, if you're like me, you'd have found a whole lot of nothing. Luckily, like I mentioned, someone gave me some sample source code that I'd like to share.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;The source code, as I was told and will now tell you, is filled with a whole lot of a whole lot of ugliness, undocumented Windows functions, and magic. Despite these scary facts this source code is pretty easy to use and grants access to some very nice to have features, that can make the creating of a console game much prettier! For example say you are creating an ASCII game in the Windows Console and realize that you're stuck with the default key size of 8x12 pixels. This sucks. Now constructing square images is going to be a gigantic pain! It would be wonderful if you could have all of your characters simply be squares! Well, there's a nice 8x8 Terminal font that is available to use.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;Here are the source files:&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;a href="http://www.mediafire.com/?vd89kh4poixkjqk"&gt;source&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;I've modified the files so that they should compile cleanly in Visual Studio 2010, and in GCC 4.X.X. Once compiled the exe should run a short demo displaying a few different things, including modifying the console's color palette and setting it to the default colors, changing the font type, and changing the font size.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;This all works by using a few functions from kernel32.dll, some of which are undocumented and thus very difficult to use. I don't know exactly how everything works, and a good amount of it I don't understand whatsoever - though this is to be expected when messing with things that don't have documentation. The goal is for me to explain just what is necessary to understand the idea of what's going on so you can apply these desired features to your own console application.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;Lets start off by explaining the main function.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;pre style="background-color: #f6f6f6; line-height: 1.1em; margin-bottom: 0.5em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left;"&gt;&lt;code&gt;&lt;span class="kt" style="color: #9966cc; font-weight: 700; line-height: 13px;"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt" style="color: #9966cc; font-weight: 700; line-height: 13px;"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm" style="color: #669933; font-style: italic; line-height: 13px;"&gt;/* Setting the color palette to the default colors&lt;/span&gt;
&lt;span class="cm" style="color: #669933; font-style: italic; line-height: 13px;"&gt;    /* Browse MSDN for COLORREF to learn more about these RGB values */&lt;/span&gt;
  &lt;span class="n"&gt;COLORREF&lt;/span&gt; &lt;span class="n"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi" style="line-height: 13px;"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mh" style="line-height: 13px;"&gt;0x00000000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00800000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00008000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00808000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mh" style="line-height: 13px;"&gt;0x00000080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00800080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00008080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00c0c0c0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mh" style="line-height: 13px;"&gt;0x00808080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00ff0000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x0000ff00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00ffff00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mh" style="line-height: 13px;"&gt;0x000000ff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00ff00ff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x0000ffff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh" style="line-height: 13px;"&gt;0x00ffffff&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="cm" style="color: #669933; font-style: italic; line-height: 13px;"&gt;/* Search MSDN for the RGB macro to easily generate COLORREF values */&lt;/span&gt;

  &lt;span class="n"&gt;SetConsolePalette&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;L""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"Small default font.&lt;/span&gt;&lt;span class="se" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;\n&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi" style="line-height: 13px;"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  
  &lt;span class="n"&gt;SetConsolePalette&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;L"Lucida Console"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"Too tiny to read Lucida!&lt;/span&gt;&lt;span class="se" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;\n&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi" style="line-height: 13px;"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  
  &lt;span class="k" style="color: #0033cc; font-weight: 700; line-height: 13px;"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;windowsVersionTest&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SetConsolePalette&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;L"Consolas"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"More of a normal Consolas size.&lt;/span&gt;&lt;span class="se" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;\n&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k" style="color: #0033cc; font-weight: 700; line-height: 13px;"&gt;else&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SetConsolePalette&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;L"Lucidas"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"More of a normal Lucidas size.&lt;/span&gt;&lt;span class="se" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;\n&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi" style="line-height: 13px;"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  
  &lt;span class="n"&gt;SetConsolePalette&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;43&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;L"Lucida Console"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"Huge Lucida!&lt;/span&gt;&lt;span class="se" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;\n&lt;/span&gt;&lt;span class="s" style="color: #cc9933; font-weight: 700; line-height: 13px;"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  
  &lt;span class="n"&gt;getchar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k" style="color: #0033cc; font-weight: 700; line-height: 13px;"&gt;return&lt;/span&gt; &lt;span class="mi" style="line-height: 13px;"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;code&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;This function has an array of hex numbers at the top. These numbers correspond to the color palette in the Windows Console's properties:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://i.imgur.com/hHeeM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i.imgur.com/hHeeM.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;The eight colors above correspond to the eight elements of the palette array. You can change them to any RBG value, and you can test out different values like in the screenshot above, by messing with the Red Blue and Green color value fields. Just be sure to leave the left two digits in your hex values as 0, as only the first six digits are used.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;SetConsolePalette is a wrapper function that sets a console's palette, console font size, and console font. The first parameter is a color palette, the second is the font's x dimension, third is the font's y dimension, and last is the name of the font you wish to switch to in the form of a &lt;a href="http://stackoverflow.com/questions/1810343/is-a-wide-character-string-literal-starting-with-l-like-lhello-world-guarantee"&gt;wide character literal&lt;/a&gt;. Available fonts are Terminal, Lucidas, and Consolas by default. In order to have more available, you have to make them! Making fonts is definitely outside of the scope of this article, however. It should be noted that Consolas is only available in Vista/7 versions of Windows, and not available in XP. If you want to use the Terminal font, on lines 109 and 139 you must set the value of the 0 to 0x30. Which line you modify depends on your OS. Just modify both of those lines if you don't know which corresponds to one you need.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;That is about all the info you really need to simply use the source code! If you want to try to learn more, study the comments throughout the source code and/or ask me questions in the comments section. You can also try googling bits of the source code and see what you come up with. Googling CONSOLE_INFO came up with a lot of variations of the source I've posted, with some differing comments throughout the source files.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: &amp;quot;Trebuchet MS&amp;quot;, sans-serif;"&gt;I haven't created my own custom font yet, though I did find a nice tool &lt;a href="http://www.cylog.org/graphics/rasterfonteditor.jsp"&gt;here&lt;/a&gt; for editing and developing raster fonts. In the near future I'll probably create my own custom font for the console game(s) I'm creating as school projects. The next article in this series is on &lt;a href="http://cecilsunkure.blogspot.com/2011/12/windows-console-game-painters-algorithm.html"&gt;painter's algorithm&lt;/a&gt;.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3937674144016441898?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3937674144016441898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/12/windows-console-game-set-custom-color.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3937674144016441898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3937674144016441898'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/12/windows-console-game-set-custom-color.html' title='Windows Console Game: Set Custom Color Palette and Font Size'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-1402267820443498937</id><published>2011-11-26T11:21:00.001-08:00</published><updated>2011-12-01T15:22:24.479-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Console'/><category scheme='http://www.blogger.com/atom/ns#' term='Events'/><category scheme='http://www.blogger.com/atom/ns#' term='Project'/><category scheme='http://www.blogger.com/atom/ns#' term='Char'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows'/><category scheme='http://www.blogger.com/atom/ns#' term='Write'/><category scheme='http://www.blogger.com/atom/ns#' term='Event Handling'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><category scheme='http://www.blogger.com/atom/ns#' term='Console'/><category scheme='http://www.blogger.com/atom/ns#' term='Character'/><category scheme='http://www.blogger.com/atom/ns#' term='Handle'/><category scheme='http://www.blogger.com/atom/ns#' term='MSDN'/><category scheme='http://www.blogger.com/atom/ns#' term='Image'/><title type='text'>Windows Console Game: Event Handling</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The last post in this series was on &lt;a href="http://cecilsunkure.blogspot.com/2011/11/windows-console-game-writing-to-console.html"&gt;writing to the console&lt;/a&gt;; this post I'd like to go over handling events within the Windows Console. There are two &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682079%28v=vs.85%29.aspx" style="font-family: 'Trebuchet MS', sans-serif;"&gt;types of events&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; we're interested in: &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms684166%28v=vs.85%29.aspx" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Keyboard Events&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; and &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms684239%28v=vs.85%29.aspx" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Mouse Events&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. The rest of the type of events we're going to ignore (and MSDN actually advises this on a couple internally used event types). In order to read keyboard events and mouse events, we need to get a record of all events that have occurred to the console since the last time these events were retrieved. This can be done with the following console functions:&amp;nbsp;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms683207%28v=vs.85%29.aspx" style="font-family: 'Trebuchet MS', sans-serif;"&gt;GetNumberOfConsoleInputEvents&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;;&amp;nbsp;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms684961%28v=VS.85%29.aspx" style="font-family: 'Trebuchet MS', sans-serif;"&gt;ReadConsoleInput&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to read the console's input buffer (the record of events that have happened from input, including user input), we have to know how many events there are in order to &lt;a href="http://cecilsunkure.blogspot.com/search/label/Malloc"&gt;dynamically allocate memory&lt;/a&gt; to store those events. This is where GetNumberOfConsoleInputEvents comes. It takes the read handle and a &lt;a href="http://msdn.microsoft.com/en-us/library/aa383751%28v=vs.85%29.aspx"&gt;DWORD&lt;/a&gt; &lt;a href="http://cecilsunkure.blogspot.com/search/label/Pointers"&gt;pointer&lt;/a&gt; as its parameters, and places the number of events there are in the address pointed by the DWORD pointer.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;/* Read console input buffer and return malloc'd INPUT_RECORD array */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;DWORD getInput(INPUT_RECORD **eventBuffer)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;{&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; /* Variable for holding the number of current events, and a point to it */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; DWORD numEvents = 0;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; /* Variable for holding how many events were read */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; DWORD numEventsRead = 0;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; /* Put the number of console input events into numEvents */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; GetNumberOfConsoleInputEvents(rHnd, &amp;amp;numEvents);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; if (numEvents) /* if there's an event */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; /* Allocate the correct amount of memory to store the events */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; *eventBuffer = malloc(sizeof(INPUT_RECORD) * numEvents);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; /* Place the stored events into the eventBuffer pointer */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; ReadConsoleInput(rHnd, *eventBuffer, numEvents, &amp;amp;numEventsRead);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; /* Return the amount of events successfully read */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; return numEventsRead;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;}&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt; &lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code is a function that places an array of INPUT_RECORD structures into the content of the pointer pointed by the parameter eventBuffer. If you're confused about the double asterisk **, then read the rest of the paragraph. If you already understand what this function is doing, skip to the next paragraph. The asterisk, known as the dereference operator, can be read as "the content pointed by". In order for this getInput function to place the INPUT_RECORD structs into the address pointed by a pointer, we have to pass a pointer to a pointer to the function -otherwise we'd only pass the value of a pointer to the function, which isn't what we want. Then, to access the the pointer we passed, we'd use one asterisk. To access the value pointed by the pointer that is pointed to by the parameter pointer, we must use a second *. See the below diagram for a visual representation. On the left, Pointer 1 is dereferenced giving you direct access to Pointer2. Pointer2 is dereferenced giving you direct access to the INPUT_RECORD. The right side of the diagram uses two dereferences to directly access INPUT_RECORD in a single stroke.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-dqXFlC3iwbE/TtFCWGRYI2I/AAAAAAAAACY/ywIAv0lKOqQ/s1600/pointers.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-dqXFlC3iwbE/TtFCWGRYI2I/AAAAAAAAACY/ywIAv0lKOqQ/s1600/pointers.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now that we have a function for getting the INPUT_RECORD structs, we need to be able to loop through each structure and analyze what kind of record it is, and depending on what it does we can do whatever action we like.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;while(1)&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; /* Get the input and number of events successfully obtained */&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; numEventsRead = getInput(&amp;amp;eventBuffer);&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; /* if more than 0 are read */&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; if (numEventsRead)&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; /* loop through the amount of records */&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; for (i = 0; i &amp;lt; numEventsRead; i++)&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* check each event */&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; switch (eventBuffer[i].EventType)&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* if type of event is a KEY_EVENT */&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; case KEY_EVENT:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; switch (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode)&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* if escape key is pressed*/&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; case VK_ESCAPE:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return 0;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code continuously loops until a user presses the escape key on their keyboard, which then makes the program close. This works by getting the INPUT_RECORD structs into the pointer eventBuffer (which points to type INPUT_RECORD) using our getInput function, and placing the return value thereof into numEventsRead. The loop then checks for a KEY_EVENT, and if found checks to see if the escape key was pressed. Similarly, you can check to see if a MOUSE_EVENT occurred in this very same way. If you understood what I've explained thus far in all of this series, you should be able to check for any keypress, with the help of the &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms645540%28v=vs.85%29.aspx"&gt;virtual keycode page&lt;/a&gt;, and then do something thereafter.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now how about writing something onto the console when you click your left mouse button? The first thing you'd need is to be able to check for a MOUSE_EVENT record, and get the mouse's x and y coordinates from the MOUSE_EVENT structure. Here's an example of a case to do such a thing:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;case MOUSE_EVENT:&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; offsetx = eventBuffer[i].Event.MouseEvent.dwMousePosition.X;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; offsety = eventBuffer[i].Event.MouseEvent.dwMousePosition.Y;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; if (eventBuffer[i].Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; writeImageToBuffer(consoleBuffer, REDRECTANGLE.chars, REDRECTANGLE.colors, REDRECTANGLE.width, REDRECTANGLE.height, offsetx - 1, offsety - 1);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; write = YES;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt; &lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This indexes the INPUT_RECORD structs when a &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms684239%28v=vs.85%29.aspx"&gt;MOUSE_EVENT&lt;/a&gt; structure is found, and then accesses the data members X and Y, as detailed &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms684239%28v=vs.85%29.aspx"&gt;here&lt;/a&gt;, and places the values of the x and y position into two integers. The large constant FROM_LEFT_1ST_BUTTON_PRESSED is a Microsoft definition for a left-click. Now what is this writeImageToBuffer function? That doesn't seem to be a MSDN documented function... Well it's not! It's a simple function I wrote to write any image onto the screen buffer, given the correct parameters. The image it is writing is called REDRECTANGLE, which is actually a structure defined in a file I've written called redRectangle.h. The red rectangle is a square of 9 red characters, so to write this square onto where the user clicks, you must use offsetx - 1, and offsety -1, otherwise the top left of the red rectangle image will be placed on the left-click location, instead of the center.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now lets check out redRectangle.h so you can see how I've set up the image of REDRECTANGLE!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;/* redRectangle.h */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#ifndef FILEREDRECTANGLEH&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#define FILEREDRECTANGLEH&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;/* A red rectangle! */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#define REDRECTANGLEW 3&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#define REDRECTANGLEH 3&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;typedef struct&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;{&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int width;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int height;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int chars[REDRECTANGLEW * REDRECTANGLEH];&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; int colors[REDRECTANGLEW * REDRECTANGLEH];&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;} _REDRECTANGLE;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;_REDRECTANGLE REDRECTANGLE =&amp;nbsp;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;{&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; REDRECTANGLEW,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; REDRECTANGLEH,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 219, 219, 219,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 219, 219, 219,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 219, 219, 219,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; },&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 4 &amp;nbsp;, 4 &amp;nbsp;, 4 &amp;nbsp;,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 4 &amp;nbsp;, 4 &amp;nbsp;, 4 &amp;nbsp;,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; 4 &amp;nbsp;, 4 &amp;nbsp;, 4 &amp;nbsp;,&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;};&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#endif /* FILEREDRECTANGLEH */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt; &lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This was the cleanest way I could think of for setting up an image that should be compatible with all versions of C. I'll start explaining from the top of the file. The two preprocessor directives ifndef and define are used to see if FILEREDRECTANGLEH is defined yet or not. If it is not yet defined, then the entire contents of the file will be included wherever an include of this file is placed. If the define FILEREDRECTANGLEH is already defined, it means that you've already included this file somewhere, and so the entire contents of the file will be skipped!&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-HxwIM8dWIc0/TtFUjALFvvI/AAAAAAAAACg/YoXf6dCwL5k/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-HxwIM8dWIc0/TtFUjALFvvI/AAAAAAAAACg/YoXf6dCwL5k/s1600/Capture.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;Screenshot of the final demonstration program, drawing on the window!&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I have two defines for each image I write in this format, and they represent width and height as 3 in this image. These defines are critical for declaring a REDRECTANGLE, as the size needs to be defined at compile, since a template for the structure needs to be created for the program to run, and as such you need to know the size of the arrays within the REDRECTANGLE structure in order to properly place it in memory! However, the use of defines in this way makes it very simple to create other copies of images. For example, to create a new image called BLUETRIANGLE, you simply use find and replace (ctrl + h for a lot of programs) and replace all instances of REDRECTANGLE, with BLUERECTANGLE. You then would modify the color and character arrays to contain the correct data, and change the WIDTH/HEIGHT values for the defines accordingly. The structure called _REDRECTANGLE is typedef'd allowing declaration of variables by skipping writing the annoying legacy struct keyword. An instance of _REDRECTANGLE called REDRECTANGLE is then declared and&amp;nbsp;initialized&amp;nbsp;with the arrays containing the values for the ASCII characters, and color values.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Using this format in conjunction with the writeImageToBuffer function, you can pretty easily create new images and write them with limited amounts of code! Now lets check out a finished product: &lt;a href="http://codepad.org/uuNESM4l"&gt;link&lt;/a&gt;&amp;nbsp;(I didn't want to post the entire unwieldy thing here).&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The last thing to mention is that if you update the screen with WriteConsoleOutputA every loop, you'll probably see little flickers as the screen is updated. This is simply a limitation of the function WriteConsoleOutputA. There are a few solutions to this. The easiest solution I've come up with is to just update screen less often, and I chose to do this by limiting the time in which the screen is updated to only when the consoleBuffer is changed. Another solution is to keep track of which portions of the screen you actually need to update, and call WriteConsoleOutputA and only write with the smallest portion of the screen as possible. Also supposedly if you update the screen with the same characters and colors multiple times it increases the chance of flickering lines, so this means you should try to avoid doing so. Lastly, you can multi-thread your program, and place just the call to WriteConsoleOutput within the additional thread. I do the first solution with a simple if statement:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; /* If write is 1, meaning the screen needs to be updated */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; if (write)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; {&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; /* Write our character buffer (a single character currently) to the console buffer */&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; WriteConsoleOutputA(wHnd, consoleBuffer, characterBufferSize, characterPosition, &amp;amp;consoleWriteArea);&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; write = NO;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; }&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This will only call the WriteConsoleOutputA function when write is not 0.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The &lt;a href="http://codepad.org/uuNESM4l"&gt;entire final program&lt;/a&gt; demonstrates drawing a square image on the screen on left-click, and a red dot to the screen on right click. There is boundary checking on the click coordinates to prevent indexing outside of the screen!&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;You might by now have realized that the characters you're writing to the screen are oddly shaped rectangles. What about nice square characters? Square characters are essential for creating a nice game :( Well the &lt;a href="http://cecilsunkure.blogspot.com/2011/12/windows-console-game-set-custom-color.html"&gt;next post in this series&lt;/a&gt; covers setting the console's font, font size, and even the color palette!&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-1402267820443498937?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/1402267820443498937/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/11/windows-console-game-event-handling.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1402267820443498937'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1402267820443498937'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/11/windows-console-game-event-handling.html' title='Windows Console Game: Event Handling'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-dqXFlC3iwbE/TtFCWGRYI2I/AAAAAAAAACY/ywIAv0lKOqQ/s72-c/pointers.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6612575104555832702</id><published>2011-11-16T15:47:00.001-08:00</published><updated>2011-12-04T13:59:38.314-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Character'/><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Console'/><category scheme='http://www.blogger.com/atom/ns#' term='Project'/><category scheme='http://www.blogger.com/atom/ns#' term='Char'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows'/><category scheme='http://www.blogger.com/atom/ns#' term='Write'/><category scheme='http://www.blogger.com/atom/ns#' term='MSDN'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><category scheme='http://www.blogger.com/atom/ns#' term='Image'/><category scheme='http://www.blogger.com/atom/ns#' term='Console'/><title type='text'>Windows Console Game: Writing to the Console</title><content type='html'>&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;Previously I showed you all how to &lt;a href="http://cecilsunkure.blogspot.com/2011/11/windows-console-game-setting-up-window.html"&gt;set up a console window&lt;/a&gt;! Now l&lt;/span&gt;ets talk about writing to the Windows console. One can use printf, or 
fprintf, but those options aren't really exactly ideal; you don't have a
 desired amount of control. We're going to use the function &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms687404%28v=VS.85%29.aspx"&gt;WriteConsoleOutput&lt;/a&gt; in order to write a buffer of characters (array of characters) onto the console's screen buffer. The screen buffer is the inside of your game's window in which characters are written to and appear. In order to use the WriteConsoleOutput function, you have to pass to it a buffer of characters, which is really just an array of characters. The character buffer will be comprised of an array of&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682013%28v=VS.85%29.aspx"&gt; CHAR_INFO&lt;/a&gt; structures, which is a Microsoft defined type that comes from windows.h. Lets get started with just writing a single character to the screen.&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;In C characters are enclosed letters or numbers enclosed in single quotes, like so: 'c'. Characters in c, like 'c', are treated very similarly to integers. The main difference is that the range of a character (unsigned) is from 0 to 255, where a range of integers reaches much much higher. Due to this a character variable will be one byte in memory (8 bits). You can actually write in your code 'c', and it will be treated as the value of whatever the ascii value of C is, which is 99 in decimal. You can even write c - 8, and it will be treated as 99 - 8. Here is my favorite chart for all the values of all the ascii characters available, note the indices are in hexidecimal:&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://benryves.com/tutorials/winconsole/ascii.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="317" src="http://benryves.com/tutorials/winconsole/ascii.gif" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682013%28v=VS.85%29.aspx"&gt;CHAR_INFO&lt;/a&gt; structure contains two data members; a character and its attributes. The character can either be a unicode or ascii character. We're going to stick with ascii characters within the Char.AsciiChar data member (remember, this is just an integer with a range of 0-255). The Attributes data member is a WORD type, which is just a Microsoft defined type. The WORD type is simply a 16-bit unsigned integer with a range of 0 through 65535. The Attributes data member can be viewed simply as a byte of data with the following value representations as colors:&lt;/span&gt;&lt;/div&gt;
&lt;ul style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;FOREGROUND_BLUE - &lt;/b&gt;0x0001&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;FOREGROUND_GREEN - &lt;/b&gt;0x0002&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;FOREGROUND_RED&lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt; - &lt;/b&gt;0x0004&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;FOREGROUND_INTENSITY&lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt; - &lt;/b&gt;0x0008&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;BACKGROUND_BLUE&lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt; - &lt;/b&gt;0x0010&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;BACKGROUND_GREEN&lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt; - &lt;/b&gt;0x0020&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;BACKGROUND_RED&lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt; - &lt;/b&gt;0x0040&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;BACKGROUND_INTENSITY&lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt; - &lt;/b&gt;0x0080&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;These values are the different colors accessible for use within the Windows console. The values are represented in hexadecimal, and should be viewed as a byte:&lt;/span&gt;&lt;/div&gt;
&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;span style="font-size: small;"&gt;&lt;a href="http://4.bp.blogspot.com/-YF0c0kKL9uQ/TsRQCNH4pbI/AAAAAAAAACA/ykOfVIapJ7o/s1600/byte.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-YF0c0kKL9uQ/TsRQCNH4pbI/AAAAAAAAACA/ykOfVIapJ7o/s1600/byte.png" /&gt;&amp;nbsp;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif; text-align: left;"&gt;
&lt;span style="font-size: small;"&gt;&amp;nbsp;In the picture above I've shown a single byte, which is 8 bits of information. Each bit can be either a 1 or a 0. The first bit is on the right (at least for this representation). If you wish for your Attributes data member to contain the value blue, then you set the first bit of this byte to 1. The second bit represents whether or not you want to add green to your color, and so on and so forth. The above image would contain the values of blue, red, and intensity. This value would be in decimal format 1 + 4 + 8, which is 13. Intensity brightens the color to be displayed. Knowing this will allow us to construct whatever color we like with some simple addition! For the first color lets mix blue, green, and intensity for a value of 11.&lt;/span&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif; text-align: left;"&gt;
&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif; text-align: left;"&gt;
&lt;span style="font-size: small;"&gt;Here is what MSDN says for the WriteConsoleOutput function in regards to parameters required:&lt;/span&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="code"&gt;
&lt;pre&gt;BOOL WINAPI WriteConsoleOutput(
  __in&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HANDLE hConsoleOutput,
  __in&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const CHAR_INFO *lpBuffer,
  __in&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;COORD dwBufferSize,
  __in&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;COORD dwBufferCoord,
  __inout&amp;nbsp;&amp;nbsp;PSMALL_RECT lpWriteRegion
);&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;hConsoleOutput is simply the output handle for our window, *lpBuffer is a pointer to a buffer of CHAR_INFO structures, dwBufferSize is the size of the buffer in character elements, dwBufferCoord is the location on your console screen buffer in which to write the image, and lpWriteRegion is just a pointer to a COORD structure for dictating which portion of the console to write to. Here's some code using this knowledge to write a character to the screen:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;pre&gt;#include &amp;lt;windows.h&amp;gt; /* for HANDLE type, and console functions */
#include &amp;lt;stdio.h&amp;gt; /* standard input/output */

HANDLE wHnd; /* write (output) handle */
HANDLE rHnd; /* read (input handle */

int main(void)
{
  /* Window size coordinates, be sure to start index at zero! */
  SMALL_RECT windowSize = {0, 0, 69, 34};

  /* A COORD struct for specificying the console's screen buffer dimensions */
  COORD bufferSize = {70, 35};
  
  /* Setting up different variables for passing to WriteConsoleOutput */
  COORD characterBufferSize = {1, 1};
  COORD characterPosition = {0, 0};
  SMALL_RECT consoleWriteArea = {0, 0, 0, 0}; 
  
  /* A CHAR_INFO structure containing data about a single character */
  CHAR_INFO characterQ;
  characterQ.Char.AsciiChar = 'Q'; /* Setting the Char.Ascii data member of characterQ to the value of 'Q' */
  
  /* Setting up the color values for our Q character: blue + green + intensity */
  characterQ.Attributes = FOREGROUND_BLUE | FOREGROUND_GREEN |
                          FOREGROUND_INTENSITY;
 
  /* initialize handles */
  wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
  rHnd = GetStdHandle(STD_INPUT_HANDLE);
 
  /* Set the console's title */
  SetConsoleTitle("Our shiny new title!");
 
  /* Set the window size */
  SetConsoleWindowInfo(wHnd, TRUE, &amp;amp;windowSize);

  /* Set the screen's buffer size */
  SetConsoleScreenBufferSize(wHnd, bufferSize);
  
  /* Write our character buffer (a single character currently) to the console buffer */
  WriteConsoleOutputA(wHnd, &amp;amp;characterQ, characterBufferSize, characterPosition, &amp;amp;consoleWriteArea);
 
  getchar();&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;The code above prints the character Q to the screen in a bright cyan color. This probably seems like a lot of work to write a single character to the screen, and it sort of is. But in actuality this isn't really how you want to write every character onto the screen. The WriteConsoleOutput function is a very slow function, and so you want to call it as little as possible. You may have heard of a term called "double buffer", well we're going to use a double buffer to do all of our modifying of a buffer off-screen, and once all our modifications are finished we can write a single image to the screen all in one go. To create this buffer, simply create an array of CHAR_INFO structures and initialize all of the Char.AsciiChar and Attribute data members! Here's an example of looping through an array of CHAR_INFOs and assigning random colors to a single Char type.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="code"&gt;
&lt;pre&gt;#include &amp;lt;windows.h&amp;gt; /* for HANDLE type, and console functions */
#include &amp;lt;stdio.h&amp;gt; /* standard input/output */
#include &amp;lt;stdlib.h&amp;gt; /* included for rand */

#define WIDTH 70
#define HEIGHT 35

HANDLE wHnd; /* write (output) handle */
HANDLE rHnd; /* read (input handle */

int main(void)
{
  int x, y;
  
  srand(time(0));
  
  /* Window size coordinates, be sure to start index at zero! */
  SMALL_RECT windowSize = {0, 0, WIDTH - 1, HEIGHT - 1};

  /* A COORD struct for specificying the console's screen buffer dimensions */
  COORD bufferSize = {WIDTH, HEIGHT};
  
  /* Setting up different variables for passing to WriteConsoleOutput */
  COORD characterBufferSize = {WIDTH, HEIGHT};
  COORD characterPosition = {0, 0};
  SMALL_RECT consoleWriteArea = {0, 0, WIDTH - 1, HEIGHT - 1};
  
  /* A CHAR_INFO structure containing data about a single character */
  CHAR_INFO consoleBuffer[WIDTH * HEIGHT];
 
  /* initialize handles */
  wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
  rHnd = GetStdHandle(STD_INPUT_HANDLE);
 
  /* Set the console's title */
  SetConsoleTitle("Our shiny new title!");
 
  /* Set the window size */
  SetConsoleWindowInfo(wHnd, TRUE, &amp;amp;windowSize);

  /* Set the screen's buffer size */
  SetConsoleScreenBufferSize(wHnd, bufferSize);
  
  for (y = 0; y &amp;lt; HEIGHT; ++y)
  {
    for (x = 0; x &amp;lt; WIDTH; ++x)
    {
      consoleBuffer[x + WIDTH * y].Char.AsciiChar = (unsigned char)219;
      consoleBuffer[x + WIDTH * y].Attributes = rand() % 256;
    }
  }
  
  /* Write our character buffer (a single character currently) to the console buffer */
  WriteConsoleOutputA(wHnd, consoleBuffer, characterBufferSize, characterPosition, &amp;amp;consoleWriteArea);
 
  getchar();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;The above code is creating a buffer called consoleBuffer, of a single dimensional array with the number of elements equal to WIDTH times HEIGHT, both of which are now defined at the top of the file. There is a loop that loops through all of the CHAR_INFO's Char.AsciiChar data member, and Attributes data members initializing them. You can see how I'm assigning a &lt;a href="http://cecilsunkure.blogspot.com/search/label/PRNG"&gt;random value&lt;/a&gt; using rand, and seeding the table at the beginning of the code with &lt;a href="http://cecilsunkure.blogspot.com/search/label/PRNG"&gt;srand&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: small;"&gt;You might be a little confused by the &lt;/span&gt;x + WIDTH * y portion of the code. I've created a single dimensional array of elements WIDTH * HEIGHT, and in order to index the array during a loop as if it were a two dimensional array, you need to use a formula. The idea behind the formula is to figure out what row you want to access, by taking the number of elements in a row and multiplying it by a value. To access the first row, you multiply the WIDTH value by 0. To access the fifth row, you'd multiply WIDTH by five. This works since as the single dimensional array is written to the screen with WriteConsoleOutput, it wraps around the screen buffer once ever WIDTH elements. Then, access a specific element within that row you add in your x value.&lt;br /&gt;
&lt;span style="font-size: small;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-6D4ABWV3VZE/TsRbDkOeDjI/AAAAAAAAACI/Px4s5Nuejzg/s1600/randomcolors.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="312" src="http://3.bp.blogspot.com/-6D4ABWV3VZE/TsRbDkOeDjI/AAAAAAAAACI/Px4s5Nuejzg/s400/randomcolors.PNG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;
&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;And there you have it! A method for writing an image onto the screen of any given size on any location of the console's screen buffer! The next post in this series will be on &lt;a href="http://cecilsunkure.blogspot.com/2011/11/windows-console-game-event-handling.html"&gt;Event Handling&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif; font-size: small;"&gt;&lt;br /&gt;
Source(s):&lt;br /&gt;
&lt;a href="http://benryves.com/"&gt;http://benryves.com/&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073%28v=vs.85%29.aspx"&gt;http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx&lt;/a&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6612575104555832702?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6612575104555832702/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/11/windows-console-game-writing-to-console.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6612575104555832702'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6612575104555832702'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/11/windows-console-game-writing-to-console.html' title='Windows Console Game: Writing to the Console'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-YF0c0kKL9uQ/TsRQCNH4pbI/AAAAAAAAACA/ykOfVIapJ7o/s72-c/byte.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6540675084568119236</id><published>2011-11-16T10:34:00.001-08:00</published><updated>2011-11-26T13:02:52.478-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Console'/><category scheme='http://www.blogger.com/atom/ns#' term='Project'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows'/><category scheme='http://www.blogger.com/atom/ns#' term='MSDN'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><category scheme='http://www.blogger.com/atom/ns#' term='Console'/><title type='text'>Windows Console Game: Setting up the Window</title><content type='html'>&lt;a href="http://i117.photobucket.com/albums/o67/MCBooga/ascii_city.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="240" src="http://i117.photobucket.com/albums/o67/MCBooga/ascii_city.png" width="320" /&gt;&lt;/a&gt;Have you ever seen cool looking games made with purely ASCII graphics? For example the image on the right is an amazing mock-up of an idea for a game that seems to revolve around creating some sort of city on a platform to defend from air-based attackers. I've done a bit of research, and have actually begun a project of creating an entire game from scratch by using the Windows Console as the platform. This is a great way for newer programmers to jump into game development for the following reasons:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Easy construction of graphics. All the graphics are just ASCII characters with varying colors!&lt;/li&gt;
&lt;li&gt; Very little overhead required to get a project up and running. The Windows Console is a very simple platform to get started on.&lt;/li&gt;
&lt;li&gt;All the documentation you would ever need can be found on MSDN and abroad.&lt;/li&gt;
&lt;li&gt;The Windows Console is packaged with every version of the Windows OS that is popular today, so no extra anything is needed to be downloaded or installed in order to get coding.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://i.imgur.com/366q2.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="271" src="http://i.imgur.com/366q2.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
This post is the first in a series that will show you some techniques that will allow you to create a game within the Windows Console. &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073%28v=vs.85%29.aspx"&gt;MSDN Documentation&lt;/a&gt; at this link contains all the info of all the documented functions available to manipulate the windows console programmatically. Most info in this post will be an explanation of the MSDN documentation with example code on how to use it. On the left is a screenshot from a game I'm developing called TerraNisi, which revolves around a walking tree character that has a goal of saving his Forest from spreading fire ghouls!&lt;br /&gt;
&lt;br /&gt;
The first thing you need to do is set up handles for your window. The handles of a window are what the Windows OS use to identify and manipulate a window. A handle datatype looks like this: &lt;span style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;HANDLE&lt;/span&gt;. This type is defined within windows.h with a &lt;span style="color: #444444; font-family: 'Courier New', Courier, monospace;"&gt;typedef&lt;/span&gt;, and thus you can declare variables of the &lt;span style="color: #444444; font-family: 'Courier New', Courier, monospace;"&gt;HANDLE&lt;/span&gt; type once you include windows.h to your project. Once we set them up we have access to manipulate our own window! Observe the following code that simply sets up handles, and calls getchar in order to pause the program once opened so you can see the window:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include &amp;lt;windows.h&amp;gt; /* for HANDLE type, and console functions */&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt; /* standard input/output */&lt;br /&gt;&lt;br /&gt;HANDLE wHnd; /* write (output) handle */&lt;br /&gt;HANDLE rHnd; /* read (input handle */&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; /* initialize handles */&lt;br /&gt;&amp;nbsp; wHnd = GetStdHandle(STD_OUTPUT_HANDLE);&lt;br /&gt;&amp;nbsp; rHnd = GetStdHandle(STD_INPUT_HANDLE);&lt;br /&gt;&amp;nbsp; getchar();&lt;br /&gt;}&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
This code doesn't do much but set up and initialize some handles for our window. Lets set the text title for the window, as this is the simplest thing I know how to do with console functions:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include &amp;lt;windows.h&amp;gt; /* for HANDLE type, and console functions */&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt; /* standard input/output */&lt;br /&gt;&lt;br /&gt;HANDLE wHnd; /* write (output) handle */&lt;br /&gt;HANDLE rHnd; /* read (input handle */&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; /* initialize handles */&lt;br /&gt;&amp;nbsp; wHnd = GetStdHandle(STD_OUTPUT_HANDLE);&lt;br /&gt;&amp;nbsp; rHnd = GetStdHandle(STD_INPUT_HANDLE);&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; SetConsoleTitle("Our shiny new title!");&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; getchar();&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
Now the title of your window should say "Our shiny new title!". Congrats! You've used another console function called SetConsoleTitle! Moving on to something a little more difficult, lets try using &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686125%28v=VS.85%29.aspx"&gt;SetConsoleWindowInfo&lt;/a&gt;.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;This function takes three parameters: the first is the output handle the screen's buffer&lt;/li&gt;
&lt;li&gt;The second is a boolean - if this parameter is &lt;span style="color: #444444; font-family: 'Courier New', Courier, monospace;"&gt;TRUE&lt;/span&gt; the coordinates specify the new upper-left and lower-right corners of the window. If it is &lt;span style="color: #444444; font-family: 'Courier New', Courier, monospace;"&gt;FALSE&lt;/span&gt;, the coordinates are relative to the current window-corner coordinates. &lt;span style="color: #444444; font-family: 'Courier New', Courier, monospace;"&gt;TRUE&lt;/span&gt; and &lt;span style="color: #444444; font-family: 'Courier New', Courier, monospace;"&gt;FALSE&lt;/span&gt; are defined within windows.h as 1 and 0, and if you don't understand the previous sentence I suggest just leaving this parameter as &lt;span style="color: #444444; font-family: 'Courier New', Courier, monospace;"&gt;TRUE&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;The third parameter is a &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686311%28v=VS.85%29.aspx" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;SMALL_RECT&lt;/a&gt; &lt;a href="http://cecilsunkure.blogspot.com/search/label/Structures"&gt;structure&lt;/a&gt; containing the size of the new window. This parameter will set the window's edges to a size smaller than the screen's buffer. This means you cannot make it larger than the default buffer size of 80x300 (width x height) until you change the buffer size, which I'll cover momentarily.&lt;/li&gt;
&lt;/ul&gt;
Observe:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include &amp;lt;windows.h&amp;gt; /* for HANDLE type, and console functions */&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt; /* standard input/output */&lt;br /&gt;&lt;br /&gt;HANDLE wHnd; /* write (output) handle */&lt;br /&gt;HANDLE rHnd; /* read (input handle */&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; /* Window size coordinates, be sure to start index at zero! */&lt;br /&gt;&amp;nbsp; SMALL_RECT windowSize = {0, 0, 69, 34};&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; /* initialize handles */&lt;br /&gt;&amp;nbsp; wHnd = GetStdHandle(STD_OUTPUT_HANDLE);&lt;br /&gt;&amp;nbsp; rHnd = GetStdHandle(STD_INPUT_HANDLE);&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; /* Set the console's title */&lt;br /&gt;&amp;nbsp; SetConsoleTitle("Our shiny new title!");&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; /* Set the window size */&lt;br /&gt;&amp;nbsp; SetConsoleWindowInfo(wHnd, TRUE, &amp;amp;windowSize);&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; getchar();&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
I chose to set the window size to 70 by 35, which is a decent size for some sort of game. Now you have successfully changed your window size! However you should immediately notice an ugly scroll bar on the right side of your console, and at the bottom of the console. This scroll bar is there because the console's screen buffer (the inside of the window) is larger than the window. To remedy this simply change the size of the screen buffer to that of your window coordinates with the &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686044%28v=VS.85%29.aspx"&gt;SetConsoleScreenBufferSize&lt;/a&gt; function. This function has only two parameters: the output handle and &lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119%28v=VS.85%29.aspx" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;COORD&lt;/a&gt; &lt;a href="http://cecilsunkure.blogspot.com/search/label/Structures"&gt;struct&lt;/a&gt; for the buffer size. Observe:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include &amp;lt;windows.h&amp;gt; /* for HANDLE type, and console functions */&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt; /* standard input/output */&lt;br /&gt;&lt;br /&gt;HANDLE wHnd; /* write (output) handle */&lt;br /&gt;HANDLE rHnd; /* read (input handle */&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; /* Window size coordinates, be sure to start index at zero! */&lt;br /&gt;&amp;nbsp; SMALL_RECT windowSize = {0, 0, 69, 34};&lt;br /&gt;&lt;br /&gt;&amp;nbsp; /* A COORD struct for specificying the console's screen buffer dimensions */&lt;br /&gt;&amp;nbsp; COORD bufferSize = {70, 35};&lt;br /&gt;&lt;br /&gt;&amp;nbsp; /* initialize handles */&lt;br /&gt;&amp;nbsp; wHnd = GetStdHandle(STD_OUTPUT_HANDLE);&lt;br /&gt;&amp;nbsp; rHnd = GetStdHandle(STD_INPUT_HANDLE);&lt;br /&gt;&lt;br /&gt;&amp;nbsp; /* Set the console's title */&lt;br /&gt;&amp;nbsp; SetConsoleTitle("Our shiny new title!");&lt;br /&gt;&lt;br /&gt;&amp;nbsp; /* Set the window size */&lt;br /&gt;&amp;nbsp; SetConsoleWindowInfo(wHnd, TRUE, &amp;amp;windowSize);&lt;br /&gt;&lt;br /&gt;&amp;nbsp; /* Set the screen's buffer size */&lt;br /&gt;&amp;nbsp; SetConsoleScreenBufferSize(wHnd, bufferSize);&lt;br /&gt;&lt;br /&gt;&amp;nbsp; getchar();&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://i.imgur.com/5Hh7u.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="251" src="http://i.imgur.com/5Hh7u.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
Thus far we have created the initialization of a nice console window! The next step is to begin writing things onto the console with a double buffering system! This may sound fancy, but in actually it's very simple in this project. The &lt;a href="http://cecilsunkure.blogspot.com/2011/11/windows-console-game-writing-to-console.html"&gt;next post&lt;/a&gt; in this series will be devoted to this!&lt;br /&gt;
&lt;br /&gt;
Source(s):&lt;br /&gt;
&lt;a href="http://benryves.com/"&gt;http://benryves.com/&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx"&gt;http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6540675084568119236?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6540675084568119236/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/11/windows-console-game-setting-up-window.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6540675084568119236'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6540675084568119236'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/11/windows-console-game-setting-up-window.html' title='Windows Console Game: Setting up the Window'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3544888257220318833</id><published>2011-11-15T21:49:00.000-08:00</published><updated>2011-11-18T09:13:28.741-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Beginner'/><category scheme='http://www.blogger.com/atom/ns#' term='Basics'/><category scheme='http://www.blogger.com/atom/ns#' term='No Knowledge'/><title type='text'>I Want to Learn Programming, but I Know Nothing!</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I want this post to be a great resource for people who have absolutely zero knowledge of programming to be able to pick up and start coding in the language C. I feel I have a fair level of empathy for those just starting to learn (since I started learning not too long ago), and want to write this post out before I get too competent as a programmer and forget my empathy altogether!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;So you want to learn to program. Great! So you're reading this article, and here you'll learn to start with C on a Windows Operating System. Starting with C is an excellent place to start, as it grounds you with a very solid base of understanding for programming in general, that of which I feel is easily transferable to other languages! I even feel that once you learn the basics of C and transfer to another language (I recommend Python!) you'll be better off in the long run than if you started with another language and then moved to C (perhaps start with Java and move to C). I won't get into deep details about this point as that's out of the scope of this article, but you should definitely take my word on the matter!&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Be aware that I'm going to use a lot of new and scary programmer terminology in this post, like statements, declarations, and initialization. Don't be afraid, as I give a link to the definition of words when I first use them. Read the links and make sure you understand what I'm referring to!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The programming in C that I'll be teaching you, as an absolute beginner, will be of the following steps:&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Write code&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Compile code into an executable&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;An executable is a .exe file that your computer can run, and when this executable file is run your computer will actually follow the steps you wrote in your code, thus running your code as a program! Compiling your code is actually a step that a &lt;a href="http://en.wikipedia.org/wiki/Compiler"&gt;compiler&lt;/a&gt; does for you, in which it translates your C code into machine code. Machine code can be viewed as a lot of very hard to read 1's and 0's, also known as binary. The reason people don't code directly in machine code is because it's so hard to read. C is a nice translation of machine code for people to work with. If you want to learn more about how computers work on a very low level, I suggest reading &lt;a href="http://www.amazon.com/Code-Language-Computer-Hardware-Software/dp/0735611319"&gt;Code: The Hidden Language of Computer Hardware and Software&lt;/a&gt;&amp;nbsp;by Charles Petzhold.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;You can write your C code in any text editor you like. You can just simply use notepad (to open hit windows key+r and type notepad, then hit enter), though I recommend using the &lt;a href="http://en.wikipedia.org/wiki/Integrated_development_environment"&gt;IDE&lt;/a&gt;&amp;nbsp;apart of Dev-C++. Once you write some C code, save your file as a .c file and then use a compiler to turn it into a .exe file.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now what about this compiler business. You might have already googled for a C compiler by now and noticed that there is a multitude of compilers out there. Luckily I have one to recommend to you! Download and install &lt;a href="http://www.bloodshed.net/devcpp.html"&gt;Dev-C++&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;When installing just install with all default settings. On your first run you must choose a language (probably English) and then you'll see a screen that looks like:&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-yFCI1ScU4KA/TsYPPYCdazI/AAAAAAAAACQ/dLtg9sNbptI/s1600/Capture.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="305" src="http://3.bp.blogspot.com/-yFCI1ScU4KA/TsYPPYCdazI/AAAAAAAAACQ/dLtg9sNbptI/s400/Capture.PNG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;You should just hit the green Next button, as this feature is actually useful later on. Hit yes again to create a cache now, followed by ok. Close the annoying tips toolbar once the program starts, and walla! You're now ready to code your C programs, and compile them!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now Dev-C++ is called Dev-C++, but in actuality you'll only be writing C code for now as I highly recommend avoiding C++ until you're proficient with C. The reasons for this are simple: if you're proficient with C, you understand its limitations. Once you understand C's limitations and hardships, you can better utilize C++!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now for the fun part, writing some code! I'm simply going to just give you some code to copy and paste into a .c file. Before this you need to create a .c file, however. Go to, in Dev-C++, File &amp;gt;&amp;gt; New &amp;gt;&amp;gt; Source File. Save this file wherever you like, but make sure it is a .c file. Now paste the following code into your .c file:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;int main(void)&lt;br /&gt;
{&lt;br /&gt;&amp;nbsp; return 0;&lt;br /&gt;
}&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This code is the minimum requirement for any C program to be able to run. Basically it starts, and then ends. In order to compile this code into an exe, hit Execute &amp;gt;&amp;gt; Compile and Run (or f9). You'll see a quick flash of two boxes. One box was Dev-C++ saying something about your program compiling - who cares. The next box was bigger and darker, and was actually your program running within a Windows Console, also known as cmd. Remember what I said moments ago? This code starts, and then &lt;b&gt;ends&lt;/b&gt;. I'll teach you how to make your program pause until you hit enter later, but for now lets get into the basics of C code &lt;a href="http://en.wikipedia.org/wiki/Syntax_(programming_languages)"&gt;syntax&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In C whenever you write lines of code those lines of code are going to be turned into actions that a computer will follow. Whenever you write a &lt;a href="http://en.wikipedia.org/wiki/Statement_(computer_science)"&gt;statement&lt;/a&gt;&amp;nbsp;it must end with a semi-colon ;. The ; is letting the computer know that it has reached the end of a statement. This is important, otherwise the computer running your code could perhaps mix two statements together as a single statement, which would result in strange behavior.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Take a look back at your code you copy/pasted and lets check out what these different lines of code do. int main(void) is setting up the part of your code the program starts at. Every single time any program is ever started that was written in C, it starts at the line with main in it. This line is actually creating what is called the main function. I won't go into detail on exactly what a function is, as all you need to know for now is that this is the first code that is run when your program is started, and then the next line below it will be read then executed, and so on. The open curly bracket { is beginning of the main function, and the } is the end of the main function. For now you're going to write all your code within these two brackets. It's standard C syntax that code within two curly brackets is one tab farther to the right than the code outside of those two curly brackets, which is why the return 0; line is indented. The last thing to explain is this return 0; business. The return 0; line is actually the line of code that ends your program. Once your code is run and this line is executed, your program ends immediately. The 0 is actually a value returned to your operating system letting it know that the program ended successfully. If this sounds confusing, then don't worry about it - all you need to know is that when this line is reached your program ends.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A variable in C is a place in &lt;a href="http://en.wikipedia.org/wiki/Computer_memory"&gt;memory&lt;/a&gt;&amp;nbsp;that stores a value. There are different types of values that can be stored, and so there are different types of variables to hold those different types of values. I'd like to talk about Integers. For a more complete list of the different types of variables available in C, check out &lt;a href="http://en.wikipedia.org/wiki/C_data_types"&gt;this link&lt;/a&gt; on Wikipedia.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Lets start with integers. An integer in C is a whole number value ranging from&amp;nbsp;−2,147,483,648 to 2,147,483,647. In order to create a variable in C of the type integer, you would write:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div class="code"&gt;
&lt;code&gt;int myInteger;&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This code &lt;a href="http://en.wikipedia.org/wiki/Declaration_(computer_programming)"&gt;declares&lt;/a&gt;&amp;nbsp;a variable with the name myInteger, and specifies it's type as an integer with the word int. Don't forget your semi-colon to end the statement! Now what is the value of myInteger? Well currently it would have an undefined value since you have not &lt;a href="http://en.wikipedia.org/wiki/Initialization_(programming)"&gt;initialized&lt;/a&gt; it yet. To initialize myInteger with a value, you can write:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div class="code"&gt;
&lt;code&gt;myInteger = 10;&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code will place the value of 10 into myInteger. Now you can type the word myInteger somewhere in your code, and it will refer to the value inside of it, which is currently 10. The equal sign is actually an &lt;a href="http://en.wikipedia.org/wiki/Operator_(programming)"&gt;operator&lt;/a&gt;. An operator in C takes a value and operates on it somehow. The equal sign, called the assignment operator, takes whatever is to its right and places the value of it into whatever is on its left. This means that the following code is invalid:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div class="code"&gt;
&lt;code&gt;10 = myInteger;&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code is invalid because it would be trying to place the value inside of the variable myInteger into 10, which is simply a constant value. Some other operators you can use to manipulate values are: - + * / %. Here is what each one does:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;- The subtraction operator. Finds the difference between two values.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;+ The addition operator. Finds the sum of two values.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;* The multiplication operator. Finds the product of two values.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;/ The division operator. Finds the quotient of two values..&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;% The modulo operator. Finds the remainder of two divided values.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;What do you think the following code does:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div class="code"&gt;
&lt;code&gt;int val;&lt;br /&gt;
val = 5;&lt;br /&gt;
val = val + 5;&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code creates an integer variable called val, and then assigns the value of 5 to it in the next line. The last line then assigns the value of val + 5 to val. You can also do the following:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;int val1, val2, val3;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div class="code"&gt;
&lt;code&gt;val1 = 2;&lt;br /&gt;
val2 = 5;&lt;br /&gt;
val3 = val1 + val2;&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code creates three variables all at once within a single line. All three variables var1, var2, and var3 are all of the type integer, as denoted by the int &lt;a href="http://en.wikipedia.org/wiki/Keyword_(computer_programming)"&gt;keyword&lt;/a&gt;. val1 and val2 are&amp;nbsp;initialized&amp;nbsp;as 2 and 5 respectively, then val3 is assigned the value of the sum of val1 and val2, which is 7. Note that val1 and val2 remain unchanged, as the assignment operator doesn't change anything to the right side of it; it only assigns the value of what is on the right, to whatever is on its left.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now what if you want to actually see the value inside of a variable? Well then you need to modify your program slightly. Take a look at the following code:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;&lt;stdio.h&gt;&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;&amp;nbsp; printf("This is a string!");&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;/stdio.h&gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
The above code introduces a couple new things. printf is a line of code that is apart of the C standard library (a bunch of features for you to use as a coder), and it prints a string onto the screen. For now, just know that anything within C code that is within double quotes "" is a string. So if you place text within double quotes, within a printf statement like above, it will be considered a string and printf will place it onto the screen when you run your program. The other new thing is the #include &amp;lt;stdio.h&amp;gt; line. This line of code actually is telling the compiler to include a portion of the C standard library into your .C file, specifically the portion with the name stdio.h. stdio stands for Standard In Out, which is referring to input and output, which is why we need it to output a string onto the screen.&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
Try compiling and running this code. Sadly, your program opened and then closed very quickly! This is because your program prints the message "This is a string!" and then abruptly reaches the return 0; line and ends. To fix this, modify your code to look like so:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include&amp;nbsp;&lt;stdio.h&gt;&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;&amp;nbsp; printf("This is a string!");&lt;br /&gt;&amp;nbsp; getchar();&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;/stdio.h&gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The line getchar(); is a function from the C standard library in stdio.h. I won't go into the details, as that's out of the scope of this post, but just know that this line waits for the user to hit enter before executing the next line of code, thus letting you read what you output to the screen.&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to have your value show up on the screen, you need to modify your code slightly:&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include&amp;nbsp;&lt;stdio.h&gt;&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;&amp;nbsp; int i;&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; i = 5;&lt;br /&gt;&amp;nbsp; printf("%d", i);&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; getchar();&lt;br /&gt;&amp;nbsp; return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;/stdio.h&gt;&lt;/code&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This new version of the code uses a special character within the string in the printf statement. The %d line is actually saying to place the variable i into the spot in the string where %d is located. %d basically means "insert variable of type integer into this location". You can place more characters before and after the %d, but not in between the percent sign and the d character. This will let you see the value of the integer i on the screen! You can now try more fancy things, here's an example program to calculate the product of two variables and print it to the screen:&lt;/span&gt;&lt;/div&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div class="code"&gt;
&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;&lt;stdio.h&gt;&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;&amp;nbsp; int i, j, k;&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; i = 5;&lt;br /&gt;&amp;nbsp; j = 10;&lt;br /&gt;&amp;nbsp; k = i * j;&lt;br /&gt;&amp;nbsp; printf("%d", k);&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; getchar();&lt;br /&gt;&amp;nbsp; return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;/stdio.h&gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="font-family: 'Trebuchet MS', sans-serif;"&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;And there you have it! You've now learned how to write a simple program to do mathematical calculations on integers and display a single integer on the screen! Congradulations!&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3544888257220318833?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3544888257220318833/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/02/i-want-to-learn-programming-but-i-know.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3544888257220318833'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3544888257220318833'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/02/i-want-to-learn-programming-but-i-know.html' title='I Want to Learn Programming, but I Know Nothing!'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-yFCI1ScU4KA/TsYPPYCdazI/AAAAAAAAACQ/dLtg9sNbptI/s72-c/Capture.PNG' height='72' width='72'/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-1584395511726513631</id><published>2011-10-11T20:20:00.000-07:00</published><updated>2011-11-16T15:37:50.563-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reg'/><category scheme='http://www.blogger.com/atom/ns#' term='Batch'/><category scheme='http://www.blogger.com/atom/ns#' term='Script'/><category scheme='http://www.blogger.com/atom/ns#' term='Call'/><category scheme='http://www.blogger.com/atom/ns#' term='Function'/><category scheme='http://www.blogger.com/atom/ns#' term='PATH'/><category scheme='http://www.blogger.com/atom/ns#' term='File'/><title type='text'>Batch Files: Tips and Tricks</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I spent a lot of time one weekend creating a utility I call AutoGCC. GCC is a compiler that comes with Cygwin for Windows. When running this compiler and finishing assignments there are a whole lot of very annoying commands and actions to complete, that are highly repetitive. Perfect situation for constructing a utility! Here is a link to the program, which I update as I find bugs or think of additional features:&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;a href="http://www.mediafire.com/?v3q5f4ntvceq4wa"&gt;http://www.mediafire.com/?v3q5f4ntvceq4wa&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;There are a few tips and tricks I learned about batch programming that I'd like to share, and to document for my own future reference.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;You can use&amp;nbsp;parentheses&amp;nbsp;to link multiple lines so they are treated as a single line. This allows you to make nicely formatted if statements, like so:&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;if not exist %CD%\error_files\ (&lt;br /&gt;
mkdir %CD%\error_files\&lt;br /&gt;
)&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;The next trick that took me forever to understand the syntax of, is string manipulation. Take the variable &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;%PATH%&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; for example. It is a string. However what if I want to modify a portion of this string to my own liking? Look at the following:&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;%PATH:;c:\AutoGCC=%&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;%PATH:;c:\folder=%&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;That code actually searches for a matching sequence of characters of the sequence "&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;;c:\folder&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;", and will set that portion of the string &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;%PATH%&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; to nothing. This is basically deleting an element from the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;%PATH%&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; variable. You could also have done:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;%PATH:;c:\folder=;c:\LOL%&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Which would change the element &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;;c:\folder&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; to &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;;c:\LOL&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;There is also a nice way to edit the path (or any registry key) of a user's machine by&amp;nbsp;editing&amp;nbsp;the registry key for the system path! This will however require a restart to take effect. Here is the syntax I used in AutoGCC to modify my system's path (type &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;reg add /?&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; for information on the reg add command):&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%PATH:;c:\AutoGCC=%" /f&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;You can also use the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;CALL&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; command to launch a specific portion of your code as if it were its own batch file. This is really useful for making functions within your batch file. Take a look at the following code:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;
&lt;code&gt;if exist %file% (&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; if %var% NEQ 2 (&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; &amp;nbsp; call FUNCTION_1&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; )&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;)&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;:FUNCTION_1&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; do stuff...&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp; ...&lt;/code&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; ...&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp; goto EOF&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;The above code will go to the line labeled with &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;FUNCTION_1&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; if &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;%file%&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; exists, and then return just below the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;goto FUNCTION_1&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; statement once the line &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;goto EOF&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt; is reached.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;goto EOF&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&amp;nbsp;sends the control to the end of the file, though since&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;FUNCTION_1&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&amp;nbsp;was called by a call command, it is as if it is an emulated batch script of its own, and the emulation will close, not the original environment that called it.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;These few things are extremely useful for me when working on my batch scripts, and hopefully will be for you as well!&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-1584395511726513631?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/1584395511726513631/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/10/batch-files-tips-and-tricks.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1584395511726513631'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1584395511726513631'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/10/batch-files-tips-and-tricks.html' title='Batch Files: Tips and Tricks'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6555408604941812436</id><published>2011-09-06T22:35:00.001-07:00</published><updated>2011-12-02T12:50:17.502-08:00</updated><title type='text'>TL Secret</title><content type='html'>&lt;span class="Apple-style-span" style="color: #010408; font-family: 'Trebuchet MS', sans-serif; font-size: x-small;"&gt;First person to quote this sentence (back on TL at the Lessons thread), and the one directly after it, will win a free one hour lesson! Additionally whoever is the 100th poster in this thread (cmon, we can get that many posts!) will win a free one hour lesson as well!&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #010408; font-family: 'Trebuchet MS', sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #010408; font-family: 'Trebuchet MS', sans-serif; font-size: x-small;"&gt;Edit: This contest is long over :P&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6555408604941812436?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6555408604941812436/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2011/09/tl-secret.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6555408604941812436'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6555408604941812436'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2011/09/tl-secret.html' title='TL Secret'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-1212591645330591804</id><published>2010-11-15T22:02:00.000-08:00</published><updated>2010-11-15T22:08:27.055-08:00</updated><title type='text'>Status Update: New Book, C++ Primer 5th Edition</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Yay! I've finished&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;i&gt;&lt;u&gt;&lt;a href="http://www.mediafire.com/?yf0z4x44k240q1o"&gt;C Programming a Moder Approach Second Edition&lt;/a&gt;&lt;/u&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;and have moved onto the book &lt;i&gt;&lt;u&gt;&lt;a href="http://www.mediafire.com/?isy8b8smh0sib85"&gt;C++ Primer Plus 5th Edition&lt;/a&gt;&lt;/u&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;(don't download unless you own the book like I do)&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;!&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Things are fantastic ex dee. Sadly, I don't have an extremely informative post this time around as I was wrapping up the end of my C book, and the topics weren't too notable. Most of the topics were things like file I/O and standard library function reference, things that aren't really new concepts, and things that wouldn't be good topics for blog posts. Although, I did cover a chapter about low-level programming, and am considering writing a post about bitwise operators and also about the low-level programming chapter in that C book.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This post mainly serves as a status update.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-1212591645330591804?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/1212591645330591804/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/new-book-c-primer-5th-edition.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1212591645330591804'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1212591645330591804'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/new-book-c-primer-5th-edition.html' title='Status Update: New Book, C++ Primer 5th Edition'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-8364302728001010324</id><published>2010-11-13T02:45:00.000-08:00</published><updated>2010-11-13T02:45:26.961-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Malloc'/><category scheme='http://www.blogger.com/atom/ns#' term='Linked Lists'/><title type='text'>Memory Allocation and Linked Lists</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Memory Allocation&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Memory allocation allows the programmer to allocate a block of memory of a desired size, and then store data into the block, along with initializing a &lt;a href="http://cecilsunkure.blogspot.com/2010/11/pointers-basics.html"&gt;pointer&lt;/a&gt; pointing to this block of memory. In order to do so, I'll explain the use of &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;realloc()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;free()&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;,&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;and &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;calloc()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;will allocate a block of memory, although it will not initialize it, and returns a pointer to the block of memory allocated. The prototype of&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&amp;nbsp;looks like: &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;void malloc(size_t size);&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;size_t&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;is an integer type defined in the C library, which is an unsigned integer.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;So,&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;size&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;is just an integer and represents the amount of bytes to be allocated. Since&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;will return a pointer to a block of allocated memory, you need a pointer in order to make use of a call to&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, like so:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;//Memory allocation for a character array of n number of elements

char *p;
p = malloc(n + 1);

//you could also do:

p = (char *) malloc(n + 1);
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Since a character in C is one byte the, numerical argument for&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&amp;nbsp;ends up just being a simple integer. However, if you were allocating memory for any other type of data you would need to use the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;sizeof()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; function to determine how much memory to allocate.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;int *p;
p = malloc(sizeof(int));

//you could also do:

p = (char *) malloc(sizeof(int));
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Just as a reminder, if you ever need to figure out how many elements an array has, use the following idiom: &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;num_Elements = (sizeof(array) / sizeof(array[0])&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;)&lt;/span&gt;. That will take the size of all the elements divided by the size of a single element, giving you an integer resultant.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;realloc()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;will change the size of a previously allocated block of memory. The prototype for&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;realloc()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;is: &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;void *&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;realloc(void *ptr, size_t size);&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;ptr&lt;/span&gt;&amp;nbsp;must point to a block of memory that was previously used in a call to&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;or&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;realloc()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. The&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;size&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;parameter can be either larger or smaller than the original block.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;calloc()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;is similar to&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;malloc()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;in all ways except in that it&amp;nbsp;initializes&amp;nbsp;the bytes to 0 within the block.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;free()&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;is very easy to use; simply pass a pointer that points to a memory block we no longer need, and it will be available for reuse later on.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Linked Lists&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A linked list is a chain of &lt;a href="http://cecilsunkure.blogspot.com/2010/11/structures-unions-and-enumerations.html"&gt;structures&lt;/a&gt; (called nodes), where each node contains a pointer to the next node in the chain. The last node in the list would contain a null pointer. The advantages of using a linked list over an array, is that you can add in nodes anywhere in the list you want, and you can delete nodes anywhere you want. You can also create many types of data structures like graphs and trees using linked lists.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here are the barebones of a basic node:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct node {
  //... here would be the node's data
  struct node *next;
};
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;To access a member of a structure through a pointer to a structure there is a "shortcut" operator called the right arrow selection operator &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. The right arrow selection operator allows you to access the member of a value pointed by a pointer. The following are equal:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;(*node).data = 10;

//this is the same as:

node-&amp;gt;data = 10;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to add a node to the beginning of the linked list, all you need to do is create a temporary variable to hold your new node, assign the value of your new node to the variable, and modify your new node to point to the old first node. Since adding a new node to the beginning of a list is such a common task, I'll show a sample function on how to do so. In order for a function to directly add a new node to a list, you need to be able to pass a pointer to the function and make it point elsewhere. Since arguments are passed by value, you can't simply pass a pointer to a function, since the function will then only be able to modify a copy of the pointer passed to it. Instead, you would need to pass a pointer to a pointer, then, you can make the pointer point elsewhere.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;void add(struct node **list, int n)
{
  struct node *new_node;

  new_node = malloc(sizeof(struct node));
  if (new_node == NULL) //the macro NULL is defined in stdlib.h
  {
    printf("Error: Malloc failed to allocate necessary space.");
    exit(EXIT_FAILURE);
  }
  new_node-&amp;gt;value = n;
  new_node-&amp;gt;mext = *list;
  *list = new_node; //modifies the value pointed by list, which is a pointer
}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In the above example, &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;list&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; is a pointer to the first node in a linked list. Since we pass a pointer to a pointer (when we call this function we would use &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&amp;amp;first&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; in the first argument, being the address of the first node), we are able to update the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;list&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; pointer to point to our newly added node.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to search through a list, from beginning to end, you would usually use a for loop. The idiom is actually very simple. You scan the value and make your comparison on the first node, then make your iteration point to the next node:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;for (p = first; p != NULL; p -&amp;gt; next)
  ... //whatever code you want to loop&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Remember how I mentioned that the last node in the list contains a null pointer? The&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;NULL&lt;/span&gt;&amp;nbsp;macro defined in &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;stdlib.h&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; has the value of a &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; pointer, and as such the loop will stop on the last node of the list.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The idea behind deleting a node is to search for your desired deletion and make the previous node point to the node directly ahead of your node to be deleted. One method of doing so, is to keep a "trailing pointer". You scan through each node and keep a pointer to the node previously scanned, so that you can delete the current node and make the previous node point the next one in the list. Here is a sample function that searches for a node with a value inside of it (this value could be the node's ID, or whatever you are searching for) and deletes the node using &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;free()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct node *delete(struct node *list, val)
{
  struct node *current, *previous;

  for (current = list, previous = NULL;
       current != NULL &amp;amp;&amp;amp; current -&amp;gt; data_Member != val;
       previous = current, current = current -&amp;gt; next)
    ;

  if (current == NULL)  //val was not found, end of list reached
    return list
  if (previous == NULL) //val was in the first node
    list = list -&amp;gt; next;
  else                  //val was found within a node
    prev -&amp;gt; next;
  
  free(current);
  return list;
}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The for loop in this function actually just has a null statement, because all the actions are done in the iteration portion of the for loop; the previous node is set to the currently being scanned, and the currently being scanned is set to the next node. The for loop stops once the value you are searching for is found, or if the end of the list is found. The two if and the else statements catch all the possible outcomes of the for loop. If the first node in the list is going to be deleted (if &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;previous&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; equals &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;), you need to make your &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;list&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; pointer point to the second node.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-8364302728001010324?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/8364302728001010324/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/memory-allocation-and-linked-lists.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8364302728001010324'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8364302728001010324'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/memory-allocation-and-linked-lists.html' title='Memory Allocation and Linked Lists'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3730941749243363934</id><published>2010-11-10T17:53:00.000-08:00</published><updated>2010-11-10T21:15:51.550-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Enumerations'/><category scheme='http://www.blogger.com/atom/ns#' term='Unions'/><category scheme='http://www.blogger.com/atom/ns#' term='Structures'/><title type='text'>Structures Unions and Enumerations</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Structures&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Out of the three data types structures are the most important, so I'll start by going over them. Structures are similar to arrays in that they hold data. However, a structure's data is referenced by name rather than numerical index. The data inside structures is private; each new structure provides a new scope. Here is an example of how to declare a structure with a couple variables of that structure type:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct {
  int ident;
  char name[LEN + 1];
  int val;
} part1, part2;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;part1&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; and &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;part2&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; are now both the exact same data-type, and each have the same members as each other. A structure can also be&amp;nbsp;initialized&amp;nbsp;while it is declared, like so:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct {
  int ident;
  char name[LEN + 1];
  int val;
} part1 = {1, "Mobo", 200},
  part2 = {2, "PSU", 75};
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Alternatively you can use the designator operator "." to assign values to structure members, like so:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct {
  int ident;
  char name[LEN + 1];
  int val;
} part1 = {.ident = 1, .name = "Mobo", .val = 200},
  part2 = {.ident = 2, .name = "PSU", .val = 75};
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;It doesn't matter where in the initializer that a member is given a value if it is given a value by a designator. However, is there is no designator, then the value will be given to the corresponding member in the order from top to bottom of the structure, and from left to right of the initializer. LEN is just a constant defined in a preprocessor directive, and could be whatever the coder chose. You must add one to compensate for an end of line null character.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Structures of the same type can be copied with the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; operator. Although, you cannot use the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;==&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; or &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;!=&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; whether or not structures are of the same type.&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;part1&lt;/span&gt;&amp;nbsp;and&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;part2&lt;/span&gt;&amp;nbsp;from the above example are both the exact same structure type. However, in the following example,&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;part1&lt;/span&gt;&amp;nbsp;and&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;part2&lt;/span&gt;&amp;nbsp;are not the exact same, and cannot be used with the&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;=&lt;/span&gt;&amp;nbsp;operator:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct {
  int ident;
  char name[LEN + 1];
  int val;
} part1;

struct {
  int ident;
  char name[LEN + 1];
  int val;
} part2;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Since you can use the assignment operator with structures it is a slight surprise that arrays within structures will be copied from one to another. This can be useful for creating "dummy" structures that are used just for the purpose of copying one array to another.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;So far I've shown two examples of declaring structures without using a structure tag. Once you've created a tag for a structure, you can treat a structure as a data type, like so:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct pc_Hardware{
  int ident;
  char name[LEN + 1];
  int val;
};

struct pc_Hardware part1 =  {.ident = 1, .name = "Mobo", .val = 200};
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;It is also possible for a function to use a structure as an argument. However, using a structure as an argument can cause a lot of overhead. It's actually usually better to pass a pointer to a structure to a function, and then use the pointer to modify the members as needed. Functions can also return structures. Similarly, you might have a function return a pointer to a structure instead of an actual structure.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A structure can also be nested within another structure, as a data member. This is useful to create "categories" of members within a structure. Suppose you have a structure that holds data about computer hardware, and there are a total of four different brands of hardware. You could have each member of the structure represent a type of hardware, and within each structure you could hold information about the hardware brand.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Unions&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A union is similar to a structure in all ways, except in that the compiler will allocate enough space for only the largest of all the union members. This means that all members of a union will all share the same space in memory. Altering one member of a union will overwrite the data of all others. This means that only one member of a union can hold data at any given time. Unions are usually used as a means of saving space. In my last computer hardware example, a union could have been used in place of a structure for holding the names of the brand, as the hardware usually wouldn't be made by two different companies at once (as long as the brand name doesn't go over the &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;LEN&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; limit, which is just a constant that can be defined as any amount you want to specify).&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Arrays of unions can also be useful. Suppose you need an array that can hold either integers or floats. You can't simply create an array that can hold either, since an array must be universally one type. You can however create an array of unions rather easily. Consider this example:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;union {
  int i;
  float f;
} Number;

Number a[100];
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now the array a can hold in each element either a float or integer type. Here is how one could assign either a float or integer into the array:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;a[0].i = 1;
a[1].f = 10.01;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The biggest problem with using unions is that there is no way to tell which data member was last altered, and thus knowing which member actually holds a value. Often times programmers will nest a union within a structure, having the structure have one other data member. This other member within the structure will act as a "tag field" so that the programmer can keep track of which member holds a value.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Enumerations&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Enumerations are good for creating new definitions of data types that have only a few different possible values. An enumeration would be good for creating a boolean variable, or perhaps a variable to represent suit in a deck of cards. The benefits of using an enumeration over preprocessor directives for&amp;nbsp;defining&amp;nbsp;such things is that anyone reading your code can easily see all the possible variants of your variable, and see that each one is of the same type. Enumerations can increase readability and code cleanliness.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;enum suit { CLUBS, SPADES, DIAMONDS, HEARTS };
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above example shows how set up an enumeration for the different suits of a deck. This is much better than using &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; directives, in that it obeys C's scope rules; an enumeration declared within a function won't affect the rest of the program. The members of this enumeration can be used just the same as &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; directives.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The members of an enumeration are actually integers.&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;CLUBS&lt;/span&gt;&amp;nbsp;is&amp;nbsp;equivalent&amp;nbsp;to the integer 0, and &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;HEARTS&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; 3. One could use the suits defined in the above enumeration just as if &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;SPADES&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; were the integer 1, and so on. You can also specify exactly the integer amount that a member will equal, like so:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;enum suit { CLUBS, SPADES = 7, DIAMONDS, HEARTS = 20 } s;

s = CLUBS;
s++;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;CLUBS&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; would default to 0, although &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;DIAMONDS&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; would default to 8, which is one more than the previous member. s was assigned the value of&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;CLUBS&lt;/span&gt;&amp;nbsp;(zero), then in the next line was incremented to 1.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Enumerations are perfect creating "tag fields" for unions to determine which of the members of a union were last modified. Here is an example:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;struct {
  enum { INT, FLOAT } kind;
  union {
    int i;
    float f;
  } u;
} Number;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above struct can be used in our original union example where an array of unions was created. This structure has advantages in that the programmer will be able to tell whether or not each array element holds an integer or float with the "tag field" &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;kind&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Sources:&amp;nbsp;&lt;a href="http://www.mediafire.com/?yf0z4x44k240q1o"&gt;http://www.mediafire.com/?yf0z4x44k240q1o&lt;/a&gt; (particularly chapter 16)&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3730941749243363934?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3730941749243363934/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/structures-unions-and-enumerations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3730941749243363934'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3730941749243363934'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/structures-unions-and-enumerations.html' title='Structures Unions and Enumerations'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-8496191749505189470</id><published>2010-11-06T14:21:00.000-07:00</published><updated>2010-11-08T19:00:13.267-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Pointers'/><title type='text'>Pointers: Basics</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A pointer in C is a data type that holds the address to a specific block of memory within your computer's memory. The address in the pointer can be used to modify the contents thereof, or to cycle through other addresses in memory adjacent to such. It is also possible to use pointer arithmetic (addition and subtraction), though you cannot multiply or divide pointers. Take a look at the following diagram:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_BTFnSlLQHZQ/TNWwSrCBzJI/AAAAAAAAABQ/jd4e1yJwVPM/s1600/Pointer_P.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="132" src="http://2.bp.blogspot.com/_BTFnSlLQHZQ/TNWwSrCBzJI/AAAAAAAAABQ/jd4e1yJwVPM/s320/Pointer_P.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;P is a pointer that has been declared and&amp;nbsp;initialized&amp;nbsp;with the value of the address 1884. P points to the block in memory next to the blocks with addresses of 1883 and 1885. C requires that every pointer point to only a specific type. There are no restrictions on what type of referenced data a pointer may reference to; pointers can even point to pointers.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;code&gt;int *p;&lt;br /&gt;
double *q;&lt;br /&gt;
char *r;&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above shows three ways of declaring a pointer as three different types of data. The only difference between declaring a pointer and a variable, is that a pointer's identifier must be preceded by the asterisk symbol.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Address and Dereference Operators&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;There are two different operators that are very commonly used with pointers. The first, which you've seen in the form of multiplication and when declaring a pointer, is the asterisk &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;*&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, which is called the dereference operator (or also known as the indirection operator). You can translate the&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;*&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;literally into "the value pointed by". So, if we take &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; from our example above, and type&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;*P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, then&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;*P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;means "&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;the value pointed by&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;"&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;*P&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;would equal whatever value is within the block of memory 1884. Actually,&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;*P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;would be another alias for the value within the address 1884. This is because by modifying&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;*P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;we actually directly modify the value within the address 1884. Suppose&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;*P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;is an &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; value:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;code&gt;*p = 76;&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This line of code would change the value within the address 1884 into the integer 76.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The second operator is the Address operator &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;amp;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&amp;amp;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; can be translated literally into "the address of". This operator is particularly useful for assigning a value to a pointer, like so:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;code&gt;int val = 7;&lt;br /&gt;
int *p;&lt;br /&gt;
p = &amp;amp;val;&lt;br /&gt;
&lt;br /&gt;
//You could also do:&lt;br /&gt;
&lt;br /&gt;
int *p = &amp;amp;val;&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Usually you wouldn't know exactly what the address of val is before assigning it to a pointer, as it could be anywhere within memory while your program is running. What is important, is that you can assign the address to a pointer.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="font-family: 'Times New Roman'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Uses of Pointers&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Imagine you need a function that modifies a variable. You cannot simply pass the variable to the function, since you can only pass the value of your variable to the function. This is due to the fact that the data within functions is private. You could however, pass a pointer to the function, and then use the dereference operator to directly modify the value pointed by the address. Consider the following:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;

int clear(int *x)
{
  *x = 0;
}

int main(void)
{
  int a = 5;

  clear(a);

  printf("%d", a);

  return 0;
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;You can also have a function return a pointer, like the following:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;

int *max( int *a, int *b)
{
  if (*a &amp;gt; *b)
    return a;
  else
    return b;
}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This function, when given pointers to two integers, will return a pointer to whichever integer is larger.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Pointers and arrays are used together all the time. Say we initialize pointer &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; and make it point to the first element of &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;a[4]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;int a[4], *p;

p = &amp;amp;a[0];&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here is what we have just done graphically:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_BTFnSlLQHZQ/TNW5zM5ldsI/AAAAAAAAABU/s4q5PlxC2jo/s1600/Pointer_P1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="139" src="http://2.bp.blogspot.com/_BTFnSlLQHZQ/TNW5zM5ldsI/AAAAAAAAABU/s4q5PlxC2jo/s320/Pointer_P1.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; now points to the first element of array &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;a[]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. Suppose we do the following:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;*p = 7;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;a[0]&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;now equals 7. Here is what we have just done graphically:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_BTFnSlLQHZQ/TNW5zgxPXcI/AAAAAAAAABY/etzjcUiveEo/s1600/Pointer_P2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="139" src="http://4.bp.blogspot.com/_BTFnSlLQHZQ/TNW5zgxPXcI/AAAAAAAAABY/etzjcUiveEo/s320/Pointer_P2.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;So far this whole process doesn't seem too useful, but, where things start getting really useful is when you use pointer arithmetic to cycle through each element of the array using a pointer. C allows the following combinations of pointer&amp;nbsp;arithmetic, and only these combination:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;blockquote&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Adding an integer to a pointer&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Subtracting an integer from a pointer&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Subtracting one pointer from another pointer&lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;
&lt;div style="font-family: 'Times New Roman'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Adding integer &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; to pointer&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&amp;nbsp;will cause&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&amp;nbsp;to point&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;i&lt;/span&gt;&amp;nbsp;elements ahead from where&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&amp;nbsp;originally pointed to. Similarly, if&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;points to &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;a[x]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, then&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;+&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;i&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;points to &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;a[x + i]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; (assuming &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;a[x + i]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; even exists).&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;If&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;points to element&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;a[x]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, then&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;P&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;-&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;i&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;points to&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;a[x - i]&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;When one pointer is subtracted from another, the result is the distance in array elements from the two pointers.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;It is also valid to compare pointers with the comparisons ==, !=, &amp;lt;=, and &amp;gt;=. However, in order for these comparisons to actually have meaning the two pointers being compared would need to point within the same array.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Pointers are also good for processing arrays, since you can apply addition and subtraction upon pointers. Though one could just as easily use array subscripting for such a task, pointers can be faster and less resource intensive (depending on the compiler; some compilers have no efficiency&amp;nbsp;discrepancy&amp;nbsp;between array subscripting and array processing via pointers).&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;#define VAL 10

int a[VAL], *p;

sum = 0;
for (p = &amp;amp;a[0]; p &amp;lt; &amp;amp;a[VAL]; p++)
  sum += *p;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The above code fragment shows how to sum all elements of an array with &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. Note that this loop will not fire once&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;p&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;equals &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;a[VAL]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, due to the properties of a for loop, thus the address&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;a[VAL]&lt;/span&gt;&amp;nbsp;won't actually be analyzed and the program will not produce an error during compilation.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A very important thing to note, is that the name of an array can be used as a pointer to the first element of an array.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;int a[5];

*a = 7; //stores 7 in a[0]

*(a + 1) = 12; //stores 12 in a[1]&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In general, &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;a + i&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; is the same as &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&amp;amp;a[i]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. Also, &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;*(a + i)&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; is&amp;nbsp;equivalent&amp;nbsp;to &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;a[i]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. Also, the fact that an array name can serve as a pointer makes it easier to process them with for loops.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;for (p = &amp;amp;a[0]; p &amp;lt; &amp;amp;a[VAL]; p++)
  sum += *p;

//this is the same as the following:

for (p =a; p &amp;lt; a + VAL; p++)
  sum += *p;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;When passing an array to a function, the compiler passes a pointer to the first element in the array to the function. This is important to know.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Using all that I've explained so far, you can write loops to process both rows or columns of 2D arrays using pointers, like so (processing a row):&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;//loop that clears a row of a 2D array

int a[rows][cols], *p, row;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;row = x; //x is an integer that represents our selected row &lt;/pre&gt;&lt;pre&gt;for (p = a[row]; p &amp;lt; a[i] + cols; p++)
  *p = 0;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Processing a column isn't as simple, since a 2D array has the first array be an array of arrays, meaning it's an array of rows.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;//loop that clears a column of a 2D array

int a[rows][cols], (*p)[cols], col;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;col = x; //x is an integer that represents our selected column

for (p = a; p &amp;lt; a[rows]; p++)
  (*p)[col] = 0;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I have declared&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;p&lt;/span&gt;&amp;nbsp;to be a pointer to an array of integers, which will be used as a row in the loop. The&amp;nbsp;parentheses&amp;nbsp;are necessary to be around the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;*p&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, otherwise&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;p&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;would be an array of pointers rather than a pointer to an array. In the expression &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;p = a&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;,&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;a&lt;/span&gt;&amp;nbsp;is equal to the address of &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;a[0]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. We know this from recalling the earlier quote:&lt;/span&gt;&lt;br /&gt;
&lt;blockquote&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS', sans-serif; line-height: 20px;"&gt;In general,&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;a + i&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;is the same as&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&amp;amp;a[i]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. Also,&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;*(a + i)&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;is&amp;nbsp;equivalent&amp;nbsp;to&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;a[i]&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;/blockquote&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Sources for this post:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;*&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;a href="http://www.mediafire.com/?yf0z4x44k240q1o"&gt;http://www.mediafire.com/?yf0z4x44k240q1o&lt;/a&gt;&amp;nbsp;(particularly chapter 12)&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-8496191749505189470?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/8496191749505189470/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/pointers-basics.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8496191749505189470'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8496191749505189470'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/pointers-basics.html' title='Pointers: Basics'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_BTFnSlLQHZQ/TNWwSrCBzJI/AAAAAAAAABQ/jd4e1yJwVPM/s72-c/Pointer_P.png' height='72' width='72'/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-2987830448809690036</id><published>2010-11-02T16:14:00.000-07:00</published><updated>2010-11-02T16:14:58.489-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='2D Array'/><title type='text'>2D Array Practice</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In the book &lt;i&gt;&lt;u&gt;&lt;a href="http://www.mediafire.com/?yf0z4x44k240q1o"&gt;C Programming a Moder Approach Second Edition&lt;/a&gt;&lt;/u&gt;&lt;/i&gt;&amp;nbsp;there is a programming project at the end of chapter 8 that asks you to write a program that creates a randomized walk across a 10x10 field, where each step is shown as a letter from the alphabet, and each blank space is shown as a period. The end result should look something like this:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;code&gt;a . . . . . . . . .&lt;br /&gt;
b c . . . . . . . .&lt;br /&gt;
e d . . . p q r . .&lt;br /&gt;
f . . . . o . s . .&lt;br /&gt;
g h i j . n u t . .&lt;br /&gt;
. . . k l m v . . .&lt;br /&gt;
. . . . . . w . . .&lt;br /&gt;
. . . . z y x . . .&lt;br /&gt;
. . . . . . . . . .&lt;br /&gt;
. . . . . . . . . .&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to do this, you initialize a 2D array, fill it with periods, randomly choose a number between 0-3 to represent a direction to move, detect if the move was a valid move (and re-randomize the move if it wasn't), then make the move chosen. Each time a move is chosen, use a new letter from the alphabet.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Overall, this was extremely simple. If I were writing this program without such specific rules as the programming book gave me, I would have made the array 12x12 instead of 10x10. This would allow me to line the edges with a value other than ".", and I could use that value on the edges to detect collision. This is a lot simpler than detecting if it is the edge of the array, since I can't simply use &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;if&amp;nbsp;board[y][x + 1] != '.'&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, as a period can't exist out of the edge of the arrays.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I'll post the source code the most interesting part of the program:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;for (column = 0; column &amp;lt; 10; column++)
{
  for (row = 0; row &amp;lt; 10; row++)
  {
    printf("%c ", board[column][row]);
  }
  printf("\n");
}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This cycles through the first row, and prints out all the contents. It does this by adding one to &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;column&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; during each iteration, and printing out a piece of the board using the token&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;column&lt;/span&gt;&amp;nbsp;as a coordinate. The same thing happens with the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;row&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; token as well. Then, the next row, and the next row. Just before these two for loops I modify the current field and place a letter in wherever the current coordinates are, so that these two loops print out the move that just&amp;nbsp;occurred&amp;nbsp;as a letter.&lt;/span&gt;&lt;br /&gt;
&lt;code&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Overall I didn't learn anything new, but I still needed to write this program in order to get used to the syntax of C. &lt;a href="http://www.mediafire.com/?hjv52fpv28n5k2o"&gt;Here&lt;/a&gt; is the source and .exe for the full program.&lt;/span&gt;&lt;br /&gt;
&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-2987830448809690036?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/2987830448809690036/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/2d-array-practice.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/2987830448809690036'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/2987830448809690036'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/2d-array-practice.html' title='2D Array Practice'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-8267072609936978711</id><published>2010-11-01T18:36:00.000-07:00</published><updated>2010-11-01T18:39:00.112-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='PRNG'/><title type='text'>PRNGs (Pseudo-Random Number Generator)</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Computers cannot truly generate random numbers, as computers are simply mechanisms that react to actions that are enacted upon them. In order to compensate for this, to generate a random number computers use a PRNG (pseudo random number generator). PRNGs can generate &lt;i&gt;seemingly&lt;/i&gt;&amp;nbsp;random numbers rather effectively.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;One way to generate a random number in C is to use the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;rand()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; function, which is lies within the standard library of C. Here is an example:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="code"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt; /* included for printf */&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* included for rand */&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;int i;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;for (i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf("%i\n", rand());&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;return 0;&lt;br /&gt;
}&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Depending on your compiler, the output of this program will output ten numbers. However, this program will always output the same ten numbers in the same order. I've learned that the &lt;/span&gt;&lt;a href="http://gcc.gnu.org/" style="font-family: 'Trebuchet MS', sans-serif;"&gt;GCC&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; compiler will have the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;rand()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; function return a value with an upper bound of&amp;nbsp;2,147,483,647 (2&lt;sup&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;32&lt;/span&gt;&lt;/sup&gt;-1), compared to upper bounds of&amp;nbsp;32,767 (2&lt;sup&gt;16&lt;/sup&gt;-1) from most other compilers.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to have this program output different numbers each time it is run, you need to do what is called seeding the PRNG. Seeding will affect the sequence in which the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;rand()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; function begins outputting numbers. In order to seed the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;rand()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; function, you use &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;srand()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;. Here is an example:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt; /* included for printf */&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* included for rand */&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;int i;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp;&amp;nbsp;srand(1)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;for (i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf("%i\n", rand());&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;return 0;&lt;br /&gt;
}&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;This seeded the PRNG with the integer 1, and will now output ten different numbers than the last program. Although, this still doesn't solve our problem; how do we randomly seed the PRNG? In order to do so, you can use the &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;time()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; function. The &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;time()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; function will return the number of seconds elapsed since Jan 1st 1970. This is the method of randomly seeding that DigiPen has shown their Freshman incoming students. Example:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt; /* included for printf */&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* included for rand */&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;int i;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp;&amp;nbsp;srand(time(0));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;for (i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf("%i\n", rand());&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;return 0;&lt;br /&gt;
}&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now this program will produce a different set of numbers every time it is run. Although, what if you wanted to produce a random number within a specific range? You could use the modulo operator, like so:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt; /* included for printf */&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt; /* included for rand */&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;int i;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&amp;nbsp;&amp;nbsp;srand(time(0));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;for (i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf("%i\n", rand() % 10 + 1); //Random int from 1-10&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;return 0;&lt;br /&gt;
}&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Although I've been told this is error-prone and tedious, and it is much preferred to create your own wrapper function around &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;rand()&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;int randomInt(int low, int high)&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;nbsp;&amp;nbsp;return (rand() % (high - low + 1) + low);&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-8267072609936978711?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/8267072609936978711/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/prngs-psuedo-random-number-generator.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8267072609936978711'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8267072609936978711'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/prngs-psuedo-random-number-generator.html' title='PRNGs (Pseudo-Random Number Generator)'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-5361873635695894851</id><published>2010-11-01T16:35:00.000-07:00</published><updated>2010-11-14T20:12:48.553-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Cygwin'/><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='GCC'/><category scheme='http://www.blogger.com/atom/ns#' term='DigiPen'/><title type='text'>Installing Cygwin and Practicing C</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Yesterday's time was mainly spent installing Cygwin and running through selected&amp;nbsp;exercises&amp;nbsp;from the book &lt;a href="http://www.mediafire.com/?yf0z4x44k240q1o" style="font-style: italic; text-decoration: underline;"&gt;C Programming a Modern Approach Second Edition&lt;/a&gt;&amp;nbsp;(yep, I have a link for you this time!). The exercises I selected to do were all selected for DigiPen Freshman to do. If you're also interested in doing these, I have them all here:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
Homework Assignments&lt;br /&gt;
&lt;ul&gt;&lt;li&gt; &lt;br /&gt;
&lt;/li&gt;
&lt;table border="1" cellpadding="10" cellspacing="0"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;Assignment&lt;/th&gt;&lt;th&gt;You should have reviewed the material by class on this date.&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1. Read Chapter one in the textbook.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2. Make sure you can read from and write to your submit folder, if you plan on using it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3. Read &lt;a href="http://academicalliance.digipen.edu/msdnaa/"&gt;this page&lt;/a&gt; about the MSDNAA&lt;/td&gt;&lt;td&gt;Thursday, September 9, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Read chapters 1 and 2 from &lt;a href="http://www.mediafire.com/?0k3f77xq4j2k0ha"&gt;An Introduction to GCC&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Friday, September 17, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 2: Exercises #1 - 10&lt;/td&gt;&lt;td&gt;Thursday, September 23, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 3: Exercises #1, #2, #4, #5&lt;/td&gt;&lt;td&gt;Friday, September 24, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 4: Exercises #1, #9, #10, #11, #12, #14, #15&lt;/td&gt;&lt;td&gt;Monday, October 4, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 5: Exercises #1, #2, #3, #5, #6, #7, #8, #10, #11&lt;/td&gt;&lt;td&gt;Friday, October 8, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 6: Exercises #1, #2, #3, #4, #5, #8, #11, #14&lt;/td&gt;&lt;td&gt;Monday, October 11, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Start reading the &lt;a href="http://www.mediafire.com/?uzfitsidco48q54"&gt;Style Guide for CS120&lt;/a&gt; We haven't covered all of the topics yet, but you can read about the things we've learned. An example of &lt;a href="http://www.perforce.com/beautifulcode/diffMerge_code.html"&gt; consistent formatting style&lt;/a&gt;. This does not follow the CS120 style requirements, but it's an example of how one professional company does things. They also talk about the &lt;a href="http://www.perforce.com/perforce/papers/prettycode.html"&gt;&lt;br /&gt;
&lt;br /&gt;
Seven Pillars of Pretty Code&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;/td&gt;&lt;td&gt;N/A&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 9: Exercises #1, #2, #8, #9&lt;/td&gt;&lt;td&gt;Monday, October 18, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Read pages 686-687 in the textbook&lt;/td&gt;&lt;td&gt;Tuesday, October 19, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 8: Exercises #1, #2, #3, #5, #10, #11&lt;/td&gt;&lt;td&gt;Friday, October 29, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Chapter 7: Exercises #2, #3, #4, #5, #6, #9 through 15&lt;/td&gt;&lt;td&gt;Wednesday, Nov 3, 2010.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/ul&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;So far, I've completed most of the exercises all the way down to the chapter five column. Here's the source and .exe's of all I've worked on yesterday (these are actually the programming projects from the book and are not the exercises listed above):&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;a href="http://www.mediafire.com/?f40uv0sz0w9fdmt"&gt;http://www.mediafire.com/?f40uv0sz0w9fdmt&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Aside from all that code I just linked you all to, I also spent a great deal of time downloading and installing &lt;a href="http://www.cygwin.com/"&gt;Cygwin&lt;/a&gt;. For those of you who don't know,&amp;nbsp;Cygwin&amp;nbsp;is Linux-like environment for Windows, and allows you to use the GCC compiler. DigiPen Freshman use it, so, I'm going to use it as well. So far I actually really enjoy using the GCC compiler, as it has such useful warnings and error reporting messages. Although, if you do want to install&amp;nbsp;Cygwin&amp;nbsp;just realize that's it is a bitch to get up and running if it's your first time doing so. Luckily I was able to read some of my friend's installation notes from their teacher in order to get it up and running. In the future I can write a short post on installing&amp;nbsp;Cygwin, and have a nice .iso torrent for installing the precise version of Cygwin that DigiPen provided their Freshman this year. Right now though my Demonoid account is new and so I can't currently upload any new torrents. Update: Demonoid account can finally upload, torrent link here:&amp;nbsp;&lt;/span&gt;&lt;a href="http://www.demonoid.com/files/details/2453265/35110160/"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;http://www.demonoid.com/files/details/2453265/35110160/&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to compile a source file with&amp;nbsp;Cygwin&amp;nbsp;you have to type out a long string with a bunch of commands. After some time of re-writing the annoying phrase "&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;gcc -mno-&lt;span class="Apple-style-span" style="white-space: normal;"&gt;Cygwin&lt;/span&gt;-Wall hello.c -o hello-no_&lt;span class="Apple-style-span" style="white-space: normal;"&gt;Cygwin&lt;/span&gt;.exe", my roommate let me know that by pressing the up arrow key, you can copy the entire last command straight into the prompt, and then hit enter.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Next I plan to read more of&amp;nbsp;&lt;/span&gt;&lt;a href="http://www.mediafire.com/?yf0z4x44k240q1o" style="font-family: 'Trebuchet MS', sans-serif; font-style: italic; text-decoration: underline;"&gt;C Programming a Modern Approach Second Edition&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;, and finish up more&amp;nbsp;exercises&amp;nbsp;that I have listed above, as well as finish up the programming projects at the end of each chapter -particularly the ones that seem difficult.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;There is one last thing I'd like to share, and it is a link to a very &lt;a href="http://lbrandy.com/blog/2010/11/using-genetic-algorithms-to-find-starcraft-2-build-orders/"&gt;interesting article&lt;/a&gt; about a program that can find optimized build orders for the SCII Zerg race. I won't comment too much about this, as it's a bit off-topic for this blog, but, I just wanted to say that the whole field of AI and learning simulations is a very, very interesting field. I would absolutely love a career in something similar, but, I have a long time before deciding what to specialize in (game programming wise).&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-5361873635695894851?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/5361873635695894851/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/installing-cygwin-and-practicing-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/5361873635695894851'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/5361873635695894851'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/11/installing-cygwin-and-practicing-c.html' title='Installing Cygwin and Practicing C'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-7327135608720113702</id><published>2010-10-31T15:42:00.000-07:00</published><updated>2010-11-01T17:36:38.317-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Blog Revives!</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Today I've decided to revive my blog, as I'm regularly studying computer science once more.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I had a hiccup in studying due to an issue with attending college, and I will be attending this next year. As such, I'll be doing independent studies of Math, Computer Science, and Game Design.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I currently live near the &lt;a href="http://digipen/"&gt;DigiPen&lt;/a&gt;&amp;nbsp;campus, and as such have access to both their library and students. I even have access to the students' assignments and some of their class resources. Since I'll be attending DigiPen next year I've decided to switch solely to C and C++ (starting with C of course). I do however want to finish one final Python project, which is my Mini RPG game. I want to finish this, because people actually seem interested in it, as seen &lt;a href="http://www.staredit.net/topic/12719/0/"&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I currently have a nice system for displaying two dimensional rooms into the windows console, as well as a battle system. I have a friend who added a teleport spell into the source file, which thrusts you up to three spaces. &lt;a href="http://www.staredit.net/265860/"&gt;Here&lt;/a&gt; is a link which shows the instructions for playing the game, as well as a lot more information about the game. Here is a download link for the current .exe at this time:&amp;nbsp;&lt;a href="http://www.mediafire.com/?h1yvm61xr5a9tfr"&gt;http://www.mediafire.com/?h1yvm61xr5a9tfr&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="code"&gt;&lt;pre&gt;Action Key:
w - Move up
a - Move Left
s - Move down
d - Move Right
e - Action button. Use this while standing next to something.
i - Inventory. Read all the text in the inventory, or you may miss an important operation (like how to discard an item, or equip an item).

Character key:
O   - This is your character.
/   - This is a wall. Your character cannot move over it
{"} - This is a treasure chest.
{_} - This is an unopened treasure chest.
[/] - This is a door.
S   - This is the store.
&amp;amp;   - This is CecilSunkure!!&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here are three links to resources that I plan to utilize in order to create a console based game like these &lt;a href="https://www.digipen.edu/?id=1170"&gt;DigiPen Freshman games&lt;/a&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms682010(VS.85).aspx"&gt;http://msdn.microsoft.com/en-us/library/ms682010(VS.85).aspx&lt;/a&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"&gt;&lt;a href="http://www.benryves.com/tutorials/winconsole/"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;http://www.benryves.com/tutorials/winconsole/&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;a href="http://www.tomshardware.com/forum/65918-13-technique-fast-win32-console-drawing"&gt;http://www.tomshardware.com/forum/65918-13-technique-fast-win32-console-drawing&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="line-height: normal; white-space: normal;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I am also currently reading through the book C Programming A Modern Approach Second Edition. This book is used by DigiPen Freshman, and so I'll be going through the entire thing before starting a console application.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="line-height: normal; white-space: normal;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="line-height: normal; white-space: normal;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="line-height: 15px; white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I plan to be updating this blog regularly with whatever I learn, with a goal of archiving a path one can take to become a professional programmer from start to finish, as well as the goal of solidifying whatever I learn. You know how the saying goes:&amp;nbsp;Docendo Discimus (Latin for "by teaching, we learn").&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-7327135608720113702?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/7327135608720113702/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/10/blog-revives.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/7327135608720113702'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/7327135608720113702'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/10/blog-revives.html' title='Blog Revives!'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-8804221304392239525</id><published>2010-07-10T12:18:00.000-07:00</published><updated>2010-09-03T18:06:32.519-07:00</updated><title type='text'>Blog Ends</title><content type='html'>And this is the end of the epic blog that once was.&lt;br /&gt;
&lt;br /&gt;
I'm unable to attend DigiPen due to my parents just recently deciding that they will not be helping me by taking out the Parent Plus loan. Since I have no rich or reliable relatives, I have no cosigner for private loans either.&lt;br /&gt;
&lt;br /&gt;
I've already taken care of my living&amp;nbsp;arrangement, and I will be moving into the Redmond area to study art on my own. This blog may just turn into my own art blog, who knows. The main point is this: There will be no more programming for a very long time on this blog.&lt;br /&gt;
&lt;br /&gt;
If you wish for a more in-depth description of my situation, or wish to ask me anything, my email is CecilSunkure@hotmail.com&lt;br /&gt;
&lt;br /&gt;
Wish me luck.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-8804221304392239525?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/8804221304392239525/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/07/blog-ends.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8804221304392239525'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8804221304392239525'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/07/blog-ends.html' title='Blog Ends'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-8372687477493344910</id><published>2010-06-22T14:08:00.000-07:00</published><updated>2010-06-22T14:08:48.549-07:00</updated><title type='text'>Absence</title><content type='html'>Well, I've been grounded.&lt;br /&gt;
&lt;br /&gt;
I shall resume posting once DigiPen starts up, or once I gain reliable computer access once again. I apologize.&lt;br /&gt;
&lt;br /&gt;
News:&lt;br /&gt;
-I have no time to get the Django powered site up and running before school starts, so I have cancelled those plans.&lt;br /&gt;
-I won't be coding in Python anymore, unless it is to finish up that Mini RPG I started a long time ago.&lt;br /&gt;
&lt;br /&gt;
Wish me luck.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-8372687477493344910?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/8372687477493344910/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/06/absence.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8372687477493344910'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/8372687477493344910'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/06/absence.html' title='Absence'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3748152728449841089</id><published>2010-05-15T21:54:00.000-07:00</published><updated>2010-05-15T22:04:33.043-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python Django'/><title type='text'>Django: DjangoBook</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I believe I'm about due for a status check! I've been hammering away at this:&amp;nbsp;&lt;a href="http://djangobook.com/"&gt;http://djangobook.com/&lt;/a&gt;. That there is a nice book on how to use the web development framework &lt;a href="http://danjgoproject.org/"&gt;Django&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Django was coded entirely in Python, and doesn't require any third party libraries for use. Django is constantly maintained by a slew of experienced coders, and is rather well documented. I've gone through 8 chapters of this book, plus the four tutorial chapters at &lt;a href="http://djangoproject.org/"&gt;this&lt;/a&gt; site in four days. I've been spending my time coding away on my own local development server -which is shipped with a Django installment. I've yet to deploy my code onto the site; chapter 12 is about that and I'm currently on chapter nine of the &lt;a href="http://djangobook.com/"&gt;DjangoBook&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;At this point in time I've coded some pages which include form submission and validation, requesting a&amp;nbsp;viewer's&amp;nbsp;metadata and displaying it in a table format, querying/modifying database information, searching through a database, the admin pages shipped with Django, and more things I can't think of off the top of my head.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Django follows a &lt;a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller"&gt;MVC&lt;/a&gt;&amp;nbsp;design pattern where the Content, Backend, and Page Layout are all separated into isolated sections. The point of this is to allow for loose coupling between the different functions of the site, where almost nothing is hard-coded. This allows for copying//pasting pre-existing files into new locations, and for developers with different&amp;nbsp;skill-sets&amp;nbsp;to work collaboratively and efficiently. This works out great between me and our layout guy, as neither of us are at all good at doing each others' jobs. Using Django terms, MVC could be called MTV. MTV stands for Models Templates and Views. Models = site contest, Templates = page layouts, and Views = backend site logic (controlled via python modules, woot!). It's great to be able to keep using Python all the while learning a web development schema. For those of you who think Python is worthless, I Lol at you and your naivety.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;And yes, all the backend logic is safely hidden away from the page source as to not allow reverse engineering of the site's workings.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;At this rate, I should have a decent amount of functioning pages up for my team to see my current progress in a couple days. I also spoke with a few DigiPen students who are entering their Freshman year as well, and they seemed pretty enthusiastic about using the site's forums. All is going well.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3748152728449841089?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3748152728449841089/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/django-djangobook.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3748152728449841089'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3748152728449841089'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/django-djangobook.html' title='Django: DjangoBook'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6081053812413837562</id><published>2010-05-13T19:33:00.000-07:00</published><updated>2010-05-15T21:59:41.946-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Linear Alegreba'/><title type='text'>Linear Algebra: Rotation Matrices</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A day or so ago I looked up the topic of using rotation matrices to modify the angle of a vector by an arbitrary amount. I visited none other than the &lt;a href="http://en.wikipedia.org/wiki/Rotation_matrix"&gt;WikiPedia page for rotation matrices&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The idea behind rotation matrices is to solve for the x and y coordinates after the rotation by using two separate equations. Here is a rotation matrix for 2 dimensions:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/math/b/5/7/b5726960cb1afa1f40c3ca9a2c83aabf.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://upload.wikimedia.org/math/b/5/7/b5726960cb1afa1f40c3ca9a2c83aabf.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;That matrix will rotate the 2D plane counterclockwise by angle&amp;nbsp;&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;θ&lt;/i&gt;&lt;/span&gt;. Using this knowledge, the new x and y coordinates for a point on a rotated vector would be solved for using these two equations:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/math/1/d/9/1d9a9eb59b0f60941af8aa9b09aa5a41.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://upload.wikimedia.org/math/1/d/9/1d9a9eb59b0f60941af8aa9b09aa5a41.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/math/6/7/9/6794b4de87caedcc56ae6b759bb33c88.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://upload.wikimedia.org/math/6/7/9/6794b4de87caedcc56ae6b759bb33c88.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Very simple! So here is an example problem: Say you have vector &lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;&lt;u&gt;AB&lt;/u&gt;.&amp;nbsp;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;i&gt;&lt;u&gt;AB&lt;/u&gt;&amp;nbsp;&lt;/i&gt;= (2, 3).&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Position_vector.svg/220px-Position_vector.svg.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Position_vector.svg/220px-Position_vector.svg.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;To solve for a rotated counterclockwise version of this vector you input your desired angle of rotation in for&amp;nbsp;&lt;i&gt;θ&lt;span class="Apple-style-span" style="font-style: normal;"&gt;, 2 for x and 3 for y. This rotation matrix rotates a 2D plane clockwise:&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/math/a/7/9/a7918c7803e622179b2f733c24a10c5a.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;img border="0" src="http://upload.wikimedia.org/math/a/7/9/a7918c7803e622179b2f733c24a10c5a.png" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Rotation in 3 dimensions is basically the same process. All that is different is the z coordinate and modified rotation matrices. You simply solve for each individual coordinate with the rotation matrices for the z, x, and y axis. These three rotation matrices and more are found at the&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="line-height: normal;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Rotation_matrix"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;WikiPedia page for rotation matrices&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6081053812413837562?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6081053812413837562/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/linear-algebra-rotation-matrices.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6081053812413837562'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6081053812413837562'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/linear-algebra-rotation-matrices.html' title='Linear Algebra: Rotation Matrices'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-7322193739500925728</id><published>2010-05-13T19:08:00.000-07:00</published><updated>2010-11-01T17:03:13.781-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Django'/><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Mini RPG and Future Plans</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I've decided to try and start a project with a couple good friends; we are going to construct a website. One is the bill payer and public relations, the other is layout design/client side coding, and I am the server side coder. I plan to use a&amp;nbsp;development&amp;nbsp;framework called &lt;a href="http://djangoproject.com/"&gt;Django&lt;/a&gt;. The website we are creating is planned to have a focus on StarCraft 1 and 2 map making. We want forums, a download database, a wiki, and a shoutbox later down the road. Our layout guy is rather talented, and the owner is pretty good at creating images. Things look bright for us three. We also want a focus on art development and coding -this would be great for DigiPen students as well. 3D modelling is a great option to have for SC2 mapping, and as is coding in general. It would be perfect if I can get my future DigiPen colleagues to use our site as a forum to communicate to each other with.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
The best thing about Django is that it allows full support of Python modules to be used for the site's logic, and makes for rather fast development. Also, I already am familiar with python, and all the while learning to construct this website I will enhance my coding skills.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;We currently have a domain set up at &lt;a href="http://mappingdynasty.org/"&gt;http://mappingdynasty.org&lt;/a&gt;. When this post was created the only thing on the site was the index.html page, which just holds some hmtl lines to display a picture that our owner created. Go ahead and check it out ^.^&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Today I also worked on my Mini RPG. I added about 75 lines of code. I added a monster class, almost finished the battle function, modified and updated the main loop, and made some minor changes to the character class to allow for holding weapons and having hit points. I am really loving object orientation for this little program; things are super organized and simplified. I can't imagine creating this program without using any classes D:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I haven't had time to actually test the source file for bugs and errors, but I believe it is working.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here is a link to my source:&amp;nbsp;&lt;a href="http://www.mediafire.com/download.php?mgmzmwnnwdg"&gt;http://www.mediafire.com/download.php?mgmzmwnnwdg&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-7322193739500925728?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/7322193739500925728/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/mini-rpg-and-future-plans.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/7322193739500925728'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/7322193739500925728'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/mini-rpg-and-future-plans.html' title='Mini RPG and Future Plans'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-574754844310586871</id><published>2010-05-11T15:31:00.000-07:00</published><updated>2010-05-11T19:23:02.704-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Linear Alegreba'/><title type='text'>Linear Algebra: Basics</title><content type='html'>&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;Today with my math teacher I went over some of the basics of linear algebra. The two topics I went over included solving for the intersection of two lines in 3D space, and finding the angle between two vectors. The rest of the mathematical topics I'll try to cover in the near future include: finding a vector orthogonal to two other vectors in 3D space; finding the time T during the intersection of ray between a circle of sphere; using a matrix to rotate a vector by an arbitrary angle. I found these topics, as well as an entire survival guide for DigiPen students, at this site &lt;a href="http://sushiwithyou.com/"&gt;here&lt;/a&gt;. Hopefully by explaining the math I went over today I can solidify my own understanding.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;First off: solving for the intersection of two lines in 3D space. I know how to do this in matrix form, as it is easiest and simplest in matrix form. Here is a matrix of the x y and z coordinates for a point in 3D space: [x, y, z]. An equation for a vector in 3D space consists of a point on a line, and the direction vector of a line multiplied by a magnitude. This magnitude represents the length of the line, whereas the timestep, or t, represents a constant.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;Here is an equation for a line in 3D space: L1 = [2, 4, -1] + t[-1, 2, 3]. I just chose these numbers at random because they really don't matter. The first matrix is a point on the line. The second matrix is the rise, run, and the z equivalent to rise or run (just like two dimensional lines). You need a point on the line, otherwise the direction vector (rise//run//zrun) could sit anywhere in space. Without the direction vector for your line, you line could be facing in any direction as long as it sits on your point. The t is the magnitude of the direction vector, and these could be used as something to define the distance something traveled over time of t. t is just a constant.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;In order to solve for the intersection of two lines, I'll quickly show the process with some variables. Here are the two lines: L1 = [a, b, c] + t[d, e, f]; L2 = [g, h, i] + r[j, k, l]. Now since these two equations each represent a line, the point of intersection is going to be a point that can be used to satisfy either of the equations. You just set both of the equations equal to each other, one variable at a time (x, y and z) and solve for each one. To solve for the x coordinate of the intersection you would use a + td = g + rj. You do this for variable a through c corresponding to d through f. Then using substitution, if need be, you can solve for t and then solve for the rest of the variables, thus getting a final matrix of [x, y, z].&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;The second thing I learned today was finding the angle between two vectors. Luckily, you only need the direction vectors of the line equations. This makes sense because no matter where the lines are in space, they will have the same angle of intersection as long as the direction of the lines face in stays constant. To do this, you use the equation of:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/math/f/5/8/f587371bc1b7d0819c76900eda99656a.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://upload.wikimedia.org/math/f/5/8/f587371bc1b7d0819c76900eda99656a.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;Theta, the zero thingy on the left, is the angle you are solving for. a and b both represent matrices that represent direction vectors, like the direction vectors in the line equations earlier in this post. Arccos is cos^-1. The a and b on the top half of the right side of the equation is pronounced as a dot b. The dot is the operator for the &lt;a href="http://en.wikipedia.org/wiki/Dot_product"&gt;dot product&lt;/a&gt;. The dot product is used to find the scalar projection of a onto b, and vise versa. I honestly don't fully understand what exactly the dot product does yet (read last paragraph, I understand it now), but for now I just need it for equations like this one, and it returns a scalar value. To use the dot product on two 1x3 matrices, you would do this: [a, b, c] dot [d, e, f] = ((a x d) + (b x e) + (c x f). The |a| represents the magnitude of a, which is the length of a. If the direction vector of a line is representing velocity vectors, then the magnitude of a would be the speed of the direction vector. To find the magnitude of a 1x3 matrix you do this: |M| = |[a, b, c]| = sqrt(a^(2) + &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;b^(2) + &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;c^(2)). Does that look familiar? It should; it's basically the&amp;nbsp;Pythagorean&amp;nbsp;Theorem in 3D. It takes the rise, run, and zrun and converts the three into a length value, just like the Pythagorean Theorem does with two lines, except this is with three.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;Now once you find a dot b, and magnitude of a times magnitude of b, you then divide a dot by &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;magnitude of a times magnitude of b, then arccos that value which results in your angle!&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;Dot product explained: Okay! I so I did a bit of research and asked a couple people some questions and now I understand what the value returned by the dot product does. It projects a vector onto another vector and returns the length. A dot B also equals B dot A, which makes sense because multiplication itself is&amp;nbsp;commutative, and the formula for the dot product is just multiplication of three values. Here is a picture to help visualize this:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://www.mvps.org/directx/articles/math/dot/dot2.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://www.mvps.org/directx/articles/math/dot/dot2.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;The blue line would be the dot product of A and B. This is very useful for collision in programming, and transforming vectors from one grid space to another. Here is a good example of using the dot product for 2D collision detection of convex polygons:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_BTFnSlLQHZQ/S-oMw-bm2gI/AAAAAAAAAAc/K0h51Mx_rFg/s1600/Diagram2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_BTFnSlLQHZQ/S-oMw-bm2gI/AAAAAAAAAAc/K0h51Mx_rFg/s320/Diagram2.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;The red and blue are both one dimensional projections of the objects onto a line. The dotted line is perpendicular one side length of one of the objects. In the diagram is looks like it is perpendicular to both, which is fine, but it is important to understand that the dotted line is normal to one of the sides of one of the polygons. Once you find a dotted line that is perpendicular to a side of one of the shapes, you use the dot product on a two dimensional matrix and project both of the shapes onto the normal to the dotted line. You can then compare the two projections to see if they overlap. If the two projections overlap, you then try the entire process over for a different side of one of the polygons. Once you try this algorithm over each of the sides of each object and no collision vector was detected (a collision vector would be the length of overlap formed by overlapping projections) in at least one of the iterations, then the two objects are not colliding with one another.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="font-size: small;"&gt;Sources:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;a href="http://www.mvps.org/directx/articles/math/dot/index.htm"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;http://www.mvps.org/directx/articles/math/dot/index.htm&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://en.wikipedia.org/wiki/Dot_product"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;http://en.wikipedia.org/wiki/Dot_product&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.gamedev.net/reference/programming/features/verletPhys/page2.asp"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;http://www.gamedev.net/reference/programming/features/verletPhys/page2.asp&lt;/span&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-574754844310586871?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/574754844310586871/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/linear-algebra-basics.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/574754844310586871'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/574754844310586871'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/linear-algebra-basics.html' title='Linear Algebra: Basics'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_BTFnSlLQHZQ/S-oMw-bm2gI/AAAAAAAAAAc/K0h51Mx_rFg/s72-c/Diagram2.PNG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-1108176967055676804</id><published>2010-05-10T09:49:00.000-07:00</published><updated>2010-05-10T19:03:46.346-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game Design'/><title type='text'>Game Design: Positive and Negative Feedback; Flow Control</title><content type='html'>&amp;nbsp;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;As I've said a few times in the &lt;a href="http://cecilsunkure.blogspot.com/2010/03/actionscript-3-flash.html"&gt;past&lt;/a&gt;, I have been&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; reading &lt;a href="http://rs100.rapidshare.com/files/87719192/Rules.of.Play.Game.Design.Fundamentals.rar"&gt;Rules of Play: Game Design Fundamentals&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;. Lately I have also been applying what I've learned into judging StarCraft mapping contests, like &lt;a href="http://www.staredit.net/topic/10858/"&gt;this&lt;/a&gt; one, and weaving them into my own map like &lt;a href="http://www.mediafire.com/download.php?mhywyyz0zwz"&gt;this&lt;/a&gt; one. I've decided to take some of the knowledge and tools I've gained and write a post here about them. First, I want to talk about positive and negative feedback back loops in two different contexts.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;The first context I want to talk about them in, is in the study of cybernetics.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;blockquote&gt;&lt;div class="first-para"&gt;&lt;i class="emphasis"&gt;Cybernetics deals with the ways a system  gauges its effect and makes necessary adjustments. The simplest cybernetic  device consists of a sensor, a comparator, and an activator. The sensor provides  feedback to the comparator, which determines whether the machine is deviating  from its established norm. The comparator then provides guidance to the  activator, which produces an output that affects the environment in some way.  This fundamental process of output-feedback-adjust-ment is the basis of  cybernetics.—&lt;/i&gt;&lt;i class="emphasis"&gt;&lt;b class="bold"&gt;Stephen Littlejohn&lt;/b&gt;&lt;/i&gt;&lt;i class="emphasis"&gt;, Theories of Human Communication&lt;/i&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;div class="first-para"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="first-para"&gt;&lt;i class="emphasis"&gt;&amp;nbsp;&lt;/i&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;As this quote explains, cybernetics is a study of how a system reacts and makes adjustments to the current state of the system. This should sound rather relevant to video games. This type of automated reaction is used all the time; have you ever played a game that automatically tiers to the user's level of skill? There are two primary types of adjustments that are made upon a system, these two are positive and negative feedback reactions, usually used within a loop.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;For example, take a room with a thermostat and a heater. When this room's sensor, the thermostat, reads a certain temperature it can trigger the heater to react. This system can be rigged to perform a negative feedback reaction in which the heater heats the room when the temperature is less than or equal to a certain amount. Assuming the room naturally cools, the room's temperate will oscillate between a few degrees and stay that way, all the while the room's specific state is constantly changing. A positive feedback loop would be if the heater turns on when the temperate is equal to or greater than a specific temperature. In a positive feedback loop, the temperature of the room will spiral up and up once it hits a certain activation point. Both positive and negative feedback loops are essential tools to be used in game design.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Too often I see games designed in StarCraft maps that are too hard or too easy, or too hard or too easy once the player reaches a certain point. I also see games made in which the leader has no way of letting the other players catch up to him, because he has such a great advantage over the other players. I also see the losers in a game have no means of catching up to the leaders. What's the point of continuing play if the outcome of the match has already been determined in the middle of the game because the leading player has a tremendous advantage over the rest of the players? These types of design flaws can be controlled with positive and negative feedback loops. Usually positive feedback loops are harmful to a game's flow (flow will be explained more in depth soon), and should be switched out for negative feedback loops. If a leading player seems to be constantly destroying all the other players in an unfair way, create a negative feedback response in which the system (the game) reacts and hinders the leading player. Similarly, you can give advantages to the players in last place to encourage and help them catch up to the leaders.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;A great example of a negative feedback response is in the game Super Mario Cart. In Super Mario Cart there are weapons you gain from running into question marked boxes with your cart. Once you hit one you obtain a random weapon. You have a much greater chance to obtain a powerful weapon when you are near last place in the race, and you have a much greater chance to obtain a weak weapon when you are in first place. This sort of automated reaction the system generates creates a much more compelling form of gameplay, in which all the racers are usually closely pitted against each other neck to neck until the very end of the race. This generates an exciting play in which the outcome of the overall match remains uncertain till the very end.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
The second meaning of positive and negative feedback would be rewards and punshiments to a player. Simply put, rewards are great to use to encourage players to make certain choices, and punishments are used to deter certain behavior. There is also a third category known as negative feedback. Negative feedback is not necessarily a punishment, but is used to deter a player from making specific choices. For example say you do not want your game to be focused on fighting, although you want to have an occasional enemy in your game. It turns out that too many players are focusing on finding and fighting enemies rather than experiencing your game how you designed it to be experienced. You do not want to punish the player as to deter them from playing at all, so what you could do instead is implement a clever negative feedback mechanism. You might be thinking that you could just make enemies stronger and therefore make the player want to avoid them -this might just make finding and killing enemies an interesting challenge for the player. Instead you could give the player absolutely no reward for killing the enemy (no points or anything), you could also make enemy encounters not worth the risk (as in making the player restart the level if an enemy kills them, or have enemies be "thieves" where if they hit you you lose items). If your goal is to make enemies something the player wants to avoid, give the player a reason to avoid them. If you find that fighting these enemies is truly fun for the players, you could also switch your game to a fighting one; it's your design, you decide. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="first-para"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Flow; flow would be the fine balance between difficulty in a game and the player's skills. Skills do not just include physical coordination; skills also include knowledge of the game's workings, knowledge of the other player's skill level, physical coordination, intelligence level, and response time. There are more than likely many many more forms of skill which I did not mention, but these should give a general idea. The difficulty of the game is pretty self-explanatory. The flow channel of a game is a narrow isle where the difficulty of the game is perfectly balanced and matched to the skill level of the player. Here is a diagram of the flow channel graphed with challenge over skill level:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_BTFnSlLQHZQ/S-gwcGvKzlI/AAAAAAAAAAM/1e4JJ-KI-fc/s1600/Diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_BTFnSlLQHZQ/S-gwcGvKzlI/AAAAAAAAAAM/1e4JJ-KI-fc/s320/Diagram.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;As you can see, it can be hard to create a flow in gameplay for all different types of players. One way that game designers achieve balanced flow is to apply positive and/or negative feedback loops to a game's processes.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here is an interesting excerpt from the book Rules of Play that talks about the design process for a game made by two designers:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;blockquote&gt;&lt;div class="first-para"&gt;In a wonderful essay published on Gamasutra.com, Jesse  Schell and Joe Shochet of Disney Imagineering write about the process of  designing Pirates of the Caribbean-Battle for the Buccaneer Gold, a game "ride"  where a group of players stands on a motion-platform pirate ship surrounded by  video projections. During the game, one player steers the ship while the other  players operate a number of cannons, firing at monsters, forts, and enemy  vessels. Pirates of the Caribbean is designed as a condensed five-minute  experience, and it was essential that players feel properly challenged at every  moment of the game. &lt;a href="http://www.blogger.com/post-edit.g?blogID=3579312376304629754&amp;amp;postID=1108176967055676804" name="720"&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="para"&gt;In their design analysis, Schell and Shochet detail a number of  design problems that had to be overcome in order to maximize player enjoyment.  For example, during playtesting they identified as a problem the fact that the  player steering the ship could take the ship to what they call "dull places,"  leading to a less engaging experience for all of the players. In the selected  quotes below, Schell and Shochet outline some solutions to this problem: &lt;/div&gt;&lt;blockquote class="blockquote"&gt;&lt;div class="first-para"&gt;&lt;i class="emphasis"&gt;Architectural Weenies: &lt;/i&gt;"Weenie" is a  phrase coined by Walt Disney himself. It refers to the technique used on movie  sets of guiding stage dogs by holding up part of a sausage… In the case of  Pirates, [there are] three main "weenies," one for each island: a volcano, an  enormous fort, and a plume of smoke coming from a burning town. No matter which  way the boat is facing, at least one of these "weenies" is in view. Since the  coolest action takes place at the islands, [we wanted] to guide the captains to  go there. &lt;/div&gt;&lt;div class="para"&gt;&lt;i class="emphasis"&gt;Guide Ships: &lt;/i&gt;Since the short-term goal of  the game is to fire on other pirate ships, captains strive to get near these  ships so that their gunners can get a clear shot. Many of the ships in the  Pirates world are "on their way" to the islands mentioned above. Many captains,  in just trying to stay near these ships find that just as they have destroyed  the ship, they have arrived at one of the islands, without even trying to get  there. &lt;/div&gt;&lt;div class="para"&gt;&lt;i class="emphasis"&gt;Sneak attacks: &lt;/i&gt;What if the captain ignores  the guide ships? Even if he heads toward one of the "weenies" it might mean as  long as a minute during which the gunners have little to shoot at. For this  reason, [we] created special "sneak attack" ships that "magically" appear behind  the players' ship, and quickly pull up alongside, when no other boats are in  range. &lt;/div&gt;&lt;div class="last-para"&gt;&lt;i class="emphasis"&gt;The Waterspout: &lt;/i&gt;This was [a] nickname  for [a] "last ditch" forcefield that surrounds the game play area.If a captain  tries to sail out of the main game play area and out to open sea, they hit the  forcefield, and the ship is "magically" pointed back to where the action is. The  few guests who see this don't even realize that anything unusual has happened.  They are just pleased to have their boat going somewhere cool.&lt;sup&gt;&lt;a href="http://www.blogger.com/post-create.g?blogID=3579312376304629754#ftn.N43" name="N43"&gt;&lt;/a&gt;&lt;/sup&gt; &lt;/div&gt;&lt;/blockquote&gt;&lt;div class="para"&gt;Schell and Shochet are thinking in very experiential terms, using  clever techniques to subtly guide player action in meaningful directions. At the  time of its release, Pirates was a very high-tech production, featuring  real-time 3D graphics, physically engaging cannon-firing interfaces, and a large  motion platform to simulate a pirate ship rocking on the waves. Often in these  instances, a desire to "properly" simulate a coherent 3D space or "correctly"  output logical behavior for computer-controlled characters overshadows the  design of the actual play experience. But Schell and Shochet had no hesitation  in making pirate ships "magically" appear to guide the player, or abandoning  "realistic" physics to have the player's ship turn on a dime to facilitate  navigation. As they put it,"By choosing to be less concerned with reality and  more concerned with what was fun, we created an experience that…is easier to  adapt to, quicker to learn, and is a better show." In game design, player  experience should always trump so-called "realism."&lt;/div&gt;&lt;div class="para"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="last-para"&gt;Boredom and anxiety, as game design watchwords, are wonderful  because they speak directly to player experience. As you shape and sculpt your  players' pleasure, you are guiding them between the Scylla and Charybdis of  anxiety and boredom.This task is made all the more difficult because, as we  know, the experience of play can only be indirectly designed. How do you create  a set of rules that maximizes the play of pleasure for your audience? &lt;/div&gt;&lt;/blockquote&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size: small;"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;The designers of this game implement two negative feedback loops. Did you see them? Onewould be the sneak attack ships. These ships react to the players being all alone at seas, and then appear out of nowhere to create an exciting experience. The other would be the water spout; the water spout reacts to the players entering the boundaries of the map, then spins them in the correct direction, thus keeping the game in a specific state. The guideships and weenies would be more a form of positive feedback in which encourage the player to make specific decisions. The guideships encourage the players to travel to the key focul points of the game while the weenies create enticing visual scenes that players are likely to travel to. Both of these provide the players with a reward and a reason to take specific actions. &lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-1108176967055676804?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/1108176967055676804/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/game-design-positive-and-negative.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1108176967055676804'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1108176967055676804'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/game-design-positive-and-negative.html' title='Game Design: Positive and Negative Feedback; Flow Control'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_BTFnSlLQHZQ/S-gwcGvKzlI/AAAAAAAAAAM/1e4JJ-KI-fc/s72-c/Diagram.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-2583108275781774526</id><published>2010-05-08T16:29:00.000-07:00</published><updated>2010-11-01T17:02:53.392-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Collision'/><category scheme='http://www.blogger.com/atom/ns#' term='Physics'/><title type='text'>2D Physics: Collision and Velocity Vectors</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I'm back from working on that SC map, and recently I found &lt;/span&gt;&lt;a href="http://www.newgrounds.com/portal/view/448915"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;this&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt; link. That link is a flash game of a 2D physics tutorial, and it covers just about everything you would need to know for 2D physics. The part I was most interested in was how to find the velocity vectors of a ball after collision with a wall, with any angle for the wall.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I ended up finding two different methods for doing so, both assuming the collision is perfectly elastic and that any acceleration given is a constant. If the acceleration is not a constant, then the timestep between calculations is going to be needed to be cut down in order to minimize the &lt;/span&gt;&lt;a href="http://gafferongames.com/"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;error generated&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;First I will go over the simplest method. This methods solves for the new angle the object travels in after collision, then uses this angle to solve for the new velocity vectors after collision with the wall.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;When a ball collides in an elastic collision with a point on the wall, the new angle formed is a reflection over the normal to the parallel of the wall. This means that the angle formed between the object's path of travel while it is moving at the wall, is equal to the angle formed between the wall and the object's path of travel as it leaves the wall. Knowing this, a simple equation can be used to solve for the new angle after collision:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;180 + 2C - A = B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;A is the angle of incidence, C is the normal to the wall at the point of impact, and B is the resulting angle. Here is an example:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i44.tinypic.com/2lnh0r4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;img border="0" height="350" src="http://i44.tinypic.com/2lnh0r4.jpg" width="400" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The angle of incidence is going to be 360, and the normal to the wall is 45 + 90.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;180 + 2(135) - 360 = 90&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now that the resulting angle is found, you can use this angle to solve for the new x and y velocity components. Using simple&amp;nbsp;trigonometry&amp;nbsp;we know that the x component is going to be cos(B), and the y component is going to be sin(B). Using the velocity of the object in place of magnitude for our equation, you can use these two equations to solve for the new x and y velocity vectors:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Velx = mag * cos(B)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Vely = mag * sin(B)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Now you all you have to do is apply your Velx and Vely to your object each timestep and it will travel away from the wall with a convincing collision. You can also use these two equations to modify the angle of travel of an object. For instance say an object is travelling near a magnet. You can create a rough simulation of "pull" by changing the angle of A to B by just adding or subtracting to A. To do this you would first need to know the angle of the line formed between the magnet and the object, which can be represented by D. Once you find this, you can use it to compare to A. Depending on whether A is greater or lesser than D you just add or subtract to A. You can also simulate acceleration towards the magnet by modifying the magnitude depending on what the distance to the magnet currently is.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Note: You can substitute in arctan(y/x) for A if you know the slope of the object's travel, which is y/x. This is known from standard trigonometry, where tan(A) = y/x.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;And there you have it! Now time for the second method, which is explained in my link at the top of the post. Actually, the link in my post doesn't really explain the process of coming up with the formulas goes, but oh well. If you know Velx and Vely from before the collision, then you can use these two&amp;nbsp;formulas:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS', sans-serif;"&gt;final Vx = Vxcos(2A) + Vysin(2A)&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS', sans-serif;"&gt;final Vy = Vxsin(2A) - Vycos(2A)&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS', sans-serif;"&gt;One more thing to note: You can use two points on a line to calculate the slope of that line by using rise over run. The formula is: A = arctan(x1 - x2/y1 - y2). This is extremely useful for collisions between two balls. You take the center coordinate for each ball, calculate the slope made between these two points, and use the normal slope to this slope as your "wall", then run the calculations this post describes to find the velocity vectors of both the x and y after collision. Instead of calculating the slope then finding the normal to this slope, you can use arctan(x1 - x2/y2-y1) to directly find the slope to the normal. This makes sense since a normal line's rise will be the same as the line's rise, although the runs will be reversed. So, the y1 and y2 switch positions, thus mirroring the runs of each slope.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-2583108275781774526?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/2583108275781774526/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/2d-physics.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/2583108275781774526'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/2583108275781774526'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/2d-physics.html' title='2D Physics: Collision and Velocity Vectors'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://i44.tinypic.com/2lnh0r4_th.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-396596518089887923</id><published>2010-05-05T09:58:00.000-07:00</published><updated>2010-05-05T09:58:39.908-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Mini RPG: Continued</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I have been working on a StarCraft map for a week long contest at &lt;a href="http://staredit.net/"&gt;StarEdit.net&lt;/a&gt;, however, today I forgot to bring my flashdrive with me so I now have no access to my mapfile and cannot work on it. Since I am unable to work on that for most of today, I decided to work on this file since I can copy//paste the source from my older post :)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Today I added in some comments to my code for easier read, added in a tiny method for the character class to add inventory items to the inventory (which is just a single list within the character class), created an item class, and started on the function that handles battles.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Overall progress is going at a decent pace while working on this project, but, I am not spending much time on it currently due to the week long contest I had mentioned.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;At the moment this file doesn't do anything new, but has new code within it.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Here is my current source file: &lt;a href="http://www.mediafire.com/?ymwminmhige"&gt;http://www.mediafire.com/?ymwminmhige &lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-396596518089887923?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/396596518089887923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/mini-rpg-continued.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/396596518089887923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/396596518089887923'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/05/mini-rpg-continued.html' title='Mini RPG: Continued'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-4510081502204579344</id><published>2010-04-26T15:24:00.000-07:00</published><updated>2010-04-26T16:36:34.549-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Mini RPG: Just Started</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I have started on my mini-RPG! As of now I have a field and character class. The field has three obstacles in it now, and the entire field is represented by the currentMap list shown at the beginning of the source code. A 1 is a wall, 0 is empty space, 2 is an obstacle (not walkable), and 3 is a newline character.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;At the moment the obstacles are represented by one of five different characters, and each obstacle is assigned one of these characters during the instantiation (creation) of the game field's instance. This way, random characters are generated for the obstacles and stay that way the entire game.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;For now the only things that this does are create a field and print it when you hit enter, and move the character around. In order to detect collision I created an isValidMove function. This function checks to see if the next move the player can take will be anything but a 0 or not, and returns True or False.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;import random&lt;br /&gt;
&lt;br /&gt;
currentMap = [&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 1, 1, 1, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 0, 0, 0, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 2, 0, 0, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 0, 0, 0, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 0, 0, 2, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 0, 0, 0, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 0, 2, 0, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 0, 0, 0, 1, 3],&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [1, 1, 1, 1, 1, 3]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class gameField:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self, mapData=[0]):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.mapData = mapData&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.string = ''&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.blocks = []&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(len(self.mapData)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for j in range(len(self.mapData[i])):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.mapData[i][j] == 2:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = random.randrange(1, 5)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 1:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.blocks.append(' v ')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 2:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.blocks.append(' x ')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 3:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.blocks.append(' ^ ')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 4:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.blocks.append(' * ')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 5:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.blocks.append(' ~ ')&lt;br /&gt;
&lt;br /&gt;
class character:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self, x=0, y=0, field=None):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.crds = [x, y]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.x = self.crds[0]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.y = self.crds[1]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.field = field&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __str__(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = ''&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = 0&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(len(self.field.mapData)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for j in range(len(self.field.mapData[i])):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[i][j] == 4:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.field.mapData[i][j] = 0&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.field.mapData[self.y][self.x] = 4&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(len(self.field.mapData)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for j in range(len(self.field.mapData[i])):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[i][j] == 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += '&amp;nbsp;&amp;nbsp; '&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif self.field.mapData[i][j] == 1:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += ' / '&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif self.field.mapData[i][j] == 2:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r += 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 1:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += self.field.blocks[0]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 2:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += self.field.blocks[1]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r == 3:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += self.field.blocks[2]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif self.field.mapData[i][j] == 3:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += '\n'&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif self.field.mapData[i][j] == 4:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += ' O '&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.field.string = s&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return self.field.string&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def isValidMove(self, move):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 'a':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y][self.x - 1] != 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 'a':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y][self.x - 1] == 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 'w':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y - 1][self.x] != 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 'w':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y - 1][self.x] == 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 's':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y + 1][self.x] != 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 's':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y + 1][self.x] == 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 'd':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y][self.x + 1] != 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move == 'd':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.field.mapData[self.y][self.x + 1] == 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&lt;br /&gt;
currentGameField = gameField(currentMap)&lt;br /&gt;
currentCharacter = character(3, 1, currentGameField)&lt;br /&gt;
&lt;br /&gt;
while True:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(currentCharacter)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; char = raw_input()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if char == 'a':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if currentCharacter.isValidMove(char):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentCharacter.x -= 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if char == 'd':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if currentCharacter.isValidMove(char):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentCharacter.x += 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if char == 'w':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if currentCharacter.isValidMove(char):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentCharacter.y -= 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if char == 's':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if currentCharacter.isValidMove(char):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentCharacter.y += 1&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;After that is complete, I'll add in an option to enter your inventory and have a chance of a random battle sequence after each step the player takes.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Hopefully in the future I will have support for having multiple rooms, doors to and from rooms, and a .txt loader that can load maps from a .txt file. We'll see how things go.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Here is an example of some output text:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;/&amp;nbsp; /&amp;nbsp; /&amp;nbsp; /&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp; v&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp; O&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ^&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; v&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp; /&amp;nbsp; /&amp;nbsp; /&amp;nbsp; / &lt;br /&gt;
&lt;br /&gt;
d&lt;br /&gt;
&amp;nbsp;/&amp;nbsp; /&amp;nbsp; /&amp;nbsp; /&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp; v&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; O&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ^&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; v&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / &lt;br /&gt;
&amp;nbsp;/&amp;nbsp; /&amp;nbsp; /&amp;nbsp; /&amp;nbsp; /&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;The character is the O, and the / are walls, the rest are just impassable obstacles. I hit the d key, and moved the character a space right.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-4510081502204579344?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/4510081502204579344/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/mini-rpg-just-started.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/4510081502204579344'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/4510081502204579344'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/mini-rpg-just-started.html' title='Mini RPG: Just Started'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6755634719535141534</id><published>2010-04-19T21:21:00.000-07:00</published><updated>2010-04-19T21:22:06.042-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Card Simulation: Complete</title><content type='html'>&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I have finished my Mau Mau card game!!&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;There are now two "unique" cards. Playing a seven makes the next player draw a card, and playing an Ace makes the next player lose a turn. The face up stack now sets itself as the deck and shuffles when the deck empties. If a player plays all their cards in their hand they win, and the program exits using sys.exit().&lt;br /&gt;
&lt;br /&gt;
I may in the future add in support for multiple Ai players, but for now I shall not. I want to start on a new and more complicated project, and I don't feel that working on this much more will help me to learn much more.&lt;br /&gt;
&lt;br /&gt;
Currently 333 lines of code.&lt;br /&gt;
&lt;br /&gt;
Here is a link to the .exe file in case you want to play it:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;&lt;a href="http://www.mediafire.com/?jjymynjngdz"&gt;http://www.mediafire.com/?jjymynjngdz&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;br /&gt;
Here is a link to the .py source file:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;&lt;a href="http://www.mediafire.com/?jnzum2zdrdv"&gt;http://www.mediafire.com/?jnzum2zdrdv&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;br /&gt;
As for my next project, I want to make a very small fighting game. The game will be text based, and will feature a small map, a battle sequence, and random battle events while walking on the map. I also want to have a interactive map (e.g. open doors), as well as a very limited inventory for dealing out different attacks. All in all, I want to have about 3 enemies. Basically this project is a mini-RPG, without&amp;nbsp;upgradeable&amp;nbsp;stats or any storyline. The point of the project is just to solidify my general coding capabilities, along with furthering my use of OO.&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6755634719535141534?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6755634719535141534/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/card-simulation-complete.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6755634719535141534'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6755634719535141534'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/card-simulation-complete.html' title='Card Simulation: Complete'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6135494714988376052</id><published>2010-04-17T16:38:00.000-07:00</published><updated>2010-04-17T17:01:56.822-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Card Simulation: Continued</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: large; line-height: 20px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS', sans-serif;"&gt;Here is a code update for the day. I have to spend the rest of my day doing homework, so I don't know what progress will be made throughout the weekend. I may work on this more tomorrow.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://download231.mediafire.com/rdxed0vz1cfg/mnmgotzwe3u/Cards.py"&gt;Here&lt;/a&gt; is a link to the current build's source code.&lt;br /&gt;
&lt;br /&gt;
As you can see, I have an Ai working to play against me. I currently don't have anything to have a player win the game, so there should be an exception thrown during one of the Ai's methods or during the makeMove method when either player has no cards in their hand.&lt;br /&gt;
&lt;br /&gt;
The Ai works by creating a list of cards available to play, then randomly choosing one to play. If none of the cards in the Ai's are playable then it will draw a card from the deck. This reminds me, if the deck empties then there should be a thrown exception as well, as I haven't coded switching the stack with the deck when the deck is empty.&lt;br /&gt;
&lt;br /&gt;
Since I created an Ai class, I should be able to instantiate multiple Ai instances with ease to allow for multiple Ai players to be playing simultaneously.&lt;br /&gt;
&lt;br /&gt;
Once I work on this some more, I plan to handle the mentioned exceptions that should be thrown, as well as begin working on creating some unique cards. For now I like the "make next player skip their turn" and "wild card". The wild card would allow the player to switch the current suit to any suit desired.&lt;br /&gt;
&lt;br /&gt;
Here is a .exe in case you wanted to play the game ^^&lt;br /&gt;
&lt;a href="http://download510.mediafire.com/13dumzc1bodg/awtwokn2wmt/Cards1.7z"&gt;http://download510.mediafire.com/13dumzc1bodg/awtwokn2wmt/Cards1.7z&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6135494714988376052?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6135494714988376052/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/card-simulation-continued.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6135494714988376052'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6135494714988376052'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/card-simulation-continued.html' title='Card Simulation: Continued'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3486661423971154819</id><published>2010-04-15T16:13:00.000-07:00</published><updated>2011-03-31T14:31:08.186-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Card Simulation</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Lately I have been documenting my experience with creating a deck simulator &lt;a href="http://www.staredit.net/topic/10588/0/"&gt;here&lt;/a&gt;, which is being written with Python 2.5.6.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I plan to continue documenting my progress at the link above, as well as on this blog. I posted the above link so that anyone that would like to can see all previous posts I've made up to my current point of development, which will be explained throughout this post.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I have chosen to create a card game called &lt;a href="http://en.wikipedia.org/wiki/Mau_Mau_%28card_game%29"&gt;MauMau&lt;/a&gt;. MauMau is a game very similar to Uno, the main difference being that MauMau is played with a traditional deck of cards.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;I currently have created a card class, deck class, MauMau class, and hand class. I manipulate these within the main function, which is the game loop. I also have created a function for determining if a player move is a valid move.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;My immediate plans for the game is to:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;- Create the used stack pile.&lt;br /&gt;
- Allow players to make a move by playing a card onto the used stack pile.&lt;br /&gt;
- Create an Ai which plays available cards, and draws if unavailable.&lt;br /&gt;
- Create rules for unique cards, like lose a turn, etc.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here is my current build:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;"""&lt;br /&gt;
To do:&lt;br /&gt;
- Create the used stack pile.&lt;br /&gt;
- Allow players to make a move by playing a card onto the used stack pile.&lt;br /&gt;
- Create an Ai which plays available cards, and draws if unavailable.&lt;br /&gt;
- Create rules for unique cards, like lose a turn, etc.&lt;br /&gt;
&lt;br /&gt;
Created by: CecilSunkure&lt;br /&gt;
"""&lt;br /&gt;
&lt;br /&gt;
import sys&lt;br /&gt;
import random&lt;br /&gt;
&lt;br /&gt;
ACES_HIGH = True&lt;br /&gt;
GAME_RUNNING = True&lt;br /&gt;
&lt;br /&gt;
class card:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; suits = ["Clubs",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Diamonds",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Hearts",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Spades" ]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ranks = ["Ace",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "2",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "3",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "4",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "5",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "6",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "7",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "8",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "9",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "10",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Jack",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Queen",&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "King" ]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self, suit=0, rank=0):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.suit = suit&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.rank = rank&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __str__(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (self.ranks[self.rank] + " of " + self.suits[self.suit])&lt;br /&gt;
&lt;br /&gt;
class deck:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.cards = []&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for suit in range(4):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for rank in range(12):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.cards.append(card(suit, rank))&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __str__(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = ''&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(len(self.cards)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = s + str(self.cards[i]) + '\n'&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return s&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def shuffle(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import random&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cards_length = len(self.cards)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(cards_length):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; j = random.randrange(i, cards_length)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.cards[i], self.cards[j] = self.cards[j], self.cards[i]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def remove(self, card):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if card in self.cards:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.cards.remove(card)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def draw(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return self.cards.pop()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def is_empty(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (len(self.cards) == 0)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def deal(self, hands, num_cards=999):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(num_cards):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.is_empty(): break &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; card = self.pop()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hand = hands[i % len(hands)]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hand.add(card)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def pop(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return self.cards.pop()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class hand(deck):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self, player=''):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.cards = []&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.player = player&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def add(self, card):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.cards.append(card)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __str__(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = self.player + "'s Hand"&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t = 0&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.is_empty():&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = s + " is empty\n"&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = s + " contains\n"&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(len(self.cards)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t += 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = s + str(t) + '. ' + str(self.cards[i]) + '\n'&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return s&lt;br /&gt;
&lt;br /&gt;
class MauMau:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self, stack=0, topCard=0):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.deck = deck()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.deck.shuffle()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.players = []&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.stack = stack&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.topCard = topCard&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def getName(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Welcome to Mau Mau!')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; player = raw_input()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('What is your name?')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; player = raw_input()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return player&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def startCard(self):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.topCard = self.deck.pop()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; def cmp_topCard(self, other):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.topCard.suit == other.suit:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.topCard.rank == other.rank:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&lt;br /&gt;
def isValidMove(move, hand, game, handlen):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Used to determine if a move by checking to see if the move is an integer, followed by and testing to see&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if the proposed move is within the length of the hand passed to this function. Finally, this function&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; makes sure that the proposed card to be played is valid within the rules of the game (same rank)."""&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; x = []&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(1, (handlen+2)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x.append(i)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int(move)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('That is not a valid move (not int).\n')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(move) not in x:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('That is not a valid move (not 1-' + str(handlen+1) + ').\n')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if move != str(handlen+1):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if game.topCard.rank == hand.cards[(int(move)-1)].rank:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif move == str(handlen+1):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; """This function is the main loop used for running the Class MauMau. The turns between Ai and human are&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; represented by integers, and those integers are used in if statements to determine which player is currently&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; taking their turn."""&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; #turn = random.randrange(2) + 1 &amp;lt;-- A comment for now, so for testing the human always goes.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; turn = 0&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; game = MauMau()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; name = game.getName()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; hand1 = hand(name)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; hand2 = hand("CPU")&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; game.deck.deal([hand1], 5)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; game.deck.deal([hand2], 5)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; game.startCard()&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if turn == 1:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('The computer will go first, as decided at random.')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You will move first, as decided at random.')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; while GAME_RUNNING:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print ('///' + '\n' + '/// ' + str(game.topCard) + '\n' + '///' + '\n')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (hand1)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if turn == 1:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print ('The computer will now take its turn, hit enter to proceed.')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Move = raw_input()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print ('Choose a card to play (1-' + str(len(hand1.cards)) + ') or draw a card (' + str((len(hand1.cards)+1)) + ').')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Move = raw_input()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isValidMove(Move, hand1, game, len(hand1.cards)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Valid move check passed.\n')&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Move == str(len(hand1.cards)+1):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hand1.add(game.deck.draw())&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Valid move check failed.\n')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
if __name__ == "__main__":&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;In order to use the stack of cards for the face up cards that were already played, I plan to implement a common stack. A stack would be a list or array that is used to grab values out of in the order of the last appended first. I created a test module (.py file) before actually using it in the game. I have yet to implement something similar, but the source for this test is here:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;#A test module for creating a stack.&lt;br /&gt;
&lt;br /&gt;
run = True&lt;br /&gt;
stack = []&lt;br /&gt;
&lt;br /&gt;
while run:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Stack, Pop, end, or continue? (1, 2, 3, enter)')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; yesno = raw_input()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if yesno == '1':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Enter a value to stack')&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a = raw_input()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack.append(a)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if yesno == '2':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack.pop()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b = stack.pop()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(b)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Stack is empty.\n')&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if yesno == '3':&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run = False&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Development is moving at a decent pace. Most of the time I am debugging the code I have written, but I am learning a ton. I've become very much more familiar with object orientation through the use of all my classes, and this project is definitely helping to enhance my overall coding ability. It's really pretty exciting :)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here is a link to a .exe of my current build: &lt;a href="http://download503.mediafire.com/m5ydqyl9zzng/nyfzyamykzi/Cards.7z"&gt;http://download503.mediafire.com/m5ydqyl9zzng/nyfzyamykzi/Cards.7z&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia, serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3486661423971154819?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3486661423971154819/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/card-simulation.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3486661423971154819'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3486661423971154819'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/04/card-simulation.html' title='Card Simulation'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3792156010557069481</id><published>2010-03-27T18:18:00.000-07:00</published><updated>2010-03-29T10:07:47.774-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AS3'/><title type='text'>ActionScript 3 (Flash)</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Well, I to be honest haven't taken a break from coding; I've been dabbling in ActionScript. I took a look at the &lt;a href="http://www.kongregate.com/labs"&gt;Shootorials&lt;/a&gt;&amp;nbsp;on &lt;a href="http://www.kongregate.com/"&gt;Kongregate&lt;/a&gt;, and I realized how easy AS3 was to use. I honestly composed this game found in &lt;a href="http://www.kongregate.com/games/Kongregate/shootorial-8"&gt;this&lt;/a&gt; tutorial (on page 1) in about 6 hours. I'm still contemplating spending my summer with C/C++, or ActionScript. Seeing how easy AS3 is to use, I may just spend my summer trying to develop a game in order to make some spare cash. On the other hand, I could spend my summer coding in C/C++ to get a bit of a headstart for DigiPen.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I found the website &lt;a href="http://freeactionscript.com/"&gt;http://freeactionscript.com&lt;/a&gt;, and immediately fell in love with all of the sample code. Take a look here if you are at all interesting in coding flash games.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I've also been spending my time reading &lt;a href="http://rs100.rapidshare.com/files/87719192/Rules.of.Play.Game.Design.Fundamentals.rar"&gt;Rules of Play: Game Design Fundamentals&lt;/a&gt;&amp;nbsp;in order to better understand what makes games fun to play. I plan to utilize this knowledge to finish my &lt;a href="http://www.staredit.net/topic/8547/"&gt;RPG&lt;/a&gt;&amp;nbsp;which is being constructed as a StarCraft 1 map file.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;So far, I plan to balance out the three of these projects during my summer, focusing mainly on finishing my RPG, and coding a decent AS3 game, all the while keeping brushed up on C/C++ with my collection of C/C++ books I have sitting on my computer.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;If you have any suggestions, comments, or questions about the above links or ideas, please feel free to let me know by commenting!!&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3792156010557069481?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3792156010557069481/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/03/actionscript-3-flash.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3792156010557069481'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3792156010557069481'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/03/actionscript-3-flash.html' title='ActionScript 3 (Flash)'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3154761948219215020</id><published>2010-02-25T10:40:00.000-08:00</published><updated>2010-02-25T10:47:24.282-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Caeser Cipher</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Now I have finished up the encryption chapter of &lt;a href="http://inventwithpython.com/chapters/"&gt;this book&lt;/a&gt;. The entire idea behind the encryption is to shift each character of a string over by a certain amount. Whenever a character of a string exited the parameters of a-z or A-Z, you just add or subtract 26, thus "wrapping" your encryption around and back, so that letters are only encrypted into letters. This took me about 30 minutes total to create. Here is my source code:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;import sys&lt;br /&gt;&lt;br /&gt;def getMode():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Do you want to encrypt decrypt, or brute force a message?')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mode = raw_input().lower()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if mode in 'encrypt decrypt e d brute b'.split():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return mode&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You need to enter in one of the following: e, d, b')&lt;br /&gt;&lt;br /&gt;def getMessage():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Enter your message.')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return raw_input()&lt;br /&gt;&lt;br /&gt;def getKey():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; key = 0&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while True:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Enter in a number for your key, 1-25.')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; key = int(raw_input())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (key &amp;gt;= 1 and key &amp;lt;= 25):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return key&lt;br /&gt;&lt;br /&gt;def getTranslatedMessage(key, mode, message):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if mode[0] == 'd':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; key = -key&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; translated = ''&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for symbol in message:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if symbol.isalpha():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num = ord(symbol)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num += key&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if symbol.isupper():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if num &amp;gt; ord('Z'):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num -= 26&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif num &amp;lt; ord('A'):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num += 26&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif symbol.islower():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if num &amp;gt; ord('z'):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num -= 26&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif num &amp;lt; ord('a'):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num += 26&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; translated += chr(num)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; translated += symbol&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return translated&lt;br /&gt;&lt;br /&gt;def playAgain():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Do you wish to continue? (yes or no)')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return raw_input().lower().startswith('y')&lt;br /&gt;&lt;br /&gt;Again = True&lt;br /&gt;&lt;br /&gt;while True:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mode = getMode()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; message = getMessage()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if mode != 'b':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Key = getKey()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Your translated text is:')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if mode != 'b':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(getTranslatedMessage(Key, mode, message))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for key in range(1, 26 + 1):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(key, getTranslatedMessage(key, 'decrypt', message))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not playAgain():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;The next chapter of &lt;a href="http://inventwithpython.com/chapters/"&gt;this book&lt;/a&gt; is &lt;a href="http://inventwithpython.com/chapter14.html"&gt;here&lt;/a&gt;. This chapter goes over the Reversi game. I won't be constructing a program from this chapter as it doesn't really introduce any new concepts. The Ai for the reversi game just runs through each possible move and chooses the one that results in the most points. I will be skipping over &lt;a href="http://inventwithpython.com/chapter14.html"&gt;chapter 14&lt;/a&gt;, and &lt;a href="http://inventwithpython.com/chapter15.html"&gt;chapter 15&lt;/a&gt;, and heading straight into &lt;a href="http://inventwithpython.com/chapter16.html"&gt;chapter 16&lt;/a&gt;. Chapter 16 is going over graphics and animation; I'll be starting to use the &lt;a href="http://pygame.org/news.html"&gt;Pygame&lt;/a&gt; engine.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3154761948219215020?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3154761948219215020/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/now-i-have-finished-up-encryption.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3154761948219215020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3154761948219215020'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/now-i-have-finished-up-encryption.html' title='Caeser Cipher'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-9059298458480154209</id><published>2010-02-24T17:08:00.000-08:00</published><updated>2010-02-25T10:43:34.291-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Sonar</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I have finished up creating a game that functions almost the same as the game in &lt;a href="http://inventwithpython.com/chapter12.html"&gt;this chapter&lt;/a&gt;. The game I created is slightly different in how the code works in a few specific areas, but altogether the example code in this chapter, and mine, should be fairly similar. Here is my source code:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;#Sonar&lt;br /&gt;&lt;br /&gt;import random&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;def drawBoard(board):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hline = '&amp;nbsp;&amp;nbsp;&amp;nbsp; '&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(1, 6):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hline += (' ' * 9) + str(i)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(hline)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp; ' + ('0123456789' * 6))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('')&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(15):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if i &amp;lt; 10:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; es = ' '&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; es = ''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('%s%s %s %s' % (es, i, getRow(board, i), i))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp; ' + ('0123456789' * 6))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(hline)&lt;br /&gt;&lt;br /&gt;def getRow(board, row):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; boardc = ''&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(60):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; boardc += board[i][row]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return boardc&lt;br /&gt;&lt;br /&gt;def getNewBoard():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; board = []&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for x in range(60):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; board.append([])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for y in range(15):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if random.randint(0, 1) == 1:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; board[x].append('~')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; board[x].append('`')&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return board&lt;br /&gt;&lt;br /&gt;def placeChests(chestAmount):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; chests = []&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(chestAmount):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; chests.append([random.randint(0, 59), random.randint(0, 14)])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return chests&lt;br /&gt;&lt;br /&gt;def isValidMove(x, y):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (x &amp;gt;= 0 and x &amp;lt;= 59 and y &amp;gt;= 0 and y &amp;lt;= 14):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;&lt;br /&gt;def makeMove(board, chests, x, y):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not isValidMove(x, y):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; smalld = 100&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for cx, cy in chests:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (abs(cx - x) &amp;gt; abs(cy - y)):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; distance = abs(cx - x)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; distance = abs(cy - y)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if distance &amp;lt; smalld:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; smalld = distance&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if smalld == 0:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; chests.remove([x, y])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'You have found a treasure chest!'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if smalld &amp;lt; 10:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; board[x][y] = str(smalld)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'Treasure has been detected at a distance of %s' % (smalld)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; board[x][y] = '0'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'Treasure is nowhere to be found within range.'&lt;br /&gt;&lt;br /&gt;def enterPlayerMove():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Enter in two integers (0-59, 0-14), otherwise type quit to exit the game.')&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while True:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; move = raw_input()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move.lower() == 'quit':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; move = move.split()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(move) == 2 and move[0].isdigit() and move[1].isdigit() and isValidMove(int(move[0]), int(move[1])):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return [int(move[0]), int(move[1])]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Enter in two integers (0-59, 1-15), otherwise type quit to exit the game.')&lt;br /&gt;&lt;br /&gt;def playAgain():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Do you want to play again? (yes or no)')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return input().lower().startswith('y')&lt;br /&gt;&lt;br /&gt;print('S O N A R !')&lt;br /&gt;&lt;br /&gt;while True:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sonarDevices = 16&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; theBoard = getNewBoard()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; previousMoves = []&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; playerMovec = []&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Where do you want to place your sonar device?')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; secretChests = placeChests(3)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while sonarDevices &amp;gt; 0:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if sonarDevices &amp;gt; 1: extraSsonar = 's'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: extraSsonar = ''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(secretChests) &amp;gt; 1: extraSchest = 's'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: extraSchest = ''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You have %s sonar device%s left. %s treasure chest%s remaining.' % (sonarDevices, extraSsonar, len(secretChests), extraSchest))&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x, y = enterPlayerMove()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; playerMovec.append([x, y])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = makeMove(theBoard, secretChests, x, y)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if sonarDevices &amp;lt; 16:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if playerMovec[len(previousMoves)-1] == previousMoves[len(previousMoves)-1]:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sonarDevices -= 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = False&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You have already placed a sonar device here!')&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; previousMoves.append([x, y])&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if sonarDevices == 16:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sonarDevices -= 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if result == False:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if result == 'You have found a treasure chest!':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for x, y in previousMoves:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; makeMove(theBoard, secretChests, x, y)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(result)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(secretChests) == 0:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You have found all the sunken treasure chests! Congratulations and good game!')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if sonarDevices == 0:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('We\'ve run out of sonar devices! Now we have to turn the ship around and head')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('for home with treasure chests still out there! Game over.')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp;&amp;nbsp; The remaining chests were here:')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for x, y in theChests:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp;&amp;nbsp; %s, %s' % (x, y))&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not playAgain():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit()&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;A large difference between my code, and the example code, is the fact that I have support for allowing the player to suffer no negative penalty (a loss of one of the sonar beacons) for trying to place a sonar over an area that was placed during the previous turn. This honestly, took me forever to figure out how to do. I finally decided on creating two copies of all previous turns, and then use these two copies to see if the last values in the two lists equal one another, before the value in the second list had been appended. This way, I compared the value of entered coordinates to the value of previous coordinates, and didn't subtract from the overall sonar amount when this happened.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;The math is simple, and the whole Cartesian Plane didn't really teach me anything new. Although, if you don't understand how the math in my makeMove() function doesn't work, you might need to read over &lt;a href="http://inventwithpython.com/chapter11.html"&gt;this chapter&lt;/a&gt; again.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I plan to go other the thirteenth chapter, &lt;a href="http://inventwithpython.com/chapter13.html"&gt;here&lt;/a&gt;, for my next post. This seems rather interesting, as it is about cryptology.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Let me know if you see something I could improve upon in this code. Hopefully my lack of commenting the code isn't detrimental, if it is let me know and I'll add in comments.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-9059298458480154209?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/9059298458480154209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/sonar.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/9059298458480154209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/9059298458480154209'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/sonar.html' title='Sonar'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-7820697148640899579</id><published>2010-02-23T14:54:00.000-08:00</published><updated>2010-02-25T10:43:45.995-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Bagels</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;The name of the game I have created for this post, is the game Bagels. Bagels is a game where the user guesses a three digit number, and the computer responds with "Fermi" for every digit that is in the right place that you guessed, and "Pico" for every digit that you guessed which is in the wrong place, but is still a digit in the secret number.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;The concepts that this game goes over are hardcoding and nested loops, the chapter is this one: &lt;a href="http://inventwithpython.com/chapter10.html"&gt;http://inventwithpython.com/chapter10.html&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;A nested loop is simply a loop within a loop. This is useful for when you want to iterate something x times, for every time y appears, or something similar.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Hardcoding is a coding practice where the coder codes the program so that changing of simple variables requires changing lots of code. This program was programmed without hardcoding, where a couple constants (variables that do not change while the program is running, which are represented with caps, although, are actually just normal variables in Python) can be changed to change how the game is played.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;import random&lt;br /&gt;&lt;br /&gt;def getSecretNumbers(numDigits):&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; numbers = list(range(10))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; random.shuffle(numbers)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; secretNum = ''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(numDigits):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; secretNum += str(numbers[i])&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return secretNum&lt;br /&gt;&lt;br /&gt;def getClues(guess, secretNum):&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if guess == secretNum:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'You have guessed the correct number!'&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; clue = []&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(len(guess)):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if guess[i] == secretNum[i]:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clue.append('Fermi')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif guess[i] in secretNum:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clue.append('Pico')&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(clue) == 0:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'Bagels'&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; clue.sort()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ' '.join(clue)&lt;br /&gt;&lt;br /&gt;def isOnlyDigits(num):&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if num == '':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in num:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if i not in '1 2 3 4 5 6 7 8 9'.split():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;&lt;br /&gt;def playAgain():&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Do you want to play again? Yes or no.')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return raw_input().lower().startswith('y')&lt;br /&gt;&lt;br /&gt;NUMDIGITS = 3&lt;br /&gt;MAXGUESS = 10&lt;br /&gt;&lt;br /&gt;print('I am thinking of a %s-digit number. Try to guess what it is.' % (NUMDIGITS))&lt;br /&gt;print('Here are some clues:')&lt;br /&gt;print('When I say:&amp;nbsp;&amp;nbsp;&amp;nbsp; That means:')&lt;br /&gt;print('&amp;nbsp; Pico&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; One digit is correct but in the wrong position.')&lt;br /&gt;print('&amp;nbsp; Fermi&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; One digit is correct and in the right position.')&lt;br /&gt;print('&amp;nbsp; Bagels&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; No digit is correct.')&lt;br /&gt;&lt;br /&gt;while True:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; secretNum = getSecretNumbers(NUMDIGITS)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('I have thought up a number. You have %s guesses to get it.' % (MAXGUESS))&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; numGuesses = 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while numGuesses &amp;lt;= MAXGUESS:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; guess = ''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while len(guess) != NUMDIGITS or not isOnlyDigits(guess):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Guess #%s: ' % (numGuesses))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; guess = raw_input()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clue = getClues(guess, secretNum)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(clue)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numGuesses += 1&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if guess == secretNum:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if numGuesses &amp;gt; MAXGUESS:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You ran out of guesses. The answer was %s.' % (secretNum))&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not playAgain():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;My code should be pretty much identicle to the code in the example program, as there aren't really any easier ways to code this program; this is a really rather cut and draw program to create.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;This part of the program confused me for a moment:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;def getSecretNumbers(numDigits):&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; numbers = list(range(10))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; random.shuffle(numbers)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;These lines create a list of numbers and then shuffle them. When I first saw these lines, the first one confused me: &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;numbers = list(range(10))&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;. I wasn't sure why the auther was changing the range() to list() inside of the range. Although, in the book it explained that if I wanted to store a list of integers, they needed to be converted from an iterator (value of range) into a list. This made sense, although the author didn't go into detail about exactly what the value of a range was. I assume it's just an amount of iterations to perform, which isn't what I would want if I wanted a list to store variables in.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;I plan to run through &lt;a href="http://inventwithpython.com/chapter11.html"&gt;chapter eleven&lt;/a&gt; of &lt;a href="http://inventwithpython.com/chapters/"&gt;this book&lt;/a&gt; for my next post. This should be a relatively easy chapter, as it just goes through the basics of a Cartesian coordinate system. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-7820697148640899579?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/7820697148640899579/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/bagels.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/7820697148640899579'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/7820697148640899579'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/bagels.html' title='Bagels'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6382290514659096594</id><published>2010-02-22T19:38:00.000-08:00</published><updated>2010-02-25T10:43:56.681-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Finishing Tic Tac Toe</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Georgia,serif; font-size: 13px; line-height: 20px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Aha! So I coded up my own version of Tic Tac Toe. I did take a look at my old program that I posted up before (&lt;a href="http://cecilsunkure.blogspot.com/2010/02/well-sadly-i-have-no-more-time-for-c.html"&gt;here&lt;/a&gt;) in a couple spots where I couldn't pinpoint my own bugs, but other than that I wrote this thing without a reference. This, pretty much just solidified my understanding and familiarity of Python syntax even further. Not much else to explain. Here is my source code:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;import random&lt;br /&gt;
&lt;br /&gt;
def drawBoard():&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;print(board[1] + '|' + board[2] + '|' + board[3])&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;print('-----')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;print(board[4] + '|' + board[5] + '|' + board[6])&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;print('-----')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;print(board[7] + '|' + board[8] + '|' + board[9])&lt;br /&gt;
&lt;br /&gt;
def pickYourLetter():&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;letter = ''&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;while (letter != 'X' and letter != 'O'):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('Would you like to X or O?')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;letter = raw_input().upper()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (letter != 'X' and letter != 'O'):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('Uhm, I said X or O.')&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if letter == 'X':&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ['X', 'O']&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ['O', 'X']&lt;br /&gt;
&lt;br /&gt;
def whoGoesFirst():&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if random.randint(0, 1) == 1:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return 'player'&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return 'computer'&lt;br /&gt;
&lt;br /&gt;
def getPlayerMove(theBoard):&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;move = ' '&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(theBoard, int(move)):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('It is your turn to move.')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;move = raw_input()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return int(move)&lt;br /&gt;
&lt;br /&gt;
def isSpaceFree(theBoard, move):&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return board[move] == ' '&lt;br /&gt;
&lt;br /&gt;
def makeMove(board, letter, move):&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;board[move] = letter&lt;br /&gt;
&lt;br /&gt;
def isWinner(b, l):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((b[1] == l and b[2] == l and b[3] == l) or&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;(b[4] == l and b[5] == l and b[6] == l) or&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;(b[7] == l and b[8] == l and b[9] == l) or&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;(b[1] == l and b[4] == l and b[7] == l) or&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;(b[2] == l and b[5] == l and b[8] == l) or&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;(b[3] == l and b[6] == l and b[9] == l) or&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;(b[1] == l and b[5] == l and b[9] == l) or&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;(b[3] == l and b[5] == l and b[7] == l))&lt;br /&gt;
&lt;br /&gt;
def isTie(board):&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;for i in range(1, 10):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isSpaceFree(board, i):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return False&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return True&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def getBoardCopy(theBoard):&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;dupeBoard = []&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;for i in board:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dupeBoard.append(i)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return dupeBoard&lt;br /&gt;
&lt;br /&gt;
def getComputerMove(theBoard, letter):&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if computerLetter == 'X':&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;playerLetter = 'O'&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;playerLetter = 'X'&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;for i in range(1, 10):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;copy = getBoardCopy(theBoard)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isSpaceFree(copy, i):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;makeMove(copy, computerLetter, i)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isWinner(copy, computerLetter):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return i&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;for i in range(1, 10):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;copy = getBoardCopy(theBoard)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isSpaceFree(copy, i):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;makeMove(copy, playerLetter, i)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isWinner(copy, playerLetter):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return i&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;move = chooseRandomMove(theBoard, [1, 3, 7, 9])&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if move != None:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return move&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if isSpaceFree(board, 5):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return 5&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return chooseRandomMove(board, [2, 4, 6, 8])&lt;br /&gt;
&lt;br /&gt;
def chooseRandomMove(theBoard, moveList):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;moves = []&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;for i in moveList:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isSpaceFree(theBoard, i):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;moves.append(i)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if len(moves) != 0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return random.choice(moves)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return None&lt;br /&gt;
&lt;br /&gt;
def playAgain():&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;print('Do you want to play again? (yes or no)')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return raw_input().lower().startswith('y')&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
while True:&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;board = [' '] * 10&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;print('This is Tic Tac Toe.')&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;playerLetter, computerLetter = pickYourLetter()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;gameIsPlaying = True&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;currentTurn = whoGoesFirst()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if currentTurn == 'player':&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('You will move first, as decided at random.')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('The computer will move first, as decided at random.')&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;while gameIsPlaying == True:&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if currentTurn == 'player':&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;drawBoard()&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;move = getPlayerMove(board)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;makeMove(board, playerLetter, move)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isWinner(board, playerLetter):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;drawBoard()&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('You have won the game!')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;gameIsPlaying = False&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isTie(board):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;drawBoard()&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('Game is a tie!!')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;gameIsPlaying = False&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;currentTurn = 'computer'&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;move = getComputerMove(board, computerLetter)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;makeMove(board, computerLetter, move)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isWinner(board, computerLetter):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;drawBoard()&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('You have lost the game!')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;gameIsPlaying = False&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if isTie(board):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;drawBoard()&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('Game is a tie!')&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;gameIsPlaying = False&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;currentTurn = 'player' &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if not playAgain():&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;break&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;And that is my code. Here is the exe file in case you are interested:&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;a href="http://www.mediafire.com/?oodckz5oxmy"&gt;http://www.mediafire.com/?oodckz5oxmy&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;Anyways, I plan to go over this chapter tomorrow and follow through with constructing the program from scratch without referring to the example code&amp;nbsp;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;a href="http://inventwithpython.com/chapter10.html"&gt;http://inventwithpython.com/chapter10.html&lt;/a&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;I had a few troubles throughout the program, and the most annoying one was in the definitions of winning. I had constantly placed a 1 in place of a lower case L, and this resulted in bad things. I also had a couple typos here and there, things like typing = instead of ==, and vise versa.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;One thing in particular that I found interesting while creating this program, was the "None" value. Here is a quote from the book "&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0b5394; font-family: Verdana,sans-serif;"&gt;Calls to functions that do not return anything (that is, they exit by
reaching the end of the function and not from a return statement) will
evaluate to &lt;/span&gt;&lt;span class="m" style="color: #0b5394; font-family: Verdana,sans-serif;"&gt;None&lt;/span&gt;&lt;span style="color: #0b5394; font-family: Verdana,sans-serif;"&gt;.&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;" This was useful in that whenever I wanted the Ai in my simulation to choose a random location to place his letter, say, one of the four corners; if all four of these corners were taken, I could return the value of None instead of one of the values for one of the four corners.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;I also learned from this chapter some interesting aspects of list referencing. Take a look at this function:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;def makeMove(board, letter, move):&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;board[move] = letter&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;It would seem like this function wouldn't do anything due to the change of the board list being of the private scope by default, since it is happening within the function itself. This is because the word "&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;board&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;" isn't actually the list board, it is just a reference to that list passed onto the function as an argument. This means that when we have the line &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;board[move] = letter &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;we aren't modifying the data stored in a variable, but we are modifying the data stored in the list, which is referenced by the word &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;board&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;". Here is a quote from the book that explains this: &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0b5394; font-family: Verdana,sans-serif;"&gt;When you assign a list to a variable with the &lt;/span&gt;&lt;span class="m" style="color: #0b5394; font-family: Verdana,sans-serif;"&gt;=&lt;/span&gt;&lt;span style="color: #0b5394; font-family: Verdana,sans-serif;"&gt; sign, you are actually assigning a reference to the list. A &lt;/span&gt;&lt;span class="term" style="color: #0b5394; font-family: Verdana,sans-serif;"&gt;reference&lt;/span&gt;&lt;span style="color: #0b5394; font-family: Verdana,sans-serif;"&gt; is a value that points to some bit of data, and a &lt;/span&gt;&lt;span class="term" style="color: #0b5394; font-family: Verdana,sans-serif;"&gt;list reference&lt;/span&gt;&lt;span style="color: #0b5394; font-family: Verdana,sans-serif;"&gt; is a value that points to a list.&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: #333333;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: 'Courier New',Courier,monospace; font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: 'Trebuchet MS',sans-serif; font-size: medium;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6382290514659096594?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6382290514659096594/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/finishing-tic-tac-toe.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6382290514659096594'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6382290514659096594'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/finishing-tic-tac-toe.html' title='Finishing Tic Tac Toe'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-1531308756231465455</id><published>2010-02-22T10:40:00.000-08:00</published><updated>2010-02-25T10:44:04.898-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Starting up Python!</title><content type='html'>&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Well, sadly, I have no more time for C++, and I probably won't for a couple months. Things are getting really busy. Although, I have good news; I started a programming club at my highschool that meets once a week, where I teach them all how to code in Python. For the time being, my next posts will be about my learnings of Python.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I chose Python after doing a little bit of research; Python is a language that was created to allow coders to create code quickly and efficiently. Readability of Python was also a concern while developing the Python environment. As such, Python is great for readability and for quickly coding programs. Although Python is great at what it was intended to be used for, Python isn't made to create highly optimized code for intense programming (things like 3D rendering). Although, you can create visual programs that render in 3D, Python code can only be optimized so much. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I spent this weekend reading some of the chapters from this excellent book on programming games in Python, with the Pygame engine. Here is the book I am currently reading: &lt;a href="http://inventwithpython.com/chapters/"&gt;http://inventwithpython.com/chapters/&lt;/a&gt;&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;.&lt;/span&gt; &lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I am currently on nine.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I am working with Python 2.6, as this has the most up-to-date third party support from things like Pygame and Py2Exe.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I recommend that anyone reading this check out the &lt;a href="http://pygame.org/"&gt;Pygame.org&lt;/a&gt; website, and sift around through the tutorials there, as well as check out the source code for some of the more simple programs that are on display there.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Over the course of chapter nine, I created a game of TicTacToe. The player can choose to play as X's or O's&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;, and I have a functioning Ai.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;At this point in time, I do have a working program, although, I created it while using the example code in the book as reference. As an exercise, my next post will be about me trying to create the entire program without reference code whatsoever.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Here is my source code for my program:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;import random&lt;br /&gt;&lt;br /&gt;def drawBoard(board):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#This function draws the board whenever it is called. The board is represented as an array.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('-----------')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('-----------')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |')&lt;br /&gt;&lt;br /&gt;def getPlayerLetter():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#This function allows the human to choose whether they play as an X or O.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; letter = ''&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while not (letter == 'X' or letter == 'O'):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('What would you like to play as? X or O?')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; letter = raw_input().upper()&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if letter != 'X' and letter != 'O':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You can only pick X or O to play as.')&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if letter == 'X':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ['X', 'O']&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ['O', 'X']&lt;br /&gt;&lt;br /&gt;def whoGoesFirst():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#This function randomly picks which player goes first.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if random.randint(0, 1) == 1:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'player'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'computer'&lt;br /&gt;&lt;br /&gt;def getPlayerMove(board):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#Captures the player's input on their desired move for their turn.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; move = ' '&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('What is your next move?')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; move = raw_input()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return int(move)&lt;br /&gt;&lt;br /&gt;def isSpaceFree(board, move):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#Return true if the move is available on the board.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return board[move] == ' '&lt;br /&gt;&lt;br /&gt;def makeMove(board, letter, move):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; board[move] = letter&lt;br /&gt;&lt;br /&gt;def isWinner(bo, le):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# Given a board and a player's letter, this function returns True if that player has won.&lt;/span&gt;&lt;br style="color: #274e13;" /&gt;&lt;span style="color: #274e13;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # We use bo instead of board and le instead of letter so we don't have to type as much.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ((b[7] == l and b[8] == l and b[9] == l) or # across the top&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (b[4] == l and b[5] == l and b[6] == l) or # across the middle&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (b[1] == l and b[2] == l and b[3] == l) or # across the bottom&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (b[7] == l and b[4] == l and b[1] == l) or # down the left side&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (b[8] == l and b[5] == l and b[2] == l) or # down the middle&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (b[9] == l and b[6] == l and b[3] == l) or # down the right side&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (b[7] == l and b[5] == l and b[3] == l) or # diagonal&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (b[9] == l and b[5] == l and b[1] == l)) # diagonal&lt;br /&gt;&lt;br /&gt;def isBoardFull(board):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#Checks to see if the board is full or not.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(1, 10):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isSpaceFree(board, i):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;br /&gt;&lt;br /&gt;def getComputerMove(board, computerLetter):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#Determines how to react depending on which letter (X or O) the computer player has.&lt;/span&gt;&lt;br style="color: #274e13;" /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if computerLetter == 'X':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; playerLetter = 'O'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; playerLetter = 'X'&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# Check if the computer can win in the next move.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(1, 10):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; copy = getBoardCopy(board)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isSpaceFree(copy, i):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; makeMove(copy, computerLetter, i)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isWinner(copy, computerLetter):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return i&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# Check if the player could win on his next move, and block them.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(1, 10):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; copy = getBoardCopy(board)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isSpaceFree(copy, i):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; makeMove(copy, playerLetter, i)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isWinner(copy, playerLetter):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# Try to take one of the corners, if they are free.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; move = chooseRandomMoveFromList(board, [1, 3, 7, 9])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if move != None:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return move&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# Try to take the center, if it is free.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isSpaceFree(board, 5):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 5&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# Move on one of the sides.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return chooseRandomMoveFromList(board, [2, 4, 6, 8])&lt;br /&gt;&lt;br /&gt;def chooseRandomMoveFromList(board, movesList):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# Returns a valid move from the passed list on the passed board.&lt;/span&gt;&lt;br style="color: #274e13;" /&gt;&lt;span style="color: #274e13;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Returns None if there is no valid move.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; possibleMoves = []&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in movesList:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isSpaceFree(board, i):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; possibleMoves.append(i)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(possibleMoves) != 0:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return random.choice(possibleMoves)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return None&lt;br /&gt;&lt;br /&gt;def getBoardCopy(board):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#This returns a copy of the actual board, used to simulate or test things.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dupeBoard = []&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in board:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dupeBoard.append(i)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return dupeBoard&lt;br /&gt;&lt;br /&gt;def playAgain():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;# This function returns True if the player wants to play again, otherwise it returns False.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('Do you want to play again? (yes or no)')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return raw_input().lower().startswith('y')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;br /&gt;while True:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#Resets the board.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; theBoard = [' '] * 10&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; playerLetter, computerLetter = getPlayerLetter()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentTurn = whoGoesFirst()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('The ' + currentTurn + ' player will go first.')&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gameIsPlaying = True&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while gameIsPlaying:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if currentTurn == 'player':&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#Player's turn.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; move = getPlayerMove(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; makeMove(theBoard, playerLetter, move)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isWinner(theBoard, playerLetter):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('You have won the game!')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gameIsPlaying = False&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isBoardFull(theBoard):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('The game is a tie!')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentTurn = 'computer'&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #274e13;"&gt;#Computer player's turn.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; move = getComputerMove(theBoard, computerLetter)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; makeMove(theBoard, computerLetter, move)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isWinner(theBoard, computerLetter):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('The computer has beaten you! You lose.')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gameIsPlaying = False&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if isBoardFull(theBoard):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drawBoard(theBoard)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print('The game is a tie!')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentTurn = 'player'&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not playAgain():&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;As you might notic&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;e, this code is pretty darn similar to my reference code. As I mentioned, I will as my next project re-write this entire program without any reference.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I would say that the most interesting part of creating this program, was the Ai. The Ai really only cycles through a copy of the currently existing board, and searches for any winning moves from either player. If it finds one, it takes it. If not, it moves to one of the currently open corners, randomly. If all the corners are taken, the Ai moves into the middle space. If that is taken, it moves onto one of the four last spaces, at random. This algorithm is pretty darn simple, but it is effective at playing Tic Tac Toe.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Overall, I would say I gained a better understanding of Python syntax more than anything while writing this program.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I tried to include a source file, and my exe, although I'm currently having troubles uploading. I'll try to remember to modify this post with some links, in case anyone wanted to download the exe or the source. Edit: No, I will not be supplying the exe//source code this time ;P&lt;span class="Apple-style-span" style="font-family: 'Franklin Gothic Medium';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-1531308756231465455?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/1531308756231465455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/well-sadly-i-have-no-more-time-for-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1531308756231465455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/1531308756231465455'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/02/well-sadly-i-have-no-more-time-for-c.html' title='Starting up Python!'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-5309856542636734076</id><published>2010-01-07T20:43:00.000-08:00</published><updated>2010-02-25T10:44:59.196-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C++ Objects</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;While reading through chapter one of&amp;nbsp;&lt;a href="http://www.planetpdf.com/codecuts/pdfs/eckel/TIC2Vone.zip"&gt;http://www.planetpdf.com/codecuts/pdfs/eckel/TIC2Vone.zip&lt;/a&gt;&amp;nbsp;I learned what classes, objects, and member objects are. I also learned what&amp;nbsp;inheritance&amp;nbsp;and polymorphism are.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;To solidify my learning of this chapter, I'll summarize each of these concepts, then go back through the book and confirm what I wrote.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Classes:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Classes are what are known as "types" of objects. A class is simply a group of objects which are related in some way, and programmers can define their own classes, as well as re-use pre-existing classes from other code.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Objects:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Objects are what makes up a class. Similar objects are grouped together into classes, and usually perform very similar functions and share similar behaviors. Objects can be cloned with each clone performing independently of the other clones.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Member Objects:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Member objects are objects from different classes all piled together to construct a new type of class. This concept is known as composition. Composition should be looked to use when creating new classes over inheritance, because it will result in simpler and cleaner designs.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Inheritance:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Inheritance is the act of taking a class and cloning it, then making necessary modifications to it.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Polymorphism:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Polymorphism is the idea of creating code to be executed without being dependent on specific data types. This is useful when you want to treat a variety of objects with the same function call. Although, this means that what code specifically executes won't be determined until runtime. This is where late binding comes into play. Late binding allows the object's properties to determine which actions to take during runtime when a particular function or command is called or sent. This will allow a single call to something like "draw" to be&amp;nbsp;applicable&amp;nbsp;to a wide range of objects of class "shape" without having to worry about which type of shape is being operated upon.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I plan to tomorrow take a look at chapter 2, which covers the implementation of objects in code.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-5309856542636734076?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/5309856542636734076/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/01/c-objects.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/5309856542636734076'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/5309856542636734076'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/01/c-objects.html' title='C++ Objects'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-6132580577090700202</id><published>2010-01-06T21:26:00.000-08:00</published><updated>2010-02-25T10:45:02.054-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'></title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS',sans-serif;"&gt;Just got back from Winter Break. This alone should explain the long span between my two last posts.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;In an attempt to keep this blog from becoming a diary, I want to jump straight back into my old daily routine of programming after homework//school.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I have some interesting links to share today. The first link is a link to a site in which I will be basing future studies on. I came across this site via a friend who is in their Junior year of college, and&amp;nbsp;pursuing&amp;nbsp;a degree in CS. He recommended that I read these books and follow along in them:&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;a href="http://www.planetpdf.com/developer/article.asp?ContentID=6634"&gt;http://www.planetpdf.com/developer/article.asp?ContentID=6634&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Times New Roman',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I will begin with the first book labeled as Volume 1 (filename TIC2Vone.pdf) found at this link:&amp;nbsp;&lt;a href="http://www.planetpdf.com/codecuts/pdfs/eckel/TIC2Vone.zip"&gt;http://www.planetpdf.com/codecuts/pdfs/eckel/TIC2Vone.zip&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Before I head into this book, I will also be studying this code:&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;a href="http://codepad.org/aqUQbCTd"&gt;http://codepad.org/aqUQbCTd&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Times New Roman',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;That, is code that demonstrates tokenizing in C++ just like I did before, and it creates a square. The difference between this code and my code, is that my code sucks and this doesn't. I will look over this code, understand it, then try to replicate something similar using it as a reference. Once I create a similar console program, I will then try to replicate the code once more without using the linked code as reference (unless I get stuck). This allows me to understand the code at a level of detail that is beneficial to me as I learn to use the C++ language.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-6132580577090700202?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/6132580577090700202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2010/01/just-got-back-from-winter-break.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6132580577090700202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/6132580577090700202'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2010/01/just-got-back-from-winter-break.html' title=''/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3702897818080888299</id><published>2009-12-05T22:44:00.000-08:00</published><updated>2010-02-25T10:45:05.722-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Classes</title><content type='html'>&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;Well, I was a bit caught up with some last minute homework last week, so I haven't posted in a couple days. Tsk Tsk.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;So what I went over today, was the Classes and Classes II page, at both of these links:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;a href="http://cplusplus.com/doc/tutorial/classes/"&gt;http://cplusplus.com/doc/tutorial/classes/&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;a href="http://cplusplus.com/doc/tutorial/classes2/"&gt;http://cplusplus.com/doc/tutorial/classes2/&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;I gained an understanding of why classes are useful, and I also found out that they are almost exactly the same as structures, except that the default access of the members of a class is private juxtapose to public.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;It was a lot of reading, and some tinkering, to understand both of these pages pretty well. I haven't created any code with the knowledge gained from these two pages, except for just some small modifications to make sure I understood what I was reading. Here is an example: I wrote a small circle class, and then printed the area of my circle:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS',sans-serif;"&gt;&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;#include &amp;lt;cstdlib&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;using namespace std;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;class CCircle {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;float x;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;void set_values (float);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;float area () {return (x*3.14159265*x);}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;};&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;void CCircle::set_values (float a) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;x = a;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;int main () {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;CCircle circ, circb, circc;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;circ.set_values (3);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;circb.set_values (5);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;circc.set_values (7);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "circ area: " &amp;lt;&amp;lt; circ.area() &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "circb area: " &amp;lt;&amp;lt; circb.area() &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "circc area: " &amp;lt;&amp;lt; circc.area() &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;system("PAUSE");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;return EXIT_SUCCESS;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span style="color: #666666;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #666666; font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="color: black; font-family: 'Trebuchet MS',sans-serif;"&gt;I plan to go over the next two pages in this tutorial, which are on friendship//inheritance, and polymorphism. Hopefully, I can read through those, and create some small programs like the one I made today. So tomorrow, I will create another small program using a class, and I will also use pointers and dynamic memory to do so. After this, I will read those next to article pages. I also have quite a bit of homework to do tomorrow, so we'll see how far I get.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Lately the articles and blogs on both gamasutra and gamedev.net have been a bit bland, so sadly, I have no interesting articles for anyone to read this time :P&lt;br /&gt;
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3579312376304629754-3702897818080888299?l=cecilsunkure.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cecilsunkure.blogspot.com/feeds/3702897818080888299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cecilsunkure.blogspot.com/2009/12/classes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3702897818080888299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3579312376304629754/posts/default/3702897818080888299'/><link rel='alternate' type='text/html' href='http://cecilsunkure.blogspot.com/2009/12/classes.html' title='Classes'/><author><name>CecilSunkure</name><uri>http://www.blogger.com/profile/16129986119923492789</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3579312376304629754.post-3873052347439058900</id><published>2009-11-30T18:34:00.000-08:00</published><updated>2010-11-01T17:04:14.139-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><title type='text'>Tokenizing Example: Square</title><content type='html'>&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Well, I did it. I created a tokenizing program, which tokeninzes a square. I had a reference file, but the thing was coded so poorly that I didn't even bother reading or understanding the darn thing. Here is the source code to the terribly written file:&amp;nbsp;&lt;a href="http://cplusplus.com/files/fraction.zip"&gt;http://cplusplus.com/files/fraction.zip&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;This program took me about 30 minutes to write, and 2 hours to debug. I had multiple bugs, and wrote the loop that creates the "middle" of the square about 4 times before I finally created something clean looking and easy to read. A common error I made, was during the draw function, I would often reset the value of a or b to the incorrect&amp;nbsp;corresponding&amp;nbsp;variables width and&amp;nbsp;height. If I were to label my variables as something extremely intuitive, I could have saved myself a lot of trouble :P&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here is the code for my square tokenizing program, it should look nothing like my reference (which I didn't ever reference), and be easier to read:&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;span style="color: #666666; font-family: 'Courier New', Courier, monospace;"&gt;/*Tokenizing Square*/&lt;br /&gt;
&lt;br /&gt;
#include&amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
#define n '\n';&lt;br /&gt;
&lt;br /&gt;
void InputData (int &amp;amp;height, int &amp;amp;width)&lt;br /&gt;
{&lt;br /&gt;
//Stores data input by the user.&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "Enter in the height of your square: ";&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;cin &amp;gt;&amp;gt; height;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; n;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "Enter in the width of your square: ";&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;cin &amp;gt;&amp;gt; width;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; n;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void Draw (int &amp;amp;height, int &amp;amp;width)&lt;br /&gt;
{&lt;br /&gt;
/*Draws the square. This function operates using the c variable to control wich&lt;br /&gt;
of the loops fires.*/&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;int a, b, c = 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;a = height;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;b = width;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;c = 3;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
/*The following two if statements check to make sure the dimensions aren't too&lt;br /&gt;
high.*/&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if(height &amp;gt; 30)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "Too large of a height, enter in something smaller!";&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; n;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; n;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c = 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;if(width &amp;gt; 30)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "Too large of a width, enter in something smaller!";&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; n;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; b;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c = 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;while (b &amp;gt; 0, c == 3)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (b == width)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; " ";&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; "__";&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b--;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (b == 0)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; n;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &am
