I am trying to make a text version of risk, and I need to store the population, or army count, for each country. So the only way I could think to do that, as you'll see in my code, is by making a variable for each country, and then storing those variables in an array for easy access by functions and so forth. I was wondering if there was a simpler way, not necessarily easier to understand, by required less typing. The section of code I'm talking about is the massive block of 20 unsigned int's just above the array that holds those variables...

/**************************** 
 * ISU program (text risk)  *
 * created by Matthew       *
 * in grade 12 programming. *
 ****************************/ 
 
/*****************************************
              Things to finish
     attackFunction()
     fortifyFunction()
 
     AI?
*****************************************/
#include <iostream>
#include <list>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
class humanPlayer
     {
     public:                                                             
          humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum); 
          ~humanPlayer(){};  
 
          void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);} 
          void showList();
          int getSpecificListEntry(unsigned int memberSpot); 
          int listSize() {return countriesOwned.size();}
          void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
          bool checkIfInList(unsigned int countryToCheck);
 
          string getPlayerName() {return playerName;}  
          string getPlayerInitial() {return playerInitial;}
 
          int getPlayerTurnNum() {return playerTurnNum;}
          void setPlayerTurnNum(unsigned int newPlayerTurnNum);
 
     private:
          list<int> countriesOwned;
          string playerName;
          string playerInitial;
          unsigned int playerTurnNum;
     };
     humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
          {
          playerName = thePlayersName;
          playerTurnNum = thePlayersTurnNum;
          playerInitial = thePlayersName[0];
          }
 
     bool humanPlayer::checkIfInList(unsigned int countryToCheck)
          {
          if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
               {
               return true;
               }
          else
               {
               return false;
               }
          }
 
     void humanPlayer::showList()
          {
          copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
          cout << endl;
          } 
 
     void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
          {
          playerTurnNum = newPlayerTurnNum;
          }
 
     int humanPlayer::getSpecificListEntry(unsigned int memberSpot)
          {
          list<int>::iterator iter;
          iter = countriesOwned.begin();
          (*iter) += memberSpot;
          return (*iter);
          }  
 
 
int getName(), attackFunction(humanPlayer &), fortifyFunction(humanPlayer &);
void countryDivider(), renderBoard(), mainTurn(), worldAffairsFunction();
unsigned int numOfPlayers;
list<humanPlayer> jailHouse;
vector<int> virginCountries;
string playerInitialList[20] = " ";
unsigned int country00 = 0;
unsigned int country01 = 0;
unsigned int country02 = 0;
unsigned int country03 = 0;
unsigned int country04 = 0;
unsigned int country05 = 0;
unsigned int country06 = 0;
unsigned int country07 = 0;
unsigned int country08 = 0;
unsigned int country09 = 0;
unsigned int country10 = 0;
unsigned int country11 = 0;
unsigned int country12 = 0;
unsigned int country13 = 0;
unsigned int country14 = 0;
unsigned int country15 = 0;
unsigned int country16 = 0;
unsigned int country17 = 0;
unsigned int country18 = 0;
unsigned int country19 = 0;
int countryPopulationArray[20] = { country00, country01, country02, country03,
                                   country04, country05, country06, country07,
                                   country08, country09, country10, country11,
                                   country12, country13, country14, country15,
                                   country16, country17, country18, country19 };
 
int main()
     {
     cout << "Welcome to text risk, the rules are the same as the board game, minus risk cards, and an altered "
          << "fortification rule set.  First off ";
 
     getName();
 
     cout << endl << "The countries are now going to be randomly divided between the " 
          << numOfPlayers << " of you:" << endl;
     countryDivider();
 
     renderBoard();
     mainTurn();
 
     cout << "\n"; system("PAUSE");
     }
 
void mainTurn()
     {
     list<humanPlayer>::iterator iter;
     int initialTurnChoice = 0;
     while(true)
          {
          for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
               {
               bool turn = true;
               while(turn == true)
                    {
                    cout << endl << "Do you, " << (*iter).getPlayerName() << ", wish to: " << endl
                         << "(1) Attack someone." << endl
                         << "(2) Fortify somewhere." << endl
                         << "(3) Don't do anything to anyone." << endl
                         << "(4) Display current world affairs." << endl;
                    cin >> initialTurnChoice;
 
                    switch(initialTurnChoice)
                         {
                         case 1: attackFunction(*iter);
                                 break;
                         case 2: fortifyFunction(*iter);
                                 break;
                         case 3: turn = false;
                                 break;
                         case 4: worldAffairsFunction();
                                 break;
                         }
                    }
               }
          }
     }
 
int getName()
     {
     cout << "how many players will there be? ";
     cin >> numOfPlayers;
     cout << endl;
 
     for(unsigned int i=0 ; i<numOfPlayers ; i++)
          {
          char name[256];
          cin.ignore(255,'\n');
          cout << "What is player " << (i+1) << "'s name? ";
          cin.get(name,256);
          jailHouse.push_back(humanPlayer(name,(i+1)));
          } 
     }   
 
void countryDivider()
     {
     list<humanPlayer>::iterator iter;
     unsigned int counter = 0;
 
     for(unsigned int i=0 ; i<20 ; i++)
          {
          virginCountries.push_back(i);
          }
 
     random_shuffle(virginCountries.begin(), virginCountries.end()); 
 
     while(counter != 20)
          {
          for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
               {
               (*iter).addToList(virginCountries.at(0));
               playerInitialList[virginCountries.at(0)] = (*iter).getPlayerInitial();
               virginCountries.erase(virginCountries.begin());
               counter += 1;
               if(counter == 20)
                    {
                    break;
                    }
               }
          }
 
     //test
     for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
          {
          cout << (*iter).getPlayerName() << ": ";
          (*iter).showList();
          }
     }
 
void renderBoard()
     {
     cout << endl
          << "         __     ____" << endl
          << "   N.A. |  \\   /    ." << endl
          << "         \\__|  \\ 2  |          ____" << endl
          << "  ____________  |" << playerInitialList[2] << " /  __  Eu  / |  |" << endl
          << " /   |  1  |  \\  \\|  /13\\ ___/  |  |" << endl
          << "/  0 |__" << playerInitialList[1] << "__|_/  |\\   ." << playerInitialList[13] << "_// 14  /   |" << endl
          << "|  " << playerInitialList[0] << " |  3 | 4|__|5\\      /  " << playerInitialList[14] << "  /    |" << endl
          << " \\/ \\|__" << playerInitialList[3] << "_|_" << playerInitialList[4] << "__|_" << playerInitialList[5] << "_\\ __ |_/|_/| 19 /" << endl
          << " /   |  6  | 7   /  /15\\ __/  | " << playerInitialList[19] << " /" << endl
          << "     |__" << playerInitialList[6] << "__|_" << playerInitialList[7] << "_/    ." << playerInitialList[15] << "_/ | 16 |  /" << endl
          << "        \\ 8  |          /  _" << playerInitialList[16] << " | |" << endl
          << "         \\ " << playerInitialList[8] << " \\         /\\ / \\/\\ |" << endl
          << "           \\ \\        /17|18/  ||" << endl
          << "            _\\_      |" << playerInitialList[17] << "_/ ." << playerInitialList[18] << "   \\|" << endl
          << "      S.A._/ 9 \\__" << endl
          << "         /___" << playerInitialList[9] << "____|" << endl
          << "         |   |    /" << endl
          << "          \\ 10|11/" << endl
          << "           |" << playerInitialList[10] << " |" << playerInitialList[11] << "/" << endl
          << "           |__|/" << endl
          << "           |12/" << endl
          << "           |" << playerInitialList[12] << "/" << endl
          << "           |/" << endl;
     } 
int attackFunction(humanPlayer & obj)
     {
     cout << endl << obj.getPlayerName() << ", which country do you wish to attack from? ";
     }
int fortifyFunction(humanPlayer & obj)
     {
     }
void worldAffairsFunction()
     {
     list<humanPlayer>::iterator iter;
     cout << endl << "This is the current state of the game, player by player: \n";
     renderBoard();
     for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
          {
          cout << (*iter).getPlayerName() << " has " << (*iter).listSize() << " countries in their possesion."
               << "\nAnd those countries are as follows: ";
          (*iter).showList();
          }
     }

Why in the world do you need those unsigned int variables to initialize the array? You can just do this and get the same effect:

int countryPopulationArray[20] = {0};

Wow, thank you, what a difference in code length....

Just to let you know, there are two more methods of doing the same thing: (but still not recommended)

int arr[20];

//first, 
for(int i = 0; i < 20; ++i)
   arr[i] = 0;

//second, better than first.
memset(arr, 0, 20);

In that case memset is better. In just about every other case not involving arrays of integral type, it's a really bad idea. memset does a zero-fill of the array, and most types in C++ either don't allow that, or it doesn't represent a zero value. Since this is C++, the copy function template from <algorithm> would be a much better choice.

But int arr[BUFSIZ] = {0}; internally uses memset to actually perform its task.

>But int arr[BUFSIZ] = {0}; internally uses memset to actually perform its task.
Would you like me to give you some time to take that back before I reply? Because you really aren't going to like it if I do. ;)

Member Avatar for iamthwee

You should also avoid "system pause", and it wouldn't be a bad idea to include #include <string> either.

commented: I agree, system("PAUSE") is evil +2

I guess I got confused between array initializers and memset. Hmm..but I do vaguely recollect someone mentioning something similar a long time back on the newsgroups.

>Hmm..but I do vaguely recollect someone mentioning something similar
>a long time back on the newsgroups.
Array initializers are required to initialize the cells to the data type's variation of 0. So double would be 0.0, float would be 0.0f, int would be 0, char would be '\0', and a regular pointer would be NULL. memset can't do that, so at the very least we could only use it for integral types.

Ah, now I recollect. It was something about whether memset was used internally when an array of integers was being initialized using initializers. It turned out that there was no standard specification as such which prevented the compiler writers from doing this, so it was pretty much guess work of whether memset would really be used or not.

BTW, thanks for giving me time to take back my reply. I wonder what would have become of me failing that. ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.