简体   繁体   English

C,数组和结构中的战舰

[英]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. 所以我试图用C构建战舰游戏的简单副本,我创建了一个Navio结构来代表飞船。

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. cords变量是一个大小为tam * 2的数组,具有我的船的坐标。 like this: [0,0,1,1,1,2] i have a ship of size 3 and cords of size 6. 像这样: [0,0,1,1,1,2]我有一艘3号的船和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 . 但是当我试图放多于一艘船时,我遇到了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). 我必须放置每条电源线的代码是:(该代码可在一艘船上100%正常工作,我怀疑问题出在我在placeBoats函数内部声明cords数组时,但我不知道该怎么做)。

    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. GDB存在是有原因的。 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. 您认为您将循环限制为数组的末尾,但是随着更改“ cords [i + 1]”,您可能进一步限制了循环,这是一种不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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