Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef BATNAV_H_INCLUDED
- #define BATNAV_H_INCLUDED
- void get_usr_coords(){
- //gotoxy(x,y);
- int x;
- int y;
- printf("Type the coordinates for the line to shot(only letters): ");
- x = getch();
- x = toupper(x);
- printf("%c",x);//imprime a entrada do user
- x -= 65;//converte de char para int
- while (x < 0 || x > 14) {//caso o usuário não digite um valor válido
- printf("\n\nWrong input, try again using valid letters: ");
- x = getch();
- x = toupper(x);
- x -= 65;
- printf("%c",x + 65);
- }
- printf("Type the coordinates for the column to shot (only numbers): ");
- y = getch();
- y -= 48;
- printf("%d",y);
- while(y < 0 || y > 14){
- scanf("%d",y);
- }
- }
- void ins_pa(int **matriz,int code){ //Inserir Porta aviôes
- //Formato: [][][][][]
- int r;
- int i;
- int j;
- r = 1;//rand() % 2; // Se 1 o navio ficará na posicionado verticalmente no tabuleiro, se 2 horizontalmente
- if (r){ // Vertical
- i = rand() % 10; // impede que passe da margem limite inferior
- j = rand() % 15;
- if(i - 1 >= 0){//outline
- matriz[i - 1 ][j] = 66;
- if(j - 1 >= 0)
- matriz[i - 1][j - 1] = 66;
- if(j + 1 < 15)
- matriz[i - 1] [j + 1] = 66;
- }
- if(i + 5 < 15){//outline
- matriz[i + 5][j] = 66;
- matriz[i + 5][j - 1] = 66;
- matriz[i + 5][j + 1] = 66;
- }
- for (r = 0; r < 5; r++,i++){
- matriz[i][j] = code;
- if(j - 1 > 0)//outline
- matriz[i][j-1] = 66;
- if(j + 1 < 15)//outline
- matriz[i][j+1] = 66;
- }
- } else{ // Horizontal
- i = rand() % 15;
- j = rand() % 10; // impede que passe da margem limite direita
- if(j - 1 >= 0){//outline
- matriz[i - 1][j] = 66;
- if(i - 1 >= 0)
- matriz[i - 1][j - 1] = 66;
- if(i + 1 < 15)
- matriz[i - 1] [j + 1] = 66;
- }
- if(i + 5 < 15){//outline
- matriz[i][j + 5] = 66;
- matriz[i - 1][j + 5] = 66;
- matriz[i + 1][j + 5] = 66;
- }
- for (r = 0; r < 5; r++,i++){
- matriz[i][j] = code;
- if(i - 1 >= 0)//outline
- matriz[i - 1][j] = 66;
- for (r = 0; r < 5; r++, j++){
- matriz[i][j] = code;
- }
- }
- }
- }
- void ins_enc(int **matriz, int code){//inserir Encouraçado
- //Formato [][][][]
- int r;
- int i;
- int j;
- int space_problem = 1;
- int z;
- while (space_problem == 1){//Enquanto não houver espaço suficiente naquela região ele gera outros valores para outras regiões até encontrar espaço suficiente
- z=0;
- r = rand() % 2;
- if (r){ // vertical
- i = rand()% 11;//impede que passe da margem limite inferior
- j = rand()% 15;
- for (r = 0; r < 4; r++){//Verifica se há espaço disponível na região
- if (matriz[i+r][j] != 0){
- } else {
- z++;//Conta quantos espaços há
- }
- }
- if (z == 4){//Verifica se os espaços disponíveis coincidem
- space_problem = 0;//remove do while
- for (z = 0; z < 4; z++,i++)
- matriz[i][j] = code;
- }
- } else { // horizontal
- i = rand()% 15;
- j = rand()% 11;//impede que passe da margem limite direita
- for (r = 0; r < 4; r++){//verificação de espaço livre
- if (matriz[i][j+r] == 0)
- z++;
- }
- if (z == 4){// caso haja espaço ele aloca o navio
- space_problem = 0;
- for (z = 0; z < 4; z++,j++)
- matriz[i][j] = code;
- }
- }
- }
- }
- void ins_hid(int **matriz,int code){//inserir Hidraviõess
- //Formato: []
- // [] []
- int space_problem = 1;
- int i;
- int j;
- int r;
- while (space_problem == 1){
- r = rand() % 2;
- if (r){//horizontal
- i = rand() % 12 + 1;
- j = rand() % 13;
- if (matriz[i][j] == 0 && matriz[i-1][j+1] == 0 && matriz[i][j+2] == 0){
- matriz[i][j] = code;
- matriz[i-1][j+1] = code;
- matriz[i][j+2] = code;
- if (matriz[i][j+1] == 0)//marca o centro do navio para que nenhum navio se posicione ali
- matriz[i][j+1] = 76;
- space_problem = 0;
- }
- } else{//vertical
- i = rand() % 12;
- j = rand() % 13;
- if (matriz[i][j] == 0 && matriz[i+1][j+1] == 0 && matriz[i+2][j] == 0){
- matriz[i][j] = code;
- matriz[i+1][j + 1] = code;
- matriz[i+2][j] = code;
- if (matriz[i+1][j] == 0)//marca o centro do navio para que nenhum navio se posicione ali
- matriz[i+1][j] = 76;
- space_problem = 0;
- }
- }
- }
- }
- void ins_cru(int **matriz,int code){//inserir Cruzadores
- //Formato [][]
- int r;
- int i;
- int j;
- int space_problem = 1;
- int z;
- while (space_problem == 1){
- z=0;
- r = rand() % 2;
- if (r){ // vertical
- i = rand()% 12;//impede que passe da margem limite inferior
- j = rand()% 14;
- for (r = 0; r < 2; r++){
- if (matriz[i+r][j] != 0){
- } else {
- z++;
- }
- }
- if (z == 4){
- space_problem = 0;
- for (z = 0; z < 2; z++,i++)
- matriz[i][j] = code;
- }
- } else { // horizontal
- i = rand()% 14;
- j = rand()% 12;//impede que passe da margem limite direita
- for (r = 0; r < 2; r++){//verificação de espaço livre
- if (matriz[i][j+r] == 0)
- z++;
- }
- if (z == 2){// caso haja espaço ele aloca o navio
- space_problem = 0;
- for (z = 0; z < 2; z++,j++)
- matriz[i][j] = code;
- }
- }
- }
- }
- void ins_sub(int **matriz, int code){//inserir Submarinos
- //Formato []
- int i = rand() % 15;
- int j = rand() % 15;
- while (matriz[i][j] !=0){
- i = rand() % 15;
- j = rand() % 15;
- }
- matriz[i][j] = code;
- }
- void show_board (int **matriz){
- int i;
- int j;
- int line;
- printf("\n\n\n\t\t---- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ----");
- for (i = 0, line = 65; i < 15; i++,line++){
- printf("\n\t\t%c - ",line);
- for (j = 0; j < 15; j++){
- printf("%d\t",matriz[i][j]);
- }
- printf("\n");
- }
- printf("\n\t\t---- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ----\n\n\n");
- }
- void fill_board(int **matriz){//insere todoso o navios no tabuleiro
- //Um Porta Aviões
- ins_pa(matriz,5);
- //Dois Encouraçados
- ins_enc(matriz,41);
- ins_enc(matriz,42);
- // Cinco Hidroaviões // função com alguns bugs desconhecidos
- ins_hid(matriz,31);
- ins_hid(matriz,32);
- ins_hid(matriz,33);
- ins_hid(matriz,34);
- ins_hid(matriz,35);
- //Três Cruzadores
- ins_cru(matriz,21);
- ins_cru(matriz,22);
- ins_cru(matriz,23);
- //Quatro Submarinos
- ins_sub(matriz,11);
- ins_sub(matriz,12);
- ins_sub(matriz,13);
- ins_sub(matriz,14);
- }
- #endif // BATNAV_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment