c++石头剪刀布小游戏

  • Post author:
  • Post category:其他



今天闲来无事做了1个小游戏,代码很简单,主要涉及到分支程序及随机数,适合刚学习c++的小白学习。


参考代码:

#include<bits/stdc++.h>
#include<Windows.h>
using namespace std;
int main () {
	
	int PC, player;
	
	bool flag = true;
	while (flag){
		system ("cls");
		srand(time(0));
		cout << "\t1. 剪刀 2. 石头 3. 布\n";
		cout << "player : ";
		cin >> player;
		cout << "PC : ";
		PC = 1 + rand() % (3-1+1);
		cout << PC << endl;
		if (player == 1){
			if (PC == 1){
				cout << "平局!";
			}
			else if (PC == 2){
				cout << "电脑赢!";
			}
			else if (PC == 3){
				cout << "玩家赢!";
			}
		}
		else if (player == 2){
			if (PC == 1){
				cout << "玩家赢!";
			}
			else if (PC == 2){
				cout << "平局!";
			}
			else if (PC == 3){
				cout << "电脑赢!";
			}
		}
		else if (player == 3){
			if (PC == 1){
				cout << "电脑赢!";
			}
			else if (PC == 2){
				cout << "玩家赢!";
			}
			else if (PC == 3){
				cout << "平局!";
			}
		}
		else{
			cout << "你输的是什么鬼玩意?";
			Sleep(3000);
			flag = false; 
		}
		Sleep (3000);
	}

	return 0 ;
}



版权声明:本文为m0_73340211原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。