Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////
- // //
- // Marc González Planes | 1WOPE //
- // //
- // HUNDIR_LA_FLOTA //
- // //
- /////////////////////////////////////
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #include <time.h>
- #include <windows.h>
- #define num_filas 10
- #define num_columnas 10
- // Variables
- int matriz [num_columnas] [num_filas];
- int c,f;
- int i,j;
- int d_rand;
- // Funcions
- int header(); //Feta per que sigui més simple de llegir, igual que la columna
- int columna();
- int direccio(); //Random per saber cap on va el vaixell
- int margen(int x,int y);//Comproba els margens i si esta vuida o no
- int main (){
- srand((unsigned) time(NULL)); //Semilla a 0
- header();
- /* GENERO LA MATRIU AMB TOT A 0 */
- for(i=0;i<num_columnas;i++){
- columna();
- for(j=0;j<num_filas;j++){
- matriz[i][j] = 0;
- printf("%c",254); // Coloco els []
- }
- putchar('\n');
- }
- /* Genero els vaixells */
- direccio();//1-Horitzontal 0-Vertical
- int v3=0; //Sumare cuan estigui complet sino el while seguira fent combinacions
- while(v3<=1){
- f=((rand())%(num_filas-2));//-2 serveix per evitar els voltants
- c=((rand())%(num_columnas-2));
- if(d_rand==0){//vertical
- if(margen(c,f)==0 && margen(c,f+1)==0 && margen(c,f+2)==0){
- matriz[c][f]='X';
- matriz[c][f+1]='X';
- matriz[c][f+2]='X';
- v3++;
- }
- }
- if(d_rand==1){//horizontal
- if(margen(c,f)==0 && margen(c+1,f)==0 && margen(c+2,f)==0){
- matriz[c][f]='X';
- matriz[c+1][f]='X';
- matriz[c+2][f]='X';
- v3++;
- }
- }
- }
- int v2=0;//Numero de v2
- while(v2<=2){
- f=((rand())%(num_filas-1));//-2 serveix per evitar els voltants
- c=((rand())%(num_columnas-1));
- if(d_rand==0){//vertical
- if(margen(c,f)==0 && margen(c,f+1)==0){
- matriz[c][f]='Y';
- matriz[c][f+1]='Y';
- v2++;
- }
- }
- if(d_rand==1){//horizontal
- if(margen(c,f)==0 && margen(c+1,f)==0){
- matriz[c][f]='Y';
- matriz[c+1][f]='Y';
- v2++;
- }
- }
- }
- int v1=0;
- while(v1<=3){f=((rand())%(num_filas));//-2 serveix per evitar els voltants
- c=((rand())%(num_columnas));
- if(d_rand==0){//vertical
- if(margen(c,f)==0){
- matriz[c][f]='Z';
- v1++;
- }
- }
- if(d_rand==1){//horizontal
- if(margen(c,f)==0){
- matriz[c][f]='Z';
- v1++;
- }
- }
- }
- /*Dibuix*/
- header();
- for(i=0;i<num_filas;i++){
- columna();
- for(j=0;j<num_columnas;j++){
- switch(matriz[j][i]){
- case 'X':
- printf("%2c",254);
- break;
- case 'Y':
- printf("%2c",254);
- break;
- case 'Z':
- printf("%2c",254);
- break;
- default:
- printf("%2c",254);
- }
- }
- putchar('\n');
- }
- // Jugar
- int trets=0; // Numero de trets, contador 15-> Or 20->Plata 25->Bronze +25->"Gracies per participar"
- int vaixells=0; //Numero total de vaixells, 'Contador'
- int t_c=9999; // On apunto en la columna
- int t_f=9999; // On apunto en la fila
- int linea=25; //Per evitar grafics
- //Num vaixells?
- gotoxy(5,17);
- printf("\tInstruccions: Primer valor-> Columna\n\t\t\t Segon valor -> Fila\n\t\t\t Format numeric\n\t\t\t Exemple: F=1 C=3\n\n");
- vaixells=(v3*3)+(v2*2)+v1;
- while(vaixells>=1){
- trets++; // Si toco vaixell tret--!!!
- pp: gotoxy(5,linea);
- textcolor(752);
- printf("Escolleix fila i columna a la que disparar: ");//1 al 10
- linea=linea+2;
- fflush(stdin);
- if(linea == 35) linea=25; // Evitar que no es vegui el tauler tota la estona
- scanf("%d %d",&t_c,&t_f);//vector1[0],vector1[1]
- t_c--;
- t_f--;
- gotoxy((t_f*2+18),(t_c+5));
- switch(matriz[t_c][t_f]){
- case 'X':
- textcolor(28);
- printf("X");
- vaixells--;
- trets--;
- break;
- case 'Y':
- textcolor(28);
- printf("Y");
- vaixells--;
- trets--;
- break;
- case 'Z':
- textcolor(28);
- printf("Z");
- vaixells--;
- trets--;
- break;
- default:
- textcolor(9);
- printf("%c",254);
- break;
- }
- gotoxy(1,36);
- printf("\tNumero de vaixells restants: %d\n",vaixells);
- printf("\tNumero de trets totals: %d\n",trets);
- }
- gotoxy(5,40);
- textcolor(225);
- if( trets<25)printf("Enhorabona, obtens la medalla d'or amb %d trets",trets);
- else if ( trets >= 25 && trets<=50)printf("Enhorabona, obtens la medalla de plata amb %d trets",trets);
- else if ( trets>51)printf("Enhorabona, obtens la medalla de bronce amb %d trets",trets);
- getchar();
- getchar();
- getchar();
- getchar();
- }
- /* FUNCIONS */
- int header(){
- int lletra='A';
- gotoxy(18,3);
- while(lletra<'K'){
- printf("%-2c",lletra);
- lletra++;
- }
- printf("\n\n");
- }
- int columna(){
- int x=0;
- x=i+1;
- printf("Fila num: %d \t",x);
- }
- int direccio(){
- d_rand=(rand()%2); //0-Vertical 1-Horitzontal
- return d_rand;
- }
- int margen(int x , int y){
- int r = 0;
- int i,j;
- for(i=(x-1);i<(x+2);i++){
- for(j=(y-1);j<(y+2);j++){
- if(matriz[i][j]!=0) r = 1;
- }
- }
- return r;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement