0

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). 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. 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:
**
KayLee
  • 1
  • 1
  • 1
    "How to use pointer to pointer pointers to pointers" Huh? Lets hope that you have no code like that! Luckily, I see no such thing in the posted code. – Lundin Aug 21 '18 at 12:28
  • 2
    It sounds like what you need is simply a 2D array of characters, or...? That is either `char board[x][y];` (variable length array) or `char (*board)[y] = malloc(sizeof (char[x][y]) );` (dynamically allocated). – Lundin Aug 21 '18 at 12:30
  • I certainly don't see anything resembling a linked list in the code you've presented, and it's not clear at this point why or where you would want to use one. – John Bollinger Aug 21 '18 at 12:37
  • Try don't use pointer to pointer to pointer to pointer ... to pointer. It's may be avoided for sure!!! – Sir Jo Black Aug 21 '18 at 12:45
  • Honestly I have no idea what you mean by 'pointer to pointer pointers to pointers insert item in this linked list'. And I find myself unable to correlate that with the code presended. I also can't guess what `ft_strncmp` is supposed to do. And I have no idea whether the second snippet is an input to your program of an output. If it's an input, I understand `ft_strncmp` skips the first line, but then there's no `1` to be compared by `if (line[10] == '1')`. OTOH if it's output, I can't know what your program does without knowing input data it processes. And so on and on... – CiaPan Aug 21 '18 at 13:09
  • To me it's unclear what you are asking. Is it so that you are **not** asking about problems with the posted code but instead asking how to implement the next part where the next part is to parse the line `***Plateau 15 17:*** ` to find the size of the map and then create a data structure for the map? – Support Ukraine Aug 21 '18 at 13:17
  • I meant pointer to pointer. Its a mistake guys – KayLee Aug 21 '18 at 13:22
  • @4386427its to parse the line ***Plateau 15 17:*** to find the size of the map and then create a data structure for the map? apologies for being unclear – KayLee Aug 21 '18 at 13:26
  • @KayLee Please ask your question in the question area, not in comments. There is a link 'edit' below your question, you can use it to expand, explain, fix or otherwise improve your question. Do not make readers browse through the discussion to get the point or they run away. – CiaPan Aug 21 '18 at 14:03
  • @CiaPan Your edit will be placed in a queue until it is peer reviewed. We welcome all constructive edits, but please make them substantial. Avoid trivial edits unless absolutely necessary. – KayLee Aug 21 '18 at 14:31

2 Answers2

0

Guess you want to access items in a 2D array.

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.

e.g.

#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;
}
Galaxy
  • 1,129
  • 11
  • 27
  • There's no need to use "mangled" 2D arrays since the year 1999. You can use pointer to VLA and achieve the same, though with far less ugly accessing. `array[i][j]` is much prettier than some `array[i*y + j]`. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Aug 21 '18 at 14:21
  • @Lundin Thanks for pointing out. I use single bracket here just to emphasize that an array in C is just a memory piece – Galaxy Aug 21 '18 at 14:34
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.

satyaGolladi
  • 180
  • 13
  • 1
    There is no reason to use this method here. It is slow and needlessly complex. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Aug 21 '18 at 14:23