Learning C++ (or programming Conway's Game of Life)

This post is about programming so if that doesn't interest you then fair warned be ye, says I.

Been learning C++. Out of a book. I'm just up to getting methods to work, which if you are a programmer you will know is not that far in, but it's a start damnit!

So I know from experience that it is much more fun to do this kind of thing if you are making something that is actually fun and you care about, so I had a crack at Conway's Game of Life, something that I'd always wanted to do since I first heard about it.

The basic premise is that you have a field of cells. Each cell dies if it is overcrowded or too lonely and cells are born if there are a specific number of living cells nearby. This gives you a fairly rudimentary but funtastic life simulator. LET'S DO IT!

Features of my version:

  • Shitty coding. (hey I'm learning) My version features no methods classes or objects or anything fancy like that just good old fashioned loops and 'if' statements.
  • Eye watering, hi-rez graphics.
  • You can change the size of the field to theoretically any size but the formatting may not work if you make it too big  so it'll look like pants. I use 10x10.
  • Hit return to display the next generation.
  • Type 'q' to quit or 'i' to invert the field (kill all live cells and spontaniously create live cells where there are none, a bit buggy since it's just a silly extra)

Stuff I learned:

Unlike Java (a similar language) which throws an error when you try to access memory outside of your declared array C++ lets you look at this memory and even write over it(!). So be fucking careful with your code lest you write over some part of your operating system and brick your PC. I got some weird errors due to this (my program was reading random memory as live cells causing life to appear where it should not be) to fix it, instead of fixing my code which would have been difficult and I am lazy, just make sure you leave a border of dead cells around the edge of the field, this acts as a wall and stops the program reading memory outside of the array.

Screenshot


Showing a 'glider' structure moving across the field.

Source Code

I cannot vouch for the quality of this code, all I know is it works for me. I take no responsibility if your computer cannot handle the unlimited power contained within and throws a wobbler.

http://pastie.org/803542

You'll need a C++ compiler. I recommend CodeBlocks, it's free.

Comments

Anonymous said…
http://markonzo.edu jessup gabtoli http://riderx.info/members/avandia-side-effects-avandia-lawsuit-avandia-lawyers-sc.aspx http://www.netknowledgenow.com/members/prozac-side-effects.aspx madame http://blog.bakililar.az/valtrex/ http://riderx.info/members/atacand-generic-atacand-drug.aspx http://www.netknowledgenow.com/members/zetia-side-effects.aspx misgivings http://aviary.com/artists/Zyprexa http://www.netknowledgenow.com/members/lasix-side-effects.aspx countypets bearings http://aviary.com/artists/protonix http://www.netknowledgenow.com/members/furosemide-side-effects.aspx scrutiny http://www.fiql.com/blogs/Nifedipine http://www.netknowledgenow.com/members/lorazepam-side-effects.aspx amrs http://riderx.info/members/buspar-side-effects.aspx http://www.netknowledgenow.com/members/ativan-side-effects.aspx cityphone recused

Popular posts from this blog

Never one to pass up a challenge...

Binary Combination Lock