用循环填充二维数组

时间:2019-01-27 21:02:25

标签: c

所以我有一个任务,需要创建一个虚拟的“木板”,并用“碎片”填充它,然后移动碎片等。但是,我无法弄清楚如何用空碎片填充数组。我才刚刚开始学习c。这是我为董事会准备的代码:

struct game_board
{
int rows, cols;
game_piece ** board;

 };

这是初始化电路板的代码:

void game_board_init(struct game_board* game_board, int rows, int cols)
{



// creates the 2d array
game_board->board = malloc(sizeof(game_board)*cols);
for (int i = 0; i < cols; i++){
    game_board->board[i] = malloc(sizeof(game_board)*rows);
}


// for loop that fills the array with empty pieces.
for(int i = 0; i < cols; i++){


}

}

如果有帮助的话,这是我用于游戏和初始化的代码

 typedef struct game_piece{
char * label;

}game_piece;


void game_piece_init(struct game_piece* piece, char* new_label)
{
piece->label = new_label;
}

2 个答案:

答案 0 :(得分:0)

// for loop that fills the array with empty pieces.
for(int i = 0; i < cols; i++){
        for(int j=0;j<rows;j++){
                game_piece_init(&game_board->board[i][j],"lol");
               //to test
               printf("%s\n",game_board->board[i][j].label);
        }

}

答案 1 :(得分:0)

这会为游戏板中的一种结构分配内存。
然后将指针的行数分配给gameboard-> board。
然后将cols数量的结构分配给指向gameboard-> board [rows]的每一行。
最后,将字符分配给gameboard-> board [rows [cols] .label,以存储new_label和零终止符。

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

typedef struct game_piece{
    char * label;
}game_piece;

struct game_board
{
    int rows, cols;
    game_piece ** board;
};

int game_piece_init(struct game_piece* piece, char* new_label)
{
    if ( ( piece->label = malloc ( strlen ( new_label) + 1))) {//allocate memory for new_lable
        strcpy ( piece->label, new_label);
    }
    else {
        fprintf ( stderr, "malloc problem new_label\n");
        return 0;
    }
    return 1;
}

struct game_board *game_board_init( int rows, int cols)
{
    struct game_board *gameboard = NULL;
    // creates the 2d array
    if ( ! ( gameboard = calloc( 1, sizeof(*gameboard)))) {
        fprintf ( stderr, "malloc problem gameboard\n");
        return NULL;
    }
    gameboard->rows = rows;//store rows and cols in struct
    gameboard->cols = cols;
    if ( ! ( gameboard->board = calloc( rows, sizeof(game_piece*)))) {//allocate rows number of pointers
        fprintf ( stderr, "malloc problem gameboard->board\n");
        return gameboard;
    }
    for (int i = 0; i < rows; i++){
        if ( ! ( gameboard->board[i] = calloc( cols, sizeof(game_piece)))) {//allocate cols number of structures
            fprintf ( stderr, "malloc problem gameboard->board[i]\n");
            return gameboard;
        }
    }

    // for loop that fills the array with empty pieces.
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
            if ( ! ( game_piece_init ( &gameboard->board[i][j], "text"))) {
                return gameboard;
            }
        }
    }
    return gameboard;
}

struct game_board *free_game_board( struct game_board *board) {
    for(int i = 0; i < board->rows; i++){
        for(int j = 0; j < board->cols; j++){
            free ( board->board[i][j].label);
        }
        free ( board->board[i]);
    }
    free ( board->board);
    free ( board);
    return NULL;
}

int main ( void) {
    struct game_board *game = NULL;

    if ( ! ( game = game_board_init ( 5, 8))) {
        return 0;
    }
    for(int i = 0; i < game->rows; i++){
        if ( game->board && game->board[i]) {//game->board and game->board[i] not NULL
            for(int j = 0; j < game->cols; j++){
                if ( game->board[i][j].label) {//game->board[i][j].label not NULL
                    printf ( "game->board[%d][%d] %s\n", i , j, game->board[i][j].label);
                }
            }
        }
    }
    game = free_game_board( game);
    return 0;
}