Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2. // Purpose: To simulate the lives of rabbits and foxes in the forest.
  3. //
  4. // Author: John Gauch - Created skeleton program.
  5. // Author: Kendall Clancy - Completed implementation.
  6. //
  7. // Change: Fixed animal loss bug in move_board function -- 11/03 JMG
  8. // Change: Fixed infinite loop bug in choose_move function -- 11/03 JMG
  9. // Change: Removed second board from main function -- 11/03 JMG
  10. // Change: Printed animal counts in draw_board -- 11/03 JMG
  11. //---------------------------------------------------------------------------
  12.  
  13. #include <cstdlib>
  14. #include <iostream>
  15. #include<stdio.h>
  16. #include<string.h>
  17. #include <ctime>
  18. using namespace std;
  19.  
  20. // Global Variables
  21. const int SIZE = 20;
  22.  
  23. //---------------------------------------------------------------------------
  24. // Purpose: To initialize the board with rabbits and foxes.
  25. // Inputs: The board, the number of rabbits, the number of foxes.
  26. // Outputs: None
  27. //---------------------------------------------------------------------------
  28. void init_board(char board[SIZE][SIZE],
  29. const int num_rabbits, const int num_foxes)
  30. {
  31. // Init board
  32. for (int r = 0; r < SIZE; r++)
  33. for (int c = 0; c < SIZE; c++)
  34. board[r][c] = ' ';
  35.  
  36. // Add rabbits at random locations
  37. int count = 0;
  38. while (count < num_rabbits)
  39. {
  40. int r = random() % SIZE;
  41. int c = random() % SIZE;
  42. if (board[r][c] == ' ')
  43. {
  44. board[r][c] = 'R';
  45. count++;
  46. }
  47. }
  48.  
  49. // Add foxes at random locations
  50. count = 0;
  51. while (count < num_foxes)
  52. {
  53. int r = random() % SIZE;
  54. int c = random() % SIZE;
  55. if (board[r][c] == ' ')
  56. {
  57. board[r][c] = 'F';
  58. count++;
  59. }
  60. }
  61. }
  62.  
  63. //---------------------------------------------------------------------------
  64. // Purpose: To draw the board with rabbits and foxes.
  65. // Inputs: The board.
  66. // Outputs: None
  67. //---------------------------------------------------------------------------
  68. void draw_board(char board[SIZE][SIZE])
  69. {
  70. // system("sleep 1");
  71. //system("clear");
  72.  
  73. // Initialize animal counts
  74. int num_rabbits = 0;
  75. int num_foxes = 0;
  76.  
  77. // Draw top of board
  78. cout << "+";
  79. for (int c = 0; c < SIZE; c++)
  80. cout << "--";
  81. cout << "+\n";
  82.  
  83. // Draw board
  84. for (int r = 0; r < SIZE; r++)
  85. {
  86. cout << "|";
  87. for (int c = 0; c < SIZE; c++)
  88. {
  89. cout << board[r][c] << " ";
  90.  
  91. // Increment animal counts
  92. if (board[r][c] == 'R')
  93. num_rabbits++;
  94. if (board[r][c] == 'F')
  95. num_foxes++;
  96. }
  97. cout << "|\n";
  98. }
  99.  
  100. // Draw bottom of the board
  101. cout << "+";
  102. for (int c = 0; c < SIZE; c++)
  103. cout << "--";
  104. cout << "+\n";
  105.  
  106. // Print animal counts
  107. cout << "Number of rabbits: " << num_rabbits << endl;
  108. cout << "Number of foxes: " << num_foxes << endl;
  109. }
  110.  
  111. //---------------------------------------------------------------------------
  112. // Purpose: To choose a random direction to move.
  113. // Inputs: The board and the current row and column location.
  114. // Outputs: The new row and column location on the board.
  115. //---------------------------------------------------------------------------
  116. void choose_move(char board[SIZE][SIZE],
  117. const int row, const int col, int & new_row, int & new_col)
  118. {
  119. // Loop until a valid move is found
  120. for (int loop = 0; loop < 10; loop++)
  121. {
  122. // Take a random step to adjacent location
  123. new_row = row + (random() % 3) - 1;
  124. new_col = col + (random() % 3) - 1;
  125.  
  126. // Check if location is in bounds and board is empty
  127. if ((new_row >= 0) && (new_row < SIZE) &&
  128. (new_col >= 0) && (new_col < SIZE) &&
  129. (board[new_row][new_col] == ' '))
  130. return;
  131. }
  132.  
  133. // Use current location if other move is possible
  134. new_row = row;
  135. new_col = col;
  136. }
  137.  
  138. //---------------------------------------------------------------------------
  139. // Purpose: To move the rabbits and foxes on the board.
  140. // Inputs: The board.
  141. // Outputs: None
  142. //---------------------------------------------------------------------------
  143. void move_animals(char board[SIZE][SIZE])
  144. {
  145. srand(time(NULL));
  146. // Loop over board to find and move animals
  147. int new_row = 0;
  148. int new_col = 0;
  149.  
  150. //------------------------------------------------------------------------------------------------------------ my code
  151. int randomgenerated = 1;
  152. int numbergenerated = 1;
  153. int r1 = 1;
  154. int r2 = 1;
  155. r1 = rand() % 20 + 1;
  156. r2 = rand() % 20 + 1;
  157.  
  158. for (int r = 0; r < SIZE; r++)
  159. {
  160. for (int c = 0; c < SIZE; c++)
  161. {
  162.  
  163. if (board[r][c] == 'R' && board[r+1][c] == 'R') //if rabbit is to the right of rabbit
  164. {
  165. //create a new rabbit on the board
  166.  
  167. while(board[r1][r2] != ' ')
  168. {
  169. r1 = rand() % 20 + 1;
  170. r2 = rand() % 20 + 1;
  171. }
  172. board[r1][r2] = 'R';
  173.  
  174. }
  175.  
  176. if (board[r][c] == 'F' && board[r][c-1] == 'F') //if a fox is below another fox
  177. {
  178. //create half chance of fox randomly on the board
  179. randomgenerated = (rand() % 10) + 1;
  180. if(randomgenerated <= 5)
  181. {
  182. //add a new fox on the board
  183. while(board[r1][r2] != ' ')
  184. {
  185. r1 = rand() % 20 + 1;
  186. r2 = rand() % 20 + 1;
  187. }
  188. board[r1][r2] = 'F';
  189. }
  190. }
  191.  
  192. if(board[r][c] == 'F' && board[r][c-1] == 'R') //eat
  193. {
  194. randomgenerated = (rand() % 10) + 1;
  195. //generate 10% chance of being eaten
  196. }
  197. if(board[r][c] == 'F' && board[r][c+1] == 'R')
  198. {
  199. randomgenerated = (rand() % 10) + 1;
  200. //generate 10% chance of being eaten
  201. if(numbergenerated == 10)
  202. {
  203. //rabbit gets eaten
  204. board[r][c+1] = ' ';
  205. }
  206. }
  207.  
  208. if(board[r][c] == 'F' && board[r-1][c] == 'R')
  209. {
  210. randomgenerated = (rand() % 10) + 1;
  211. //generate 10% chance of being eaten
  212. if(numbergenerated == 10)
  213. {
  214. //rabbit gets eaten
  215. board[r-1][c] = ' ';
  216. }
  217. }
  218.  
  219. if(board[r][c] == 'F' && board[r+1][c] == 'R')
  220. {
  221. //generate 10% chance of being eaten
  222. randomgenerated = (rand() % 10) + 1;
  223. if(numbergenerated == 10)
  224. {
  225. board[r+1][c] = ' ';
  226. }
  227. }
  228.  
  229.  
  230. } //end first for
  231. } // end second for
  232.  
  233. //-------------------------------------------------------------------------------------- my code
  234.  
  235. for (int r = 0; r < SIZE; r++)
  236. for (int c = 0; c < SIZE; c++)
  237. if (isupper(board[r][c]))
  238. {
  239. // Find empty location for move
  240. choose_move(board, r, c, new_row, new_col);
  241.  
  242. // Move animal into empty location
  243. board[new_row][new_col] = tolower(board[r][c]);
  244.  
  245. // Make old location empty
  246. if ((new_row != r) || (new_col != c))
  247. board[r][c] = ' ';
  248. }
  249.  
  250. // Loop over board to change letters to upper case
  251. for (int r = 0; r < SIZE; r++)
  252. for (int c = 0; c < SIZE; c++)
  253. board[r][c] = toupper(board[r][c]);
  254.  
  255.  
  256. }
  257.  
  258. //---------------------------------------------------------------------------
  259. // Purpose: The main program.
  260. // Inputs: None.
  261. // Outputs: None.
  262. //---------------------------------------------------------------------------
  263. int main()
  264. {
  265. // Seed the random number generator
  266. srandom(time(NULL));
  267. srand(time(NULL));
  268. // Prompt user
  269. cout << "Welcome to the fox and rabbit simulation\n\n";
  270. int num_rabbits = 0;
  271. cout << "Enter the number of rabbits: ";
  272. cin >> num_rabbits;
  273. int num_foxes = 0;
  274. cout << "Enter the number of foxes: ";
  275. cin >> num_foxes;
  276. int num_steps = 0;
  277. cout << "Enter the number of simulation steps: ";
  278. cin >> num_steps;
  279.  
  280. // Initialize board
  281. char board[SIZE][SIZE];
  282. init_board(board, num_rabbits, num_foxes);
  283.  
  284. // Draw the board
  285. draw_board(board);
  286. cout << "Start " << endl;
  287.  
  288. // Run the simulation
  289. for (int step = 1; step <= num_steps; step++)
  290. {
  291. // Move the animals
  292. move_animals(board);
  293.  
  294. // Draw the board
  295. draw_board(board);
  296.  
  297.  
  298.  
  299. cout << "Step " << step << endl;
  300. }
  301. return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement