ncurses 链表未显示在屏幕上

ncurses linked list not showing in screen

我制作了一个链接列表并用 4 个“#”字符填充它,我想用 ncurses 库在屏幕上打印它们,因为我正在尝试制作蛇游戏但我不知道为什么它不打印,这是代码:

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


}






我知道我正在完善链表的链接过程,因为我测试了它在另一个文件中用 stdio.h 打印它并且它有效

你的程序有几个bug,我已经修复了。最严重的是你的代码不能用C++编译。如果打算在C++下构建,需要先修复编译错误

  1. newPiece = malloc(sizeof(struct snake));,这里需要转换一下:newPiece = (snake *)malloc(sizeof(struct snake));,在c++中一般不推荐使用malloc,使用[=15=更自然]

  2. first = addNewPiece("#"); 将字符串文字作为 char 参数传递,这里需要传递一个字符。

  3. mvwprintw(win, y, x, t);,你误解了API,应该固定为mvwprintw(win, y, x, "%c", t->piece);,你需要在使用库之前看到document API 查看它期望的参数类型

  4. 您打印屏幕后忘记刷新屏幕了!

  5. 你没有在 for 循环中递增索引,这是一个无限循环。

你的代码有点c风格,如果你想用C++写,需要重构,一些建议:

  • 使用std::vector来存储蛇体,那么我们就不需要手动管理内存了。并避免了容易出错的链表处理。那么你的大部分代码都可以简化。
  • 使用日志库和打印日志来帮助调试

固定代码应该可以工作,我的控制台上有一条垂直的蛇。

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


看到PO的评论后,我们知道这个问题被错误地标记为c++,c版本代码:

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