Thread: Optimization.

  1. #1
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

    Optimization.

    I am trying to write a combat.. module, I guess would be the word.. that will be able to be 100% stand-alone, not needing any global variables or anything else of that nature - EG, could be included in a header file.

    The problem. I wrote a program (this happened to just be a test), but I ended up with some global variables no matter what I tried to do. I was wondering if I could get some help / advice in how to keep everything self-contained and accessable to everything.

    Thank you in advance.

    Code:
    #include <iostream>
    #include <ctime> // For srand(time(NULL))
    #include <windows.h> // For Sleep()
    
    class Creature
    {
    	public:
    		Creature();
    		~Creature();
    		int GetStat(char* stat);
    		int SetStat(char* stat, int value);
    		
    	private:
    		int HP;
    		int MP;
    		int DMG;
    		int DEF;
    		int Armor;
    		int Dodge;
    };
    
    Creature::Creature()
    {
    	HP = 10;
    	MP = 10;
    	DMG = 6;
    	DEF = 6;
    	Armor = 0;
    	Dodge = 1;
    }
    
    Creature::~Creature() {}
    
    Creature::GetStat(char* stat)
    {
    	if (strcmp(stat, "HP") == 0) return(HP);
    	else if (strcmp(stat, "MP") == 0) return(MP);
    	else if (strcmp(stat, "DMG") == 0) return(DMG);
    	else if (strcmp(stat, "DEF") == 0) return(DEF);
    	else if (strcmp(stat, "Armor") == 0) return(Armor);
    	else if (strcmp(stat, "Dodge") == 0) return(Dodge);
    }
    
    Creature::SetStat(char* stat, int value)
    {
    	if (strcmp(stat, "HP") == 0) HP = value;
    	else if (strcmp(stat, "MP") == 0) MP = value;
    	else if (strcmp(stat, "DMG") == 0) DMG = value;
    	else if (strcmp(stat, "DEF") == 0) DEF = value;
    	else if (strcmp(stat, "Armor") == 0) Armor = value;
    	else if (strcmp(stat, "Dodge") == 0) Dodge = value;
    	
    	return(0);
    }
    
    // Globals
    
    int Attack(int target);
    int fight();
    
    Creature play; // Needed because more than one function accesses the class?
    Creature enemy; // Needed because more than one function accesses the class?
    
    int turn = 1; // Needed because you can't pass a value back to main() to pass back to another function?
    int dead = 0; // Needed because this is read by more than one function, can't pass back to main()?
    
    int main()
    {	
    	std::cout << "The fight will continue until one of the two are dead.\n\n";
    	
    	enemy.SetStat("HP", 12);
    	
    	while ((dead != 1) && (dead != -1))
    	{
    		fight();
    		srand(time(NULL));
    		Sleep(rand()%500);
    	}
    	
    	if (dead == -1) std::cout << "You win!";
    	else std::cout << "You lose.";
    	
    	std::cin.get();
    	
    	return(0);
    }
    
    int Attack(int target)
    {
    	srand(time(NULL));
    	
    	if (target == 1)
    	{
    		int HP = play.GetStat("HP");
    		int MP = play.GetStat("MP");
    		int DMG = enemy.GetStat("DMG");
    		int DEF = play.GetStat("DEF");
    		int Armor = play.GetStat("Armor");
    		int Dodge = play.GetStat("Dodge");
    	
    		if (rand()%Dodge >= (DEF * Dodge))
    		{
    			std::cout << "Miss.\n";
    			return(-1);
    		}
    	
    		else if (DMG < Armor)
    		{
    			std::cout << "Could not break through target's armor.\n";
    		}
    		
    		HP = (HP - ((rand()%DMG + 1) - Armor));
    		
    		play.SetStat("HP", HP);
    		
    		std::cout << HP << std::endl;
    		
    		if (HP <= 0)
    		{
    			dead = 1;
    			return(-1);
    		}
    		
    		return(0);
    	}
    	
    	else if (target == -1)
    	{
    		int HP = enemy.GetStat("HP");
    		int MP = enemy.GetStat("MP");
    		int DMG = play.GetStat("DMG");
    		int DEF = enemy.GetStat("DEF");
    		int Armor = enemy.GetStat("Armor");
    		int Dodge = enemy.GetStat("Dodge");
    	
    		if (rand()%Dodge >= (DEF * Dodge))
    		{
    			std::cout << "Miss.\n";
    			return(-1);
    		}
    	
    		else if (DMG < Armor)
    		{
    			std::cout << "Could not break through target's armor.\n";
    		}
    		
    		HP = (HP - ((rand()%DMG + 1) - Armor));
    		
    		enemy.SetStat("HP", HP);
    		
    		std::cout << HP << std::endl;
    		
    		if (HP <= 0)
    		{
    			dead = -1;
    			return(1);
    		}
    		
    		return(0);
    	}
    	
    	return(0);
    }
    
    int fight()
    {	
    	if (turn == 1)
    	{
    		std::cout << "Enemy's HP: ";
    		
    		Attack(-1);
    		
    		turn = -1;
    		
    		return(0);
    	}
    	
    	else if (turn == -1)
    	{
    		std::cout << "Player's HP: ";
    		
    		Attack(1);
    		
    		turn = 1;
    		
    		return(0);
    	}
    	
    	return(0);	
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    As with your classes and your turn and dead pass them by reference. Something like this
    Code:
    int attack(int target,Creature &player,Creature &enemy);
    Woop?

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by prog-bman
    As with your classes and your turn and dead pass them by reference. Something like this
    Code:
    int attack(int target,Creature &player,Creature &enemy);
    Thank you! Didn't know you could pass a memory address of a class. This will make some other code I'm writing a LOT easier!

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Oh and also with your srand you only need to call it once at the begining of main
    Woop?

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by prog-bman
    Oh and also with your srand you only need to call it once at the begining of main
    *nods* Fixed.

    Am I right, though, that you can't pass a variable's value back to main()?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well when your working with references you are changing what is at that memory address so you are changing what its value is in main. If you are talking about returning a value you could just make your function return a value that changes the variable in main.
    Woop?

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by prog-bman
    Well when your working with references you are changing what is at that memory address so you are changing what its value is in main. If you are talking about returning a value you could just make your function return a value that changes the variable in main.
    So, in theory, I could pass the memory address of any variables that need to be in multiple functions to the non-main() functions that need them; and I wouldn't have to have a single thing in the global scope?

    Such as..

    Code:
    #include <iostream>
    
    int addx(int &var);
    
    int main()
    {
        int x, *px;
        px = &x;
        
        std::cin >> *px;
        
        addx(*px);
        
        std::cout << x;
        
        std::cin.ignore();
        std::cin.get();
        
        return(0);
    }
    
    int addx(int &var)
    {
    	var = (var + 14);
    	
    	return(var);
    }

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You don't even need to make a pointer at all.
    You can just do this:
    Code:
    #include <iostream>
    
    void changeX(int &x);
    int main(void)
    {
      int x = 10;
      std::cout<<"X equals: "<<x<<std::endl;
      changeX(x);
      std::cout<<"X equals: "<<x<<std::endl;
      std::cin.get();
      return 0;
    }
    
    void changeX(int &x)
    {
      x=x+10;
    }
    Woop?

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by prog-bman
    You don't even need to make a pointer at all.
    You can just do this:

    Code:
    #include <iostream>
    
    void changeX(int &x);
    int main(void)
    {
      int x = 10;
      std::cout<<"X equals: "<<x<<std::endl;
      changeX(x);
      std::cout<<"X equals: "<<x<<std::endl;
      std::cin.get();
      return 0;
    }
    
    void changeX(int &x)
    {
      x=x+10;
    }
    Ok. *THAT* is nifty. Very, very nifty.

    Thanks for your patience and help, again!

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Your very welcome
    Woop?

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ Lithorien :

    I am a newbie , just know some little error about your prog:

    Code:
    int Creature::GetStat(char* stat)    // add type "int"
    Code:
    int Creature::SetStat(char* stat, int value)  // add type "int"

  12. #12
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Actually, ToySoldier, when you're working with classes, you do not need to add the type to the prototypes that you build for the class. When you define the functions inside your class, there is where you would add the "int" type.

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Actually Im pretty sure you need to define a type your member function. It is just like any other function just because you protype it doesn't mean you can use it without a type.
    Last edited by prog-bman; 09-28-2004 at 09:42 PM.
    Woop?

  14. #14
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Ok, then for some reason my compiler's letting a glaring error like that through, since it compiles and runs perfectly fine.

    *confused*

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    default return type

    Thats probably because if return type is omitted C defaults to int. Although I would say that it _is_ a good idea to specify it by yourself.

    -Olli

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 03:52 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. optimization flags
    By markucd in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2006, 09:08 AM
  4. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 02:53 AM
  5. Optimization stuff
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2004, 06:02 AM