Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 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. //5 pthread_create
  78. //pthread_join(ptzombie,NULL);
  79. while(1) {
  80. int ch = get_input();
  81.  
  82. switch(ch) {
  83. case 's':
  84. if(gold>=10){
  85. pthread_create(&ptsoldier, NULL, thread_solder, NULL);
  86. pthread_join(ptsoldier, NULL);
  87. print_msg("Soldier created!");
  88. }
  89. else{
  90. print_fail("Not enough gold!");
  91. }
  92. break;
  93. case 'm':
  94. if(gold>=100){
  95. pthread_create(&ptminer, NULL, thread_create_miner, NULL);
  96. pthread_join(ptminer, NULL);
  97. print_msg("Miner created!");
  98. for(int i=0;i<minior;i++){
  99. pthread_create(&ptminer, NULL, thread_miner, NULL);
  100. //pthread_join(ptminer,NULL);
  101. }
  102.  
  103. }
  104. else{
  105. print_fail("Not enough gold!");
  106. }
  107. break;
  108. case 'x':
  109. if(gold>=100){
  110. pthread_create(&ptsoldier, NULL, thread_solders, NULL);
  111. pthread_join(ptsoldier, NULL);
  112. print_msg("10 x soldiers created!");
  113.  
  114.  
  115. }
  116. else{
  117. print_fail("Not enough gold!");
  118. }
  119. break;
  120. }
  121. }
  122. pthread_mutex_destroy(&mutex);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement