BlackORTS
BlackORTS

Reputation: 23

ncurses linked list not showing in screen

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

Upvotes: 0

Views: 136

Answers (1)

prehistoricpenguin
prehistoricpenguin

Reputation: 6326

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--;
    }
  }
}


Upvotes: 1

Related Questions