Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #define TAILLEXMAX 18
- #define TAILLEYMAX 24
- #define PORTEAVIONID 4
- #define TAILLE_PORTEAVION 6
- #define CROISEURID 3
- #define TAILLE_CROISEUR 4
- #define DESTROYERID 2
- #define TAILLE_DESTROYER 3
- #define CORVETTEID 1
- #define TAILLE_CORVETTE 1
- void waitFor(unsigned int secs){
- unsigned int retTime = time(0) + secs;
- while (time(0) < retTime);
- }
- int doRand(int startVal, int endVal){
- waitFor(0.05);
- srand(time(NULL)*rand());
- if(startVal == 0 && endVal == 1){
- return rand() % 2;
- }else{
- return (rand() % ((endVal + startVal -1)) + startVal);
- }
- }
- void FilerWrite(){
- FILE* ftp = fopen("./test1.txt","a+");
- int i = 0;
- while(i < 1000){
- fprintf(ftp,"Hello world %d",i);
- i++;
- }
- fclose(ftp);
- }
- void FilerRead(){
- FILE* fptr = fopen("./test1.txt","r");
- char c;
- while(1)
- {
- c = fgetc(fptr);
- if ( feof(fptr) )
- {
- break;
- }
- printf ("%c", c);
- }
- fclose(fptr);
- }
- 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
- int i=0;
- switch(random_position){
- case 1:
- for(i=0;i<=taille;i++){
- if(joueur[x+i][y] != 0)
- return 1;
- }
- break;
- case 2:
- for(i=0;i<=taille;i++){
- if(joueur[x-i][y] != 0)
- return 1;
- }
- break;
- case 3:
- for(i=0;i<=taille;i++){
- if(joueur[x][y+i] != 0)
- return 1;
- }
- break;
- case 4:
- for(i=0;i<=taille;i++){
- if(joueur[x][y-i] != 0)
- return 1;
- }
- break;
- }
- return 0;
- }
- void PlacementUnBateauIA(int joueur[18][24], int tailleX, int tailleY, int taillebateau, int idbateau){ //Fonction pour placement des bateaux
- int j=0;
- int y=0;
- int x=0;
- int random_position=0;
- int superpo=1;
- random_position=doRand(1,4);
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- superpo=1;
- for(j=0;j<taillebateau;j++){
- if(random_position==1){ //Vers le bas
- while(x+taillebateau>=tailleX || superpo==1){
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- superpo=Superposition(random_position, taillebateau, joueur, x, y);
- }
- joueur[x][y]=idbateau;
- joueur[x+j][y]=idbateau;
- }else if(random_position==2){ //Vers le haut
- while(x-taillebateau<0 || superpo==1){
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- superpo=Superposition(random_position, taillebateau, joueur, x, y);
- }
- joueur[x][y]=idbateau;
- joueur[x-j][y]=idbateau;
- }else if(random_position==3){ //Vers la droite
- while(y+taillebateau>=tailleY || superpo==1){
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- superpo=Superposition(random_position, taillebateau, joueur, x, y);
- }
- joueur[x][y]=idbateau;
- joueur[x][y+j]=idbateau;
- }else if(random_position==4){ //Vers la gauche
- while(y-taillebateau<0 || superpo==1){
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- superpo=Superposition(random_position, taillebateau, joueur, x, y);
- }
- joueur[x][y]=idbateau;
- joueur[x][y-j]=idbateau;
- }
- }
- }
- void PlacementBateauxIA(int joueur[18][24], int tailleX, int tailleY){
- PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_PORTEAVION, PORTEAVIONID);
- PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_CROISEUR, CROISEURID);
- PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_CROISEUR, CROISEURID);
- PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_CORVETTE, CORVETTEID);
- PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_DESTROYER, DESTROYERID);
- PlacementUnBateauIA(joueur, tailleX, tailleY, TAILLE_DESTROYER, DESTROYERID);
- }
- void PlacementUnBateauJoueur(int joueur[18][24], int tailleX, int tailleY, int taillebateau, int idbateau){ //Fonction pour placement des bateaux des joueurs
- int i=0;
- int y=0;
- int x=0;
- int isOKBatal[4]={1,1,1,1};
- int superpo=1;
- int platoF[18][24];
- int direction;
- int batopose=0;
- for(i=0; i<18; i++)
- memcpy(&platoF[i], &joueur[i], sizeof(joueur[0]));
- printf("Vous devez maintenant placer vos bateaux !\n\n");
- do{
- printf("Placement du Bateau de %d cases de longueur :\n\n",taillebateau);
- AffichagePlateau(joueur, tailleX, tailleY);
- printf("Entrez les coordonnées X et Y du point de départ du bateau :\n\n");
- do{
- printf("Position Y (horizontal) : ");
- scanf("%d", &y);
- printf("Position X (vertical) : ");
- scanf("%d", &x);
- }while(joueur[x][y] != 0);
- platoF [x] [y] = idbateau;
- printf("\n\n");
- printf("Voici la position de départ de votre bateau :\n\n");
- AffichagePlateau(platoF, tailleX, tailleY);
- for(i=1; i<=4; i++){
- superpo=Superposition(i, taillebateau, joueur, x, y);
- if(i==1){
- if(x+taillebateau>tailleX || superpo==1){
- isOKBatal[i-1]=0;
- }
- }else if(i==2){
- if(x-taillebateau<0 || superpo==1){
- isOKBatal[i-1]=0;
- }
- }else if(i==3){
- if(y+taillebateau>tailleY || superpo==1){
- isOKBatal[i-1]=0;
- }
- }else if(i==4){
- if(y-taillebateau<0 || superpo==1){
- isOKBatal[i-1]=0;
- }
- }
- }
- for(i=0; i<=3; i++){
- if(isOKBatal[i]==1){
- if(i==0){
- printf("1. Vers le bas\n");
- }else if(i==1){
- printf("2. Vers le haut\n");
- }else if(i==2){
- printf("3. Vers la droite\n");
- }else if(i==3){
- printf("4. Vers la gauche\n");
- }
- }
- }
- }while(isOKBatal[0] == 0 && isOKBatal[1] == 0 && isOKBatal[2] == 0 && isOKBatal[3] == 0);
- do{
- scanf("%d",&direction);
- switch(direction){
- case 1:
- if(isOKBatal[0]==1){
- for(i=0; i<taillebateau; i++){
- joueur[x][y]=idbateau;
- joueur[x+i][y]=idbateau;
- }
- batopose=1;
- }
- else
- printf("Tu ne peux pas faire cela, recommence\n");
- break;
- case 2:
- if(isOKBatal[1]==1){
- for(i=0; i<taillebateau; i++){
- joueur[x][y]=idbateau;
- joueur[x-i][y]=idbateau;
- }
- batopose=1;
- }
- else
- printf("Tu ne peux pas faire cela, recommence\n");
- break;
- case 3:
- if(isOKBatal[2]==1){
- for(i=0; i<taillebateau; i++){
- joueur[x][y]=idbateau;
- joueur[x][y+i]=idbateau;
- }
- batopose=1;
- }
- else
- printf("Tu ne peux pas faire cela, recommence\n");
- break;
- case 4:
- if(isOKBatal[3]==1){
- for(i=0; i<taillebateau; i++){
- joueur[x][y]=idbateau;
- joueur[x][y-i]=idbateau;
- }
- batopose=1;
- }
- else
- printf("Tu ne peux pas faire cela, recommence\n");
- break;
- }
- }while(batopose==0);
- }
- void AffichagePlateau(int joueur[18][24], int tailleX, int tailleY){ //Fonction pour afficher le plateau d'un joueur
- int i=0;
- int j=0;
- 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};
- printf(" ");
- for(i=0;i<tailleX;i++){
- if(i==0 || i==1 || i==2 || i==3 || i==4 || i==5 || i==6 || i==7 || i==8 || i==9){
- printf(" %d", i);
- }else{
- printf(" %d", i);
- }
- }
- printf("\n ");
- for(i=0;i<tailleX;i++){
- printf("---");
- }
- for(i=0;i<tailleY;i++){
- if(i==0 || i==1 || i==2 || i==3 || i==4 || i==5 || i==6 || i==7 || i==8 || i==9){
- printf("\n");
- printf("%d | ", valeur[i]);
- }else{
- printf("\n");
- printf("%d | ", valeur[i]);
- }
- for(j=0;j<tailleX;j++){
- printf(" %d ", joueur[i][j]);
- }
- }
- printf("\n\n");
- }
- char SerumDeVerite(int joueur[18][24], int tailleX, int tailleY, int x, int y){ //Fonction pour tester
- int i=0;
- int j=0;
- char *toucheroupas={'0'};
- for(i=0;i<tailleX;i++){
- for(j=0;j<tailleY;j++){
- if(joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4 || joueur[x][y]==5){
- toucheroupas = "Touche-Coule";
- }else{
- toucheroupas = "Touche";
- }
- }
- }
- return toucheroupas;
- }
- char *ImmaFiringMahLazor(int x, int y, int joueur[18][24], int tirprecedent[3], int tailleX, int tailleY){
- while(joueur[x][y]==9 || joueur[x][y]==7){
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- }
- if(joueur[x][y]==0){
- joueur[x][y]=9;
- printf("L'IA attaque en %d/%d\n\n", x, y);
- return "Plouf!";
- }else if(joueur[x][y]==CORVETTEID || joueur[x][y]==DESTROYERID || joueur[x][y]==CROISEURID || joueur[x][y]==PORTEAVIONID){
- tirprecedent[0]=x; tirprecedent[1]=y; tirprecedent[2]=joueur[x][y];
- joueur[x][y]=7;
- printf("L'IA attaque en %d/%d\n\n", x, y);
- return "Touche!";
- //return SerumDeVerite(joueur, tailleX, tailleY, x, y);
- }
- return "Vous avez deja tire ici";
- }
- void IAvsIA(int joueur1[18][24], int joueur2[18][24], int x, int y, int tailleX, int tailleY){
- int tirprecedent2[3]={0};
- int tirprecedent1[3]={0};
- printf("IA vs. IA !\n\n");
- do{
- printf("Choisissez la longueur de votre plateau (compris entre 8 et 10) : ");
- scanf("%d[0-9]", &tailleX);
- }while(tailleX<8 || tailleX>24);
- printf("\n");
- fflush(stdin);
- do{
- printf("Choisissez la largeur de votre plateau (compris entre 8 et 10) : ");
- scanf("%d[0-9]", &tailleY);
- }while(tailleY<8 || tailleY>18);
- printf("\n");
- fflush(stdin);
- system("cls");
- //Placement bateau IA1
- PlacementBateauxIA(joueur1, tailleX, tailleY);
- //Placement bateau IA2
- PlacementBateauxIA(joueur2, tailleX, tailleY);
- //Affichage du plateau de l'IA 1
- printf("Plateau de l'IA 1 :\n\n");
- AffichagePlateau(joueur1, tailleX, tailleY);
- printf("\n\n");
- //Affichage du plateau de l'IA 2
- printf("Plateau de l'IA 2 :\n\n");
- AffichagePlateau(joueur2, tailleX, tailleY);
- printf("\n");
- getchar();
- system("cls");
- while(getchar()){
- //L'IA 1 tire sur l'IA 2
- printf("Le joueur 1 tire :\n\n");
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- if(tirprecedent1[0]==0 && tirprecedent1[1]==0){
- printf("%s", ImmaFiringMahLazor(x, y, joueur2, tirprecedent1, tailleX, tailleY));
- }else{
- printf("%s", ImmaFiringMahLazor(tirprecedent1[0], tirprecedent1[1], joueur2, tirprecedent1, tailleX, tailleY));
- }printf("\n\n");
- AffichagePlateau(joueur2, tailleX, tailleY);
- printf("\n\n");
- //L'IA 2 tire sur l'IA 1
- printf("Le joueur 2 tire :\n\n");
- x=doRand(0, tailleX-1);
- y=doRand(0, tailleY-1);
- if(tirprecedent2[0]==0 && tirprecedent2[1]==0){
- printf("%s", ImmaFiringMahLazor(x, y, joueur1, tirprecedent2, tailleX, tailleY));
- }else{
- printf("%s", ImmaFiringMahLazor(tirprecedent2[0], tirprecedent2[1], joueur1, tirprecedent2, tailleX, tailleY));
- }printf("\n\n");
- AffichagePlateau(joueur1, tailleX, tailleY);
- printf("\n\n");
- getchar();
- system("cls");
- }
- }
- void Menu(int joueur1[18][24], int joueur2[18][24],int x,int y,int tailleX, int tailleY){
- int choix=0;
- int mode=0;
- do{
- printf("Bienvenue dans BattleShip!\n\n");
- printf("1. Jouer\n");
- printf("2. Quitter\n\n");
- printf("Votre choix : ");
- scanf("%d[0-9]", &choix);
- fflush(stdin);
- system("cls");
- }while(choix!=1 && choix!=2);
- if(choix==1){
- printf("Choississez votre mode de jeu :\n\n");
- printf("1. IA vs. IA\n");
- printf("2. Joueur vs. IA\n");
- printf("3. Joueur vs. Joueur\n");
- printf("Votre choix : ");
- scanf("%d", &mode);
- system("cls");
- switch(mode){
- case 1:
- IAvsIA(joueur1, joueur2, x, y, tailleX, tailleY); //Mode IA vs IA
- break;
- case 2:
- //Joueur vs. IA
- JoueurVsIA(joueur1, joueur2, 18, 24);
- break;
- case 3:
- //Joueur vs. Joueur
- break;
- }
- }
- }
- void JoueurVsIA(int joueur1[18][24], int joueur2[18][24],int tailleX, int tailleY){
- int x, y, diff, i;
- int tailleBateau[6]={TAILLE_PORTEAVION, TAILLE_CROISEUR, TAILLE_CROISEUR, TAILLE_DESTROYER, TAILLE_DESTROYER, TAILLE_CORVETTE};
- int idBateau[6]={PORTEAVIONID, CROISEURID, CROISEURID, DESTROYERID, DESTROYERID, CORVETTEID};
- int platoF[18][24];
- for(i=0; i<18; i++){
- memcpy(&platoF[i], &joueur1[i], sizeof(joueur1[0]));
- }
- do{
- printf("JOUEUR vs. IA !\n\n");
- printf("1. Mode Izi Pizi\n");
- printf("2. Mode Narmol\n");
- printf("3. Mode Aim Boat\n\n");
- printf("Votre choix : ");
- scanf("%d", &diff);
- fflush(stdin);
- system("cls");
- }while(diff<1 && diff>3);
- for(i=0; i<6; i++){
- printf("Placez vos Bateaux !\n\n");
- PlacementUnBateauJoueur(joueur1, tailleX, tailleY, tailleBateau[i], idBateau[i]);
- }
- PlacementBateauxIA(joueur1, tailleX, tailleY);
- system("cls");
- printf("Voici votre plateau pour cette partie !\n\n");
- AffichagePlateau(joueur1, tailleX, tailleY);
- PlacementBateauxIA(joueur2, tailleX, tailleY);
- waitFor(10);
- system("cls");
- printf("Vous commencez !\n\n");
- while(1){
- printf("Voici votre plateau : \n\n");
- AffichagePlateau(platoF, tailleX, tailleY);
- TireJoueur(joueur2,platoF);
- if(FinPartie(joueur2)==1)
- break;
- if(diff==1){
- IAfacile(joueur1);
- }else if(diff==2){
- //IAnormal(joueur1);
- }else if(diff==3){
- IAhardcore(joueur1);
- }
- if(FinPartie(joueur1)==1)
- break;
- }
- }
- void IAfacile(int joueur[18][24]){
- int x, y;
- do{
- x=doRand(0, 24);
- y=doRand(0, 18);
- }while(joueur[x][y]==9 || joueur[x][y]==7);
- if(joueur[x][y]==0){
- joueur[x][y]=9;
- printf("L'IA rate ! Plouf!\n ");
- }else if(joueur[x][y]==1 || joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4){
- joueur[x][y]=7;
- printf("\nL'IA touche!\n");
- }
- }
- void IAfacille();
- void IAhardcore(int joueur[18][24]){
- int x, y;
- do{
- x=doRand(0, 24);
- y=doRand(0, 18);
- }while(joueur[x][y]==9 || joueur[x][y]==7 || joueur[x][y]==0);
- if(joueur[x][y]==0){
- joueur[x][y]=9;
- printf("L'IA attaque en %d/%d\n\n", x, y);
- printf("L'IA rate ! Plouf!\n ");
- }else if(joueur[x][y]==1 || joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4){
- joueur[x][y]=7;
- printf("\nL'IA touche!\n ");
- }
- }
- void TireJoueur(int joueur[18][24]){//Fait tirer le joueur et remplace les cases
- int x, y;
- printf("C'est votre tour !\n\n");
- printf("Entrez la coordonnee x: ");
- scanf("%d",&x);
- printf("\nEntrez la coordonnee y: ");
- scanf("%d",&y);
- if(joueur[x][y]==0){
- joueur[x][y]=9;
- printf("\nPlouf\n ");
- }else if(joueur[x][y]==1 || joueur[x][y]==2 || joueur[x][y]==3 || joueur[x][y]==4){
- joueur[x][y]=7;
- printf("\nTouché !!!\n ");
- }else if(joueur[x][y]==7){
- printf("\nTu as déjà tié ici recommence !\n ");
- TireJoueur(joueur);
- }
- }
- int FinPartie(int joueur[18][24]){//check le plateau reçu, return 1 si tout est détruit, 0 sinon
- int j,i;
- for(i=0; i<18; i++){
- for(j=0; j<24; j++){
- if(joueur[i][j]==1 || joueur[i][j]==2 || joueur[i][j]==3 || joueur[i][j]==4)
- return 0;
- }
- }
- return 1;
- }
- void JeuBatailleNavale(){
- }
- int main(){
- while(1){
- int joueur1[100][100]={0};
- int joueur2[100][100]={0};
- int tailleX=0;
- int tailleY=0;
- int x=0;
- int y=0;
- Menu(joueur1, joueur2, x, y, tailleX, tailleY);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement