Thread: Randomizer for CounterStrike - help

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Question Randomizer for CounterStrike - help

    Hey guys, I'm working on a program that will randomly generate a mapcycle.txt file for Counter-Strike so that when it's run, it creates a list that has no repeated members. I keep getting an error in my Client file everytime I call the functions in my Class. Anyone have any ideas?

    //-------------------------------------------------------//
    // NAME: Christopher Jackson //
    //-------------------------------------------------------//

    //-------------------------------------------------------------------------------// Counter-Strike Map Randomizer
    // CLIENT FILE
    // DESCRIPTION: Randomizes map names for the multiplayer
    // Counter-Strike map cycle list
    //-------------------------------------------------------------------------------

    #include "List.h"
    #include <fstream>

    using namespace std;


    int main()
    {
    List cstrike;
    ofstream fileout;
    fileout.open("mapcycle.txt");
    bool tempTruth = cstrike.IsFull();
    while(!tempTruth)
    {
    int random = cstrike.RandomNumber();
    switch(random)
    {
    case 1 : fileout << "cs_747" << endl; break;
    case 2 : fileout << "cs_italy" << endl; break;
    case 3 : fileout << "cs_backalley" << endl; break;
    case 4 : fileout << "cs_office" << endl; break;
    case 5 : fileout << "cs_militia" << endl; break;
    case 6 : fileout << "cs_assault" << endl; break;
    case 7 : fileout << "cs_siege" << endl; break;
    case 8 : fileout << "de_dust" << endl; break;
    case 9 : fileout << "de_vegas" << endl; break;
    case 10 : fileout << "de_inferno" << endl; break;
    case 11 : fileout << "de_train" << endl; break;
    case 12 : fileout << "de_prodigy" << endl; break;
    case 13 : fileout << "de_cbble" << endl; break;
    case 14 : fileout << "de_dust2" << endl; break;
    case 15 : fileout << "de_aztec" << endl; break;
    case 16 : fileout << "de_vertigo" << endl; break;
    case 17 : fileout << "de_storm" << endl; break;
    case 18 : fileout << "de_torn" << endl; break;
    case 19 : fileout << "de_survivor" << endl; break;
    default: fileout << "* ERROR IN CODE *"; break;
    }
    }

    fileout.close();
    return 0;
    }


    //------------------------------------------------------------------------------
    // Counter-Strike Map Randomizer
    // IMPLEMENTATION FILE
    // DESCRIPTION: Randomizes map names for the multiplayer
    // Counter-Strike map cycle list
    //------------------------------------------------------------------------------

    #include "List.h"
    #include <iostream>

    #include <stdlib.h>
    #include <time.h>

    using namespace std;

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    List::List()
    {
    length = 0;
    }

    //-----------------------------------------------------------------------------

    bool List::IsFull() const
    {
    return (length == MAX_LENGTH);
    }

    //-----------------------------------------------------------------------------

    void List::Insert(/*in*/ ItemType item)
    {
    data[length] = item;
    length++;
    }

    //-----------------------------------------------------------------------------

    bool List::IsPresent(/*in*/ ItemType item) const
    {
    int index = 0;
    while (index < length && item != data[index])
    index++;
    return (index < length);
    }

    //----------------------------------------------------------------------------

    int List::RandomNumber()
    {
    int tempRandom;

    srand(time(NULL));
    do
    {
    tempRandom = rand()%19+1;
    }
    while (IsPresent(tempRandom));

    Insert(tempRandom);
    return tempRandom;
    }


    Thanks to all of you who read this far.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    just putting [ code] brackets around it:
    Code:
    //-------------------------------------------------------// 
    // NAME: Christopher Jackson // 
    //-------------------------------------------------------// 
    
    //-------------------------------------------------------------------------------// Counter-Strike Map Randomizer 
    // CLIENT FILE 
    // DESCRIPTION: Randomizes map names for the multiplayer 
    // Counter-Strike map cycle list 
    //------------------------------------------------------------------------------- 
    
    #include "List.h" 
    #include <fstream> 
    
    using namespace std; 
    
    
    int main() 
    { 
    List cstrike; 
    ofstream fileout; 
    fileout.open("mapcycle.txt"); 
    bool tempTruth = cstrike.IsFull(); 
    while(!tempTruth) 
    { 
    int random = cstrike.RandomNumber(); 
    switch(random) 
    { 
    case 1 : fileout << "cs_747" << endl; break; 
    case 2 : fileout << "cs_italy" << endl; break; 
    case 3 : fileout << "cs_backalley" << endl; break; 
    case 4 : fileout << "cs_office" << endl; break; 
    case 5 : fileout << "cs_militia" << endl; break; 
    case 6 : fileout << "cs_assault" << endl; break; 
    case 7 : fileout << "cs_siege" << endl; break; 
    case 8 : fileout << "de_dust" << endl; break; 
    case 9 : fileout << "de_vegas" << endl; break; 
    case 10 : fileout << "de_inferno" << endl; break; 
    case 11 : fileout << "de_train" << endl; break; 
    case 12 : fileout << "de_prodigy" << endl; break; 
    case 13 : fileout << "de_cbble" << endl; break; 
    case 14 : fileout << "de_dust2" << endl; break; 
    case 15 : fileout << "de_aztec" << endl; break; 
    case 16 : fileout << "de_vertigo" << endl; break; 
    case 17 : fileout << "de_storm" << endl; break; 
    case 18 : fileout << "de_torn" << endl; break; 
    case 19 : fileout << "de_survivor" << endl; break; 
    default: fileout << "* ERROR IN CODE *"; break; 
    } 
    } 
    
    fileout.close(); 
    return 0; 
    } 
    
    
    //------------------------------------------------------------------------------ 
    // Counter-Strike Map Randomizer 
    // IMPLEMENTATION FILE 
    // DESCRIPTION: Randomizes map names for the multiplayer 
    // Counter-Strike map cycle list 
    //------------------------------------------------------------------------------ 
    
    #include "List.h" 
    #include <iostream> 
    
    #include <stdlib.h> 
    #include <time.h> 
    
    using namespace std; 
    
    ////////////////////////////////////////////////////////////////////// 
    // Construction/Destruction 
    ////////////////////////////////////////////////////////////////////// 
    
    List::List() 
    { 
    length = 0; 
    } 
    
    //----------------------------------------------------------------------------- 
    
    bool List::IsFull() const 
    { 
    return (length == MAX_LENGTH); 
    } 
    
    //----------------------------------------------------------------------------- 
    
    void List::Insert(/*in*/ ItemType item) 
    { 
    data[length] = item; 
    length++; 
    } 
    
    //----------------------------------------------------------------------------- 
    
    bool List::IsPresent(/*in*/ ItemType item) const 
    { 
    int index = 0; 
    while (index < length && item != data[index]) 
    index++; 
    return (index < length); 
    } 
    
    //---------------------------------------------------------------------------- 
    
    int List::RandomNumber() 
    { 
    int tempRandom; 
    
    srand(time(NULL)); 
    do 
    { 
    tempRandom = rand()%19+1; 
    } 
    while (IsPresent(tempRandom)); 
    
    Insert(tempRandom); 
    return tempRandom; 
    }
    why bother with classes?
    pseudocode:
    get a array or linked list of the map names
    randomly select them and print it to the file. make sure nothing is picked twice

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    I agree.

    Here is a little algorithm I made to shuffle a deck. I'm sure you can convert it to handle arrays of strings.

    Code:
    srand( time(NULL) );
    
    void shuffleDeck(int deck[])
    {
       int i,      // Counter 
           left,   // Left marker 
           right;  // Right marker
       
       // Swap 150 times
       for(i = 0; i < 150; i++) 
       {
          left = rand() % 52;   // Choose a random number for index (0 - 51)
          right = rand() % 52;  // Chose another random number for index (0 - 51)
          
          if( left != right )   // If the indexes don't equal, swap the elements
             swap(deck, left, right);
       }
    }
    It essentially takes an array and just keeps swapping elements over and over again randomly. After a certain number of times swapping, the array (or deck in this case) will be shuffled. I also make sure that the two indexes don't equal each other b/c it would be pointless to try and swap an element with itself.
    Last edited by biosx; 03-18-2002 at 04:18 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you don't post List.h so can't comment on whether there is problem there.

    In main()'s while loop add this line after switch statement is completed:

    tempTruth = cstrike.IsFull();

    so that you check if cstrike is full after each new number is added. Otherwise the while loop will run forever since tempTruth is not otherwise changed by any of code within the while loop, that I can see anyway.

    Only use srand() one time per program. Take it out of the class and put it into main() but outside and before the while loop in main()

    If ItemType isn't typedefed to type int someplace, change ItemType to int where ever it appears.

    Consider ygfperson's advise, unless you are doing this partly for practice writing classes.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You could use a std::map to ensure that there are no repeats. Something like -

    Code:
    #include <iostream>
    #include <vector>
    #include <map>
    #include <ctime>
    #include <string>
    using namespace std;
    
    class MapRandomiser
    {
    private:
    	int no_of_maps;
    	vector <string> mapnames;
    	map<int,string> mapmap;
    	
    public:
    	MapRandomiser(){
    	srand((unsigned)time(0));
    	}
    	
    	void AddMap(string mapname){
    		mapnames.push_back(mapname);
    	}
    
    	void Shuffle(){
    		no_of_maps=mapnames.size();
    		mapmap.clear();
    		vector<string>::iterator it=mapnames.begin();
    		while(it!=mapnames.end())
    		{
    			int i = rand()%no_of_maps;
    			if(mapmap.insert(make_pair(i,*it)).second)
    				it++;
    		}
    			
    	}
    
    	friend ostream& operator<<(ostream& os,MapRandomiser& m){
    		
    		if(m.mapmap.empty())
    			return os;
    
    		map<int,string>::iterator it;
    
    		for(it=m.mapmap.begin();it!=m.mapmap.end();++it)
    			os<<it->second << '\n';
    		return os;
    	}
    };
    
    	
    
    int main(){
    
    	MapRandomiser mr;
    	mr.AddMap("cs_747");
    	mr.AddMap("cs_italy");
    	mr.AddMap("cs_backalley");
    	mr.AddMap("cs_office");
    	mr.AddMap("cs_militia");
    	mr.AddMap("cs_assault");
    	mr.AddMap("cs_siege");
    	mr.Shuffle();
    	cout << mr;
      
    	return 0;
    }
    Last edited by Sorensen; 03-18-2002 at 04:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomizer
    By barneygumble742 in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2005, 08:06 AM