当前位置:   article > 正文

C++练手小项目_c++小项目

c++小项目

简单的剪刀石头布小游戏函数实现

  • 开发环境:vs2019
  • 包含头文件:#include<time.h>
  • 控制台型英文界面
  • 说明:
  • 0—石头
  • 1—剪刀
  • 2—布
void thereGame()
{
	while (1)
	{
		cout << "    StoneScissorsCloth\n-Two wins form three games-\n";
		cout << "----------" << endl;
		cout << "  0.Stone\n  1.Scissors\n  2.cloth\n" << endl;
		cout << "----------" << endl;

		int UseNum;
		int frequency = 0;
		//种下随机数种子
		//srand函数用来设置rand函数
		srand((unsigned int)time(NULL));
		int PcNum = rand() % 3;// PcNum范围:rand() % n  --- [0 , n - 1]
		cout << "Please select a number at 0-2:" << endl;
		scanf_s("%d", &UseNum);
		while (UseNum < 0 || UseNum>2)
		{
			cout << "Error!Please re-enter." << endl;
			cout << "Please select a number at 0-2:" << endl;
			scanf_s("%d", &UseNum);
		}
		getchar();//清空缓存区
		switch (UseNum)
		{
		case 0:
			cout << "UseNum is stone." << endl;
			break;
		case 1:
			cout << "UseNum is Scissors." << endl;
			break;
		case 2:
			cout << "UseNum is cloth." << endl;
			break;
		}

		switch (PcNum)
		{
		case 0:
			cout << "Pc is stone." << endl;
			break;
		case 1:
			cout << "Pc is Scissors." << endl;
			break;
		case 2:
			cout << "Pc is cloth." << endl;
			break;
		}

		int winOrFailure = UseNum - PcNum;

		switch (winOrFailure)
		{
		case 0:
			cout << "Equal." << endl;
			break;
		case -1:
		case 2:
			cout << "you win." << endl;
			break;
		case -2:
		case 1:
			cout << "you failure" << endl;
			break;
		}

		cout << "Are you continue?(enter n eixt)." << endl;
		cout << "Press any key to continue." << endl;
		char ch = 0;
		while ((ch = getchar()) != '\n')
		{
			if (ch == 'n' || ch == 'N')
				return;
		}
		system("cls");//系统清屏函数, 在 .c文件中可能无法使用
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

附上运行截图:

在这里插入图片描述
思维导图:

在这里插入图片描述

  • 有不足之处还请多多指教,欢迎讨论。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/1009073
推荐阅读
相关标签
  

闽ICP备14008679号