Thread: Text-Based RPG

  1. #16
    Registered User
    Join Date
    Sep 2009
    Quote Originally Posted by Bubba View Post
    Your shop() function could just as well take a structure and you could pass it by reference which would simplify the code a bit and would rid you of the 15 parameters you have to pass. Also just because you can modify a variable passed on the stack does not mean you have to do it this way. If you find you are altering a lot of parameters that are passed to the function you may want to revisit the design.

    Shop() only needs the parameters passed by reference if it intends on modifying the parameters. In fact when I see a pass by reference without a const it tells me that the function DOES modify the parameter.

    void MatrixMultiply(Matrix &result,const Matrix &matrix1,const Matrix &matrix2)
    This function very clearly tells the programmer that it will concatenate matrix1 and matrix2 and result will be the result of the concatenation. Matrix1 and Matrix2 are passed by reference to avoid a copy constructor call which would happen if they were passed by value. The const indicates that the function will not modify matrix1 nor matrix2. The lack of const for result clearly indicates to the programmer that result will be modified by the function. What the function prototype does not indicate is the order of operations. Since Matrix1 * Matrix2 != Matrix2 * Matrix1 this function would need a comment specifying the order in which the matrices are concatenated. One could say that since they are labeled as 1 and 2 it would indicate Matrix1 * Matrix2 but there is nothing in the prototype that would beyond a shadow of a doubt indicate the order of concatenation.

    Note that this function is a bit unwieldy since it cannot be used as an rvalue which would be quite handy.
    o_O? Like I said, I'm a beginner, so I have no idea what you are talking about...

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    void work()
    	cout << "\n\nHow long would you like to work? (eight-hour maximum) ";
    	cin >> workHours;
    	pay = workHours * 100;
    	if (workHours > 8)
    		workHours = 8;
    	if (workHours >= 1)
    		cout << "\n\nYou worked for " << workHours
    			 << " hour(s) and earned " << pay << " gold piece(s)!\n\n";
    		gold += pay;
    		cout << "You did not work.\n\n";
    A bit of a bug in there: you calculate pay before making sure that workHours is at most 8, so if I typed 100 I'd get paid for 100 hours, while appearing to only work for 8 hours.

    Your updated code looks better modularized, but charCreation() could use some work, too. You have a lot of repeated code similar to this.
    			hitPoints = rand() % 50 + 50;
    			attack = rand() % 15 + 5;
    			defense = rand() % 15 + 5;
    			magic = rand() % 5 + 5;
    			accuracy = rand() % 5 + 10;
    			cout << "Name: " << charName << endl;
    			cout << "Class: Warrior\n";
    			cout << "HP: " << hitPoints << endl;
    			cout << "ATK: " << attack << endl;
    			cout << "DEF: " << defense << endl;
    			cout << "MGK: " << magic << endl;
    			cout << "ACC: " << accuracy << endl;
    			cout << "\nIs this information correct? (y/n) ";
    			cin >> recreate;
    			if (recreate != 'n' && recreate != 'y')
    				cout << "\nYou have entered an invalid character.";
    				recreate = 'n';
    You could put that all into a function, passing in the relevant variables such as the 15 and 5 in "rand() % 15 + 5". That would probably result in a lot of arguments, but you could at least put the printing code into a function, as that changes very little.

    Similarly for shop(). I'd like to introduce a little bit of what is probably new code as a suggestion to you . . . it might be food for thought, at least.
    #include <iostream>
    #include <iomanip>
    struct player_t {
        int gold;
    struct weapon_t {
        char *name;
        int damage;
        int speed;
        int range;
    void shop(player_t &player);
    int main() {
        // create a new instance of the player_t structure
        player_t player; = 100;  // set the player's gold
        return 0;
    struct shop_item_t {
        weapon_t weapon;
        int cost;
    void shop(player_t &player) {
        // create an array of shop_item_t's, and initialize the elements right here
        shop_item_t inventory[] = {
            // Name, damage, speed, range, and then cost.
            {{"Foil", 5, 12, 1}, 20},
            {{"Rapier", 8, 10, 1}, 30},
            {{"Short bow", 3, 8, 30}, 25},
            {{"Long bow", 5, 7, 40}, 60}
        for(;;) {
            std::cout << "--- Shop contents:\n";
            for(size_t x = 0; x < sizeof(inventory) / sizeof(inventory[0]); x ++) {
                std::cout << (x + 1) << ") "
                    << std::left << std::setw(20) << inventory[x]
                    << "\tDamage: " << std::setw(3) << inventory[x].weapon.damage
                    << "\tSpeed: " << std::setw(3) << inventory[x].weapon.speed
                    << "\tRange: " << std::setw(3) << inventory[x].weapon.range
                    << "\n";
            std::cout << "You have " << << " gold.\n";
            std::cout << "Enter your choice (0 to quit): ";
            int choice;
            std::cin >> choice;
            if(choice == 0) return;
            choice --;  // convert from 1-based counting to 0-based
            if(choice >= 0 && choice < sizeof(inventory) / sizeof(inventory[0])) {
                if( >= inventory[choice].cost) {
                    // not yet implemented: add the new item to the user's inventory
           -= inventory[choice].cost;
                    std::cout << "You bought a " << inventory[choice]
                        << " for " << inventory[choice].cost << " gold.\n";
                else {
                    std::cout << "You can't afford to buy that.\n";
            else {
                std::cout << "Invalid option.\n";
    [Code coloured with codeform.]

    As you can see, I've made use of structures and of arrays. (And arrays of structures.) You should really learn about those soon (or better yet, about classes); they can make you more productive and your code less repetitive.
    Last edited by dwks; 09-12-2009 at 04:21 PM.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    o_O? Like I said, I'm a beginner, so I have no idea what you are talking about...
    Might I suggest you do some further research then before proceeding with your game.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    If you don't want to do research, just get your feet wet, and keep doing what you're doing. Then when you decide it's too much of a pain in the ass to do it that way, do some research and figure out a better way.

    Hope is the first step on the road to disappointment.

  5. #20
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    I will side with abachler on this in agreeing that if you don't want to research then learn by doing. Jump in and code and when it breaks or you hit a wall (because both it and you will) figure out how to get around it. Nothing is better experience than having 'been there' and found a solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. text rpg help
    By xxwerdxx in forum Game Programming
    Replies: 1
    Last Post: 11-26-2005, 08:16 PM
  3. Check out My Text Rpg Game
    By knight543 in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2002, 10:40 PM
  4. Text based GUI?
    By jon_nc17 in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2002, 11:45 AM
  5. Text Based Game
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2002, 06:21 PM