简体   繁体   English

如何使用指向指针的指针指向指向此链接列表的指针的项目

[英]How to use pointer to pointer pointers to pointers insert item in this linked list

Good Day, I am working on a filler game where: two players gain points by placing on a board, one after the other, the game piece obtained by the game master (in the form of an executable Ruby program). 美好的一天,我正在研究一款填充游戏,其中:两个玩家通过放置在板上来获得积分,一个又一个地是由游戏大师获得的游戏作品(以可执行的Ruby程序的形式)。 The game ends when the game piece cannot be placed anymore. 当无法再放置游戏块时,游戏结束。

Below is the code used to read my player number and starting piece: 以下是用于读取我的玩家编号和起始片段的代码:

***void init_player(t_player *player)***
{
    char    *line;

    get_next_line(0, &line);
    if ((!(ft_strncmp(line, "$$$ exec p", 10))))
    {
        if (line[10] == '1')
        {
            player->id = '1';
            player->my_shape = 'O';
            player->current_shape = 'o';
        }
        else
        {
            player->id = '2';
            player->my_shape = 'X';
            player->current_shape = 'x';
        }
        ft_strdel(&line);
        return ;
    }
    return ;
}
int     main(void)
{
    t_player    *me;
    me = (t_player *)malloc(sizeof(*me));

    init_player(me);
    ft_putchar(me->my_shape);
    ft_putchar('\n');
    return (0);
}

Now I need help in reading the map size by creating a pointer to pointer of size n + 1 and n being 15 in this please see map below. 现在,我需要通过创建指向大小为n + 1且n为15的指针的指针来读取地图大小,请参见下面的地图。 Or I can try another approach you guys can advise. 或者,我可以尝试其他可以建议的方法。 Thank you Check Map below 谢谢检查下面的地图

$$$ exec p1 : [players/abanlin.filler]
***Plateau 15 17:***
    01234567890123456
000 .................
001 .................
002 .................
003 .................
004 .................
005 .................
006 .................
007 .................
008 ..O..............
009 .................
010 .................
011 .................
012 ..............X..
013 .................
014 .................
Piece 1 2:
**

Guess you want to access items in a 2D array. 猜猜您要访问2D数组中的项目。

In C, there is nothing different between 2D and 1D arrays, they are both memory sequence, just with few difference in the way you look at them. 在C语言中,2D和1D数组之间没有什么不同,它们都是内存序列,只是看待它们的方式几乎没有什么不同。

eg 例如

#include <stdlib.h>
#include <assert.h>

typedef struct {
  // Here mem can be used like a width * height 2D array
  // You can change 'char' here to whatever type you want
  char *mem;
  size_t width;
  size_t height;
} MyMap;

MyMap *create_map(size_t width, size_t height) {
  assert(width > 0 && height > 0);
  // As the size of 'char' is 1 byte, mem should take
  // (width * height * 1) bytes from memory. If you changed
  // type of mem, the size should be 'width * height * sizeof(NewType)'
  MyMap *self = (MyMap *)malloc(sizeof(MyMap) + width * height);
  assert(self);
  self->mem = (char *)(self + 1);
  self->width = width;
  self->height = height;
  return self;
}

void delete_map(MyMap **self){
  if (self && *self) {
    free(*self);
    *self = NULL;
  }
}

/**
 * @param x must be between 0 and width
 * @param y must be between 0 and height
 */
void set_map(MyMap *self, size_t x, size_t y, char value) {
  assert(self);
  assert(x < self->width && y < self->height);
  size_t index = self->width * y + x;
  self->mem[index] = value;
}

int main () {
  const size_t height = 15;
  const size_t width = height + 1;
  MyMap *map = create_map(width, height);
  set_map(map, 0, 0, 'X');
  delete_map(&map);
  return 0;
}

You can read map like below, 您可以阅读以下地图,

#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 5

int main() {
    int i,j;
    char **board;
    if((board = (char **)calloc(sizeof(char *) , ROW)) == NULL){
        printf("calloc failed at memory allocation for dptr\n");
    }
    printf("Enter board of size %d x %d", ROW, COL);
    for(i = 0; i < ROW; i++){
        if((board[i] = (char *)calloc(sizeof(char) , COL)) == NULL){
            printf("calloc failed at memory allocation for ptr %d\n", i);
        }
        for(j = 0; j < COL; j++){
            if(scanf("%c",&board[i][j]) < 1){
                printf("scanf failed!");
            }
        }
    }
//printing board
    for(i = 0; i < ROW; i++){
        for(j = 0; j < COL; j++){
            printf("%c ",board[i][j]);
        }
        printf("\n");
    }
    for(i = 0; i < ROW; i++){
        free(board[i]);
    }
    free(board);
    return 0;
}

Note: You can take the size of the board form user too, instead defining ROW and COL. 注意:您也可以采用电路板表单用户的大小,而不是定义ROW和COL。

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

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