Advertisement
Guest User

n2

a guest
Oct 20th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <unistd.h>
  5. #include <pthread.h>
  6. #include <time.h>
  7. #include <windows.h>
  8. //game table size
  9. #define HEIGHT 20
  10. #define LENGHT 40
  11.  
  12. //how many lines of free space does player have to operate
  13. #define SPACEFORPLAYER 10
  14.  
  15. #define BORDERS 'x'
  16.  
  17. #define PLAYERSHELL '*'
  18. #define ENEMYSHELL '!'
  19.  
  20. //Can be used to modify ship models but ship fragments are hardcoded in this iteration so it will require further changes
  21. #define SHIPHEIGHT 1
  22. #define SHIPLENGHT 3
  23.  
  24. #define ENEMYSHIPCOUNT (LENGHT/4)*((HEIGHT-SPACEFORPLAYER)/2)
  25. #define ENEMYAPPROXCOUNT (LENGHT/3)
  26.  
  27.  
  28. static char gameTable[HEIGHT][LENGHT];
  29. const char enemyShip[SHIPHEIGHT][SHIPLENGHT]={{'|','^','|'}};//shipType 'e' for enemy
  30. const char playerShip[SHIPHEIGHT][SHIPLENGHT]={{'(','=',')'}};//shipTtype 'p' for player
  31. pthread_mutex_t lockRefresh=PTHREAD_MUTEX_INITIALIZER;
  32. pthread_t shot;
  33. pthread_t enemyShot;
  34. pthread_t enemyShotDelay;
  35. pthread_t gtimer;
  36.  
  37.  
  38. typedef struct
  39. {
  40. size_t shotPosX;
  41. size_t shotPosY;
  42.  
  43. }shootingParams;
  44.  
  45. void GameTableInitiate();
  46. void GameStatePrint();
  47. void ClearScreen();
  48. void AddEnemies();
  49. void DrawShip(char shipType, size_t shipPosX, size_t shipPosY);
  50. void ClearShip(size_t shipPosX,size_t shipPosY);//used both in movement as well as when you hit enemy
  51. void TestForGameOver(size_t shipPosX,size_t shipPosY);
  52. void PlayerMovement(size_t shipPosX,size_t shipPosY);
  53. void ShootingInputConversion(size_t shipPosX,size_t shipPosY);
  54. void *PlayerShooting(void *pShooting);
  55. void EnemyShipDestroy(size_t shipPosX, size_t shipPosY);
  56. shootingParams scanForEnemy();
  57. void *EnemyShooting(void *f);
  58. void *EnemyShootingDelay();
  59. void *GlobalTimer();
  60.  
  61. int main()
  62. {
  63.  
  64. GameTableInitiate(gameTable[HEIGHT][LENGHT]);
  65. AddEnemies();
  66.  
  67. //print starting position of the ship-last possible lane on x and i the middle of y
  68. size_t shipPosX=HEIGHT-2;
  69. size_t shipPosY=LENGHT/2;
  70. DrawShip('p', shipPosX,shipPosY);
  71. GameStatePrint(gameTable[HEIGHT][LENGHT]);
  72. scanForEnemy();
  73. pthread_create(&enemyShotDelay,NULL,EnemyShootingDelay,NULL);
  74. pthread_create(&gtimer,NULL,GlobalTimer,NULL);
  75. PlayerMovement(shipPosX,shipPosY);
  76.  
  77.  
  78. return 0;
  79. }
  80.  
  81. void GameTableInitiate()
  82. {
  83.  
  84. for(size_t i=0;i<HEIGHT;i++)
  85. {
  86. for(size_t j=0;j<LENGHT;j++)
  87. {
  88. if(i==0||i==HEIGHT-1)
  89. {
  90. gameTable[i][j]=BORDERS;
  91. }
  92. else
  93. {
  94.  
  95. gameTable[i][j]=' ';
  96. gameTable[i][0]=BORDERS;
  97. gameTable[i][LENGHT-1]=BORDERS;
  98. }
  99. }
  100. }
  101.  
  102. }
  103. void GameStatePrint()
  104. {
  105. //in case if some 1 wanted to make it as a pthread close func in while(1)
  106. pthread_mutex_lock(&lockRefresh);
  107.  
  108. ClearScreen();
  109. for(size_t i=0;i<HEIGHT;i++)
  110. {
  111. for(size_t j=0;j<LENGHT;j++)
  112. {
  113. printf("%c", gameTable[i][j]);
  114. }
  115. printf("\n");
  116. }
  117. pthread_mutex_unlock(&lockRefresh);
  118.  
  119.  
  120. //
  121. //uSleep(refreshTime);
  122.  
  123. }
  124. void ClearScreen()
  125. { //kind off overkill to make function out of this but i went with it anyway
  126. system("@cls||clear");
  127. }
  128.  
  129. void DrawShip(char shipType, size_t shipPosX, size_t shipPosY)
  130. {
  131. switch(shipType)
  132. {
  133. case 'e':
  134. for(size_t i=0;i<SHIPHEIGHT;i++)
  135. {
  136. for(size_t j=0;j<SHIPLENGHT;j++)
  137. {
  138. gameTable[shipPosX][shipPosY+j]=enemyShip[i][j];
  139. }
  140. }
  141. break;
  142. case 'p':
  143. for(size_t i=0;i<SHIPHEIGHT;i++)
  144. {
  145. for(size_t j=0;j<SHIPLENGHT;j++)
  146. {
  147. gameTable[shipPosX][shipPosY+j]=playerShip[i][j];
  148. }
  149. }
  150. break;
  151. default:
  152. break;
  153. }
  154.  
  155. }
  156. void AddEnemies()
  157. {
  158. size_t gapX=0;
  159. size_t gapY=2;
  160. //so we dont spawn ships in map border
  161. size_t startPointForX=2;
  162. size_t startPointForY=2;
  163.  
  164. //ship spawn to this point
  165. size_t endLineForEnemies=HEIGHT-SPACEFORPLAYER;
  166.  
  167. for(;startPointForX<endLineForEnemies;)
  168. {
  169.  
  170. for(;startPointForY<LENGHT-SHIPLENGHT;)//adding ships if we have a place for it
  171. {
  172. DrawShip('e',startPointForX,startPointForY);
  173. gapY=gapY+4;
  174. startPointForY=gapY;
  175. }
  176.  
  177. gapX=gapX+2;
  178. startPointForX=gapX;
  179. startPointForY=2;
  180. gapY=2;
  181. }
  182.  
  183. }
  184. void PlayerMovement(size_t shipPosX,size_t shipPosY)
  185. {
  186. char Controls;
  187.  
  188. while(1)
  189. {
  190. Controls=getch();
  191. switch(Controls)
  192. {
  193. case 'a':
  194. ClearShip(shipPosX,shipPosY);
  195. shipPosY=shipPosY-1;
  196. TestForGameOver(shipPosX,shipPosY);
  197. DrawShip('p',shipPosX,shipPosY);
  198. //GameStatePrint();
  199. break;
  200. case 'd':
  201. ClearShip(shipPosX,shipPosY);
  202. shipPosY=shipPosY+1;
  203. TestForGameOver(shipPosX,shipPosY);
  204. DrawShip('p',shipPosX,shipPosY);
  205. //GameStatePrint();
  206. break;
  207. case 'w':
  208. ClearShip(shipPosX,shipPosY);
  209. shipPosX=shipPosX-1;
  210. TestForGameOver(shipPosX,shipPosY);
  211. DrawShip('p',shipPosX,shipPosY);
  212. //GameStatePrint();
  213. break;
  214. case 's':
  215. ClearShip(shipPosX,shipPosY);
  216. shipPosX=shipPosX+1;
  217. TestForGameOver(shipPosX,shipPosY);
  218. DrawShip('p',shipPosX,shipPosY);
  219. //GameStatePrint();
  220. break;
  221. case 'e':
  222. ShootingInputConversion(shipPosX,shipPosY);
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. }
  229. void ClearShip(size_t shipPosX,size_t shipPosY)
  230. {
  231. for(size_t i=0;i<SHIPHEIGHT;i++)
  232. {
  233. for(size_t j=0;j<SHIPLENGHT;j++)
  234. {
  235. gameTable[shipPosX][shipPosY+j]=' ';
  236. }
  237. }
  238.  
  239. }
  240. void TestForGameOver(size_t shipPosX,size_t shipPosY)
  241. {
  242. //this loop checks if any of the player ship elements collide with enemy ship or borders
  243. for(size_t i=0;i<SHIPLENGHT;i++)
  244. {
  245. if(gameTable[shipPosX][shipPosY+i]==BORDERS||gameTable[shipPosX][shipPosY+i]=='^'||gameTable[shipPosX][shipPosY+i]=='|'||gameTable[shipPosX][shipPosY+i]==ENEMYSHELL)
  246. {
  247. //ClearScreen();
  248. printf("game over");
  249. Sleep(2000);
  250. exit(1);
  251. }
  252. }
  253. }
  254. void ShootingInputConversion(size_t shipPosX,size_t shipPosY)
  255. {//function converts ship position and converts it so that it can be accepted by thread which will remember shells that already have been shot
  256. shipPosX=shipPosX-1;
  257. shipPosY=shipPosY+1;
  258.  
  259. shootingParams *s=malloc(sizeof(shootingParams));
  260. s->shotPosX=shipPosX;
  261. s->shotPosY=shipPosY;
  262.  
  263. pthread_create(&shot,NULL,PlayerShooting,s);
  264.  
  265.  
  266. }
  267. void *PlayerShooting(void *s)
  268. {
  269.  
  270. int j=1;
  271. size_t shootingTrajectoryX;
  272. size_t shootingTrajectoryY;
  273. shootingTrajectoryX=((shootingParams*)s)->shotPosX;
  274. shootingTrajectoryY=((shootingParams*)s)->shotPosY;
  275.  
  276.  
  277. for(int i=0;i<30;i++)
  278. {
  279. if(gameTable[shootingTrajectoryX-j][shootingTrajectoryY]==BORDERS)
  280. {
  281. goto jump;
  282. }
  283. else if(gameTable[shootingTrajectoryX-j][shootingTrajectoryY]=='^')
  284. {
  285. ClearShip(shootingTrajectoryX-j,shootingTrajectoryY-1);
  286. gameTable[shootingTrajectoryX-j+1][shootingTrajectoryY]=' ';
  287. //GameStatePrint();
  288. goto jump;
  289. }
  290. else if(gameTable[shootingTrajectoryX-j][shootingTrajectoryY]=='|'&&gameTable[shootingTrajectoryX-j][shootingTrajectoryY+1]=='^')
  291. {
  292. ClearShip(shootingTrajectoryX-j,shootingTrajectoryY);
  293. gameTable[shootingTrajectoryX-j+1][shootingTrajectoryY]=' ';
  294. //GameStatePrint();
  295. goto jump;
  296. }
  297. else if(gameTable[shootingTrajectoryX-j][shootingTrajectoryY]=='|')
  298. {
  299. ClearShip(shootingTrajectoryX-j,shootingTrajectoryY-2);
  300. gameTable[shootingTrajectoryX-j+1][shootingTrajectoryY]=' ';
  301. //GameStatePrint();
  302. goto jump;
  303. }
  304. else
  305. {
  306. gameTable[shootingTrajectoryX-j][shootingTrajectoryY]=PLAYERSHELL;
  307. gameTable[shootingTrajectoryX-j+1][shootingTrajectoryY]=' ';
  308. j++;
  309. //GameStatePrint();
  310. Sleep(1000);
  311. }
  312. }
  313. jump:
  314. gameTable[shootingTrajectoryX-j+1][shootingTrajectoryY]=' ';
  315. free(s);
  316. pthread_cancel(shot);
  317.  
  318. }
  319. shootingParams scanForEnemy()
  320. {
  321. shootingParams s;
  322. int tempTableX[ENEMYAPPROXCOUNT];
  323. int tempTableY[ENEMYAPPROXCOUNT];
  324.  
  325.  
  326. size_t index=0;
  327.  
  328.  
  329. for(size_t i=0;i<HEIGHT;i++)
  330. {
  331. for(size_t j=0;j<LENGHT;j++)
  332. {
  333. if(gameTable[i][j]=='^'&&gameTable[i+2][j]!='^')
  334. {
  335. tempTableX[index]=i;
  336. tempTableY[index]=j;
  337.  
  338. index++;
  339. }
  340. }
  341. }
  342. int r=rand()% index;
  343.  
  344.  
  345. s.shotPosX=tempTableX[r];
  346. s.shotPosY=tempTableY[r];
  347.  
  348.  
  349. return s;
  350. }
  351. void *EnemyShooting(void *f)
  352. {
  353. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
  354.  
  355. size_t shootingTrajectoryX;
  356. size_t shootingTrajectoryY;
  357. shootingTrajectoryX=((shootingParams*)f)->shotPosX;
  358. shootingTrajectoryY=((shootingParams*)f)->shotPosY;
  359. Sleep(1000);
  360.  
  361.  
  362. while(shootingTrajectoryX<LENGHT-1)
  363. {
  364. shootingTrajectoryX++;
  365. if(gameTable[shootingTrajectoryX+1][shootingTrajectoryY]==BORDERS)
  366. {
  367. goto jump;
  368. }
  369. else if(gameTable[shootingTrajectoryX+1][shootingTrajectoryY]=='='||gameTable[shootingTrajectoryX+1][shootingTrajectoryY]=='('||gameTable[shootingTrajectoryX+1][shootingTrajectoryY]==')')
  370. {
  371. //ClearScreen();
  372. printf("game over");
  373. Sleep(2000);
  374. exit(1);
  375. }
  376. else if(gameTable[shootingTrajectoryX+1][shootingTrajectoryY]==PLAYERSHELL)
  377. {
  378. gameTable[shootingTrajectoryX][shootingTrajectoryY]=' ';
  379. goto jump;
  380. }
  381.  
  382. else
  383. {
  384. gameTable[shootingTrajectoryX+1][shootingTrajectoryY]=ENEMYSHELL;
  385.  
  386. gameTable[shootingTrajectoryX][shootingTrajectoryY]=' ';
  387.  
  388. //GameStatePrint();
  389. Sleep(1000);
  390. }
  391. }
  392.  
  393.  
  394. jump:
  395. gameTable[shootingTrajectoryX][shootingTrajectoryY]=' ';
  396.  
  397. pthread_cancel(enemyShot);
  398.  
  399. }
  400. void *EnemyShootingDelay()
  401. {
  402.  
  403. shootingParams f;
  404. while(1)
  405. {
  406. //warning-if you remove this Sleep you will kill your PC
  407. Sleep(1000);
  408. f=scanForEnemy();
  409.  
  410. pthread_create(&enemyShot,NULL,EnemyShooting,&f);
  411. }
  412. }
  413.  
  414. void *GlobalTimer()
  415. {
  416.  
  417. shootingParams f;
  418. while(1)
  419. {
  420. //warning-if you remove this Sleep you will kill your PC
  421. Sleep(300);
  422.  
  423. //f=scanForEnemy();
  424. GameStatePrint();
  425. //pthread_create(&enemyShot,NULL,EnemyShooting,&f);
  426. }
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement