简单的剪刀石头布小游戏函数实现
- 开发环境: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((unsigned int)time(NULL));
int PcNum = rand() % 3;
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");
}
}
附上运行截图:
思维导图:
|