想写一个猜数字游戏,一个线程游戏,一个线程计时,我想把线程封装到一个类里,然后一运行,设置完时间,程序就死循环了
下面是我的代码 不知道咋改了
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
clock_t begin, end;
double usetime;
int n,num
DWORD WINAPI monitor(LPVOID lpParameter) {
end = clock();
usetime = double(end - begin) / CLOCKS_PER_SEC;
}
DWORD WINAPI game(LPVOID lpParameter) {
cout << "请输入一个数:";
cin >> n;
if (n > num) {cout << "太大了" << endl;}
else if (n < num) {cout << "太小了" << endl;}
}
class Thread {
public:
HANDLE h1, h2;
void Create() {
h2 = CreateThread(NULL, 0, game, NULL, 0, NULL);
h1 = CreateThread(NULL, 0, monitor, NULL, 0, NULL);
}
};
class Game : public Thread {
public:
int sec, flag=0;
void Play() {
srand(time(NULL));
int num = rand() % 10000 + 1;
cout << "请设置游戏时间(秒):";
cin >> sec;
begin = clock();
while (n != num) {
Create();
if (usetime > sec) {
flag = 1;
break;
}
}
if (flag) {cout << "游戏超时,该数字为" << num << endl;}
else {cout << "正确猜出数" << num << endl;}
}
};
int main() {
Game game;
game.Play();
system("pause");
return 0;
}