我想给用户一定的时间进入移动,我发现一个教程,特点是 fflush()
和 select()
但是,出现了一个错误。
未定义对select@20的引用
我的一个脱胎换骨版本的代码。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <windows.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
int main() {
struct timeval tmo;
fd_set readfds;
for (int x = 0;;x++) {
do {
printf("\nEnter a move: ");
fflush(stdout);
/* wait only 5 seconds for user input */
FD_ZERO(&readfds);
FD_SET(0, &readfds);
tmo.tv_sec = 5;
tmo.tv_usec = 0;
switch (select(1, &readfds, NULL, NULL, &tmo)) {
case -1:
err(1, "select");
break;
case 0:
printf("User didn't give an input.");
return (1);
}
scanf(" %c", &userInput);
translateUserInput(userInput, &dx, &dy);
}
while (!isDirectionOk(field, snake, dx, dy));
}
}
对了,我用的是Windows系统