简体   繁体   中英

Battleship in C, arrays and structs

So i'm trying to build a simple replica of a battleship game in C, i create a struct Navio to represent the ships.

struct Navio{
    int *cords;
    int tam;
    int hits;
    int afundado;
};

the cords variable is an array with size of tam * 2, that have the cordinates of my ship. like this: [0,0,1,1,1,2] i have a ship of size 3 and cords of size 6.

The player has to place each cords on it's own. but when i'm trying to put more than 1 ship i get an segmentation fault . The code that i have to place each cord is that: (the code works 100% with one ship, i suspect that the problem is when i declare the cords array inside the placeBoats function, but i don't know how to make).

    void placeBoats(int board[nLin][nCol],struct Navio *navios, int nNavios){
    int navio, tamanho, i, l, lin, col, navAnterior;
    system("clear");

    for(navio = 0; navio < nNavios; navio++){ //Para cada navio
        //pega tamanho de cada navio
        while(1){
            printf("\nTamanho do Navio %d: ", navio+1);
            scanf("%d", &tamanho);
            if(tamanho >= 2 && tamanho <= 6) break;
            else{
                printf("O tamanho do navio tem que ser no mínimo 2 casas e no máximo 6\n");
            }
        }

        navios[navio].tam = tamanho;
        int cords[tamanho * 2]; //array que armazena cordenadas de cada navio
        resetArray(cords, tamanho*2);

        //CORDENADAS DE CADA NAVIO
        printf("\nNAVIO %d", navio+1);
        for(i = 0, l = 0; i < tamanho * 2; i += 2, l++){
            //TO DO - VALDIAR POSIÇÃO
            while(1){
                printf("\nCordenadas %d\n", l + 1);
                printf("Linha Cordenada %d (0 - 5): ", l + 1);
                scanf("%d", &lin);
                //cords[i]
                printf("Coluna Cordenada %d (0 - 9): ", l + 1);
                scanf("%d", &col);
                //cords[i+1]
                if(validPlace(lin,col) && !checkColosion(lin,col, board)){
                    cords[i] = lin;
                    cords[i + 1] = col;
                    break;
                }
                else{
                    printf("Essa posição não é válida. Tente outra\n");
                }
            }

            board[lin][col] = 1;
            printBoard(board);
        }
        //bota array no struct
        copyArrayToStruct(cords, (tamanho * 2), navios, navio);
    }
}

void copyArrayToStruct(int *origem, int tamOrigem, struct Navio *navios, int index){
    int i;
    // printf("NAVIO %d: \n", index + 1);
    for(i = 0; i < tamOrigem; i+=2){
        navios[index].cords[i] = origem[i];
        navios[index].cords[i + 1] = origem[i + 1];
        // printf("X = %d - Y = %d\n", origem[i], origem[i+1]);
    }
}

GDB exists for a reason. And your problem is in the end of iteration. You think you are limiting the cycle to the end of the array, but you are probably limiting it further, as you change "cords[i+1]" , which is a bad practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM