Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Samuele Mazzoglia 3^Dinfo 12/02
- //Battaglia navale
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #include<time.h>
- int main()
- {
- srand(time(0)); //imposti il valore del tempo a 0
- int v[10][10],v1[10][10];
- char m[10][10],m1[10][10];
- int i,j,dir,x,y,nave,cont=0,contx,conty,win=0,aff=0,turno=0,win1=0,aff1=0;
- //azzeramento matrice
- for(i=0;i<10;i++)
- {
- for(j=0;j<10;j++)
- {
- v[i][j]=0;
- }
- }
- //azzeramento matrice
- for(i=0;i<10;i++)
- {
- for(j=0;j<10;j++)
- {
- m[i][j]='s';
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Giocatore 1
- do
- {
- cont++;
- printf("Inserisci di quanti blocchi e' la nave: ");
- scanf("%d",&nave);
- dir=rand()%2+1; //direzione 1 orizzontale 2 verticale
- x=rand()%10+1;
- y=rand()%10+1;
- contx=x;
- conty=y;
- if(dir==1)
- {
- if(x<=5)
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else
- {
- contx=contx+1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v[y-1][x-1]=nave;
- v[y-2][x-1]=9;
- v[y][x-1]=9;
- x=x+1;
- }
- }
- else
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else
- {
- contx=contx-1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v[y-1][x-1]=nave;
- v[y-2][x-1]=9;
- v[y][x-1]=9;
- x=x-1;
- }
- }
- }
- if(dir==2)
- {
- if(y<=5)
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else{
- conty=conty+1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v[y-1][x-1]=nave;
- v[y-1][x-2]=9;
- v[y-1][x]=9;
- y=y+1;
- }
- }
- else
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else{
- conty=conty-1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v[y-1][x-1]=nave;
- v[y-1][x-2]=9;
- v[y-1][x]=9;
- y=y-1;
- }
- }
- }
- //stampa matrice navi
- for(i=0;i<10;i++)
- {
- printf("\n");
- for(j=0;j<10;j++)
- {
- printf("\t%d",v[i][j]);
- }
- }
- printf("\n");
- }
- while(cont<15);
- //pulisce lo schermo per non far vedere le navi
- system("cls");
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Giocatore 2
- do
- {
- cont1++;
- printf("Inserisci di quanti blocchi e' la nave: ");
- scanf("%d",&nave);
- dir=rand()%2+1; //direzione 1 orizzontale 2 verticale
- x=rand()%10+1;
- y=rand()%10+1;
- contx=x;
- conty=y;
- if(dir==1)
- {
- if(x<=5)
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v1[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else
- {
- contx=contx+1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v1[y-1][x-1]=nave;
- v1[y-2][x-1]=9;
- v1[y][x-1]=9;
- x=x+1;
- }
- }
- else
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v1[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else
- {
- contx=contx-1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v1[y-1][x-1]=nave;
- v1[y-2][x-1]=9;
- v1[y][x-1]=9;
- x=x-1;
- }
- }
- }
- if(dir==2)
- {
- if(y<=5)
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v1[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else{
- conty=conty+1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v1[y-1][x-1]=nave;
- v1[y-1][x-2]=9;
- v1[y-1][x]=9;
- y=y+1;
- }
- }
- else
- {
- //controllo
- for(i=0;i<nave;i++)
- {
- if(v1[contx][conty]!=0)
- {
- contx=rand()%10+1;
- conty=rand()%10+1;
- i=-1;
- x=contx;
- y=conty;
- }
- else{
- conty=conty-1;
- }
- }
- //assegnazione
- for(i=0;i<nave;i++)
- {
- v1[y-1][x-1]=nave;
- v1[y-1][x-2]=9;
- v1[y-1][x]=9;
- y=y-1;
- }
- }
- }
- //stampa matrice navi
- for(i=0;i<10;i++)
- {
- printf("\n");
- for(j=0;j<10;j++)
- {
- printf("\t%d",v1[i][j]);
- }
- }
- printf("\n");
- }
- while(cont1<15);
- //pulisce lo schermo per non far vedere le navi
- system("cls");
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Sparo del giocatore 1
- if(turno%2==0)
- {
- printf("Inserisci la cordanata x: ");
- scanf("%d",&x);
- printf("Inserisci la cordanata y: ");
- scanf("%d",&y);
- //sparo
- 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)
- {
- printf("Colpito");
- m[y-1][x-1]='X';
- win++;
- aff++;
- if(aff==v[y-1][x]-1)
- {
- printf("\nAffondata");
- aff=0;
- }
- }
- else
- {
- printf("Mancato");
- m[y-1][x-1]='O';
- }
- //stampa matrice navi
- for(i=0;i<10;i++)
- {
- printf("\n");
- for(j=0;j<10;j++)
- {
- printf("\t%c",m[i][j]);
- }
- }
- printf("\n");
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Sparo del giocatore 2
- else
- {
- printf("Inserisci la cordanata x: ");
- scanf("%d",&x);
- printf("Inserisci la cordanata y: ");
- scanf("%d",&y);
- //sparo
- 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)
- {
- printf("Colpito");
- m1[y-1][x-1]='X';
- win1++;
- aff1++;
- if(aff1==v1[y-1][x]-1)
- {
- printf("\nAffondata");
- aff1=0;
- }
- }
- else
- {
- printf("Mancato");
- m1[y-1][x-1]='O';
- }
- //stampa matrice navi
- for(i=0;i<10;i++)
- {
- printf("\n");
- for(j=0;j<10;j++)
- {
- printf("\t%c",m1[i][j]);
- }
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement