服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - 通过 ncurses 在 Linux 上写一个猜谜游戏

通过 ncurses 在 Linux 上写一个猜谜游戏

2021-09-08 22:01Linux中国Jim Hall Linux

使用 ncurses 的灵活性和强大功能在 Linux 上创建一个猜数字游戏。

通过 ncurses 在 Linux 上写一个猜谜游戏

使用 ncurses 的灵活性和强大功能在 Linux 上创建一个猜数字游戏。

在我的 上一篇文章,我简要介绍了使用 ncurses 库通过 C 语言编写文本模式交互式应用程序。使用 ncurses,我们可以控制文本在终端上的显示位置和方式。如果你通过阅读手册页探索 ncurses 库函数,你会发现显示文本有很多不同的方式,包括粗体文本、颜色、闪烁文本、窗口、边框、图形字符和其它功能,这些都可以使你的应用脱颖而出。

如果你想探索一个更高级的程序来演示其中一些有趣的功能,有一个简单的“猜数字”游戏,我已更新为使用 ncurses 编写的了。该程序在一个范围内选择一个随机数,然后要求用户进行重复猜测,直到他们猜到这个秘密数字。当用户进行猜测时,程序会告知他们猜测的数字是太低还是太高。

请注意,程序限定可能的数字范围是 0 到 7。将值保持在有限的个位数数字范围内,可以更轻松的使用 getch() 函数从用户读取单个数字。我还使用了 getrandom 内核系统调用来生成随机数,设定数字最大值为 7,以从 0 (二进制 0000)到 7 (二进制 0111)中选择一个随机数。

  1. #include <curses.h>;
  2. #include <string.h>;          /* for strlen */
  3. #include <sys/random.h>;      /* for getrandom */
  4.  
  5. int
  6. random0_7()
  7. {
  8.    int num;
  9.    getrandom(&num, sizeof(int), GRND_NONBLOCK);
  10.    return (num & 7); /* from 0000 to 0111 */
  11. }
  12.  
  13. int
  14. read_guess()
  15. {
  16.   int ch;
  17.  
  18.   do {
  19.     ch = getch();
  20.   } while ((ch < '0') || (ch > '7'));
  21.  
  22.   return (ch - '0'); /* turn into a number */
  23. }

通过使用 ncurses,我们可以增加一些有趣的视觉体验。通过添加函数,我们可以在屏幕顶部显示重要的文本信息,在屏幕底部显示状态消息行:

  1. void
  2. print_header(const char *text)
  3. {
  4.   move(0, 0);
  5.   clrtoeol();
  6.  
  7.   attron(A_BOLD);
  8.   mvaddstr(0, (COLS / 2) - (strlen(text) / 2), text);
  9.   attroff(A_BOLD);
  10.   refresh();
  11. }
  12.  
  13. void
  14. print_status(const char *text)
  15. {
  16.   move(LINES - 1, 0);
  17.   clrtoeol();
  18.  
  19.   attron(A_REVERSE);
  20.   mvaddstr(LINES - 1, 0, text);
  21.   attroff(A_REVERSE);
  22.   refresh();
  23. }

通过这些函数,我们就可以构建猜数字游戏的主要部分。首先,程序为 ncurses 设置终端,然后从 0 到 7 中选择一个随机数。显示数字刻度后,程序启动一个循环,询问用户的猜测。

当用户进行猜测时,程序会在屏幕上提供反馈。如果猜测太低,程序会在屏幕上的数字下方打印一个左方括号。如果猜测太高,程序会在屏幕上的数字下方打印一个右方括号。这有助于用户缩小他们的选择范围,直到他们猜出正确的数字。

  1. int
  2. main()
  3. {
  4.   int number, guess;
  5.  
  6.   initscr();
  7.   cbreak();
  8.   noecho();
  9.  
  10.   number = random0_7();
  11.   mvprintw(1, COLS - 1, "%d", number); /* debugging */
  12.  
  13.   print_header("Guess the number 0-7");
  14.  
  15.   mvaddstr(9, (COLS / 2) - 7, "0 1 2 3 4 5 6 7");
  16.  
  17.   print_status("Make a guess...");
  18.  
  19.   do {
  20.     guess = read_guess();
  21.  
  22.     move(10, (COLS / 2) - 7 + (guess * 2));
  23.  
  24.     if (guess < number) {
  25.       addch('[');
  26.       print_status("Too low");
  27.     }
  28.  
  29.     else if (guess > number) {
  30.       addch(']');
  31.       print_status("Too high");
  32.     }
  33.  
  34.     else {
  35.       addch('^');
  36.     }
  37.   } while (guess != number);
  38.  
  39.   print_header("That's right!");
  40.   print_status("Press any key to quit");
  41.   getch();
  42.  
  43.   endwin();
  44.  
  45.   return 0;
  46. }

复制这个程序,自己尝试编译它。不要忘记你需要告诉 GCC 编译器链接到 ncurses 库:

  1. $ gcc -o guess guess.c -lncurses

我留下了一个调试行,所以你可以看到屏幕右上角附近的秘密数字:

通过 ncurses 在 Linux 上写一个猜谜游戏

图1:猜数字游戏。注意右上角的秘密数字。

开始使用 ncurses 

该程序使用了 ncurses 的许多其它函数,你可以从这些函数开始。例如,print_header 函数在屏幕顶部居中以粗体文本打印消息,print_status 函数在屏幕左下角以反向文本打印消息。使用它来帮助你开始使用 ncurses 编程。

原文链接:https://linux.cn/article-13756-1.html

延伸 · 阅读

精彩推荐