Monday, March 24, 2014

Operator Overloading

I've  been learning something really cool.

Basically, C++ has some built in operators.  These can manipulate the data in the program.  For instance, there are mathematical operators.  For instance, if I tell my program:

5 + 5

It will return 10.

But what if I create my own class? 

Take, for instance, a class for vectors.  Vectors have direction and magnitude.  What they are and how they work can be read here.  Vectors can be simulated with a set of 3 coordinates, similar to a point on a 3D graph, which shows where the vector terminates and from which its magnitude can be calculated.

So, if the vector has three coordinates, how can I tell the program to add two together?  I would have to write a function telling the program to add the corresponding coordinates from each vector (AX + BX, AY + BY, AZ + BZ).  This works alright, but the syntax behind it can be confusing, or at best, hard to read.  Perhaps I write a method called "add" to add vectorA and vectorB together.  To implement this into the program, I might have to write something like this:

vectorC = vectorA.add(vectorB);

Using operator overloading, I can now make my vector class work just like built in data types in C++.  If I overload the + operator, I can tell the program that when I use + with two vectors, it means to add each corresponding coordinate.  Now, to have the program do what I want it to do, all I have to do is type:

vectorC = vectorA + vectorB;

It seems trivial, but it makes things a lot easier to read and understand when I'm creating my own data types.

Thursday, March 20, 2014

I'm still here!

Just wanted to say that I'm still around.  Things got a little crazy for a while.  I was pretty sick for a while and out of commission.

I've been feeling a lot better and getting back into the swing of things.

In the course that I am taking, one of my homework assignments was to add features to a text based RPG game, where the source of the base game was pretty much given as an introduction to classes.

The basic RPG asks you to pick a class, and gives you the option to move in four directions (it keeps track of your position using coordinates), rest, view your character's stats, or quit the game.  If you move, There is a chance you will be attacked by one of several types of enemies, from weak goblins to very strong orc lords.

Some of the features I was asked to implement were easy.  I had to add character classes and races into the game, and give bonuses and different stats to each.  One other thing they had me to was to add a chance of being attacked while resting.  This one was especially easy, but for some reason I was stumped for a while.  It turned out I was making things a lot more complicated than they really were, and when I figured out what I was doing wrong it took about 2 minutes to implement this.

I was pretty confused about passing pointers and references into functions, which was covered in the previous chapter, and while working on the assignments I had some ideas about things I could add in, but was having trouble doing what I wanted, and got a little frustrated.  Eventually I gave up on the assignments and moved on to the next chapter..  Progress was slow, though, because I felt bad about not finishing the assignments.

So finally, I've begun working on that game again.  So far, I've added a store, that will be randomly assigned to a set of coordinates from ([-5 - 5], [ -5, 5]), so that every game the store will be located in a different place, but won't be super hard to get to.  In addition, I've added a "check map" option to the main game menu, so it's easy to figure out where the store is.

I've also added a message and a new menu that comes up when you enter the store, and disabled the ability to be attacked when you're on the coordinate the store occupies.

The next step is to add inventory to the store in the form of weapons, armor, and potions.  Then I'll need to add the methods that will allow you to purchase and use the items in the store.  I think I'm going to hard code items into the store that will be available, but I'm also considering upgrades for weapons and armor instead of actual new items.  I feel like that will make it easier to keep progressing in the game after a few levels.

After I'm done with that, the last assignment I have is to make it possible to be attacked by more than one enemy at a time.  This one sounds tricky, and I'm sure I'm going to have to work at getting that one right.  I'm looking forward to getting this project out of the way.  Once it's done and working, I'll have something to be proud of.  It'll be a real morale booster!

I'll update this blog after a little more progress.