Tuesday, February 9, 2010

Back to C++

Replicators on a toroidal surface
(Ruleset: B36/S23 "HighLife")

Many have written cellular automaton programs.
A cellular automaton (pl. cellular automata, abbrev. CA) is a discrete model studied in computability theory, mathematics, physics, theoretical biology and microstructure modeling. It consists of a regular grid of cells, each in one of a finite number of states, such as "On" and "Off".  [1]
Cellular automatons are often referred to as "Conways Game of Life", which is one type of cellular automaton. For exploring cellular automatons on your computer,  I can highly recommend both Golly (runs on Windows, OS X or Linux) or Mirek's Cellebration (Windows, and wherever Java applets can run.) They're both great programs. There are others available, too. Just ask your favorite search engine. Since I want to explore writing software, and cellular automatons seem like fun, I wrote my own cellular automation program, in Python. It's now working fairly well. I just cleaned up a few bugs features and added a toroidal surface mode, which is really cool. (I got that idea from Nathaniel Johnston's blog.) Now that the program is working well enough,  I want to convert it to C++, which is one of the languages that I studied in night school. It can't be that hard, right? It might even be fun.


I started coding in Python, because it's rather easy peasy compared to other programming languages I have used: interpreted, with a large standard library, yet still fast enough. Currently, Python is the main tool I use for programming. (I also started studying Scheme, initially to try to get a handle on recursion, later to learn functional programming. Still working on it.)

Now it's time to use a compiled language for a while, so C++ it is. Why a compiled language? Because I fell like it. Why C++? For a most excellent reason, namely I studied a few years back in night school and I'm already somewhat familiar with it.
`
People tend to favor what they are already familiar with.

I studied C++ a number of years ago, while the language standard was still evolving. I used to write C++ something like this:

#include <iostream.h>

void main()
{
    cout << "Hello, world!" << endl;
   
    return 0;
}
And it worked just fine. (Yes, it is odd for a function that returns void returning an integer. Yet I remember this working. Go figure. Some things in life just don't make sense.)[2]

This certainly does not work now. To be fair, the professor told us that would probably be the case, but not to worry, because it wouldn't change that much.) Well, what hat has changed? The C++ compiler I am using (GNU g++, gcc version 4.4.1), is supposed to mostly follow the current C++ standard. It supposedly is closer to the standard than many other compilers. Here's some changes that I noted: the current C++ standard now uses namespaces, iostream.h is now iostream and main must return an integer. Rewriting the code gives:
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}

which works.
mikey@hatshepsut:~$ g++ -o hello hello.cpp
mikey@hatshepsut:~$ ./hello
Hello, world!
mikey@hatshepsut:~$
This works, too:
#include <iostream>

int main()
{
    std::cout << "Hello, world!" << std::endl;

    return 0;
}
Aside from this stuff, it's seems to be the same. I guess I'll find out as I go along.

My Python program uses pygame for displaying the graphics. Pygame is built on SDL (Simple DirectMedia Layer). SDL is " a cross-platform, free and open source software multimedia library written in C that presents a simple interface to various platforms' graphics, sound, and input devices."[3]  SDL also works with C++. I used Python's built-in list data type. C++ has the Standard Template Library, which provides both a vector and a list container, either of which should work for me. That's all the magic there is, at least for this project. Let's see how it goes.

Notes:
[1] Wikipedia article: Cellular automaton (Tue Feb  9 10:59:37 CST 2010)
[2] No, I am not imagining this. Track down a copy of How to think like a computer scientist  C++ Version First Edition by Allen B. Downey on the Internet if you don't believe me.
[3] Wikipedia article: Simple DirectMedia Layer (Tue Feb  9 10:26:16 CST 2010)

No comments:

Post a Comment