Simple ncurses Console Game

I wrote a simple ncurses game in C++ on Fedora.

I’ll actually be releasing the code and talking about how to build the game as part of a new Eclipse-C++ course I’m currently creating (sign up to my mailing list if you’d like updates on this).

It was pretty straightforward (once I finally got the ghost AI working properly – breadth first search anyone?). Ahem.

Anyway – here’s a little video I made of the game.

I cropped the screen area to get rid of distractions, which means the quality suffers a little bit, but you get the idea. And you’ve got to luuurve the cheesy music provided by iMovie…

Enjoy!

 

Polymorphism and Overloading in C++

A reader sent me an interesting question the other day. They asked if polymorphism and overloading were essentially the same thing.

My initial reaction was Huh?

What are people being taught if they think that these two concepts are the same thing?

But a quick google search revealed that yes, many, many people are struggling to differentiate between these two terms – and the internet is full of conflicting and unhelpful information.

Read morePolymorphism and Overloading in C++

Understanding Pointers (with Lego) Part 1

Ahhh pointers.

I’ve been wanting to write a series of posts on pointers for a long time. When I’ve finished, I’ll upload a complete PDF eGuide. In the meantime, enjoy 🙂

Pointers.

A concept that some programmers avoid through fear, others abuse through ignorance, and yet others manage to use with grace and efficiency.

Every programmer who does more than dabble in C and C++ needs to understand this subject. Properly.

This is Part 1 of a multi-part series, and there will be a lovely quiz at the end, which I expect you will all get 100% on after reading my posts, right?

Let’s crack on.

Read moreUnderstanding Pointers (with Lego) Part 1

Boost Compiled Libraries in 3 Minutes

Boost is mostly made of just header files, as we saw last week, which means you include them in your source, add the correct namespace and you’re good to go.

However, there are a handful of compiled libraries too, so let’s take a very quick look at how we can use these. If you haven’t installed Boost, take a look at Boost in 3 Minutes to get you up and running.

 

1) Include the header of the library you are interested in using

I’m going to use the filesystem library:

#include <boost/filesystem.hpp>

Time taken: 30 seconds

 

2) Write some code using this library

Don’t forget to qualify the methods with a namespace (else it will not compile!)

#include <boost/filesystem.hpp>
#include <iostream>

int main()
{
    //don't forget this bit!
    using namespace boost::filesystem;

    path p("/var/log/dmesg");

    if (exists(p))
    {
        std::cout << p << " size is " 
            << file_size(p) << std::endl;
    }

    return 0;
}

Time taken: 2 minutes

 

3) Compile your code with the boost libraries

Now, instead of just compiling our file, we have to include the filesystem library and let the compiler know where it lives:

g++ boost.cpp -I/usr/include/boost -lboost_filesystem -lboost_system

To clarify, the line above is made up of the following things:

 

Firstly I’ve passed the source code file to the compiler.

Secondly I’ve added the path to boost using the -I option.

Thirdly I’ve added the libraries I want to link to using -l.

 

In this case there are two libraries that I need to include, as the filesystem binary depends on the system binary. You will get a linker error if there are any dependencies when you include your initial library. This will tell you what else you might need to add.

Time taken: 30 seconds

 

 

The output of the little program above should be something like:

/var/log/dmesg size is 61318

And there you have it – boost compiled libraries in under 3 minutes 🙂