Thread: Criteria based random numbers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Here's a basic framework for you. Add any number of weapons you like. Make sure you add some code somehwere to do srand()...I assume you already know that part. You could also incorporate some of what Salem said so that certain weapons are more likely than others to come up.

    Code:
    # include <vector>
    # include <iostream>
    using namespace std;
    
    class weapon {
        public:
        weapon() {}
        virtual ~weapon() {}
    
        // other virtual functions here
        virtual void use_it() = 0;
    };
    
    class shotgun : public weapon {
        public:
        // implement virtual functions here
        void use_it() {
            cout << "Firing shotgun!" << endl;
        }
    };
    
    class grenade : public weapon {
        public:
        // implement virtual functions here
        void use_it() {
            cout << "Throwing grenade" << endl;
        }
    };
    
    // Factory functions to create your objects
    weapon* create_shotgun()
    {
        return static_cast<weapon *>(new shotgun());
    }
    
    weapon* create_grenade()
    {
        return static_cast<weapon *>(new grenade());
    }
    
    // serves out weapons on a random basis
    class WeaponProvider {
        public:
        WeaponProvider() {
            // TODO: Add srand() code here
        }
    
        // register any number of weapons
        void register_weapon(weapon*(*create_function)()) {
            options_.push_back(create_function);
        }
    
        // get a random weapon, don't forget to delete it when you're done
        weapon* get_weapon() {
            int n = rand() % options_.size();
    
            return options_[n]();
        }
    
        private:
        vector<weapon*(*)()> options_;
    };
    
    // demo it
    int main() {
        WeaponProvider wp;
    
        wp.register_weapon(create_shotgun);
        wp.register_weapon(create_grenade);
    
        for (int i = 0; i < 10; i++) {
           weapon* w = wp.get_weapon();
           w->use_it();
           delete w;
        }
    
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 02:56 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM