【问题标题】:Unable to print tokens in game board无法在游戏板中打印代币
【发布时间】:2016-08-23 04:26:51
【问题描述】:

我正在编写一个简单的棋盘游戏。我想在程序运行时首先打印空棋盘,然后向用户询问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);
}

【问题讨论】:

  • 编译时,始终启用所有警告,然后修复这些警告。发布的代码会导致编译器输出几个警告。 (对于gcc,至少使用:-Wall -Wextra -pedantic 我也使用:-Wconversion -std=gnu99)只是为了让您入门,不使用参数:argcargv。所以代码应该使用以下签名作为main:int main( void )

标签: c pointers


【解决方案1】:
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(&amp;data, 0, sizeof(data)); 进行初始化,将初始值设置为零。

请注意,您还可以通过避免使用二维数组来简化游戏。知道 X/Y 坐标和 Width/Height,您可以计算索引为x + y * width。或者使用除法和 mod 运算符% 从一维数组索引中提取 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    相关资源
    最近更新 更多