当前位置:C++技术网 > 资讯 > C++随机数及扑克牌比牌程序的实现

C++随机数及扑克牌比牌程序的实现

更新时间:2016-04-28 21:57:30浏览次数:1+次

在一副牌中,两个人随机挑选一张,比较牌的大小。这样的问题是比较容易解决的。如果,要加上无放回抽取,那么你可以用上STL中的remove函数。先看看实现:

#include "iostream"
#include "windows.h"
#include "vector"
#include "algorithm"
#include "iterator"
#include "time.h"

using namespace std;

int main()
{
vector<int> coll;
int arr[] ={2,3,4,5,6,7,8,9,10,11,12,13,14};
copy(arr,arr+13,back_inserter(coll));
    srand((int)time(NULL));//srand()函数产生一个以当前时间开始的随机种子

for (int i=1; i<=3; i++)
{
int x=rand();
x=x%13+2;//a + rand() % (b-a+1) 就表示 a~b之间的一个随机数

int y=rand();
y=y%13+2;

if(x==y)
{
int x_typeA=rand();
x_typeA=x_typeA%4+1;

int y_typeB=rand();
y_typeB=y_typeB%4+1;/*static suit const spades   = 4;//黑桃
static suit const hearts   = 3;//红心
static suit const clubs    = 2;//梅花
static suit const diamonds = 1;//方块*/

cout<<"牌面的值:"<<endl;
cout<<x<<"   "<<y<<"   "<<endl;

cout<<"牌的类型:"<<endl;
cout<<x_typeA<<"   "<<y_typeB<<"    "<<endl;
if (x_typeA==y_typeB)
{
cout<<"平手!"<<endl;
}
else if (x_typeA<y_typeB)
{
cout<<"A输了!"<<endl;
}
else{
cout<<"A赢了!"<<endl;
}
}
else if (x<y)
{
cout<<"牌面的值:"<<endl;
cout<<x<<"   "<<y<<"   "<<endl;
cout<<"A输了"<<endl;
}
else{
cout<<"牌面的值:"<<endl;
cout<<x<<"   "<<y<<"   "<<endl;
cout<<"A赢了!"<<endl;
}
}
system("pause");
return 0;
}