views:

37

answers:

2

I've currently started on my semester project for my programming course- programming a fully autonomous driving simulator. All cars are steered through AI and the map is printed in the console.

The first sheet wants us to create a couple of basic classes: FCCompact (The car), Scanner (AI), ID (terrain), and World (The map).

The Scanner is currently implemented as a "HAS-A" in my FCCompact class like so:

// FCCompact.h
class FCCompact
{
private:
    struct fImpl;
    fImpl *fImpl_;
/* ... */

// FCCompact.cpp
struct fImpl
{
    Scanner scanner_;
    /* ... */
};

The scanner function in question is

const ID& scanForID( int fovNumber,            // How far it can see
                     FCCompact::Direction dir, // Direction car is facing
                     const Location& loc);     // Location of car

So far so good. However, the entire map is located in a container vector< deque<ID> >(1) in the class World; I'm not sure how to access it.

The functions posted so far have been given to us by the teacher, so they should be able to be implemented with the parameters given. I just have no clue how to do that.

My first idea was to call World::getID(Location &), but it's not static. Upon making it static, it can no longer access non-static members (duh, I forgot that). I have also made a static function that calls a non-static function; same problem (double duh).

Then I tossed the teacher's guidelines and simply passed in the entire World object, but that didn't really work either.

What can I do?

Please keep in mind that this is technically homework and that I do not want full, qualified answers that even gives me the entire implementation of what I want to do. I want to learn as much as possible from this project, so please just point me in the right direction.

(1): Weird choice for a container type? I figured if the world is printed on the console, quick access to each row with a vector would be beneficial, since there will be no insertion or deletion of queues and the access time is brilliant.

A deque, on the other hand, is handily for accessing ranges, which is favorable since I will frequently change adjacent columns (cars driving along the x- or y-axis). This choice did not come swiftly; I'm still torn between this, a list, or a simple vector again.

Classes

class World

class ID;
class World
{
    typedef std::pair<int, int> Location;

private:    
// vector<deque<ID> > map
// int mapHeight, mapWidth
// string mapName
    struct wImpl;
    wImpl *pimpl_;

public:
    World();
    ~World();

    void create(const std::string& name = "FC-City");

    int getHeight() const;
    int getWidth() const;

    ID getID(const Location& loc);

    friend std::ostream& operator<<(std::ostream& os, World& world)
    {
        return os;
    }

private:
    void loadMap(std::string name = "FC-City");

private:
    World(const World& other);
    World& operator=(const World& other);
};

class Scanner

class ID;
class Scanner
{
    typedef std::pair<int, int> Location;

public:
    Scanner();
    ~Scanner();

    const ID& scanForID( int fovNumber, 
                         FCCompact::Direction dir, 
                         const Location& loc);

private:
    Scanner(const Scanner& other);
    Scanner& operator==(const Scanner& other);
};

class FCCompact

class ID;
class FCCompact
{
    typedef std::pair<int, int> Location;

public:
    enum Direction
    {
        N, E, S, W, 
        NA = -1
    };

private:
// Scanner scanner
// Location loc
// Direction dir
// ID id
    struct    FCCImpl;
    FCCImpl  *pimpl_;

public:
    FCCompact( const ID& id,
               const Location& loc,
               const Direction& dir);

    FCCompact( const char ch,
               const Location& loc,
               const Direction& dir);

    const ID& getID()  const;
    Location  getLoc() const;
    Direction getDir() const;
    void      setDir( Direction& dir );

    void step();
    void hide();
    void show();
};

class ID

class ID
{
public:
    enum Trait
    {
        Traversable,
        NotTraversable,
        Mobile,
        Undef
    };

private:
    Trait trait_;
    char appearance_;

public:
    ID( char outputChar, Trait trait )
        : appearance_(outputChar), trait_(trait)
    { }

    ID( char outputChar )
        : appearance_(outputChar), trait_(Trait::Undef)
    { }

    char getToken()  const { return appearance_; }
    Trait getTrait() const { return trait_; }

    void setTrait(Trait trait) { trait_ = trait; }
};
+1  A: 

Sounds like you want the singleton pattern for class World.

class World {
public:
  static World* getWorld();

private:
  static World* _singleton;
};

You can initialize _singleton from your constructor and/or create(). getWorld() can return a reference (or throw an exception on failure) if you prefer. Up to you to think about: what if somebody tries creating multiple Worlds? Should you clean up World::_singleton at the end of the program?

aschepler
The fact suggesting a singleton raised those questions at the end should be a hint it creates more problems than it solves.
GMan
+1  A: 

Scanner should have a constructor that accepts a reference to a World; store a pointer to that argument so later when you need a world you have one.

Don't use a singleton, they're stupid. You could use a global but there really isn't a need here. A Scanner needs a World to operate on, ergo you construct it with a World to operate on:

struct bar;

struct foo
{
    // foo cannot operate without a bar,
    // therefore it needs to be supplied with
    // one to be able to construct
    foo(const bar& pBar) :
    mBar(&pBar)
    {}

    void i_need_bar()
    {
        mBar->use_bar();
    }

    bar* mBar;
};
GMan
Ah f- that was a function I created, not mandated by the teacher. I tacked it on to hack myself the functionality that I need to access the map. I'm not actually sure that that was what the professor intended me to do (as there are multiple viable options)
SoulBeaver
GMan
@GMan: Thanks for the advice! getID should be implemented as a const, yes. I hadn't changed it to a const-function yet since I previously changed the definition around.
SoulBeaver