相信大家小时候都玩过剪刀石头布的游戏,现在用C++来实现一下。
还有,就是小编刚学C++不久,可能有一些错误,请各位大佬多多指教
思路:
通过时间作为种子,生成一个随机数,然后取其各位作为判断石头剪刀布的标准
代码:
#include#include #include #include #include #include using namespace std; void wrong_form() { cout<<"emmmmm "; Sleep(500); cout<<"Maybe your form is wrong,check it."; Sleep(1000); system("cls"); } void srp() { //初始化 int ran=0,guess=0; string computer; srand(time(0)); ran=rand()%10; switch(ran) { case 0: case 1: case 2: computer="S"; break; case 3: case 4: case 5: computer="R"; break; case 6: case 7: case 8: computer="P"; break; } // cout<<"S->sissor"< rock"< paper"< >player; if(player=="S"||player=="R"||player=="P") { if(player==computer)cout<<"You Win!"< >cnte; if(cnte=="A"||cnte=="B") { if(cnte=="A")system("cls"); else if(cnte=="B") { system("cls"); gotoxy(10,5); cout<<"Goodbye!"; Sleep(1000); return 0; } } else { wrong_form(); srp(); } } return 0; }
其实吧,这个程序有一些浪费空间,就比如那些string类型的变量完全可以改成char类型还有就是那个石头剪刀布的判断感觉有点麻烦,但是也没想出什么好方法



