【问题标题】:Random Number in curses C诅咒C中的随机数
【发布时间】:2018-12-06 05:50:05
【问题描述】:

为了学习和娱乐,我在业余时间做了三个月的 C 程序。我第一次尝试在 C 中创建一个 curses 文件,但老实说,我什至不知道 curses 和 ncurses 是什么,因为它们无论如何都是一样的......所以我想用猜测做一个简单的方框盒子里有一个正确的数字只是为了测试它。我已经成功创建了一个盒子,它包含在下面。我想制作我刚刚在框内添加的字符键,Q 键用于退出,C 键用于清除或重置getchar使用 while-do 循环来猜测正确或错误的数字,包括计算你的次数已经猜到了。顺便说一句:我在 Linux 系统中使用indent 格式化代码。

所以首先我刚刚学会了如何在屏幕显示中创建一个框,如下所示(包含代码):

      lqqqqqqqqqqqqqqqqqqk
      xthis is my box    x
      x                  x
      x                  x
      x                  x
      x                  x
      x                  x
      x                  x
      x                  x
      mqqqqqqqqqqqqqqqqqqj

“这是我的盒子”的代码:

#include <stdio.h>
#include <ncurses.h>

int
main (int argc, char **argv)
{

  initscr ();
  int height, width, start_y, start_x;
  height = 10;
  width = 20;
  start_y = start_x = 10;

  WINDOW *win = newwin (height, width, start_y, start_x);
  refresh ();

  box (win, 0, 0);
  mvwprintw (win, 1, 1, "this is my box");
  wrefresh (win);

  int c = getch ();

  endwin ();



  return 0;
}

我工作了将近三天的代码源:

#include <stdio.h>
#include <ncurses.h> /*This is similar as curses file*/
//using namespace std;

int
main (int argc, char **argv)
{

  initscr ();
  int height, width, start_y, start_x;
  int tries, num, guess;

  srand(time(0)); //random number generator
  num = rand() % 100 +1; //only 1 through 100


  height = 10;
  width = 20;
  start_y = start_x = 10;

  WINDOW *win = newwin (height, width, start_y, start_x);
  refresh ();


  box (win, 0, 0);
  mvwprintw (win, 1, 1, "Guess the correct number!\n");

  do {
  while((ch = getch()) != ERR) {
    switch(ch) {
    case 'Q': shutdown();
      case 'C': clear(); break;
   }

  printf("Enter your guess number (1-100): ");
  scanf("%d", &guess);
  tries++;

  if (guess > num){
    printf("Try Again Next Time! Press any key to exit!\n");
  } else if (guess < num){

   printf("Try again...\n");
  } else {

    printf("\nCORRECT! You got it right after %d guesses!", tries);
 }
}
  wrefresh (win);


  endwin ();



  return 0;
}

这是我对我的想法的期望(例如,我复制了输出并对其进行了编辑):

  lqqqqqqqqqqqqqqqqqqk
  xGuess the correct x
  xnumber!           x
  x                  x
  xEnter your guess  x
  xnumber (1-100):___x
  x                  x
  xTry again...      x
  x                  x
  mqqqqqqqqqqqqqqqqqqj

对于预期的输出,我仍然可以编辑尺寸,但现在使用当前尺寸。除非您想添加“自动调整大小”,否则会很甜蜜。感谢阅读和帮助。

【问题讨论】:

  • 不太清楚你在问什么。也许将您的问题编辑为更少的故事和更多的问题。不管你是编码新手,不知道 curses 和 ncurses 之间的区别,或者你如何缩进你的代码。
  • 你唯一要做的就是最后的条件在哪里。
  • @RetiredNinja,我正在尝试制作一个带有诅咒的游戏,我刚刚创建了在屏幕上绘制一个框并且代码工作正常,然后我有了一个想法,我想在里面添加一个游戏将盒子放入一个名为“猜对数字”的迷你游戏中,因此您只需在框内输入正确的数字,消息(“正确的数字或​​再试一次”)应该出现在框外,您猜对了多少次数字。我想解释一下,因为我的英语不好,顺便说一句。我只是想练习和学习更多的 C 程序代码。希望这是有道理的。

标签: c ncurses curses


【解决方案1】:

这是我的两分钱来帮助你,但请在这个解决方案中加入一粒盐,因为这个解决方案可能包含一些愚蠢的东西......

也许您可以选择零件,或者至少从这个零件中学习一些如何不做的事情。 示例是 0-10 以便于测试...通过更改 MAXVALUE 和 MINVALUE 定义/常量字符串以匹配它们来更改它们...

祝你好运,祝你有美好的一天。

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


// PREDEFINED VALUES FOR DEFINING NON CHANGING VALUES IN CODE THIS CASE
#define WINDOWHEIGHT 20
#define WINDOWWIDTH 60
#define WINDOWSTARTX 0
#define WINDOWSTARTY 0
#define CORRECT 1
#define INCORRECT 0
#define START 2
#define WRONGFORMAT 3
#define MAXVALUE 10
#define MINVALUE 0
// PREDEFINED VALUES FOR DEFINING NON CHANGING VALUES IN THIS CASE


// initialising global structure for saving amount of right and wrong guesses and number to compare with.
struct game {
        int rightGuesses;
        int wrongGuesses;
        int rightNumber;
} myGame;

void initializeGame()
{

        // Returns a pseudo-random integer between 0 and MAXVALUE.
        int randomNumber = rand() % MAXVALUE;
        myGame.rightGuesses=0;
        myGame.rightNumber=randomNumber;
}

WINDOW *create_newwin(int height, int width, int starty, int startx)
{
        WINDOW *local_win;
        local_win = newwin(height, width, starty, startx);
        box(local_win, 0, 0);
        wrefresh(local_win);
        return local_win;
}


int getGuess()
{
        int guess=0;
        char guessString[32];
        scanf("%s", guessString);

        // Read number as string by using scanf, but convert to int for comparison with atoi()
        guess= atoi(guessString);
        size_t allowedEntries = strspn(guessString, "0123456789");

        // Some checking if guess was between allowed range + its a number + checking if answer is correct or not
        if(guess>=MINVALUE && guess<=MAXVALUE && guessString[allowedEntries] == '\0')
        {
                if(guess==myGame.rightNumber)
                        return CORRECT;
                else
                        return INCORRECT;
        }
        else
                return WRONGFORMAT;

}

/**
    Function for updating views regarding the input values...
 **/

void updateWindowTexts(WINDOW* window, int state)
{

        char* greetingsString = "Guess the correct number!";
        char* instructionsString = "Enter number 0-10 and press enter";
        char* correctGuess = "That was correct! Lets play again";
        char* incorrectGuess = "Sorry that was not right";
        char* wrongFormat = "incorrect number, please enter number between 0-10";
        char* correctAnswersString = "Correct answers:";
        char correctAnswers[32];
        char wrongAnswers[32];
        const char rightAnswersBase[] = "Right numbers so far: ";
        sprintf(correctAnswers, "%s%d", rightAnswersBase, myGame.rightGuesses);
        const char wrongAnswersBase[] = "Wrong numbers so far: ";
        sprintf(wrongAnswers, "%s%d", wrongAnswersBase, myGame.wrongGuesses);


        wclear(window);
        box (window, 0, 0);

        mvwprintw (window, 1, (WINDOWWIDTH/2)-(strlen(greetingsString)/2), greetingsString);
        mvwprintw (window, (WINDOWHEIGHT-3), (WINDOWWIDTH/2)-(strlen(correctAnswers)/2), correctAnswers);
        mvwprintw (window, (WINDOWHEIGHT-2), (WINDOWWIDTH/2)-(strlen(wrongAnswers)/2), wrongAnswers);
        mvwprintw (window, (WINDOWHEIGHT/2), (WINDOWWIDTH/2)-(strlen(instructionsString)/2), instructionsString);


        switch (state) {
        case START:
                break;
        case CORRECT:
                mvwprintw (window, WINDOWHEIGHT-5, (WINDOWWIDTH/2)-(strlen(correctGuess)/2), correctGuess);
                myGame.rightGuesses++;
                break;
        case INCORRECT:
                mvwprintw (window, (WINDOWHEIGHT-5), (WINDOWWIDTH/2)-(strlen(incorrectGuess)/2), incorrectGuess);
                myGame.wrongGuesses++;
                break;
        case WRONGFORMAT:
                mvwprintw (window, (WINDOWHEIGHT-5), (WINDOWWIDTH/2)-(strlen(wrongFormat)/2), wrongFormat);
                break;

        }
        wrefresh (window);

}

int main (int argc, char **argv)
{
        WINDOW *my_win;
        initscr();
        // Here we call crea_newwin to make new window, paremeters are static and defined at the top of file
        // You can try to play with these numbers
        my_win = create_newwin(WINDOWHEIGHT, WINDOWWIDTH, WINDOWSTARTY, WINDOWSTARTX);
        // Initialization of random generator, should only be called once.
        srand(time(NULL));

        initializeGame();
        // Update window once before enteringing loop
        updateWindowTexts(my_win,START);
        while(1)
        {
                updateWindowTexts(my_win,getGuess());
        }
        return 0;
}

【讨论】:

  • 好答案,您需要#include &lt;time.h&gt; 才能调用:srand (time (NULL)); 但您发布的文件中没有出现关于答案变量的其他错误??
  • 哦,抱歉,忘记在答案中也包含那个。很抱歉,但我不太明白这一点:“但是您发布的文件中没有出现关于答案变量的其他错误??”
猜你喜欢
  • 2016-09-26
  • 2014-04-23
  • 1970-01-01
  • 2010-11-21
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 2011-06-08
相关资源
最近更新 更多