Advertisement
TripleAlpha

Untitled

Dec 2nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. /*Author: [redacted]
  2. *Date: 12/02/2019
  3. *Instructor: Dr. Thompson
  4. *Description: Anthony leads the army of Colony A in conquest of the other colonies.*/
  5.  
  6. #include<iostream>
  7. #include<cstdlib>
  8. #include<ctime>
  9.  
  10. using namespace std;
  11.  
  12. const int SIZE = 10;
  13.  
  14. //prints header
  15. void printHeader() {
  16. cout << "\n+------------------------------------------------------+\n"
  17. << "| Computer Science and Engineering |\n"
  18. << "| CSCE 1030 - Computer Science I |\n"
  19. << "| [redacted] |\n"
  20. << "+--------------------------------------------------------+\n" << endl;
  21. }
  22.  
  23. //prints description of game
  24. void printIntro() {
  25. printf("\n%35s\n\n", "Welcome to Anthony's Battle!");
  26. printf("----------------------------------------------------------\n");
  27. cout << "Anthony the ant has decided that he wants to take over all\n"
  28. << "the adjacent ant colonies with his army of ants from colo-\n"
  29. << "ny \'A\'. To do this, Anthony's army will attempt to capture\n"
  30. << "ants from columns \'B\' through \'J\', with at least 1 ant re-\n"
  31. << "maining when the column \'J\' ant colony is found. Each col-\n"
  32. << "umn's ant colony is identified with a - in one of the rows\n"
  33. << "for that column, while patrols are represented by integers\n"
  34. << "between 1 and 10 for the number of ants in that particular\n"
  35. << "patrol. On the way to column \'J\' if Anthony's army encoun-\n"
  36. << "ters a patrol, the number in the patrol is subtracted from\n"
  37. << "Anthony's army total. Once his army finds the colony, how-\n"
  38. << "ever, any patrols not confronted will be added to his army\n"
  39. << "total. If Anthony makes really good decisions, then he can\n"
  40. << "take over all of the adjacent ant colonies; otherwise, his\n"
  41. << "army will be defeated!" << endl;
  42. printf("----------------------------------------------------------\n");
  43. }
  44.  
  45. //initializes colonies matrix
  46. void populateColonies(int** colonies, int patrolSize) {
  47. srand(time(0)); //seeds rand
  48. int colPos; //will hold random int representing position of colony
  49. //iterates through each column in the colonies matrix
  50. for(int col = 0; col < SIZE; col++) {
  51. //iterates through each position in each column
  52. for(int row = 0; row < SIZE; row++) {
  53. //assigns each patrol's size a to a number between 1 and 10 (inc.)
  54. patrolSize = rand() % 10 + 1;
  55. colonies[row][col] = patrolSize;
  56. }
  57. //randomly determines position of colony and sets to 0
  58. colPos = rand() % 10;
  59. colonies[colPos][col] = 0;
  60. }
  61. }
  62.  
  63. //displays matrix of colonies
  64. void displayColonies(int** colonies, int currentCol) {
  65. cout << " ";
  66.  
  67. //print column headers as letters referring to colonies
  68. for(char i = 'A'; i <= 'J'; i++) {
  69. printf("%3c", i);
  70. }
  71. cout << "\n +-------------------------------+" << endl;
  72.  
  73. //iterate through matrix and print successive rows
  74. for(int row = 0; row < SIZE; row++) {
  75. cout << row << "| ";
  76. //iterate up to but not including current column
  77. for(int col = 0; col < currentCol; col++) {
  78. if(colonies[row][col] == 1212) {
  79. printf("%2s ", "XX");
  80. }
  81. else {
  82. printf("%02d ", colonies[row][col]);
  83. }
  84. }
  85. //iterate from current column to end
  86. for(int col = currentCol; col < SIZE; col++) {
  87. if(colonies[row][col] == 1212) {
  88. printf("%2s ", "XX");
  89. }
  90. else {
  91. printf("%2s ", "**");
  92. }
  93. }
  94. cout << "|" << endl;
  95. }
  96. cout << " +-------------------------------+\n" << endl;
  97. }
  98.  
  99. //checks status of row selection during game
  100. bool checkStatus(int row, int col, int armySize, int** colonies) {
  101. if(colonies[row][col] == 0) {
  102. return true;
  103. }
  104. else if(colonies[row][col] == 1212) {
  105. cout << "Ant patrol at this location has already been removed." << endl;
  106. }
  107. else {
  108. armySize -= colonies[row][col];
  109. cout << "Anthony's army suffered casualties and has " << armySize << "ants remaining." << endl;
  110. //1212 is a sentinel value indicating visit (XX)
  111. colonies[row][col] = 1212;
  112. return false;
  113. }
  114. }
  115.  
  116. int main () {
  117. //prints header and introduction by calling corresponding functions
  118. printHeader();
  119. printIntro();
  120.  
  121. //dynamic colonies matrix declared
  122. int** colonies = new int*[SIZE];
  123. //arrays declared iteratively within matrix to eliminate null pointers
  124. for(int col = 0; col < SIZE; col++) {
  125. colonies[col] = new int[SIZE];
  126. }
  127.  
  128. int patrolSize; //int to hold number of ants in each cell
  129. //passes colony matrix by pointer to populate it
  130. populateColonies(colonies, patrolSize);
  131.  
  132. //stores size of Anthony's army
  133. int armySize = 0;
  134.  
  135. //finds sum of patrol sizes for Anthony's army (Colony A)
  136. for(int row = 0; row < SIZE; row++) {
  137. armySize += colonies[row][0];
  138. }
  139.  
  140. //initial display at start of game
  141. cout << "Initializing board...\n" << endl;
  142. displayColonies(colonies, 1);
  143. cout << "Now let's begin... Anthony's army is " << armySize << "strong! \n" << endl;
  144.  
  145. //loop to run game
  146. char currentCol = 'B';
  147. int row = 0;
  148. while(armySize >= 1 && currentCol <= 'J') {
  149. //ensures that row position is within range
  150. while(!(row >= 0 && row <= 9)) {
  151. printf("Enter row position in column %c to strike: ", currentCol);
  152. cin >> row;
  153. if(!(row >= 0 && row <= 9))
  154. cout << "Please enter a row position between 0 and 9 (inclusive)." << endl;
  155. }
  156. //col offset by 65 to convert from char to int
  157. if(checkStatus(row, currentCol - 65, armySize, colonies)) {
  158. //conquers remaining patrols and assimilates into army
  159. for(int row = 0; row < SIZE; row++) {
  160. if(colonies[row][currentCol - 65] != 1212)
  161. armySize += colonies[row][currentCol - 65];
  162. }
  163. currentCol++;
  164. }
  165. //subtract 65 to convert from char to int equivalent
  166. displayColonies(colonies, currentCol - 65);
  167. }
  168.  
  169. //ending message (for victory or loss)
  170. if(armySize >= 1) { //indicates victory
  171. cout << "Congratulations! Anthony's army was victorious, with " << armySize << "ants remaining!" << endl;
  172. }
  173. else {
  174. cout << "Sorry, but Anthony's army has been defeated before taking over all ant colonies." << endl;
  175. //current column is set to 9 so that the entire board is printed
  176. displayColonies(colonies, 9);
  177. }
  178.  
  179. //returns memory for colonies matrix back to freestore
  180. delete[] colonies;
  181. for(int col = 0; col < SIZE; col++) {
  182. delete[] colonies[col];
  183. }
  184.  
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement