跳到正文

个人记录

石头剪刀布

Smile_Hammer约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年4月20日
收录
2026年4月20日
更新
2026年11月11日
篇幅
约 3 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
#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;
}
原文