Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include "ui.h"
  5. int soldiers=0;
  6. int gold=100;
  7. int minior=0;
  8. int zombies=1;
  9. int health=100;
  10. pthread_mutex_t mutex;
  11. void *thread_solder(void *arg){
  12. pthread_mutex_lock(&mutex);
  13. gold-=10;
  14. print_gold(gold);
  15. soldiers++;
  16. print_soldiers(soldiers);
  17. pthread_mutex_unlock(&mutex);
  18. }
  19. void *thread_solders(void *arg){
  20. pthread_mutex_lock(&mutex);
  21. gold-=100;
  22. print_gold(gold);
  23. soldiers+=10;
  24. print_soldiers(soldiers);
  25. pthread_mutex_unlock(&mutex);
  26. }
  27. void *thread_miner(void *arg){
  28. while(1){
  29. pthread_mutex_lock(&mutex);
  30. gold+=10;
  31. print_gold(gold);
  32. pthread_mutex_unlock(&mutex);
  33.  
  34. sleep(1);
  35. }
  36. }
  37. void *thread_create_miner(void *arg){
  38. pthread_mutex_lock(&mutex);
  39. gold-=100;
  40. print_gold(gold);
  41. minior++;
  42. pthread_mutex_unlock(&mutex);
  43. }
  44. void *thread_zombies(void *arg){
  45.  
  46. while(1){
  47. //pthread_mutex_lock(&mutex);
  48. print_health(health);
  49. for(int i=5;i>=0;i--){
  50. print_zombies(i,zombies);
  51. sleep(1);
  52. }
  53. if(zombies>soldiers){
  54. health-=zombies-soldiers;
  55. print_fail("Zombie attack succeded ;(!\n");
  56. if(health<=0){
  57. game_end(zombies);
  58. }
  59. }
  60. else{
  61. print_succ("Zombie attack deflected! :)\n");
  62. }
  63. zombies*=2;
  64. //pthread_mutex_unlock(&mutex);
  65. }
  66.  
  67. }
  68. int main() {
  69. pthread_t ptzombie;
  70. pthread_t ptsoldier;
  71. pthread_t ptminer;
  72. pthread_mutex_init(&mutex, NULL);
  73. init();
  74. print_gold(gold);
  75. print_soldiers(soldiers);
  76. pthread_create(&ptzombie, NULL, thread_zombies, NULL);
  77. while(1) {
  78. int ch = get_input();
  79.  
  80. switch(ch) {
  81. case 's':
  82. pthread_mutex_lock(&mutex);
  83. if(gold>=10){
  84. pthread_create(&ptsoldier, NULL, thread_solder, NULL);
  85. pthread_join(ptsoldier, NULL);
  86. print_msg("Soldier created!");
  87. }
  88. else{
  89. print_fail("Not enough gold!");
  90. }
  91. pthread_mutex_unlock(&mutex);
  92. break;
  93. case 'm':
  94. pthread_mutex_lock(&mutex);
  95. if(gold>=100){
  96. pthread_create(&ptminer, NULL, thread_create_miner, NULL);
  97. pthread_join(ptminer, NULL);
  98. print_msg("Miner created!");
  99. for(int i=0;i<minior;i++){
  100. pthread_create(&ptminer, NULL, thread_miner, NULL);
  101. //pthread_join(ptminer,NULL);
  102. }
  103.  
  104. }
  105. else{
  106. print_fail("Not enough gold!");
  107. }
  108. pthread_mutex_unlock(&mutex);
  109. break;
  110. case 'x':
  111. pthread_mutex_lock(&mutex);
  112. if(gold>=100){
  113. pthread_create(&ptsoldier, NULL, thread_solders, NULL);
  114. pthread_join(ptsoldier, NULL);
  115. print_msg("10 x soldiers created!");
  116.  
  117.  
  118. }
  119. else{
  120. print_fail("Not enough gold!");
  121. }
  122. pthread_mutex_unlock(&mutex);
  123. break;
  124. }
  125.  
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement