📄 utility.c
字号:
//utility.c
#include <errno.h>
#include <io.h>
#include <time.h>
#include <windows.h>
#include "data.h"
void Trace_board(int is[ ])
{
int i = 0;
char board[10][4] = {
"0 ","1 ","2 ","3 ","4 ",
"5 ","6 ","7 ","8 ","9 ",
};
memcpy(board[is[0]], "*R", 2);
memcpy(board[is[1]], "*B", 2);
memcpy(board[is[2]], "*B", 2);
//board[is[0]] = 'R';
//board[is[1]] = 'B';
//board[is[2]] = 'B';
// 0
//| \
//| 1
//| /\
//|/ \
//2─━3
//| /|
//| / |
//|/ |
//4─━5
//| /|
//| / |
//|/ |
//6─━7
//| /|
//| / |
//|/ |
//8─━9
printf(" \n");
printf(" %s\n",board[0]);
printf(" ∣﹨\n");
printf(" ∣ %s\n",board[1]);
printf(" ∣/ ﹨\n");
printf(" %s—— %s\n", board[2], board[3]);
printf(" ∣ /∣\n");
printf(" ∣/ ∣\n");
printf(" %s—— %s\n", board[4], board[5]);
printf(" ∣ /∣\n");
printf(" ∣/ ∣\n");
printf(" %s—— %s\n", board[6], board[7]);
printf(" ∣ /∣\n");
printf(" ∣/ ∣\n");
printf(" %s—— %s\n", board[8], board[9]);
printf("\n");
}
//输出
void Print(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
fflush(stdout);
va_end(ap);
}
//命令处理
int Option( )
{
nArgs = ReadParse(buffer, args, " ");
if (!nArgs)
return (1);
else if (OptionMatch("end", *args) || OptionMatch("quit", *args))
{
exit(0);
}
else if (OptionMatch("go", *args) || OptionMatch("move", *args))
{
return (-1);
}
else if (OptionMatch("time", *args))
{
if (nArgs < 2) {
return (1);
}
timeRemaining = atoi(args[1]);
oTimeRemaining = timeRemaining;
return (1);
}
else if (OptionMatch("timeleft", *args))
{
if (nArgs < 3) {
return (1);
}
timeRemaining = atoi(args[1]);
oTimeRemaining = atoi(args[2]);
return (1);
}
else
return (0);
return (1);
}
//匹配
int OptionMatch(char *command, char *input)
{
if (!strcmp(command, input))
return (1);
if (strstr(command, input) == command)
return (1);
return (0);
}
//中断,并等待接收用户的输入
void Interrupt(int ply)
{
int left = 0, readStat, result;
do {
readStat = Read(0, buffer);
if (readStat <= 0)
break;
nArgs = ReadParse(buffer, args, " ;");
if (nArgs == 0) {
break;
}
result = Option( );
if (result)
{
break;
}
else {
nArgs = ReadParse(buffer, args, " ;");
}
} while (1);
}
//将读入的命令行,识别成独立的单词
int ReadParse(char *buffer, char *args[], char *delims)
{
char *next, tbuffer[512];
int nArgs;
strcpy(tbuffer, buffer);
for (nArgs = 0; nArgs < 256; nArgs++)
*(args[nArgs]) = 0;
next = strtok(tbuffer, delims);
if (!next)
return (0);
strcpy(args[0], next);
for (nArgs = 1; nArgs < 256; nArgs++) {
next = strtok(0, delims);
if (!next)
break;
strcpy(args[nArgs], next);
}
return (nArgs);
}
//
int ReadInput(void)
{
char buffer[512], *end;
int bytes;
do
bytes = read(fileno(inputStream), buffer, 512);
while (bytes < 0 && errno == EINTR);
if (bytes == 0) {
if (inputStream != stdin)
fclose(inputStream);
inputStream = stdin;
return (0);
} else if (bytes < 0) {
exit(1);
}
end = cmdBuffer + strlen(cmdBuffer);
memcpy(end, buffer, bytes);
*(end + bytes) = 0;
return (1);
}
//读入数据到
int Read(int wait, char *buffer)
{
char *eol, *ret, readdata;
*buffer = 0;
if (strchr(cmdBuffer, '\n'));
else if (!wait) {
if (CheckInput()) {
readdata = ReadInput();
if (!strchr(cmdBuffer, '\n'))
return (0);
if (!readdata)
return (-1);
} else
return (0);
}
else
{
while (!strchr(cmdBuffer, '\n')) {
readdata = ReadInput();
if (!readdata)
return (-1);
}
}
eol = strchr(cmdBuffer, '\n');
*eol = 0;
ret = strchr(cmdBuffer, '\r');
if (ret)
*ret = ' ';
strcpy(buffer, cmdBuffer);
memmove(cmdBuffer, eol + 1, strlen(eol + 1) + 1);
return (1);
}
//检查用户的输入
int CheckInput(void)
{
int i;
static int init = 0, pipe;
static HANDLE inh;
DWORD dw;
if (strchr(cmdBuffer, '\n'))
return (1);
if (!init) {
init = 1;
inh = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(inh, &dw);
if (!pipe) {
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(inh);
}
}
if (pipe) {
if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) {
return 1;
}
return dw;
} else {
GetNumberOfConsoleInputEvents(inh, &dw);
return dw <= 1 ? 0 : dw;
}
return (i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -