CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 25

Threaded View

  1. #3
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: I want to optimize my code

    Quote Originally Posted by monarch_dodra View Post
    Your main looks fine, although I would say your class interfaces are discussable (set_position with no argument setting a random position is not a great, imo), but they are minor details.

    Once a program works, if it works it works, there is not much else to say. What sets a "good and optimized" program from the "bad one" is how easy it is to maintain and expand on it.

    I said your main looks fine, but I'd be more interested in your classes: Do Zomby, Man and Hole all derive from an "Placeable object" class? Does Man and Zomby derive from a "Movable Object" class, itself deriving from Placeable? Does moveable_object have a method called "collides_with"? Or did you write it for both zomby and man. Did you write an "enemy_class" from which zomby derives? For when you have new types of enemies, they can derive from enemy_class too?

    Also, you might want to seperate the interface from your objects. Zomby should NOT have a function called show position. Rather, you should have (this is just an example) a class called interface_manager or something, that has a method called update position, and takes a zomby as an argument. Or maybe it would have a method called "refresh all positions".

    These are all ideas. The real question is this:

    If I say to you "Why not add badgers in your game" are you going to answer:

    - "No problem, that's just 5 lines of codes to create an empty badger class, since it derives from "moveable_enemies" where everything is well defined. I can place them inside my vector<moveable_enemy*>, and then that's it. In five minutes, you are playing with badgers."

    - "Aww man! This is going to take forever!"

    There. If you meant optimization as in long loops, or bugs, I noticed that you run all the way to the end of the loop, even if you win or lose. You should break when you find a winning or losing state. No need to run until the end of the program.

    PS: So, how long will it take you to add badgers? What if I tell you they have to move twice as fast as zombies?

    WOW i do see where you are coming from.
    and i really like your ideas:

    update_position could be a static function? i would just call it and pass my man, hole and zombie as a parameter.

    and set position yeah i should allow to pass a parameter so i could change the range

    and to add badgers it would not take long at all xD

    Body std::vector<Badgers> badgers

    Code:
    #pragma once
    #include"RandomNumber.h"
    
    class Body
    {
    private:
    
    	RandomNumber Random;
    protected:
    	
    	int x;
    	int y;
    	int move;
    	int moved;
    
    public:
    
    	Body(void);
    	~Body(void);
    
    	void movement_Man(void);
    	void movement_Zombie(void);
    	void movement_Zombie_Hunt(Body& );
    
    	bool collides_with( Body& );
    
    	void set_position(void);
    
    	int get_rand(int);
    };
    man.h
    Code:
    #pragma once
    #include"Body.h"
    
    class Man: public Body
    {
    public:
    	void show_position(void);
    };
    it the same for hole and zombie
    Last edited by Mariusmssj; April 16th, 2010 at 05:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured