Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #import <stdlib.h>
  2. #import <stdbool.h>
  3. #import <stdio.h>
  4.  
  5. #pragma mark Ant
  6.  
  7. typedef struct {
  8. bool hasFood;
  9. int location;
  10. } Ant;
  11.  
  12. #pragma mark -
  13. #pragma mark Cell
  14.  
  15. typedef struct {
  16. int index;
  17. double pheromone;
  18. enum type {
  19. normal,
  20. lfood,
  21. rfood,
  22. colony
  23. };
  24. } Cell;
  25.  
  26. #pragma mark -
  27. #pragma mark World
  28.  
  29. // Struct representing the entire world. Contains some useful
  30. // precalculated metadata.
  31. typedef struct {
  32. Cell *cells;
  33. int pathlen;
  34. int (^size)();
  35. } World;
  36.  
  37. int World_size(World w) {
  38. return w.pathlen * 2 + 3;
  39. }
  40.  
  41. // Creates a World
  42. // Make sure to free it when done!
  43. World *makeWorld(int p_len){
  44. __block World *world = malloc(sizeof(World));
  45. void *cells;
  46.  
  47. // Define "methods" of World
  48. int (^World_size)() = ^{
  49. return (*world).pathlen * 2 + 3;
  50. };
  51.  
  52. // Fill in fields
  53. world->cells = cells;
  54. world->pathlen = p_len;
  55. world->size = World_size;
  56.  
  57. return world;
  58. }
  59.  
  60. #pragma mark -
  61.  
  62. void runSim(int t, int p_len, int n_ants, int decay, int phval, void *ctx){
  63. int n_cells = 2 * p_len + 3;
  64.  
  65. // Initialize world
  66. World *world = makeWorld(p_len);
  67. printf("size: %d\n", world->size());
  68.  
  69. World *world2 = makeWorld(p_len + 3);
  70. printf("size: %d\n", world2->size());
  71.  
  72. free(world);
  73. free(world2);
  74. }
  75.  
  76. int main(int argc, char **argv){
  77. runSim(10, 2, 1, 0.01, 1, NULL);
  78. }
Add Comment
Please, Sign In to add comment