Published using Google Docs
Cpp Notebook
Updated automatically every 5 minutes

Cpp Notebook


Assignment 2 (Dungeon Game)


Alright folks. Assignment 2 is out. Check it out now and begin by designing the classes needed to form the game.

To-do list:

1. Read about what is "Dungeon" and the requirement of the assignment.

2. Design the classes and how they interact with each other.

3. Review your design. Ask yourself questions using -> Why, What, Where, When, Who, & How? Do I really need this function/variable? What will happen if I don't have this function/variable?


    string str1 = "hello world !!!";

    string output;

    istringstream in_stream(str1);

    in_stream >> output;

    cout << output <<"\n";   // display hello

    in_stream >> output;

    cout << output <<"\n";   // display world

    in_stream >> output;

    cout << output <<"\n";   // display !!!


Labs


http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/  Reading for lab 5 ["Intro to polymorphism"]


Object-oriented concepts


The concept of object-oriented (OO) design can be understood easily actually.

For example, a car. What properties does a car have?

Name: Toyota Corolla GTS

Model: AE86

Drive-type: Rear Wheel Drive

Colour: White

Horsepower: 112.1 bhp

Top speed: 125.0 mph

0 to 60mph time: 8.53 s

0 to 100mph time: 24.9 s

See. The above was just some of the properties that you can put into your car object design. Sure, there's a lot more properties of our AE86 that we can add. But the above, serve the purpose of illustrating object-oriented concept.


An apple.

An apple is red in colour.

An apple has its own distinctive round shape.

An apple has the scientific name: Malus domestica

An apple matures in autumn.

An apple is sweet.

An 'Apple' is also the brand name of Apple Inc.

What other properties an apple have?


Cpp


File I/O:

If below, is the text file ('example.txt') that your program want to read:

2    3    5    7

11    13     17    19

23     29    31     37

41     43     47     53

59     61     67     71

You can write a program like this:

#include <fstream>

#include <iostream>

using namespace std;

int main(void)

{

    int num[20];

    int i = 0;

    ifstream textFile("example.txt");

    if(textFile)

    {

    while(!textFile.eof())

    {

            textFile>>num[i]>>num[i+1]>>num[i+2]>>num[i+3];

            cout<<num[i]<<" "<<num[i+1]<<" "<<num[i+2]<<" "<<num[i+4]<<endl;

            i += 4;

    }

    }

return 1;

}


Allocating memory for 2D array:

    int **tempINT;

   

    tempINT = new int*[row];

    for(int i=0 ; i<row ; ++i)

        {

                tempINT[i] = new int[col];

        }

 to create a 4 by 4 array

specify row = 4, col = 4


De-allocating memory for 2D array:

    for(int i=0 ; i<row ; ++i)

    {

            delete [] tempINT[i];

    }

delete [] tempINT;

tempINT = NULL;


Cpp website:

http://www.cprogramming.com/tutorial/

http://www.cppreference.com

http://www.research.att.com/~bs/C++.html by Bjarne Stroustrup, designer and original implementor of C++

check out the FAQ (How do I... section) as well ->  http://faq.cprogramming.com/cgi-bin/smartfaq.cgi


Gotchas


Always remember to include "using namespace std;" in your program.

It's needed for using STL such as vectors.


If you have Vector2D class like this:

class Vector2D

{

pulic:

    inline Vector2D(): x(0.0), y(0.0) {}

    Vector2D operator -( const Vector2D& other);

private:

    double x, y;

}

Your member function prototype should be like this:

Vector2D Vector2D::operator -( const Vector2D& other)

{

    return Vector2D( x-other.x, y-other.y);

}

And not this:

Vector2D::Vector2D operator -( const Vector2D& other)

I know this may be confusing at first, but I'd learn from my mistakes, and it shouldn't be a problem when you're used to it.


How to avoid memory leaks:

http://www.codersource.net/c++_memory_leaks.aspx 


Tips


1. Always begin by drawing flowcharts [by Dr. Chua H.S.], whenever you have little or no idea at all of how to start [added by me]. 


Minimize the use of the 'endl' manipulator - '\n' is cheaper in terms of performance

refer to http://www.devx.com/tips/Tip/14184


Determine if i is a power of 2:

bool IsPowerOf2(unsigned int i)

{

    return !( i & ( i - 1 ) ) && i;

}

 from Nitage [http://www.gamedev.net/community/forums/topic.asp?topic_id=464697]


Assignment 1


http://en.wikipedia.org/wiki/Shortest_path_problem list some possible solutions to the problem