How can I access these arrays without ifs

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

How can I access these arrays without ifs

 
0
  #1
Jun 12th, 2007
I was wondering how I could access my several countryAttachedXX arrays(in this post they start on line 85), without many if statements, determined by user input. I was thinking maybe an array of arrays, but had difficulty finding out how to do that on the web. Thanks,
-Matt
  1. /****************************
  2.  * ISU program (text risk) *
  3.  * created by Matthew *
  4.  * in grade 12 programming. *
  5.  ****************************/
  6.  
  7. #include <iostream>
  8. #include <list>
  9. #include <iterator>
  10. #include <vector>
  11. #include <algorithm>
  12. using namespace std;
  13. class humanPlayer
  14. {
  15. public:
  16. humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum);
  17. ~humanPlayer(){};
  18.  
  19. void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);}
  20. void showList();
  21. int getSpecificListEntry(unsigned int memberSpot);
  22. int listSize() {return countriesOwned.size();}
  23. void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
  24. bool checkIfInList(unsigned int countryToCheck);
  25.  
  26. string getPlayerName() {return playerName;}
  27. string getPlayerInitial() {return playerInitial;}
  28.  
  29. int getPlayerTurnNum() {return playerTurnNum;}
  30. void setPlayerTurnNum(unsigned int newPlayerTurnNum);
  31.  
  32. private:
  33. list<int> countriesOwned;
  34. string playerName;
  35. string playerInitial;
  36. unsigned int playerTurnNum;
  37. };
  38. humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
  39. {
  40. playerName = thePlayersName;
  41. playerTurnNum = thePlayersTurnNum;
  42. playerInitial = thePlayersName[0];
  43. }
  44.  
  45. bool humanPlayer::checkIfInList(unsigned int countryToCheck)
  46. {
  47. if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
  48. {
  49. return true;
  50. }
  51. else
  52. {
  53. return false;
  54. }
  55. }
  56.  
  57. void humanPlayer::showList()
  58. {
  59. copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
  60. cout << endl;
  61. }
  62.  
  63. void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
  64. {
  65. playerTurnNum = newPlayerTurnNum;
  66. }
  67.  
  68. int humanPlayer::getSpecificListEntry(unsigned int memberSpot)
  69. {
  70. list<int>::iterator iter;
  71. iter = countriesOwned.begin();
  72. (*iter) += memberSpot;
  73. return (*iter);
  74. }
  75.  
  76.  
  77. int getName(), attackFunction(humanPlayer &), fortifyFunction(humanPlayer &);
  78. void countryDivider(), renderBoard(), mainTurn(), worldAffairsFunction();
  79. unsigned int numOfPlayers;
  80. list<humanPlayer> jailHouse;
  81. list<humanPlayer>::iterator iter;
  82. vector<int> virginCountries;
  83. string playerInitialList[20] = " ";
  84. int countryPopulationArray[20] = {1};
  85. int countryAttached00[2] = {1,3};
  86. int countryAttached01[4] = {0,2,3,4};
  87. int countryAttached02[4] = {1,4,5,13};
  88. int countryAttached03[4] = {0,1,4,6};
  89. int countryAttached04[6] = {1,2,3,5,6,7};
  90. int countryAttached05[3] = {2,4,7};
  91. int countryAttached06[4] = {3,4,7,8};
  92.  
  93. int main()
  94. {
  95. cout << "Welcome to text risk, the rules are the same as the board game,\n"
  96. << "minus risk cards, and an altered fortification rule set. \n"
  97. << "First off ";
  98.  
  99. getName();
  100.  
  101. cout << endl << "The countries are now going to be randomly divided between the "
  102. << numOfPlayers << " of you:" << endl;
  103. countryDivider();
  104.  
  105. renderBoard();
  106. mainTurn();
  107.  
  108. cout << "\n"; system("PAUSE");
  109. }
  110.  
  111. void mainTurn()
  112. {
  113. int initialTurnChoice = 0;
  114. while(true)
  115. {
  116. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  117. {
  118. bool turn = true;
  119. while(turn == true)
  120. {
  121. cout << endl << "Do you, " << (*iter).getPlayerName() << ", wish to: " << endl
  122. << "(1) Attack someone." << endl
  123. << "(2) Fortify somewhere." << endl
  124. << "(3) Don't do anything to anyone." << endl
  125. << "(4) Display current world affairs." << endl;
  126. cin >> initialTurnChoice;
  127.  
  128. switch(initialTurnChoice)
  129. {
  130. case 1: attackFunction(*iter);
  131. break;
  132. case 2: fortifyFunction(*iter);
  133. break;
  134. case 3: turn = false;
  135. break;
  136. case 4: worldAffairsFunction();
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. }
  143.  
  144. int getName()
  145. {
  146. cout << "how many players will there be? ";
  147. cin >> numOfPlayers;
  148. cout << endl;
  149.  
  150. while(true)
  151. {
  152. if(numOfPlayers < 2)
  153. {
  154. cout << endl << "You cannot play risk with less than two players, \n"
  155. << "it's just not going to work. So how many people are\n"
  156. << "actually playing risk, total? ";
  157. cin >> numOfPlayers;
  158. }
  159. if(numOfPlayers > 8)
  160. {
  161. cout << endl << "Have you ever tried to play risk with more than 8 players?\n"
  162. << "Cut a couple people and then say how many people are going to\n"
  163. << "play risk, total: ";
  164. cin >> numOfPlayers;
  165. }
  166. if(numOfPlayers > 1)
  167. {
  168. if(numOfPlayers < 9)
  169. {
  170. break;
  171. }
  172. }
  173. }
  174.  
  175. for(unsigned int i=0 ; i<numOfPlayers ; i++)
  176. {
  177. char name[256];
  178. cin.ignore(255,'\n');
  179. cout << "What is player " << (i+1) << "'s name? ";
  180. cin.get(name,256);
  181. jailHouse.push_back(humanPlayer(name,(i+1)));
  182. }
  183. }
  184.  
  185. void countryDivider()
  186. {
  187. unsigned int counter = 0;
  188.  
  189. for(unsigned int i=0 ; i<20 ; i++)
  190. {
  191. virginCountries.push_back(i);
  192. }
  193.  
  194. random_shuffle(virginCountries.begin(), virginCountries.end());
  195.  
  196. while(counter != 20)
  197. {
  198. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  199. {
  200. (*iter).addToList(virginCountries.at(0));
  201. playerInitialList[virginCountries.at(0)] = (*iter).getPlayerInitial();
  202. virginCountries.erase(virginCountries.begin());
  203. counter += 1;
  204. if(counter == 20)
  205. {
  206. break;
  207. }
  208. }
  209. }
  210.  
  211. //test
  212. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  213. {
  214. cout << (*iter).getPlayerName() << ": ";
  215. (*iter).showList();
  216. }
  217. }
  218.  
  219. void renderBoard()
  220. {
  221. cout << endl
  222. << " __ ____" << endl
  223. << " N.A. | \\ / ." << endl
  224. << " \\__| \\ 2 | ____" << endl
  225. << " ____________ |" << playerInitialList[2] << " / __ Eu / | |" << endl
  226. << " / | 1 | \\ \\| /13\\ ___/ | |" << endl
  227. << "/ 0 |__" << playerInitialList[1] << "__|_/ |\\ ." << playerInitialList[13] << "_// 14 / |" << endl
  228. << "| " << playerInitialList[0] << " | 3 | 4|__|5\\ / " << playerInitialList[14] << " / |" << endl
  229. << " [url="http://www.daniweb.com/techtalkforums/"]\\/[/url] \\|__" << playerInitialList[3] << "_|_" << playerInitialList[4] << "__|_" << playerInitialList[5] << "_\\ __ |_/|_/| 19 /" << endl
  230. << " / | 6 | 7 / /15\\ __/ | " << playerInitialList[19] << " /" << endl
  231. << " |__" << playerInitialList[6] << "__|_" << playerInitialList[7] << "_/ ." << playerInitialList[15] << "_/ | 16 | /" << endl
  232. << " \\ 8 | / _" << playerInitialList[16] << " | |" << endl
  233. << " \\ " << playerInitialList[8] << " \\ /\\ / [url="http://www.daniweb.com/techtalkforums/"]\\/\\[/url] |" << endl
  234. << " \\ \\ /17|18/ ||" << endl
  235. << " _\\_ |" << playerInitialList[17] << "_/ ." << playerInitialList[18] << " \\|" << endl
  236. << " S.A._/ 9 \\__" << endl
  237. << " /___" << playerInitialList[9] << "____|" << endl
  238. << " | | /" << endl
  239. << " \\ 10|11/" << endl
  240. << " |" << playerInitialList[10] << " |" << playerInitialList[11] << "/" << endl
  241. << " |__|/" << endl
  242. << " |12/" << endl
  243. << " |" << playerInitialList[12] << "/" << endl
  244. << " |/" << endl;
  245. }
  246. int attackFunction(humanPlayer & obj)
  247. {
  248. unsigned int countryToAttackFrom;
  249. unsigned int countryToAttack;
  250. cout << endl << obj.getPlayerName() << ", which country do you wish to attack from? ";
  251. obj.showList();
  252. cin >> countryToAttackFrom;
  253.  
  254. if(obj.checkIfInList(countryToAttackFrom) == false)
  255. {
  256. cout << "You must attack from your own country.\n";
  257. return 0;
  258. }
  259.  
  260. if(countryPopulationArray[countryToAttackFrom] < 2)
  261. {
  262. cout << "You are unable to attack from that country, the army is too small.\n";
  263. return 0;
  264. }
  265.  
  266. cout << "Now, which country do you wish to attack? The only eligable countries to\n"
  267. << "attack are: ";
  268.  
  269. }
  270. int fortifyFunction(humanPlayer & obj)
  271. {
  272. }
  273. void worldAffairsFunction()
  274. {
  275. cout << endl << "This is the current state of the game, player by player: \n";
  276. renderBoard();
  277. for(iter = jailHouse.begin(); iter != jailHouse.end(); ++iter)
  278. {
  279. cout << (*iter).getPlayerName() << " has " << (*iter).listSize() << " countries in their possesion."
  280. << "\nAnd those countries are as follows: ";
  281. (*iter).showList();
  282. }
  283. }
Last edited by Matt Tacular; Jun 12th, 2007 at 9:49 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,663
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How can I access these arrays without ifs

 
0
  #2
Jun 12th, 2007
>I was thinking maybe an array of arrays
You were thinking correctly. Something like this perhaps?
  1. #include <iostream>
  2. #include <ios>
  3. #include <limits>
  4.  
  5. int main()
  6. {
  7. int a[][5] = {
  8. {1,2,3,4,5},
  9. {5,4,3,2,1},
  10. };
  11. int index;
  12.  
  13. std::cout<<"Enter 1 for ascending or 2 for descending: ";
  14.  
  15. while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
  16. std::cerr<<"Invalid input\n";
  17. std::cin.clear();
  18. std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
  19. std::cout<<"Enter 1 for ascending or 2 for descending: ";
  20. }
  21.  
  22. for ( int i = 0; i < 5; i++ )
  23. std::cout<< a[index - 1][i] <<' ';
  24. std::cout<<'\n';
  25. }
The only problem here is that each sub-array has to have the same number of cells (5 in the previous code). So if you want a ragged array you either need to do some dynamic allocation (which I don't recommend just yet) or pad the shorter arrays with a sentinel value like -1.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: How can I access these arrays without ifs

 
0
  #3
Jun 12th, 2007
what about doing it with a struct? this way every array is independent from the other, so you can create an array of structs...

now, what i would find complicated is instructing the structs the size of the array of ints... maybe creating an int variable in the struct that indicates the size of the array...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: How can I access these arrays without ifs

 
0
  #4
Jun 12th, 2007
Is there a way I can do an actual array of arrays? Not a multidimensional one. That way I could have different sized arrays in it. Or how could I do a pointer to an array, then an array of those pointers?
Last edited by Matt Tacular; Jun 12th, 2007 at 10:44 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,663
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How can I access these arrays without ifs

 
0
  #5
Jun 12th, 2007
>Is there a way I can do an actual array of arrays? Not a multidimensional one.
There's no such thing as a multidimensional array in C++. It's always an array of arrays. What you want is a ragged array, and the only way to do it is to manually simulate the arrays:
  1. #include <iostream>
  2. #include <ios>
  3. #include <limits>
  4.  
  5. int main()
  6. {
  7. int **a = new int*[2];
  8.  
  9. a[0] = new int[5];
  10. a[1] = new int[3];
  11.  
  12. for ( int i = 0; i < 5; i++ )
  13. a[0][i] = i + 1;
  14.  
  15. for ( int i = 0; i < 3; i++ )
  16. a[1][i] = 3 - i;
  17.  
  18. int index;
  19.  
  20. std::cout<<"Enter 1 for ascending or 2 for descending: ";
  21.  
  22. while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
  23. std::cerr<<"Invalid input\n";
  24. std::cin.clear();
  25. std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
  26. std::cout<<"Enter 1 for ascending or 2 for descending: ";
  27. }
  28.  
  29. int limit = 5;
  30.  
  31. if ( index != 1 )
  32. limit = 3;
  33.  
  34. for ( int i = 0; i < limit; i++ )
  35. std::cout<< a[index - 1][i] <<' ';
  36. std::cout<<'\n';
  37.  
  38. delete[] a[1];
  39. delete[] a[0];
  40. delete[] a;
  41. }
That's a lot harder and a lot easier to screw up. Alternatively, and this is your best option, use a vector of vectors:
  1. #include <iostream>
  2. #include <ios>
  3. #include <limits>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. std::vector< std::vector<int> > v;
  9.  
  10. v.push_back ( std::vector<int> ( 5 ) );
  11. v.push_back ( std::vector<int> ( 3 ) );
  12.  
  13. for ( int i = 0; i < 5; i++ )
  14. v[0][i] = i + 1;
  15.  
  16. for ( int i = 0; i < 3; i++ )
  17. v[1][i] = 3 - i;
  18.  
  19. std::vector<int>::size_type index;
  20.  
  21. std::cout<<"Enter 1 for ascending or 2 for descending: ";
  22.  
  23. while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
  24. std::cerr<<"Invalid input\n";
  25. std::cin.clear();
  26. std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
  27. std::cout<<"Enter 1 for ascending or 2 for descending: ";
  28. }
  29.  
  30. for ( std::vector<int>::size_type i = 0; i < v[index - 1].size(); i++ )
  31. std::cout<< v[index - 1][i] <<' ';
  32. std::cout<<'\n';
  33. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: How can I access these arrays without ifs

 
0
  #6
Jun 12th, 2007
the problem is that in arrays of arrays all arrays have the same length, meaning the same quantity of elements, in this case, ints...

that's why i proposed the idea of using a struct with an int and an array of ints, where the int specifies the length of the array...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,663
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How can I access these arrays without ifs

 
0
  #7
Jun 12th, 2007
>that's why i proposed the idea of using a struct with an int and an array
>of ints, where the int specifies the length of the array...
And that's different from a ragged array, how? You still have to dynamically allocate the memory for each instance of the structure. The only real benefit to an array of structures is you can easily store the size of each array. And of course, it's still inferior to using a vector of vectors.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC