Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. #define TAILLEXMAX 18
  7. #define TAILLEYMAX 24
  8. #define PORTEAVIONID 4
  9. #define TAILLE_PORTEAVION 6
  10. #define CROISEURID 3
  11. #define TAILLE_CROISEUR 4
  12. #define DESTROYERID 2
  13. #define TAILLE_DESTROYER 3
  14. #define CORVETTEID 1
  15. #define TAILLE_CORVETTE 1
  16.  
  17. void waitFor(unsigned int secs){
  18.  
  19. unsigned int retTime = time(0) + secs;
  20. while (time(0) < retTime);
  21.  
  22. }
  23.  
  24. int doRand(int startVal, int endVal){
  25.  
  26. waitFor(0.05);
  27. srand(time(NULL)*rand());
  28. if(startVal == 0 && endVal == 1){
  29. return rand() % 2;
  30. }else{
  31. return (rand() % ((endVal + startVal -1)) + startVal);
  32. }
  33.  
  34. }
  35.  
  36. void FilerWrite(){
  37. FILE* ftp = fopen("./test1.txt","a+");
  38. int i = 0;
  39. while(i < 1000){
  40. fprintf(ftp,"Hello world %d",i);
  41. i++;
  42. }
  43. fclose(ftp);
  44. }
  45.  
  46. void FilerRead(){
  47. FILE* fptr = fopen("./test1.txt","r");
  48. char c;
  49. while(1)
  50. {
  51. c = fgetc(fptr);
  52. if ( feof(fptr) )
  53. {
  54. break;
  55. }
  56. printf ("%c", c);
  57. }
  58. fclose(fptr);
  59. }
  60.  
  61. int Superposition(int random_position, int taille, int joueur[18][24], int x, int y){ //Fonction pour vérifier s'il y a superposition ou pas
  62.  
  63. int i=0;
  64.  
  65. switch(random_position){
  66. case 1:
  67. for(i=0;i<=taille;i++){
  68. if(joueur[x+i][y] != 0)
  69. return 1;
  70. }
  71. break;
  72. case 2:
  73. for(i=0;i<=taille;i++){
  74. if(joueur[x-i][y] != 0)
  75. return 1;
  76. }
  77. break;
  78. case 3:
  79. for(i=0;i<=taille;i++){
  80. if(joueur[x][y+i] != 0)
  81. return 1;
  82. }
  83. break;
  84. case 4:
  85. for(i=0;i<=taille;i++){
  86. if(joueur[x][y-i] != 0)
  87. return 1;
  88. }
  89. break;
  90. }
  91. return 0;
  92.  
  93. }
  94.  
  95. void PlacementUnBateauIA(int joueur[18][24], int tailleX, int tailleY, int taillebateau, int idbateau){ //Fonction pour placement des bateaux
  96.  
  97. int j=0;
  98. int y=0;
  99. int x=0;
  100. int random_position=0;
  101. int superpo=1;
  102.  
  103. random_position=doRand(1,4);
  104. x=doRand(0, tailleX-1);
  105. y=doRand(0, tailleY-1);
  106. superpo=1;
  107. for(j=0;j<taillebateau;j++){
  108. if(random_position==1){ //Vers le bas
  109. while(x+taillebateau>=tailleX || superpo==1){
  110. x=doRand(0, tailleX-1);
  111. y=doRand(0, tailleY-1);
  112. superpo=Superposition(random_position, taillebateau, joueur, x, y);
  113. }
  114. joueur[x][y]=idbateau;
  115. joueur[x+j][y]=idbateau;
  116. }else if(random_position==2){ //Vers le haut
  117. while(x-taillebateau<0 || superpo==1){
  118. x=doRand(0, tailleX-1);
  119. y=doRand(0, tailleY-1);
  120. superpo=Superposition(random_position, taillebateau, joueur, x, y);
  121. }
  122. joueur[x][y]=idbateau;
  123. joueur[x-j][y]=idbateau;
  124. }else if(random_position==3){ //Vers la droite
  125. while(y+taillebateau>=tailleY || superpo==1){
  126. x=doRand(0, tailleX-1);
  127. y=doRand(0, tailleY-1);
  128. superpo=Superposition(random_position, taillebateau, joueur, x, y);
  129. }
  130. joueur[x][y]=idbateau;
  131. joueur[x][y+j]=idbateau;
  132. }else if(random_position==4){ //Vers la gauche
  133. while(y-taillebateau<0 || superpo==1){
  134. x=doRand(0, tailleX-1);
  135. y=doRand(0, tailleY-1);
  136. superpo=Superposition(random_position, taillebateau, joueur, x, y);
  137. }
  138. joueur[x][y]=idbateau;
  139. joueur[x][y-j]=idbateau;
  140. }
  141. }
  142.  
  143.  
  144. }
  145.  
  146. void PlacementBateauxIA(int joueur[18][24], int tailleX, int tailleY){
  147.  
  148. PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_PORTEAVION, PORTEAVIONID);
  149. PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_CROISEUR, CROISEURID);
  150. PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_CROISEUR, CROISEURID);
  151. PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_CORVETTE, CORVETTEID);
  152. PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_DESTROYER, DESTROYERID);
  153. PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_DESTROYER, DESTROYERID);
  154.  
  155. }
  156.  
  157. void PlacementUnBateauJoueur(int joueur[18][24], int tailleX, int tailleY, int taillebateau, int idbateau){ //Fonction pour placement des bateaux des joueurs
  158.  
  159. int i=0;
  160. int y=0;
  161. int x=0;
  162. int isOKBatal[4]={1,1,1,1};
  163. int superpo=1;
  164. int platoF[18][24];
  165. int direction;
  166. int batopose=0;
  167.  
  168. for(i=0; i<18; i++)
  169. memcpy(&platoF[i], &joueur[i], sizeof(joueur[0]));
  170.  
  171. printf("Vous devez maintenant placer vos bateaux !\n\n");
  172. do{
  173. printf("Placement du Bateau de %d cases de longueur :\n\n",taillebateau);
  174. AffichagePlateau(joueur, tailleX, tailleY);
  175. printf("Entrez les coordonnées X et Y du point de départ du bateau :\n\n");
  176. do{
  177. printf("Position Y (horizontal) : ");
  178. scanf("%d", &y);
  179. printf("Position X (vertical) : ");
  180. scanf("%d", &x);
  181. }while(joueur[x][y] != 0);
  182.  
  183. platoF [x] [y] = idbateau;
  184. printf("\n\n");
  185. printf("Voici la position de départ de votre bateau :\n\n");
  186. AffichagePlateau(platoF, tailleX, tailleY);
  187.  
  188. for(i=1; i<=4; i++){
  189. superpo=Superposition(i, taillebateau, joueur, x, y);
  190. if(i==1){
  191. if(x+taillebateau>tailleX || superpo==1){
  192. isOKBatal[i-1]=0;
  193. }
  194. }else if(i==2){
  195. if(x-taillebateau<0 || superpo==1){
  196. isOKBatal[i-1]=0;
  197. }
  198. }else if(i==3){
  199. if(y+taillebateau>tailleY || superpo==1){
  200. isOKBatal[i-1]=0;
  201. }
  202. }else if(i==4){
  203. if(y-taillebateau<0 || superpo==1){
  204. isOKBatal[i-1]=0;
  205. }
  206. }
  207. }
  208.  
  209.  
  210. for(i=0; i<=3; i++){
  211. if(isOKBatal[i]==1){
  212. if(i==0){
  213. printf("1. Vers le bas\n");
  214. }else if(i==1){
  215. printf("2. Vers le haut\n");
  216. }else if(i==2){
  217. printf("3. Vers la droite\n");
  218. }else if(i==3){
  219. printf("4. Vers la gauche\n");
  220. }
  221. }
  222. }
  223. }while(isOKBatal[0] == 0 && isOKBatal[1] == 0 && isOKBatal[2] == 0 && isOKBatal[3] == 0);
  224.  
  225.  
  226. do{
  227. scanf("%d",&direction);
  228.  
  229. switch(direction){
  230.  
  231. case 1:
  232. if(isOKBatal[0]==1){
  233. for(i=0; i<taillebateau; i++){
  234. joueur[x][y]=idbateau;
  235. joueur[x+i][y]=idbateau;
  236. }
  237. batopose=1;
  238. }
  239. else
  240. printf("Tu ne peux pas faire cela, recommence\n");
  241. break;
  242.  
  243. case 2:
  244. if(isOKBatal[1]==1){
  245. for(i=0; i<taillebateau; i++){
  246. joueur[x][y]=idbateau;
  247. joueur[x-i][y]=idbateau;
  248. }
  249. batopose=1;
  250. }
  251. else
  252. printf("Tu ne peux pas faire cela, recommence\n");
  253. break;
  254.  
  255. case 3:
  256. if(isOKBatal[2]==1){
  257. for(i=0; i<taillebateau; i++){
  258. joueur[x][y]=idbateau;
  259. joueur[x][y+i]=idbateau;
  260. }
  261. batopose=1;
  262. }
  263. else
  264. printf("Tu ne peux pas faire cela, recommence\n");
  265. break;
  266.  
  267. case 4:
  268. if(isOKBatal[3]==1){
  269. for(i=0; i<taillebateau; i++){
  270. joueur[x][y]=idbateau;
  271. joueur[x][y-i]=idbateau;
  272. }
  273. batopose=1;
  274. }
  275. else
  276. printf("Tu ne peux pas faire cela, recommence\n");
  277. break;
  278. }
  279.  
  280. }while(batopose==0);
  281.  
  282.  
  283. }
  284.  
  285. void AffichagePlateau(int joueur[18][24], int tailleX, int tailleY){ //Fonction pour afficher le plateau d'un joueur
  286.  
  287. int i=0;
  288. int j=0;
  289. int valeur[24]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
  290.  
  291. printf(" ");
  292. for(i=0;i<tailleX;i++){
  293. if(i==0 || i==1 || i==2 || i==3 || i==4 || i==5 || i==6 || i==7 || i==8 || i==9){
  294. printf(" %d", i);
  295. }else{
  296. printf(" %d", i);
  297. }
  298. }
  299. printf("\n ");
  300.  
  301. for(i=0;i<tailleX;i++){
  302. printf("---");
  303. }
  304.  
  305. for(i=0;i<tailleY;i++){
  306. if(i==0 || i==1 || i==2 || i==3 || i==4 || i==5 || i==6 || i==7 || i==8 || i==9){
  307. printf("\n");
  308. printf("%d | ", valeur[i]);
  309. }else{
  310. printf("\n");
  311. printf("%d | ", valeur[i]);
  312. }
  313. for(j=0;j<tailleX;j++){
  314. printf(" %d ", joueur[i][j]);
  315. }
  316. }
  317. printf("\n\n");
  318.  
  319. }
  320.  
  321. char SerumDeVerite(int joueur[18][24], int tailleX, int tailleY, int x, int y){ //Fonction pour tester
  322.  
  323. int i=0;
  324. int j=0;
  325. char *toucheroupas={'0'};
  326.  
  327. for(i=0;i<tailleX;i++){
  328. for(j=0;j<tailleY;j++){
  329. if(joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4 || joueur[x][y]==5){
  330. toucheroupas = "Touche-Coule";
  331. }else{
  332. toucheroupas = "Touche";
  333. }
  334. }
  335. }
  336. return toucheroupas;
  337.  
  338. }
  339.  
  340. char *ImmaFiringMahLazor(int x, int y, int joueur[18][24], int tirprecedent[3], int tailleX, int tailleY){
  341.  
  342. while(joueur[x][y]==9 || joueur[x][y]==7){
  343. x=doRand(0, tailleX-1);
  344. y=doRand(0, tailleY-1);
  345. }
  346. if(joueur[x][y]==0){
  347. joueur[x][y]=9;
  348. printf("L'IA attaque en %d/%d\n\n", x, y);
  349. return "Plouf!";
  350. }else if(joueur[x][y]==CORVETTEID || joueur[x][y]==DESTROYERID || joueur[x][y]==CROISEURID || joueur[x][y]==PORTEAVIONID){
  351. tirprecedent[0]=x; tirprecedent[1]=y; tirprecedent[2]=joueur[x][y];
  352. joueur[x][y]=7;
  353. printf("L'IA attaque en %d/%d\n\n", x, y);
  354. return "Touche!";
  355. //return SerumDeVerite(joueur, tailleX, tailleY, x, y);
  356. }
  357. return "Vous avez deja tire ici";
  358.  
  359. }
  360.  
  361. void IAvsIA(int joueur1[18][24], int joueur2[18][24], int x, int y, int tailleX, int tailleY){
  362.  
  363. int tirprecedent2[3]={0};
  364. int tirprecedent1[3]={0};
  365.  
  366. printf("IA vs. IA !\n\n");
  367.  
  368. do{
  369. printf("Choisissez la longueur de votre plateau (compris entre 8 et 10) : ");
  370. scanf("%d[0-9]", &tailleX);
  371. }while(tailleX<8 || tailleX>24);
  372. printf("\n");
  373. fflush(stdin);
  374.  
  375. do{
  376. printf("Choisissez la largeur de votre plateau (compris entre 8 et 10) : ");
  377. scanf("%d[0-9]", &tailleY);
  378. }while(tailleY<8 || tailleY>18);
  379. printf("\n");
  380. fflush(stdin);
  381. system("cls");
  382.  
  383. //Placement bateau IA1
  384. PlacementBateauxIA(joueur1, tailleX, tailleY);
  385. //Placement bateau IA2
  386. PlacementBateauxIA(joueur2, tailleX, tailleY);
  387.  
  388.  
  389. //Affichage du plateau de l'IA 1
  390. printf("Plateau de l'IA 1 :\n\n");
  391. AffichagePlateau(joueur1, tailleX, tailleY);
  392. printf("\n\n");
  393.  
  394. //Affichage du plateau de l'IA 2
  395. printf("Plateau de l'IA 2 :\n\n");
  396. AffichagePlateau(joueur2, tailleX, tailleY);
  397. printf("\n");
  398. getchar();
  399. system("cls");
  400.  
  401. while(getchar()){
  402.  
  403. //L'IA 1 tire sur l'IA 2
  404. printf("Le joueur 1 tire :\n\n");
  405.  
  406. x=doRand(0, tailleX-1);
  407. y=doRand(0, tailleY-1);
  408.  
  409. if(tirprecedent1[0]==0 && tirprecedent1[1]==0){
  410. printf("%s", ImmaFiringMahLazor(x, y, joueur2, tirprecedent1, tailleX, tailleY));
  411. }else{
  412. printf("%s", ImmaFiringMahLazor(tirprecedent1[0], tirprecedent1[1], joueur2, tirprecedent1, tailleX, tailleY));
  413. }printf("\n\n");
  414.  
  415. AffichagePlateau(joueur2, tailleX, tailleY);
  416. printf("\n\n");
  417.  
  418. //L'IA 2 tire sur l'IA 1
  419. printf("Le joueur 2 tire :\n\n");
  420.  
  421. x=doRand(0, tailleX-1);
  422. y=doRand(0, tailleY-1);
  423.  
  424. if(tirprecedent2[0]==0 && tirprecedent2[1]==0){
  425. printf("%s", ImmaFiringMahLazor(x, y, joueur1, tirprecedent2, tailleX, tailleY));
  426. }else{
  427. printf("%s", ImmaFiringMahLazor(tirprecedent2[0], tirprecedent2[1], joueur1, tirprecedent2, tailleX, tailleY));
  428. }printf("\n\n");
  429.  
  430. AffichagePlateau(joueur1, tailleX, tailleY);
  431. printf("\n\n");
  432. getchar();
  433. system("cls");
  434. }
  435.  
  436. }
  437.  
  438. void Menu(int joueur1[18][24], int joueur2[18][24],int x,int y,int tailleX, int tailleY){
  439.  
  440. int choix=0;
  441. int mode=0;
  442.  
  443. do{
  444. printf("Bienvenue dans BattleShip!\n\n");
  445. printf("1. Jouer\n");
  446. printf("2. Quitter\n\n");
  447. printf("Votre choix : ");
  448. scanf("%d[0-9]", &choix);
  449. fflush(stdin);
  450. system("cls");
  451. }while(choix!=1 && choix!=2);
  452.  
  453. if(choix==1){
  454. printf("Choississez votre mode de jeu :\n\n");
  455. printf("1. IA vs. IA\n");
  456. printf("2. Joueur vs. IA\n");
  457. printf("3. Joueur vs. Joueur\n");
  458. printf("Votre choix : ");
  459. scanf("%d", &mode);
  460. system("cls");
  461.  
  462. switch(mode){
  463.  
  464. case 1:
  465. IAvsIA(joueur1, joueur2, x, y, tailleX, tailleY); //Mode IA vs IA
  466. break;
  467.  
  468. case 2:
  469. //Joueur vs. IA
  470. JoueurVsIA(joueur1, joueur2, 18, 24);
  471. break;
  472.  
  473. case 3:
  474. //Joueur vs. Joueur
  475. break;
  476. }
  477. }
  478.  
  479. }
  480.  
  481. void JoueurVsIA(int joueur1[18][24], int joueur2[18][24],int tailleX, int tailleY){
  482.  
  483.  
  484. int x, y, diff, i;
  485. int tailleBateau[6]={TAILLE_PORTEAVION, TAILLE_CROISEUR, TAILLE_CROISEUR, TAILLE_DESTROYER, TAILLE_DESTROYER, TAILLE_CORVETTE};
  486. int idBateau[6]={PORTEAVIONID, CROISEURID, CROISEURID, DESTROYERID, DESTROYERID, CORVETTEID};
  487. int platoF[18][24];
  488.  
  489. for(i=0; i<18; i++){
  490. memcpy(&platoF[i], &joueur1[i], sizeof(joueur1[0]));
  491. }
  492.  
  493. do{
  494. printf("JOUEUR vs. IA !\n\n");
  495. printf("1. Mode Izi Pizi\n");
  496. printf("2. Mode Narmol\n");
  497. printf("3. Mode Aim Boat\n\n");
  498. printf("Votre choix : ");
  499. scanf("%d", &diff);
  500. fflush(stdin);
  501. system("cls");
  502. }while(diff<1 && diff>3);
  503.  
  504. for(i=0; i<6; i++){
  505. printf("Placez vos Bateaux !\n\n");
  506. PlacementUnBateauJoueur(joueur1, tailleX, tailleY, tailleBateau[i], idBateau[i]);
  507. }
  508.  
  509. PlacementBateauxIA(joueur1, tailleX, tailleY);
  510. system("cls");
  511.  
  512. printf("Voici votre plateau pour cette partie !\n\n");
  513. AffichagePlateau(joueur1, tailleX, tailleY);
  514.  
  515. PlacementBateauxIA(joueur2, tailleX, tailleY);
  516.  
  517. waitFor(10);
  518. system("cls");
  519.  
  520. printf("Vous commencez !\n\n");
  521. while(1){
  522. printf("Voici votre plateau : \n\n");
  523. AffichagePlateau(platoF, tailleX, tailleY);
  524. TireJoueur(joueur2,platoF);
  525. if(FinPartie(joueur2)==1)
  526. break;
  527. if(diff==1){
  528. IAfacile(joueur1);
  529. }else if(diff==2){
  530. //IAnormal(joueur1);
  531. }else if(diff==3){
  532. IAhardcore(joueur1);
  533. }
  534. if(FinPartie(joueur1)==1)
  535. break;
  536. }
  537.  
  538.  
  539. }
  540.  
  541. void IAfacile(int joueur[18][24]){
  542. int x, y;
  543.  
  544. do{
  545. x=doRand(0, 24);
  546. y=doRand(0, 18);
  547. }while(joueur[x][y]==9 || joueur[x][y]==7);
  548.  
  549. if(joueur[x][y]==0){
  550. joueur[x][y]=9;
  551. printf("L'IA rate ! Plouf!\n ");
  552. }else if(joueur[x][y]==1 || joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4){
  553. joueur[x][y]=7;
  554. printf("\nL'IA touche!\n");
  555. }
  556. }
  557.  
  558. void IAfacille();
  559.  
  560. void IAhardcore(int joueur[18][24]){
  561. int x, y;
  562.  
  563. do{
  564. x=doRand(0, 24);
  565. y=doRand(0, 18);
  566. }while(joueur[x][y]==9 || joueur[x][y]==7 || joueur[x][y]==0);
  567.  
  568. if(joueur[x][y]==0){
  569. joueur[x][y]=9;
  570. printf("L'IA attaque en %d/%d\n\n", x, y);
  571. printf("L'IA rate ! Plouf!\n ");
  572. }else if(joueur[x][y]==1 || joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4){
  573. joueur[x][y]=7;
  574. printf("\nL'IA touche!\n ");
  575. }
  576. }
  577.  
  578. void TireJoueur(int joueur[18][24]){//Fait tirer le joueur et remplace les cases
  579. int x, y;
  580.  
  581. printf("C'est votre tour !\n\n");
  582. printf("Entrez la coordonnee x: ");
  583. scanf("%d",&x);
  584. printf("\nEntrez la coordonnee y: ");
  585. scanf("%d",&y);
  586.  
  587. if(joueur[x][y]==0){
  588. joueur[x][y]=9;
  589. printf("\nPlouf\n ");
  590. }else if(joueur[x][y]==1 || joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4){
  591. joueur[x][y]=7;
  592. printf("\nTouché !!!\n ");
  593. }else if(joueur[x][y]==7){
  594. printf("\nTu as déjà tié ici recommence !\n ");
  595. TireJoueur(joueur);
  596. }
  597. }
  598.  
  599. int FinPartie(int joueur[18][24]){//check le plateau reçu, return 1 si tout est détruit, 0 sinon
  600. int j,i;
  601.  
  602. for(i=0; i<18; i++){
  603. for(j=0; j<24; j++){
  604. if(joueur[i][j]==1 || joueur[i][j]==2 || joueur[i][j]==3 || joueur[i][j]==4)
  605. return 0;
  606. }
  607. }
  608.  
  609. return 1;
  610. }
  611.  
  612. void JeuBatailleNavale(){
  613.  
  614. }
  615.  
  616. int main(){
  617.  
  618. while(1){
  619.  
  620. int joueur1[100][100]={0};
  621. int joueur2[100][100]={0};
  622. int tailleX=0;
  623. int tailleY=0;
  624. int x=0;
  625. int y=0;
  626.  
  627. Menu(joueur1, joueur2, x, y, tailleX, tailleY);
  628. break;
  629.  
  630. }
  631.  
  632. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement