石头剪刀布

· · 个人记录

#include<bits/stdc++.h>
#include <windows.h>   // GetAsyncKeyState
#include <conio.h>  
using namespace std;   // _kbhit, _getch (用于非阻塞检测ESC)
void cls() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD topLeft = {0, 0};
    SetConsoleCursorPosition(hConsole, topLeft);
}
//Player1使用asd来控制剪刀石头布,Player2使用bnm来控制剪刀石头布 
int main() {
    int wj1=0,wj2=0,cnt1=0,cnt2=0;
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    while (true) {
        // 检测 ESC 键退出
        if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
            break;
        }

        // 检测目标按键 (虚拟键码)
        bool a = GetAsyncKeyState('A') & 0x8000;
        bool s = GetAsyncKeyState('S') & 0x8000;
        bool d = GetAsyncKeyState('D') & 0x8000;
        bool b = GetAsyncKeyState('B') & 0x8000;
        bool n = GetAsyncKeyState('N') & 0x8000;
        bool m = GetAsyncKeyState('M') & 0x8000;

        // 构建输出字符串
        std::string pressed;
        if (a) wj1=0;
        if (s) wj1=1;
        if (d) wj1=2;
        if (b) wj2=0;
        if (n) wj2=1;
        if (m) wj2=2;

        if(wj1==0)cout<<"石头";
        else if(wj1==1)cout<<"剪刀";
        else cout<<"布  ";
        cout<<" vs "; 
        if(wj2==0)cout<<"石头";
        else if(wj2==1)cout<<"剪刀";
        else cout<<"布  ";
        cout<<endl;
        if((wj1==0&&wj2==2)||(wj1==1&&wj2==0)||(wj1==2&&wj2==1))cnt1++;
        else if(wj1!=wj2)cnt2++;
        cout<<cnt2<<" "<<cnt1<<endl;
        cls();
        // 短暂休眠,避免占用过高CPU
    }

    std::cout << "\nExited.\n";
    return 0;
}