Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <ncurses.h>
  6. #include <time.h>
  7.  
  8. struct gracz{
  9. int x;
  10. int y;
  11. int wynik;
  12. int sk_x;
  13. int sk_y;
  14. int dziala;
  15. };
  16.  
  17. struct skarb{
  18. int x;
  19. int y;
  20. };
  21.  
  22. struct ogolne{
  23. struct skarb sk;
  24. struct gracz gracze[4];
  25. int znalezione;
  26. int flag;
  27. };
  28.  
  29.  
  30.  
  31. void* draw_treasure(void* arg)
  32. {
  33. struct ogolne* og=(struct ogolne*)arg;
  34. while(og->gracze[0].wynik+og->gracze[1].wynik+og->gracze[2].wynik+og->gracze[3].wynik<=10)
  35. {
  36. for(int i=0;i<4;i++)
  37. {
  38. if((og->gracze[i].x==og->sk.x&&og->gracze[i].y==og->sk.y))
  39. {
  40. og->gracze[i].wynik++;
  41. srand(time(NULL));
  42. og->sk.x=rand()%COLS;
  43. og->sk.y=rand()%(LINES-6);
  44. for(int k=0;k<4;k++)
  45. {
  46. og->gracze[i].sk_x=og->sk.x;
  47. og->gracze[i].sk_y=og->sk.y;
  48. }
  49.  
  50. }
  51. }
  52.  
  53. usleep(15000);
  54. }
  55.  
  56. for(int i=0;i<4;i++)
  57. og->gracze[i].dziala=0;
  58.  
  59. return NULL;
  60. }
  61.  
  62. void* move_player(void*arg) {
  63. //pthread_mutex_lock(&nc);
  64. struct gracz* gr=(struct gracz*)arg;
  65.  
  66. while(gr->dziala)
  67. {
  68. if(gr->x>gr->sk_x)
  69. gr->x--;
  70. else if(gr->x<gr->sk_x)
  71. gr->x++;
  72. if(gr->y>gr->sk_y)
  73. gr->y--;
  74. else if(gr->y<gr->sk_y)
  75. gr->y++;
  76.  
  77. usleep(100000);
  78. }
  79.  
  80. return NULL;
  81. }
  82.  
  83. void* display(void* arg)
  84. {
  85. struct ogolne* og=(struct ogolne*)arg;
  86.  
  87. int xp[4];
  88. int yp[4];
  89.  
  90. for(int i=0;i<4;i++)
  91. {
  92. mvprintw(og->gracze[i].y,og->gracze[i].x,"%c",'x');
  93.  
  94. xp[i]=og->gracze[i].x;
  95. yp[i]=og->gracze[i].y;
  96. }
  97.  
  98. while(og->gracze[0].wynik+og->gracze[1].wynik+og->gracze[2].wynik+og->gracze[3].wynik<=10)
  99. {
  100. refresh();
  101. for(int i=0;i<4;i++)
  102. {
  103. mvprintw(yp[i],xp[i],"%c",' ');
  104.  
  105. }
  106.  
  107. mvprintw(og->sk.y,og->sk.x,"%c",'o');
  108.  
  109. for(int i=0;i<4;i++)
  110. {
  111. mvprintw(og->gracze[i].y,og->gracze[i].x,"%c",'x');
  112.  
  113. xp[i]=og->gracze[i].x;
  114. yp[i]=og->gracze[i].y;
  115. }
  116.  
  117. for(int i=0;i<4;i++)
  118. {
  119. move(LINES-4+i,0);
  120. printw("Player %d: %d", i+1,og->gracze[i].wynik);
  121. }
  122. usleep(15000);
  123. }
  124.  
  125. return NULL;
  126. }
  127.  
  128.  
  129. int main() {
  130. initscr();
  131. cbreak();
  132. noecho();
  133. curs_set(0);
  134.  
  135.  
  136. pthread_t gra[4];
  137. pthread_t skarb;
  138. pthread_t disp;
  139.  
  140. srand(time(NULL));
  141.  
  142. struct ogolne og;
  143.  
  144. og.sk.x=rand()%COLS;
  145. og.sk.y=rand()%(LINES-6);
  146. og.flag=1;
  147.  
  148.  
  149. for(int i=0;i<4;i++)
  150. {
  151. og.gracze[i].x=rand()%COLS;
  152. og.gracze[i].y=rand()%(LINES-6);
  153. og.gracze[i].sk_x=og.sk.x;
  154. og.gracze[i].sk_y=og.sk.y;
  155. og.gracze[i].wynik=0;
  156. og.gracze[i].dziala=1;
  157. }
  158.  
  159.  
  160.  
  161. for(int i=0;i<4;i++)
  162. {
  163. pthread_create(&gra[i],NULL,move_player,(void*)&og.gracze[i]);
  164. }
  165.  
  166. pthread_create(&disp,NULL,display,(void*)&og);
  167. pthread_create(&skarb,NULL,draw_treasure,(void*)&og);
  168.  
  169. pthread_join(skarb,NULL);
  170.  
  171. for(int i=0;i<4;i++)
  172. pthread_join(gra[i],NULL);
  173.  
  174.  
  175. pthread_join(disp,NULL);
  176.  
  177. //getch();
  178.  
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement