You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现X移动至向量中心时检测A键按下的功能?

解决方案:实现X在向量中心时的按键检测逻辑

首先修复你代码里的明显bug:外层循环和内层循环重复使用变量i,会导致循环逻辑混乱,内层循环需改用其他变量(比如j)。

针对核心需求,我们需要实现三个关键功能:计算向量中心位置、非阻塞式检测键盘输入(避免程序卡住)、区分正确时机按A、错误时机按A、中心位置未按A三种场景。

以下是兼容Windows和Linux的完整实现代码:

#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <cstdio>

// 跨平台非阻塞键盘输入实现
#ifdef _WIN32
#include <conio.h>
bool kbhit() {
    return _kbhit();
}

char getch() {
    return _getch();
}
#else
#include <termios.h>
#include <fcntl.h>
bool kbhit() {
    struct termios oldt, newt;
    int ch;
    int oldf;

    // 临时设置终端为非阻塞模式
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

    ch = getchar();

    // 恢复终端原始设置
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    fcntl(STDIN_FILENO, F_SETFL, oldf);

    if(ch != EOF) {
        ungetc(ch, stdin);
        return true;
    }
    return false;
}

char getch() {
    return getchar();
}
#endif

int main() {
    std::vector<char> hi = {'X','O','O','O','O','O','O','O'};
    // 计算向量中心索引,8个元素时对应索引3(第4个位置),可按需调整
    const int centerIndex = hi.size() / 2 - 1;
    bool pressedAtCenter = false;
    bool pressedWrongTime = false;

    for (int i = 0; i < hi.size() - 1; i++) {
        // 打印当前向量状态
        for (int j = 0; j < hi.size(); j++) {
            std::cout << hi[j];
        }
        std::cout << std::endl;

        // 检查X是否处于中心位置
        bool isAtCenter = (hi[centerIndex] == 'X');

        // 处理键盘输入
        while (kbhit()) {
            char key = getch();
            if (key == 'A' || key == 'a') {
                if (isAtCenter) {
                    pressedAtCenter = true;
                    std::cout << "✅ 正确时机按下A键!" << std::endl;
                } else {
                    pressedWrongTime = true;
                    std::cout << "❌ 错误时机按下A键!" << std::endl;
                }
            }
        }

        // 中心位置等待1秒,结束后检查是否未按A
        if (isAtCenter && !pressedAtCenter) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
            if (!pressedAtCenter) {
                std::cout << "❌ 中心位置未按下A键!" << std::endl;
            }
        } else {
            // 非中心位置,等待后继续移动
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }

        // 清屏并移动X
        std::cout << "\E[2J\E[2H";
        char temp = hi[i + 1];
        hi[i + 1] = hi[i];
        hi[i] = temp;
    }

    // 程序结束输出总结
    std::cout << "\n--- 结果总结 ---" << std::endl;
    if (pressedAtCenter) {
        std::cout << "✅ 成功在中心位置按下A键" << std::endl;
    } else {
        std::cout << "❌ 未在中心位置按下A键" << std::endl;
    }
    if (pressedWrongTime) {
        std::cout << "⚠️ 存在错误时机按下A键的情况" << std::endl;
    }

    return 0;
}

关键逻辑说明

  1. 中心位置计算:通过hi.size() / 2 - 1自动计算中心索引,适配任意长度的向量,若你认为的"中心"是另一位置,直接修改该公式即可。
  2. 非阻塞输入:kbhit()检测是否有按键输入,getch()读取字符,确保X的移动流程不会因等待输入而中断。
  3. 状态判定:
    • X在中心时按下A,标记pressedAtCenter为真并提示正确;
    • X不在中心时按下A,标记pressedWrongTime为真并提示错误;
    • 中心位置等待1秒后仍未按A,直接标记未按并提示。
  4. 显示优化:将清屏操作移至移动前,避免原代码的显示闪烁问题。

内容的提问来源于stack exchange,提问作者The Escaper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 20:50:30