我想创建一个简单的程序,用户必须输入提示的数字(1-4)。如果继续条件是基于输入用户选择的级别所定义的时间数量,当然还要输入正确的数字。问题是该程序会识别出数字是否正确,但是您可以随心所欲地使用它并且它不会停止游戏。这" 15分钟"程序应该是一个深入研究C ++的程序,然而它已经变成了一个混乱时钟算法和混乱的周末长项目。以下是完整的源代码。任何帮助都会很棒......
编辑:Here是一个在线C ++模拟器的链接,因此您可以测试该程序的当前状态...
#include <iostream>
#include <time.h>
#include <ctime>
#include <unistd.h>
using namespace std;
int main() {
//input
string numberGuessed;
int intNumberGuessed;
int score = 0;
int actualNumber;
int gameOver = 0;
//Level Select Variables
char level = {0};
string input = " ";
double timeForInput;
//initialize random seed
srand (time(NULL));
//Generates random number
actualNumber = rand() % 4 + 1;
//LEVEL SELECTOUR
cout << "Select A Level: 'e' 'm', or 'h': ";
getline(cin, input);
if (input.length() == 1) {
level = input[0];
}
if (level == 'e') {
cout << "You Have Selected Easy..." << endl;
cout<< "You Have .5 Second to Enter" << endl;
timeForInput = 5;
} else if(level == 'm'){
cout << "You Have Selected Medium..." << endl;
cout<< "You Have .2 Seconds to Enter" << endl;
timeForInput = 2;
} else if(level == 'h'){
cout << "You Have Selected HARD!!!" << endl;
cout<< "You ONLY Have .1 Seconds to Enter" << endl;
timeForInput = 1;
} else {
cout << "You LOSE! GOOD DAY SIR!" << endl;
}
//Instructions and Countdown
cout<<"Press The Number and Hit Enter in The Amount of Time Provided"<<endl;
sleep(1);
cout<<"3"<<endl;
sleep(1);
cout<<"2"<<endl;
sleep(1);
cout<<"1"<<endl;
sleep(1);
cout<<"GO!"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<< "Enter the Numbers As They Appear:"<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
sleep(1);
double duration = 0.0;
do {
//Procedere For Each Round
clock_t start;
clock_t finish;
start = clock();
finish = clock();
double delay = (double)(finish-start);
//Clock
start = clock();
cout<<"The number is: "<< actualNumber<<endl;
getline(cin, numberGuessed);
intNumberGuessed = stoi(numberGuessed);
finish = clock();
double elapsed = (double)(finish-start);
elapsed-=delay;
duration = elapsed/CLOCKS_PER_SEC;
cout<<duration<<endl;
//Test User's input
if((intNumberGuessed == actualNumber) && (duration <= (timeForInput/10))){
score += 1;
gameOver = 0;
} else if ((intNumberGuessed != actualNumber) || (duration >= (timeForInput/10))) {
gameOver = 1;
}
//Reset Number
actualNumber = rand() % 4 + 1;
} while (gameOver != 1);
cout<<"You Failed!"<<endl;
sleep(1);
cout<<"Your Score Was: "<<score<<endl;
return 0;
}
答案 0 :(得分:3)
在标准中,clock()
被指定为返回进程使用的近似处理器时间。特别是这意味着表达式(finish-start)
产生的持续时间不一定等于已经过的挂钟时间量。例如,如果你测量4个线程来咀嚼CPU 1秒钟,那么你应该得到大约4秒的结果。
这与您的程序相关的方式是,只是等待输入或休眠的程序没有使用任何处理器时间,因此(finish-start)
的结果将为零。
#include <iostream>
#include <chrono> // std::chrono::seconds, milliseconds
#include <thread> // std::this_thread::sleep_for
#include <ctime> // std::clock()
int main() {
auto start_processor_usage = std::clock();
auto start_wall_clock = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(1));
auto finish_processor_usage = std::clock();
auto finish_wall_clock = std::chrono::steady_clock::now();
std::cout << "CPU usage: " << (finish_processor_usage - start_processor_usage) / CLOCKS_PER_SEC << '\n';
std::cout << "Wall clock: " << (finish_wall_clock - start_wall_clock) / std::chrono::milliseconds(1) << '\n';
}
上述程序应该输出如下内容:
CPU usage: 0
Wall clock: 1000
请注意,虽然* nix平台通常正确实现clock()
以返回处理器使用率,但Windows却没有。在Windows clock()
上返回挂钟时间。在Windows和其他平台之间切换时,您需要牢记这一点。
好的,好的。使用
<random>
的随机int的语法是什么? - Noguiguy
#include <random>
int main() {
// initialize with random seed
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 engine(seed);
// define distribution: improved version of "% 4 + 1"
auto one_to_four = std::uniform_int_distribution<>(1, 4);
// generate number
auto actualNumber = one_to_four(engine);
}
答案 1 :(得分:0)
我也发现clock()没有给出我想要的结果。 所以我刚写了一个简单的Timer类来使用Windows上的QueryPerformanceCounter和Linux上的clock_gettime进行持续时间计算
试试这个:https://github.com/AndsonYe/Timer
:)