标签:c++
//this is a program witch played by two people
//二人游戏,若第一个抛骰子,抛两次的和为7或11则第一人直接胜利,第二人直接失败
//若第一个人抛骰子,抛两次的和为2,3或12,则第一个人直接失败,第二人胜利
//若第一个人抛筛子,抛两次的和以上均不是,则由第二个人抛,直到有一方胜利
#include <iostream>
#include <stdlib.h>
#include <ctime> //获取系统时间的头文件
#include <windows.h> //Sleep 函数
#include <conio.h> //_kbhit 获取键盘敲击
using namespace std;
int RollDice(); //函数原型
void SetSeed();
void PlayGame();
int RollDice() //抛骰子函数
{
int die1 = 1 + rand() % 6;
int die2 = 1 + rand() % 6;
int sum = die1 + die2;
cout << "Player rooled " << die1 << "+" << die2 << "=" << sum << endl;
return sum;
}
void SetSeed() //设置种子函数 利用系统时间设置随机种子
{
srand((unsigned)time(NULL)); //获得系统时间设置种子
}
enum GameStatus{WIN,LOSE,PLAYING}; //枚举游戏状态;
void PlayGame()
{
int sumA = 0, sumB = 0, game = 1; //game为一个旗标 ,当为1时持续投掷
GameStatus status = PLAYING;
while (status == PLAYING)
{
SetSeed(); //设置种子
cout << "请AAAAAAAA投掷:\n(按任意键开始投掷,按任意键结束投掷)\n";
system("pause");
while (game)
{
sumA = RollDice();
Sleep(20);
if (_kbhit())
{
game = 0;
}
}
game = 1;
if (sumA == 7 || sumA == 11)
{
status = WIN;
cout << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA赢了" << endl;
}
else if (sumA == 2 || sumA == 3 || sumA == 12)
{
status = LOSE;
cout << "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB赢了" << endl;
}
else
{
cout << "请BBBBBBBBBB投掷 :\n(按任意键开始投掷,按任意键结束投掷)\n";
system("pause");
while (game)
{
Sleep(20);
sumB = RollDice();
if (_kbhit())
{
game = 0;
}
}
game = 1;
if (sumB == 7 || sumB == 11)
{
status = WIN;
cout << "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB赢了" << endl;
}
else if (sumB == 2 || sumB == 3 || sumB == 12)
{
status = WIN;
cout << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA赢了" << endl;
}
else
; //do nothing; 继续下一轮
}
}
}
int main()
{
char x;
PlayGame();
system("pause");
cout << "再来一盘吗? \n" << "(Y/N):\n" << endl;
while (1)
{
if (_kbhit())
{
x = _getch();
if ((x == 'y') || (x == 'Y'))
{
PlayGame();
}
if ((x == 'n') || (x == 'N'))
{
system("pause");
return 0;
}
}
}
return 0;
}
标签:c++
原文地址:http://blog.csdn.net/shinepan/article/details/39343653