User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,511 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,652 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 549 | Replies: 6 | Solved
Reply
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

How can I access these arrays without ifs

  #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 8:49 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: How can I access these arrays without ifs

  #2  
Jun 12th, 2007
>I was thinking maybe an array of arrays
You were thinking correctly. Something like this perhaps?
#include <iostream>
#include <ios>
#include <limits>

int main()
{
  int a[][5] = {
    {1,2,3,4,5},
    {5,4,3,2,1},
  };
  int index;

  std::cout<<"Enter 1 for ascending or 2 for descending: ";

  while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
    std::cerr<<"Invalid input\n";
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout<<"Enter 1 for ascending or 2 for descending: ";
  }

  for ( int i = 0; i < 5; i++ )
    std::cout<< a[index - 1][i] <<' ';
  std::cout<<'\n';
}
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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,401
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: How can I access these arrays without ifs

  #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  
Join Date: Jun 2006
Location: America
Posts: 186
Reputation: Matt Tacular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Junior Poster

Re: How can I access these arrays without ifs

  #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 9:44 am.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: How can I access these arrays without ifs

  #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:
#include <iostream>
#include <ios>
#include <limits>

int main()
{
  int **a = new int*[2];

  a[0] = new int[5];
  a[1] = new int[3];

  for ( int i = 0; i < 5; i++ )
    a[0][i] = i + 1;

  for ( int i = 0; i < 3; i++ )
    a[1][i] = 3 - i;

  int index;

  std::cout<<"Enter 1 for ascending or 2 for descending: ";

  while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
    std::cerr<<"Invalid input\n";
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout<<"Enter 1 for ascending or 2 for descending: ";
  }

  int limit = 5;

  if ( index != 1 )
    limit = 3;

  for ( int i = 0; i < limit; i++ )
    std::cout<< a[index - 1][i] <<' ';
  std::cout<<'\n';

  delete[] a[1];
  delete[] a[0];
  delete[] a;
}
That's a lot harder and a lot easier to screw up. Alternatively, and this is your best option, use a vector of vectors:
#include <iostream>
#include <ios>
#include <limits>
#include <vector>

int main()
{
  std::vector< std::vector<int> > v;

  v.push_back ( std::vector<int> ( 5 ) );
  v.push_back ( std::vector<int> ( 3 ) );

  for ( int i = 0; i < 5; i++ )
    v[0][i] = i + 1;

  for ( int i = 0; i < 3; i++ )
    v[1][i] = 3 - i;

  std::vector<int>::size_type index;

  std::cout<<"Enter 1 for ascending or 2 for descending: ";

  while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
    std::cerr<<"Invalid input\n";
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout<<"Enter 1 for ascending or 2 for descending: ";
  }

  for ( std::vector<int>::size_type i = 0; i < v[index - 1].size(); i++ )
    std::cout<< v[index - 1][i] <<' ';
  std::cout<<'\n';
}
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,401
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: How can I access these arrays without ifs

  #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  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: How can I access these arrays without ifs

  #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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC