Advertisement
Guest User

Untitled

a guest
May 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. //Samuele Mazzoglia 3^Dinfo 12/02
  2. //Battaglia navale
  3.  
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<math.h>
  7. #include<time.h>
  8.  
  9. int main()
  10. {
  11. srand(time(0)); //imposti il valore del tempo a 0
  12. int v[10][10],v1[10][10];
  13. char m[10][10],m1[10][10];
  14. int i,j,dir,x,y,nave,cont=0,contx,conty,win=0,aff=0,turno=0,win1=0,aff1=0;
  15.  
  16. //azzeramento matrice
  17. for(i=0;i<10;i++)
  18. {
  19. for(j=0;j<10;j++)
  20. {
  21. v[i][j]=0;
  22. }
  23. }
  24.  
  25. //azzeramento matrice
  26. for(i=0;i<10;i++)
  27. {
  28. for(j=0;j<10;j++)
  29. {
  30. m[i][j]='s';
  31. }
  32. }
  33. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. //Giocatore 1
  36. do
  37. {
  38. cont++;
  39. printf("Inserisci di quanti blocchi e' la nave: ");
  40. scanf("%d",&nave);
  41.  
  42. dir=rand()%2+1; //direzione 1 orizzontale 2 verticale
  43. x=rand()%10+1;
  44. y=rand()%10+1;
  45. contx=x;
  46. conty=y;
  47.  
  48. if(dir==1)
  49. {
  50. if(x<=5)
  51. {
  52. //controllo
  53. for(i=0;i<nave;i++)
  54. {
  55. if(v[contx][conty]!=0)
  56. {
  57. contx=rand()%10+1;
  58. conty=rand()%10+1;
  59. i=-1;
  60. x=contx;
  61. y=conty;
  62. }
  63. else
  64. {
  65. contx=contx+1;
  66. }
  67. }
  68. //assegnazione
  69. for(i=0;i<nave;i++)
  70. {
  71. v[y-1][x-1]=nave;
  72. v[y-2][x-1]=9;
  73. v[y][x-1]=9;
  74. x=x+1;
  75. }
  76. }
  77. else
  78. {
  79. //controllo
  80. for(i=0;i<nave;i++)
  81. {
  82. if(v[contx][conty]!=0)
  83. {
  84. contx=rand()%10+1;
  85. conty=rand()%10+1;
  86. i=-1;
  87. x=contx;
  88. y=conty;
  89. }
  90. else
  91. {
  92. contx=contx-1;
  93. }
  94. }
  95. //assegnazione
  96. for(i=0;i<nave;i++)
  97. {
  98. v[y-1][x-1]=nave;
  99. v[y-2][x-1]=9;
  100. v[y][x-1]=9;
  101. x=x-1;
  102. }
  103. }
  104. }
  105.  
  106. if(dir==2)
  107. {
  108. if(y<=5)
  109. {
  110. //controllo
  111. for(i=0;i<nave;i++)
  112. {
  113. if(v[contx][conty]!=0)
  114. {
  115. contx=rand()%10+1;
  116. conty=rand()%10+1;
  117. i=-1;
  118. x=contx;
  119. y=conty;
  120. }
  121. else{
  122. conty=conty+1;
  123. }
  124. }
  125. //assegnazione
  126. for(i=0;i<nave;i++)
  127. {
  128. v[y-1][x-1]=nave;
  129. v[y-1][x-2]=9;
  130. v[y-1][x]=9;
  131. y=y+1;
  132. }
  133. }
  134. else
  135. {
  136. //controllo
  137. for(i=0;i<nave;i++)
  138. {
  139. if(v[contx][conty]!=0)
  140. {
  141. contx=rand()%10+1;
  142. conty=rand()%10+1;
  143. i=-1;
  144. x=contx;
  145. y=conty;
  146. }
  147. else{
  148. conty=conty-1;
  149. }
  150. }
  151. //assegnazione
  152. for(i=0;i<nave;i++)
  153. {
  154. v[y-1][x-1]=nave;
  155. v[y-1][x-2]=9;
  156. v[y-1][x]=9;
  157. y=y-1;
  158. }
  159. }
  160. }
  161.  
  162. //stampa matrice navi
  163. for(i=0;i<10;i++)
  164. {
  165. printf("\n");
  166. for(j=0;j<10;j++)
  167. {
  168. printf("\t%d",v[i][j]);
  169. }
  170. }
  171.  
  172. printf("\n");
  173.  
  174. }
  175. while(cont<15);
  176.  
  177. //pulisce lo schermo per non far vedere le navi
  178. system("cls");
  179. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  180. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181. //Giocatore 2
  182. do
  183. {
  184. cont1++;
  185. printf("Inserisci di quanti blocchi e' la nave: ");
  186. scanf("%d",&nave);
  187.  
  188. dir=rand()%2+1; //direzione 1 orizzontale 2 verticale
  189. x=rand()%10+1;
  190. y=rand()%10+1;
  191. contx=x;
  192. conty=y;
  193.  
  194. if(dir==1)
  195. {
  196. if(x<=5)
  197. {
  198. //controllo
  199. for(i=0;i<nave;i++)
  200. {
  201. if(v1[contx][conty]!=0)
  202. {
  203. contx=rand()%10+1;
  204. conty=rand()%10+1;
  205. i=-1;
  206. x=contx;
  207. y=conty;
  208. }
  209. else
  210. {
  211. contx=contx+1;
  212. }
  213. }
  214. //assegnazione
  215. for(i=0;i<nave;i++)
  216. {
  217. v1[y-1][x-1]=nave;
  218. v1[y-2][x-1]=9;
  219. v1[y][x-1]=9;
  220. x=x+1;
  221. }
  222. }
  223. else
  224. {
  225. //controllo
  226. for(i=0;i<nave;i++)
  227. {
  228. if(v1[contx][conty]!=0)
  229. {
  230. contx=rand()%10+1;
  231. conty=rand()%10+1;
  232. i=-1;
  233. x=contx;
  234. y=conty;
  235. }
  236. else
  237. {
  238. contx=contx-1;
  239. }
  240. }
  241. //assegnazione
  242. for(i=0;i<nave;i++)
  243. {
  244. v1[y-1][x-1]=nave;
  245. v1[y-2][x-1]=9;
  246. v1[y][x-1]=9;
  247. x=x-1;
  248. }
  249. }
  250. }
  251.  
  252. if(dir==2)
  253. {
  254. if(y<=5)
  255. {
  256. //controllo
  257. for(i=0;i<nave;i++)
  258. {
  259. if(v1[contx][conty]!=0)
  260. {
  261. contx=rand()%10+1;
  262. conty=rand()%10+1;
  263. i=-1;
  264. x=contx;
  265. y=conty;
  266. }
  267. else{
  268. conty=conty+1;
  269. }
  270. }
  271. //assegnazione
  272. for(i=0;i<nave;i++)
  273. {
  274. v1[y-1][x-1]=nave;
  275. v1[y-1][x-2]=9;
  276. v1[y-1][x]=9;
  277. y=y+1;
  278. }
  279. }
  280. else
  281. {
  282. //controllo
  283. for(i=0;i<nave;i++)
  284. {
  285. if(v1[contx][conty]!=0)
  286. {
  287. contx=rand()%10+1;
  288. conty=rand()%10+1;
  289. i=-1;
  290. x=contx;
  291. y=conty;
  292. }
  293. else{
  294. conty=conty-1;
  295. }
  296. }
  297. //assegnazione
  298. for(i=0;i<nave;i++)
  299. {
  300. v1[y-1][x-1]=nave;
  301. v1[y-1][x-2]=9;
  302. v1[y-1][x]=9;
  303. y=y-1;
  304. }
  305. }
  306. }
  307.  
  308. //stampa matrice navi
  309. for(i=0;i<10;i++)
  310. {
  311. printf("\n");
  312. for(j=0;j<10;j++)
  313. {
  314. printf("\t%d",v1[i][j]);
  315. }
  316. }
  317.  
  318. printf("\n");
  319.  
  320. }
  321. while(cont1<15);
  322.  
  323. //pulisce lo schermo per non far vedere le navi
  324. system("cls");
  325. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  326. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  327. //Sparo del giocatore 1
  328. if(turno%2==0)
  329. {
  330. printf("Inserisci la cordanata x: ");
  331. scanf("%d",&x);
  332. printf("Inserisci la cordanata y: ");
  333. scanf("%d",&y);
  334.  
  335. //sparo
  336. if(v[y-1][x-1]==5 ||v[y-1][x-1]==4 ||v[y-1][x-1]==3 ||v[y-1][x-1]==2 ||v[y-1][x-1]==1)
  337. {
  338. printf("Colpito");
  339. m[y-1][x-1]='X';
  340. win++;
  341. aff++;
  342. if(aff==v[y-1][x]-1)
  343. {
  344. printf("\nAffondata");
  345. aff=0;
  346. }
  347. }
  348. else
  349. {
  350. printf("Mancato");
  351. m[y-1][x-1]='O';
  352. }
  353.  
  354. //stampa matrice navi
  355. for(i=0;i<10;i++)
  356. {
  357. printf("\n");
  358. for(j=0;j<10;j++)
  359. {
  360. printf("\t%c",m[i][j]);
  361. }
  362. }
  363. printf("\n");
  364. }
  365. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  366. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  367. //Sparo del giocatore 2
  368. else
  369. {
  370. printf("Inserisci la cordanata x: ");
  371. scanf("%d",&x);
  372. printf("Inserisci la cordanata y: ");
  373. scanf("%d",&y);
  374.  
  375. //sparo
  376. if(v[y-1][x-1]==5 ||v[y-1][x-1]==4 ||v[y-1][x-1]==3 ||v[y-1][x-1]==2 ||v[y-1][x-1]==1)
  377. {
  378. printf("Colpito");
  379. m1[y-1][x-1]='X';
  380. win1++;
  381. aff1++;
  382. if(aff1==v1[y-1][x]-1)
  383. {
  384. printf("\nAffondata");
  385. aff1=0;
  386. }
  387. }
  388. else
  389. {
  390. printf("Mancato");
  391. m1[y-1][x-1]='O';
  392. }
  393.  
  394. //stampa matrice navi
  395. for(i=0;i<10;i++)
  396. {
  397. printf("\n");
  398. for(j=0;j<10;j++)
  399. {
  400. printf("\t%c",m1[i][j]);
  401. }
  402. }
  403. printf("\n");
  404. }
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement