0

i made a linked list and filled it with 4 "#" characters, i wanted to print them in screen with the ncurses library because i'm trying to make a snake game but idk why it doesn't print, here's the code:

#include<ncurses.h>
#include<stdlib.h>

struct snake{

    char piece;
    struct snake *nextPiece;

};

struct snake *addNewPiece(const char piece){

    struct snake *newPiece = NULL;
    newPiece = malloc(sizeof(struct snake));

    if(newPiece != NULL){
        newPiece->nextPiece = NULL;
        newPiece->piece = piece;
    }else{

        endwin();
        printf("mem aloc failure, the program couldn't alloc the memory correctly");
    }
    return newPiece;

}

int main(){

    int i = 0;

    struct snake *first = NULL;
    struct snake *added = NULL;

    initscr();
    noecho();

    int yMaxStd,xMaxStd;

    getmaxyx(stdscr,yMaxStd,xMaxStd);

    WINDOW* win = newwin(20,50,(yMaxStd/2)-10,10);
    box(win,0,0);
    refresh();
    wrefresh(win);
    leaveok(win, true);

    int yMaxWin,xMaxWin;

    getmaxyx(win,yMaxWin,xMaxWin);
    wmove(win,yMaxWin/2,yMaxWin/2);

    //llenar lista con 4 #
    while(i<=4){
        if(first == NULL){
            first = addNewPiece("#");
            if(first != NULL){
                added = first;
            }
        }else{
            added->nextPiece = addNewPiece("#");
            if(added->nextPiece != NULL){
                added = added->nextPiece;
            }
        }
    }

    if(first == NULL){
        endwin();
        printf("Men alloc failure.");
    }

    printinscreen(first,win);


    getch();
    endwin();
    return 0;

}

void printinscreen(struct snake *piece,WINDOW* win){ 

    struct snake *t;

    t = piece;

    int y = 5,x = 5;

    if(t == NULL){
        endwin();
    }else{
        while(t != NULL){
            mvwprintw(win,y,x,t);
            t = t->nextPiece;
            y--;
        }
    }


}






i know that im making good the linking process of the linked list becuase i tested it printing it with stdio.h in another file and it worked

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
BlackORTS
  • 23
  • 5
  • 1
    Do basic debugging. Use a debugger. Have a look at what happens once it gets to the `while(i<=4)` loop. Does it ever exit that loop? – kaylum May 27 '21 at 01:01
  • The suggested answer points out several problems, but OP's question about a blank screen is a duplicate [curses library: why does getch() clear my screen?](https://stackoverflow.com/questions/19748685/curses-library-why-does-getch-clear-my-screen) – Thomas Dickey May 27 '21 at 08:15
  • That is not C++ code, please don't blindly apply tags without reading their description! – Ulrich Eckhardt May 29 '21 at 07:52

1 Answers1

1

Your programs have several bugs, I have fixed them. The most serious is that your code cannot be compiled in C++. If it's intended to build it under C++, you need to fix the compile errors at first.

  1. newPiece = malloc(sizeof(struct snake));, need a convertion here: newPiece = (snake *)malloc(sizeof(struct snake));, generally it's not recomended to use malloc in c++, it's more naturally to use new

  2. first = addNewPiece("#"); Pass string literal as char argument, need to pass a character here.

  3. mvwprintw(win, y, x, t);, you have misunderstand the API, should be fixed as mvwprintw(win, y, x, "%c", t->piece);, you need to see the document before use a library API to see what types of arguments it expects

  4. You forget to refresh the screen after printing the screen!

  5. You haven't increment the index in for loop, it's an infinite loop.

Your code is somewhat c style, if you are trying to write with C++, it needs to be refactored, some suggestions:

  • use std::vector to store the snake body, then we don't need to manage the memory by hand. And avoid the error-prone linked list processing. Then most part of your code can be simplified.
  • Use a logging library and print logs to help to debug

The fixed code should work, I get a vertical snake on my console.

#include <ncurses.h>
#include <stdlib.h>

struct snake {
  char piece;
  struct snake *nextPiece;
};

struct snake *addNewPiece(const char piece) {
  struct snake *newPiece = NULL;
  newPiece = (snake *)malloc(sizeof(struct snake));

  if (newPiece != NULL) {
    newPiece->nextPiece = NULL;
    newPiece->piece = piece;
  } else {
    endwin();
    printf("mem aloc failure, the program couldn't alloc the memory correctly");
  }
  return newPiece;
}

void printinscreen(struct snake *piece, WINDOW *win);
int main(int argc, char *argv[]) {
  int i = 0;

  struct snake *first = NULL;
  struct snake *added = NULL;

  initscr();
  noecho();

  int yMaxStd, xMaxStd;

  getmaxyx(stdscr, yMaxStd, xMaxStd);

  WINDOW *win = newwin(20, 50, (yMaxStd / 2) - 10, 10);
  box(win, 0, 0);
  refresh();
  wrefresh(win);
  leaveok(win, true);

  int yMaxWin, xMaxWin;

  getmaxyx(win, yMaxWin, xMaxWin);
  wmove(win, yMaxWin / 2, yMaxWin / 2);

  // llenar lista con 4 #
  while (i <= 4) {
    if (first == NULL) {
      first = addNewPiece('#');
      if (first != NULL) {
        added = first;
      }
    } else {
      added->nextPiece = addNewPiece('#');
      if (added->nextPiece != NULL) {
        added = added->nextPiece;
      }
    }
    ++i;
  }

  if (first == NULL) {
    endwin();
    printf("Men alloc failure.");
  }

  printinscreen(first, win);
  refresh();
  wrefresh(win);

  getch();
  endwin();
  return 0;
}

void printinscreen(struct snake *piece, WINDOW *win) {
  struct snake *t;

  t = piece;

  int y = 5, x = 5;

  if (t == NULL) {
    endwin();
  } else {
    while (t != NULL) {
      mvwprintw(win, y, x, "%c", t->piece);
      t = t->nextPiece;
      y--;
    }
  }
}


After seeing the PO's comments, we know that the question have been falsely tagged with c++, the c version code:

#include <ncurses.h>
#include <stdlib.h>

struct snake {
  char piece;
  struct snake *nextPiece;
};

struct snake *addNewPiece(const char piece) {
  struct snake *newPiece = NULL;
  newPiece = malloc(sizeof(struct snake));

  if (newPiece != NULL) {
    newPiece->nextPiece = NULL;
    newPiece->piece = piece;
  } else {
    endwin();
    printf("mem aloc failure, the program couldn't alloc the memory correctly");
  }
  return newPiece;
}

void printinscreen(struct snake *piece, WINDOW *win);
int main(int argc, char *argv[]) {
  int i = 0;

  struct snake *first = NULL;
  struct snake *added = NULL;

  initscr();
  noecho();

  int yMaxStd, xMaxStd;

  getmaxyx(stdscr, yMaxStd, xMaxStd);

  WINDOW *win = newwin(20, 50, (yMaxStd / 2) - 10, 10);
  box(win, 0, 0);
  refresh();
  wrefresh(win);
  leaveok(win, true);

  int yMaxWin, xMaxWin;

  getmaxyx(win, yMaxWin, xMaxWin);
  wmove(win, yMaxWin / 2, yMaxWin / 2);

  // llenar lista con 4 #
  while (i <= 4) {
    if (first == NULL) {
      first = addNewPiece('#');
      if (first != NULL) {
        added = first;
      }
    } else {
      added->nextPiece = addNewPiece('#');
      if (added->nextPiece != NULL) {
        added = added->nextPiece;
      }
    }
    ++i;
  }

  if (first == NULL) {
    endwin();
    printf("Men alloc failure.");
  }

  printinscreen(first, win);
  refresh();
  wrefresh(win);

  getch();
  endwin();
  return 0;
}

void printinscreen(struct snake *piece, WINDOW *win) {
  struct snake *t;

  t = piece;

  int y = 5, x = 5;

  if (t == NULL) {
    endwin();
  } else {
    while (t != NULL) {
      mvwprintw(win, y, x, "%c", t->piece);
      t = t->nextPiece;
      y--;
    }
  }
}


prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
  • it seems like my reply didn't get posted so i'll reply again. Thanks for helping me, i didn't notice that i didnt refresh hehe, the think with malloc is that i couldn't use a snake * refence in a snake type struct in C, btw i was doing the coding in C and not C++ thats why, im sorry for not saying it. Thanks for helping me, now i can finally make the snake game :) – BlackORTS May 29 '21 at 05:06