Thread: Game difficulty

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    87

    Game difficulty

    Ok now i'm getting slightly confused, I've defined a difficulty class which should change a few settings of my game.

    Code:
    class CDifficulty{
    public:
    	CDifficulty(const char* itsSetting = "",const int nRings = 3):
    	  setting(itsSetting),numRings(nRings)
    	{}
    	CDifficulty(const string& itsSetting,int nRings):
    	  setting(itsSetting),numRings(nRings)
    	{}
    	int setDifficulty(const string itsSetting){
    		setting = itsSetting;
    		if(setting == "easy"){
    			numRings = 3;
    			return OK;
    		}else if(setting == "medium"){
    			numRings = 5;
    			return OK;
    		}else if(setting == "hard"){
    			numRings = 7;
    			return OK;
    		}else
    			return FAILED;
    	}
    	string getDifficulty(){ return setting; }
    private:
    	string setting;
    	int numRings;
    };
    The difficulty should determine how many rings there are in the game, set up as follows.

    Code:
    void createRings(struct gameStruct hanoiGame){
    	int i;
    	if(hanoiGame.diff->getDifficulty() == "easy"){
    		for(i = 3; i>0; i--)
    			hanoiGame.A->addRing(i);
    	}else if(hanoiGame.diff->getDifficulty() == "medium"){
    		for(i = 5; i>0; i--)
    			hanoiGame.A->addRing(i);
    	}else if(hanoiGame.diff->getDifficulty() == "hard"){
    		for(i = 7; i>0; i--)
    			hanoiGame.A->addRing(i);
    	}
    }
    The user is able to choose the difficulty from the menu set up, I have the following set of actions defined for this.

    Code:
    void diffSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int choice = 0;
    
    	cin >> choice;
    	switch(choice){
    	case 1:
    		if((hanoiGame.diff->setDifficulty("easy")) != OK){
    			cout << "WE MAY HAVE A PROBLEM";
    			break;
    		}
    	case 2:
    		if((hanoiGame.diff->setDifficulty("medium")) != OK){
    			cout << "WE MAY HAVE A PROBLEM";
    			break;
    		}else{
    			cout << hanoiGame.diff->getDifficulty();  //TEST PURPOSES
    			break;
    		}
    	case 3:
    		if((hanoiGame.diff->setDifficulty("hard")) != OK){
    			cout << "WE MAY HAVE A PROBLEM";
    			break;
    		}
    	case 4:
    		system("CLS");
    		hanoiMenus.mainMenu->showMenu();
    		mainSelect(hanoiMenus,hanoiGame);
    		break;
    	default:
    		cout << "Invalid";
    	}
    
    }
    Now heres the part thats puzzling me, If I were to choose option 2 "medium" then according to my setDifficulty function the number of rings should be 5, but instead it seems to add the same number of rings as hard i.e. 7 instead of 5!!!! Am I missing something blatantly obvious???

    Ah I just noticed something, its not just with medium, its with all the options, If I choose easy it sets the number of rings to 7 aswell.

    hmmm.... Curioser and Curioser said Alice as she wandered down the rabbit hole.
    PuterPaul.co.uk - Portfolio site

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    I wouldn't use strings for difficulty. Just use a short or char...

    I didn't really read through it all, just pointing that using strings when not neccessary isn't the best idea ^_^

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Yeah I did have it set as an int before (gets the number of rings) that was really just set up for testing purposes, any ideas why this is being such a *****!!!!!

    I've attached the zipped VC++ project files, if someone could take a look i'd be really really grateful.
    PuterPaul.co.uk - Portfolio site

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Personally I'd just use a switch statement in your create rings section:

    Code:
    void createRings(struct gameStruct hanoiGame) {
    int i;
    switch (hanoiGame.diff->getDifficulty()) {
        default:
        case 1: /* easy */ i = 3; break;
        case 2: /* medium */ i = 5; break;
        case 3: /* hard */ i = 7; break;
    }
    for (; i > 0; --i)
        hanoiGame.A->addRing(i);
    }
    Or even betterr:
    Code:
    int i = hanoiGame.diff->getDifficulty() + (hanoiGame.diff->getDifficulty() + 1); /* works if you use numbers 1, 2, 3 */
    for (; i > 0; --i)
        hanoiGame.A->addRing(i);
    Not sure if this is where your problem is though.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    This is it normal running
    PuterPaul.co.uk - Portfolio site

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    This is after choosing option 1
    PuterPaul.co.uk - Portfolio site

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. So you want to be a game programmer?
    By dxfoo in forum Game Programming
    Replies: 23
    Last Post: 09-26-2006, 08:38 AM
  3. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 03:58 PM
  4. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM