c - Ncurses:比较输入和文本文件中选定的单词

标签 c ncurses

我想做“下落的单词”打字游戏(在单词落在控制台底部之前输入单词),但我陷入困境,因为我不知道我的声明是否有效。

现在这个功能:

  • 打开包含单词列表的文本文件

  • 从文件中选择随机单词(我没有这样做,是在网上找到的)。

  • 打印下落动画(列数也是随机的)

问题是:我不知道如何将变量“输入”与当前下降的单词进行比较。到目前为止,我对 scanw 函数的尝试都失败了,我不确定 fgets 是否会更好。

编辑:我尝试使用 strcmp 和 if 函数,但它并没有真正帮助 - 我的意愿是让这个词落下,同时我可以写这个词。如果两个字符串相同,它应该打印“SAME”,但它不起作用,我不知道为什么......

#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
#include <curses.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

int col = 0;
int row = 0;
extern WINDOW *stdscr;

int ruch()
{
FILE *fp = NULL;
char words[67];
char input[67];
int i = 0 , ran = 0;

while(1)
{
int ch = 0;
int each = 0;

int x = rand()%70;

//opening file
if ( ( fp = fopen("list.txt" , "r")) == NULL) {
    perror ( "could not open file");
    exit ( 1);
}

for( i = 0; fgets(words , sizeof(words) , fp) ; i++);//loop to count words in file
ran = rand() % i;//random word
rewind(fp);
for(i = 0 ; i < ran ; i++)//loop to get random word
    fgets(words , sizeof(words) , fp);
fclose(fp);
if ( words[strlen(words)-1] == '\n')
    words[strlen(words)-1] = '\0';// remove newline

//animation
for( i=0;i<22;i++)
{
    getmaxyx(stdscr, row, col);
    clear();
    mvprintw(1+i ,x , words );
    refresh();
    usleep(500000);
    ch = getch();//get a character
    if ( ch != ERR) {
        input[each] = ch;//add character to input
        each++;
        input[each] = '\0';//terminate input
        mvprintw(1, 1, input);
        if(strcmp(input,words) == 0) {
            //mvprintw(5, 1, "SAME\n");
            //return 1;
            break;

        }

        else {
            if ( strlen ( input) >= strlen ( words)) {//correct number of characters input but do not match
                clear();
                mvprintw(6, 1, "YOU TYPO'D, GAME OVER");
                return 2;
            }
        }
        continue;
    }
    //usleep(100);
    continue;
    mvprintw(6, 1, "GAME OVER");

}
continue;
 }

}

int main()
{
initscr();
srand(time(NULL));
nodelay ( stdscr, TRUE);//so getch does not wait for a character
raw();
ruch();
getch();
usleep(3000000);
endwin();
return 0;
}

请记住,我是 C 语言的初学者:)很想获得一些帮助。

问候。

最佳答案

#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
#include <curses.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

int col = 0;
int row = 0;
extern WINDOW *stdscr;

int ruch()
{
    FILE *fp = NULL;
    char words[67];
    char input[67];
    int i = 0 , ran = 0;
    int ch = 0;
    int each = 0;
    srand(time(NULL));
    int x = rand()%70;

    //opening file
    if ( ( fp = fopen("list.txt" , "r")) == NULL) {
        perror ( "could not open file");
        exit ( 1);
    }

    //choosing random word from the file
    for(; fgets(words , sizeof(words) , fp) ; i++);//loop to count words in file
    ran = rand() % i;//random word
    rewind(fp);
    for(i = 0 ; i < ran ; i++)//loop to get random word
        fgets(words , sizeof(words) , fp);
    fclose(fp);
    if ( words[strlen(words)-1] == '\n')
        words[strlen(words)-1] = '\0';// remove newline

    //animation
    for( i=0;i<22;i++)
    {
        getmaxyx(stdscr, row, col);
        clear();
        mvprintw(1+i ,x , words );
        refresh();
        usleep(500000);
        ch = getch();//get a character
        if ( ch != ERR) {
            input[each] = ch;//add character to input
            each++;
            input[each] = '\0';//terminate input
            mvprintw(1, 1, input);
            if(strcmp(input,words) == 0) {
                mvprintw(5, 1, "SAME\n");
                return 1;
            }
            else {
                if ( strlen ( input) >= strlen ( words)) {//correct number of characters input but do not match
                    mvprintw(6, 1, "NOT SAME");
                    return 2;
                }
            }
        }
        usleep(100);
    }

    return 0;
}

int main()
{
    initscr();
    nodelay ( stdscr, TRUE);//so getch does not wait for a character
    raw();
    ruch();
    getch();
    usleep(3000000);
    endwin();
    return 0;
}

这在 while 循环之前打开了文件。输入斜线“/”结束循环。

#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
#include <curses.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

int col = 0;
int row = 0;
extern WINDOW *stdscr;

int ruch()
{
    FILE *fp = NULL;
    char words[67];
    char input[67];
    char score[80] = {"Score\n"};
    int i = 0 , ran = 0;
    int ch = 0;
    int each = 0;
    int points = 0;
    int filewords = 0;
    int x = 0;

    //opening file
    if ( ( fp = fopen("list.txt" , "r")) == NULL) {
        perror ( "could not open file");
        exit ( 1);
    }
    for( filewords = 0; fgets(words , sizeof(words) , fp) ; filewords++);//loop to count words in file

    while ( 1) {
        //choosing random word from the file
        ran = rand() % filewords;//random word
        rewind(fp);
        for(i = 0 ; i < ran ; i++)//loop to get random word
            fgets(words , sizeof(words) , fp);
        if ( words[strlen(words)-1] == '\n')
            words[strlen(words)-1] = '\0';// remove newline

        x = rand()%(col - strlen(words));

        clear();
        mvprintw(0, (col - strlen ( score))/2, score);
        mvprintw(1, (col - strlen ( score))/2, "To end, type a '/'.  Get ready...");
        refresh();
        usleep(2000000);

        //animation
        for( i=0;i<row-1;i++)
        {
            clear();
            mvprintw(0, (col - strlen ( score))/2, score);
            mvprintw(1+i ,x , words );
            refresh();
            usleep(400000);
            ch = getch();//get a character
            if ( ch != ERR) {
                if ( ch == '/') {
                    fclose(fp);//close the file
                    return 0;
                }
                input[each] = ch;//add character to input
                each++;
                input[each] = '\0';//terminate input
                mvprintw(1, 1, input);
                if(strcmp(input,words) == 0) {
                    points += strlen ( words) + ( row - i);
                    sprintf ( score, "Score %d correct\n", points);
                    each = 0;
                    break;
                }
                else {
                    if ( strlen ( input) >= strlen ( words)) {//correct number of characters input but do not match
                        points -= (int)(strlen ( words));
                        sprintf ( score, "Score %d typo %s %s\n", points, words, input);
                        each = 0;
                        break;
                    }
                }
            }
        }
        if ( i == row - 1) {
            points -= row;
            sprintf ( score, "Score %d timeout\n", points);
        }
        each = 0;
        getch();
    }
}

int main()
{
    srand(time(NULL));
    initscr();
    nodelay ( stdscr, TRUE);//so getch does not wait for a character
    raw();
    getmaxyx(stdscr, row, col);
    ruch();
    getch();
    usleep(30000);
    endwin();
    return 0;
}

关于c - Ncurses:比较输入和文本文件中选定的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606829/

相关文章:

无法创建命名管道

c - 使用自身地址初始化对象

python - 如何开发一个基于 curses 的 UI?

ruby - 在浏览器中运行的 JRuby 的 ncurses 模拟?

CentOS - 解析 XML 文件时出错

c - 以 IEEE 754 交换格式记录/读取 C double

c - 强制宏在 2 个标记之间放置一个空格

c - GNU/Linux 替代 Turbo C 函数 `clrscr` 和 `cprintf`

c++ - ncurses可用于Windows吗?

c++ - 如何在 ncurses 中启用 32k 颜色对?