无法在游戏板上打印令牌

时间:2016-08-23 04:26:51

标签: c pointers

我正在写一个简单的棋盘游戏。程序运行时我想要做的是首先打印空板,然后询问用户XY坐标,然后在他输入的坐标中用玩家令牌打印板。当我运行该程序时,它打印空板然后询问我X坐标,然后是Y坐标,然后它给出了以下错误:

  

X:1,Y:2

     

Y:2,Y:3

     

分段错误(核心转储)

是什么造成了这个错误,我该如何解决?谢谢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>

struct p1Data{
    int xCoordinate;
    int yCoordinate;
    bool flag;
};

char **gameBoard;
int height, width, boardArea, xCor, yCor, dataSize, i;

char **allocateMemory(int boardHeight, int boardWidth);
void fillBoard(char **board, int height, int width, struct p1Data data[], int sizeData);
void printBoard(char **board, int boardHeight, int boardWidth);
void freeBoardArray(char **board, int boardHeight);

int main(int argc, char** argv ){
    height = 4;
    width = 4;

    boardArea = height * width;

    struct p1Data data[boardArea];

    dataSize = sizeof(data)/sizeof(data[0]);

    data[0].flag = false;
    gameBoard = allocateMemory(height, width);
    fillBoard(gameBoard, height, width, data, dataSize);
    printBoard(gameBoard, height, width);
    printf("\n");

    for(i = 0; i < boardArea; i++ ){
        printf("Enter X-Coordinate: ");
        scanf("%d", &xCor);
        printf("Enter Y-Coordinate: ");
        scanf("%d", &yCor);

        data[i].flag = true;
        data[i].xCoordinate = xCor;
        data[i].yCoordinate = yCor;

        fillBoard(gameBoard, height, width, data, dataSize);
        printBoard(gameBoard, height, width);
        printf("\n");
    }

    return 0;
}

char **allocateMemory(int boardHeight, int boardWidth){
    int i;
    char **gameBoard;

    gameBoard = (char **)malloc(sizeof(char *)*boardHeight);

    for( i = 0; i < boardWidth; i++ ){
        gameBoard[i] = (char *)malloc(sizeof(char)*boardWidth);
    }

    return gameBoard;
}

void fillBoard(char** board, int height, int width, struct p1Data data[], int sizeData){
    int i, j, x, y;

    for(i = 0; i < height; i++){
        for(j = 0; j < width; j++ ){
            board[i][j] = '.';
        }
    }

    if(data[0].flag == true ){
        for( i = 0; i < sizeData; i++ ){
            x = data[i].xCoordinate;
            y = data[i].yCoordinate;

            board[x][y] = 'O';
            printf("X: %d, Y: %d\n", x, y);
        }
    }
}
void printBoard(char **board, int boardHeight, int boardWidth){
    int i, j;

    printf("/");

    for( i = 0; i < boardWidth; i++ ){
        printf("-");
    }

    printf("\\");

    printf("\n");

    for(i = 0; i < boardHeight; i++ ){
            printf("|");
        for(j = 0; j < boardWidth; j++ ){
            printf("%c", board[i][j]);
        }

        printf("|");
        printf("\n");
    }

    printf("\\");

    for( i = 0; i < boardWidth; i++ ){
        printf("-");
    }

    printf("/");
}

void freeBoardArray(char **board, int boardHeight){
    int i;
    for( i = 0; i < boardHeight; i++ )
        free(board[i]);
    free(board);
}

1 个答案:

答案 0 :(得分:4)

if (data[0].flag == true) {
    for (i = 0; i < sizeData; i++) {
        x = data[i].xCoordinate; //<== not intialized
        y = data[i].yCoordinate; //<== not intialized

        board[x][y] = 'O'; //<= x/y are undefined
        printf("X: %d, Y: %d\n", x, y);
    }
}

您必须初始化data[i].xCoordinatedata[i].yCoordinate,否则这些值是未定义的。稍后您调用board[x][y]会导致错误。

//initialize here:
for (i = 0; i < boardArea; i++) 
{
    data[i].flag = false;
    data[i].xCoordinate = 0;
    data[i].yCoordinate = 0;
}
fillBoard(gameBoard, height, width, data, dataSize);
...

在这种情况下,您还可以使用memset(&data, 0, sizeof(data));初始化将初始值设置为零。

注意,您还可以通过避免使用二维阵列来简化游戏。知道X / Y坐标和宽度/高度,您可以将索引计算为x + y * width。或者使用除法和运算符%从1-D数组索引中提取X / Y坐标。例如:

void printBoard(char *data, int w, int h)
{
    int x, y;
    for (y = 0; y < h; y++)
    {
        for (x = 0; x < w; x++)
            printf("%c", data[x + y * w] ? 'O' : '.');
        printf("\n");
    }
    printf("\n");
}

int main(int argc, char** argv)
{
    int width = 4;
    int height = 4;
    int area = width * height;
    int i, xCor, yCor;
    char data[area];
    memset(data, 0, area);
    for (i = 0; i < area; i++)
    {
        printBoard(data, width, height);
        printf("Enter X-Coordinate: ");
        scanf("%d", &xCor);
        printf("Enter Y-Coordinate: ");
        scanf("%d", &yCor);

        int index = xCor + yCor * width;
        if (index < area)
            data[index] = 1;
    }
    return 0;
}