gpt4 book ai didi

c - curses C 中的随机数

转载 作者:行者123 更新时间:2023-11-30 14:44:01 25 4
gpt4 key购买 nike

为了学习和娱乐,我在业余时间做了三个月的 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

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

最佳答案

这是我的两分钱来帮助你,但请对这个解决方案持保留态度,因为这个解决方案可能包含一些愚蠢的东西......

也许你可以从中挑选一些部分,或者至少学习一些不该做的事情。示例是 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;
}

关于c - curses C 中的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53645355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com