Advertisement
Guest User

game5

a guest
Oct 4th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #define NUM_WORKERS 5
  7. #define MAX_MINERALS 4096
  8. #define MAX_POPULATION 100
  9. #define VICTORY 20
  10. #define WARRIOR_PRICE 25
  11. #define WORKER_PRICE 25
  12.  
  13. unsigned int minerals = 0;
  14. unsigned short warriors = 0;
  15. unsigned short workers = NUM_WORKERS;
  16. unsigned short status = 1;
  17. pthread_mutex_t base_mutex;
  18. /*
  19. void *searching(unsigned short N);
  20. void transporting(unsigned short N);
  21. void unloading(unsigned short N);
  22. */
  23. //void *worker(void *N);
  24. void *worker (void *N)
  25. {
  26. int i;
  27. size_t n = (size_t) N;
  28. for (;;)
  29. {
  30. if (status == 0)
  31. {
  32. break;
  33. }
  34. if (warriors >= VICTORY)
  35. {
  36. break;
  37. }
  38. // Searching
  39. printf("Worker %zu is searching\n", n);
  40. sleep(2);
  41. // Transporting
  42. printf("Worker %zu is transporting\n", n);
  43. sleep(2);
  44. // Unloading
  45. if (minerals + (workers-NUM_WORKERS)*WORKER_PRICE + warriors*WARRIOR_PRICE >= MAX_MINERALS)
  46. {
  47. status = 0;
  48. if (N == 0)
  49. printf("There is no more minerals on the map!\n");
  50. break;
  51. }
  52. pthread_mutex_lock(&base_mutex);
  53. minerals +=4;
  54. printf("Worker %zu unloaded resources to Base station\n", n);
  55. pthread_mutex_unlock(&base_mutex);
  56. }
  57. }
  58. int main()
  59. {
  60. size_t i;
  61. pthread_t threads[NUM_WORKERS];
  62. pthread_mutex_init(&base_mutex, NULL);
  63. for (i = 0; i < NUM_WORKERS; i++)
  64. {
  65. int error = pthread_create(&threads[i], NULL, worker, (void*)i);
  66. if (error != 0)
  67. {
  68. fprintf(stderr, "Error creating thread %zd: error: %d\n", i, error);
  69. }
  70. }
  71. for (;;)
  72. {
  73. if(warriors>=VICTORY)
  74. {
  75. break;
  76. }
  77. char c;
  78. scanf("%c", &c);
  79. if(workers+warriors<MAX_POPULATION)
  80. {
  81. if(c == 'w')
  82. {
  83. if (minerals >= WARRIOR_PRICE)
  84. {
  85. pthread_mutex_lock(&base_mutex);
  86. printf("Warrior is being trained\n");
  87.  
  88. minerals -= WARRIOR_PRICE;
  89. pthread_mutex_unlock(&base_mutex);
  90. sleep(2);
  91. warriors++;
  92. printf("Warrior is ready for duty\n");
  93. }
  94. else
  95. printf("You do not have enough minerals!\n");
  96. }
  97. else if(c == 'c')
  98. {
  99. if (minerals>=WORKER_PRICE)
  100. {
  101. pthread_mutex_lock(&base_mutex);
  102. printf("Worker is being trained\n");
  103.  
  104. minerals -= WORKER_PRICE;
  105. pthread_mutex_unlock(&base_mutex);
  106. sleep(1);
  107. workers++;
  108. printf("Worker has been spowned\n");
  109. int error = pthread_create(&threads[workers-1], NULL, worker, (void*)workers-1);
  110. if (error != 0)
  111. {
  112. fprintf(stderr, "Error creating thread %d: error: %d\n", workers-1, error);
  113. }
  114. }
  115. else
  116. printf("You do not have enough minerals!\n");
  117. }
  118. else if (c != '\n')
  119. {
  120. printf("Wrong command!\n");
  121. }
  122. }
  123. else
  124. {
  125. break;
  126. }
  127. }
  128. for (i = 0; i < workers; i++)
  129. {
  130. int error = pthread_join(threads[i], NULL);
  131. if (error != 0)
  132. {
  133. fprintf(stderr, "Error joining thread %zd: error: %d\n", i, error);
  134. }
  135. }
  136. if (warriors>=VICTORY)
  137. {
  138. printf("\t===Win===\n");
  139. printf("All minerals on the map: %d\n", MAX_MINERALS);
  140. printf("Uncollected minerals: %d\n", MAX_MINERALS-(minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE));
  141. printf("Collected minerals: %d\n", minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE);
  142. }
  143. else if(workers+warriors>=MAX_POPULATION)
  144. {
  145. printf("\t===GAME OVER!===\n");
  146. printf("You reached population limit!\n");
  147. printf("All minerals on the map: %d\n", MAX_MINERALS);
  148. printf("Uncollected minerals: %d\n", MAX_MINERALS-(minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE));
  149. printf("Collected minerals: %d\n", minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE);
  150. }
  151. /*
  152. if (minerals >= MAX_MINERALS)
  153. {
  154. printf("\t===GAME OVER!===\n");
  155. printf("There is no more minerals on the map!\n");
  156. }
  157. */
  158. pthread_mutex_destroy(&base_mutex);
  159. pthread_exit(NULL);
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement