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

Threaded View

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

    Re: I want to optimize my code

    ok this is what i got so far:

    and i will add the other characters i just want to know am i on the right track ^^

    Body.h
    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);
    
    	int get_rand(int);
    
            void set_position(int);
    
    	virtual void Move(void);
        virtual bool IsHumanInRange(Body &);
        virtual void Hunt(Body &);
    
    };
    Body.cpp
    Code:
    #include<iostream>
    #include<time.h>
    #include<stdlib.h>
    #include"Body.h"
    #include"RandomNumber.h"
    
    Body::Body(void)
    {
    	int x = 0;
    	int y = 0;
    	int move = 0;
    	int moved = 0;
    }
    
    int Body::get_rand(int upper)
    {
    	return Random.rand_gen(upper);
    }
    
    void Body::set_position(int range)
    {
    	x = get_rand(range);
    	y = get_rand(range);
    }
    
    Body::~Body(void)
    {
    }
    man.h
    Code:
    #pragma once
    #include"Body.h"
    
    class Man: public Body
    {
    public:
    	virtual void move(void);
    	void show_position(void);
    };
    man.cpp
    Code:
    #include<iostream>
    #include"console.h"
    #include"Man.h"
    #include"Body.h"
    
    void Body::Move(void)
    {	
    	std::cin>> move;
    	
    	if((move == 8)&&(moved != 2))
    	{
    		y = y - 1;
    	}
    	else if((move == 2)&&(moved != 8))
    	{
    		y = y + 1;
    	}
    	else if((move == 6)&&(moved != 4))
    	{
    		x = x + 1;
    	}
    	else if((move == 4)&&(moved != 6))
    	{
    		x = x - 1;
    	}
    	moved = move;
    
    }
    void Man::show_position(void)
    {
    	gotoxy(x,y);
    	{
    		std::cout<< "M";
    	}
    }
    zombie.h
    Code:
    #pragma once
    #include"Body.h"
    
    class Zombie: public Body
    {
    public:
    	virtual void Move(void);
    	virtual void Hunt(Body &);
    	void show_position(void);
    };
    Code:
    #include<iostream>
    #include"console.h"
    #include"Zombie.h"
    #include"Body.h"
    
    void Body::Move(void)
    {
    	int choice;
    	choice = get_rand(4);
    
    	if(choice == 0)
    	{
    		y = y - 1;
    	}
    	else if(choice == 1)
    	{
    		y = y + 1;
    	}
    	else if(choice == 2)
    	{
    		x = x + 1;
    	}
    	else
    	{
    		x = x - 1;
    	}
    }
    
    void Body::Hunt(Body & body)
    {
    	if(this->x > body.x)
    	{
    		this->x = this->x - 1;
    	}
    	else if (this->x < body.x)
    	{
    		this->x = this->x + 1;
    	}
    	else if(this->y > body.y)
    	{
    		this->y = this->y - 1;
    	}
    	else if(this->y < body.y)
    	{
    		this->y = this->y + 1;
    	}
    }
    
    void Zombie::show_position(void)
    {
    	gotoxy(x,y);
    	{
    		std::cout<< "Z";
    	}
    }
    a question:
    class Badger: public Body
    {
    public:
    void ~movement(void) why the (~) destructor there?


    and i declined
    virtual void Move(void);
    virtual bool IsHumanInRange(Body &);
    virtual void Hunt(Body &);
    in body but defined them individually in man and zombie. Now i need to define "virtual bool IsHumanInRange(Body &);" in zombie or body?

    thanks, i do get your idea, but since i am still new to C++ i want to ask all the question i can xD sorry if i get annoying
    Last edited by Mariusmssj; April 16th, 2010 at 07:13 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