Guest User

Untitled

a guest
Oct 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. //Version 6 with ai
  2. #include "stdio.h"
  3. #include "windows.h"
  4. #include <time.h>
  5.  
  6.  
  7. const int n = 110;
  8. const int s = 115;
  9. const int e = 101;
  10. const int w = 119;
  11.  
  12. void DrawBoard(char* grid)
  13. {
  14. int count = 0;
  15. printf(" 0 1 2 3 4 5 6 7 8 9\n");
  16.  
  17. for(int a = 0; a < 10; ++a)
  18. {
  19. printf("%d",a);
  20. for(int b = 0; b < 10; b++)
  21. {
  22. printf("%c ",grid[count]);
  23. ++count;
  24. }
  25. printf("\n");
  26.  
  27. }
  28. }
  29.  
  30. void PlaceShips(char* grid, int& shipSize)
  31. {
  32. int x = 0;
  33. int y = 0;
  34. char direction = 0;
  35. int error = 0;
  36. int error2 = 0;
  37.  
  38. printf("Place Ship %d wide\n", shipSize);
  39.  
  40. while(error != 1 || error2 != 1)
  41. {
  42. printf("X:");
  43. error = scanf_s("%d",&x);
  44. printf("Y:");
  45. error2 = scanf_s("%d",&y);
  46. fflush(stdin);
  47.  
  48. if(x < 0 || x > 9 || y < 0 || y > 9 || grid[y*10+x]!='=')
  49. {
  50. printf("Try again. Invalid number entered\n");
  51. error = 99;
  52. }
  53. }
  54.  
  55. error = 0;
  56.  
  57. while(error != 1)
  58. {
  59. printf("What Direction should it be placed in?\n");
  60. error = scanf_s("%c",&direction);
  61. fflush(stdin);
  62.  
  63. if(direction == 'n' || direction == 'e' || direction == 'w' || direction == 's' || error != 1)
  64. {
  65. int position = (((y + 1) * 10 + x) + 1) - 11;
  66.  
  67. switch(direction)
  68. {
  69. case n:
  70. {
  71. if((y-shipSize+1)<0)
  72. {
  73. printf("Not enough room\n");
  74. error = 0;
  75. }
  76. else if(grid[position - shipSize * 10 + 10] == '=')
  77. {
  78. for(int a = 0; a < shipSize; ++a)
  79. {
  80. grid[position - a * 10] = 's';
  81. }
  82. --shipSize;
  83. }
  84. else
  85. {
  86. printf("Not enough room\n");
  87. error = 0;
  88. }
  89. break;
  90. }
  91.  
  92. case e:
  93. {
  94. if((x+shipSize-1)>9)
  95. {
  96. printf("Not enough room\n");
  97. error = 0;
  98. }
  99. else if(grid[position + shipSize-1] == '=')
  100. {
  101. for(int a = 0; a < shipSize; ++a)
  102. {
  103. grid[position + a] = 's';
  104. }
  105. --shipSize;
  106. }
  107. else
  108. {
  109. printf("Not enough room\n");
  110. error = 0;
  111. }
  112. break;
  113. }
  114.  
  115. case w:
  116. {
  117. if((x-shipSize+1)<0)
  118. {
  119. printf("Not enough room\n");
  120. error = 0;
  121. }
  122. else if(grid[position - shipSize] == '=')
  123. {
  124. for(int a = 0; a < shipSize; ++a)
  125. {
  126. grid[position - a] = 's';
  127. }
  128. --shipSize;
  129. }
  130. else
  131. {
  132. printf("Not enough room\n");
  133. error = 0;
  134. }
  135. break;
  136. }
  137.  
  138. case s:
  139. {
  140. if((y+shipSize-1)>9)
  141. {
  142. printf("Not enough room\n");
  143. error = 0;
  144. }
  145. else if(grid[position + shipSize * 10 - 10] == '=')
  146. {
  147. for(int a = 0; a < shipSize; ++a)
  148. {
  149. grid[position + a * 10] = 's';
  150. }
  151. --shipSize;
  152. }
  153. else
  154. {
  155. printf("Not enough room\n");
  156. error = 0;
  157. }
  158.  
  159. break;
  160. }
  161. }
  162. }
  163. else
  164. {
  165. printf("Error, try again\n");
  166. fflush(stdin);
  167. error = 0;
  168. }
  169. }
  170.  
  171.  
  172.  
  173.  
  174. }
  175.  
  176. void PlayerAttack(char* opponent, char* record, int &count)
  177. {
  178. int x = 0;
  179. int y = 0;
  180. int error = 0;
  181. int error2 = 0;
  182. printf("Which spot would you like to attack?\n");
  183. while(error != 1 || error2 != 1)
  184. {
  185. printf("X:");
  186. error = scanf_s("%d",&x);
  187. printf("Y:");
  188. error2 = scanf_s("%d",&y);
  189. fflush(stdin);
  190.  
  191. if(x < 0 || x > 9 || y < 0 || y > 9)
  192. {
  193. printf("Try again. Invalid number entered\n");
  194. error = 99;
  195. }
  196. else if(record[y*10+x]!='=')
  197. {
  198. printf("Try again. Already tried the spot\n");
  199. error = 99;
  200. }
  201. }
  202. if(opponent[y*10+x]!='s')
  203. {
  204. record[y*10+x] = 'm';
  205. printf("It's a miss!\n");
  206. }
  207. else
  208. {
  209. record[y*10+x] = 'h';
  210. printf("It's a hit!\n");
  211. --count;
  212. }
  213. }
  214. //=================================ai=============================
  215. int AIFire(int lastHit, int lasterHit, char grid[100])
  216. {
  217. int target = -1;
  218. int targetDir = -1;
  219. bool fired = false;
  220. while (!fired)
  221. {
  222. if ((lastHit == -1)&&(lasterHit == -1))
  223. {
  224. target = rand() % 100;
  225. if (grid[target] != 'm')
  226. {
  227. fired = true;
  228. }
  229. }
  230. else
  231. {
  232. if (lasterHit == -1)
  233. {
  234. targetDir = rand() % 4 + 1;
  235. switch (targetDir)
  236. {
  237. case 1: //UP
  238. {
  239. if (((lastHit - 10) > 0)&&(grid[lastHit - 10] != 'X')&&(grid[lastHit - 10] != 'm'))
  240. {
  241. target = (lastHit - 10);
  242. fired = true;
  243. }
  244. }
  245. case 2: //DOWN
  246. {
  247. if (((lastHit + 10) < 100)&&(grid[lastHit + 10] != 'X')&&(grid[lastHit + 10] != 'm'))
  248. {
  249. target = (lastHit + 10);
  250. fired = true;
  251. }
  252. }
  253. case 3: //LEFT
  254. {
  255. if (((lastHit - 1) > 0)&&(grid[lastHit - 1] != 'X')&&((lastHit % 10) != 0)&&(grid[lastHit - 1] != 'm'))
  256. {
  257. target = (lastHit - 1);
  258. fired = true;
  259. }
  260. }
  261. case 4: //RIGHT
  262. {
  263. if (((lastHit + 1) < 100)&&(grid[lastHit + 1] != 'X')&&(((lastHit + 1) % 10) != 0)&&(grid[lastHit + 1] != 'm'))
  264. {
  265. target = (lastHit + 1);
  266. fired = true;
  267. }
  268. }
  269. }
  270. }
  271. else
  272. {
  273. if (((lastHit - 1) == lasterHit)||((lastHit + 1) == lasterHit))
  274. {
  275. if ((lastHit > lasterHit)&&(grid[lastHit + 1] != 'X'))
  276. {
  277. if (grid[lastHit + 1] != 'm')
  278. {
  279. target = lastHit + 1;
  280. fired = true;
  281. }
  282. else
  283. {
  284. int i = lastHit;
  285. while (grid[i] != '=')
  286. {
  287. i--;
  288. }
  289. target = i;
  290. fired = true;
  291. }
  292. }
  293. if ((lastHit < lasterHit)&&(grid[lastHit - 1] != 'X'))
  294. {
  295. target = lastHit - 1;
  296. fired = true;
  297. }
  298. }
  299. if (((lastHit - 10) == lasterHit)||((lastHit + 10) == lasterHit))
  300. {
  301. if ((lastHit > lasterHit)&&(grid[lastHit + 10] != 'X'))
  302. {
  303. target = lastHit + 10;
  304. fired = true;
  305. }
  306. if ((lastHit < lasterHit)&&(grid[lastHit - 10] != 'X'))
  307. {
  308. target = lastHit - 10;
  309. fired = true;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. return target;
  316. }
  317. //---------------------------------------------------------------
  318.  
  319. void main()
  320. {
  321. //ai stuff
  322. srand(time(NULL));
  323. int lastHit = -1;//needed in main
  324. int lasterHit = -1;
  325. //first two for player, other two for computer
  326. char grid[100];
  327. char grid1[100];
  328. char grid2[100];
  329. //char grid3[100];
  330. //count the ship left for player and computer for winning condition checking
  331. int playerShipNum = 17;
  332. int comShipNum = 17;
  333.  
  334. // Clear Grid
  335. for(int a = 0; a < 100; ++a)
  336. {
  337. grid[a] = '=';
  338. grid1[a] = '=';
  339. grid2[a] = '=';
  340. //grid3[a] = '=';
  341. }
  342. int shipSize = 5;
  343.  
  344. DrawBoard(grid);// first draw
  345.  
  346.  
  347.  
  348.  
  349. // =======================================SHIP PLACMENT====================================================================
  350. bool is3ship = false;
  351. for(int a = 0; a < 5; ++a) // once this is done all of the players ships are placed----------------------------------------
  352. {
  353.  
  354. PlaceShips(grid, shipSize);
  355. system("cls");
  356. DrawBoard(grid);
  357.  
  358. if(shipSize == 2) // pretty function to get double 3 ships
  359. {
  360. if(is3ship == false)
  361. {
  362. ++shipSize;
  363. is3ship = true;
  364. }
  365. }
  366. } // -----------------------------------------------------------------------------------------------------------------------
  367. system("pause");
  368. system("cls");
  369. //Start the game
  370. while(comShipNum!=0&&playerShipNum!=0)
  371. {
  372. //These are for testing functions purpose
  373. /*DrawBoard(grid);
  374. DrawBoard(grid1);
  375. PlayerAttack(grid,grid1,comShipNum);*/
  376.  
  377. //player's turn
  378. printf("Player's turn:\n");
  379. DrawBoard(grid1);
  380. PlayerAttack(grid2,grid1,comShipNum);
  381. system("pause");
  382. system("CLS");
  383.  
  384. printf("Computer's turn:\n");
  385. //computer attack function goes here... I think is the AI function?
  386. int target = AIFire(lastHit, lasterHit, grid);// needs to know when a ship has been sunk
  387.  
  388. // check if the loction is a hit players grid
  389.  
  390. //if hit need to reduce the playerShipNum
  391. DrawBoard(grid);
  392. system("pause");
  393. system("CLS");
  394. }
  395.  
  396. if(comShipNum==0)
  397. {
  398. printf("You win!!\n");
  399. }
  400. else
  401. {
  402. printf("You loss...\n");
  403. }
  404.  
  405. system("pause");
  406. }
Add Comment
Please, Sign In to add comment