52张扑克牌,随机洗牌,然后发牌,并把每个玩家的牌按从大到小的顺序显示
创新互联公司专注于武川企业网站建设,响应式网站建设,商城网站制作。武川网站建设公司,为武川等地区提供建站服务。全流程按需网站策划,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务大小规则:AKQJ1098765432
输出效果:
完整代码:
#include#include#includeusing namespace std;
//卡牌结构体
typedef struct deck
{
int row = -1; //花色
int column = -1; //面值
}deck;
//遍历deck数组看是否已经用过某张牌,若用过返回真
bool judge(deck wDeck[], int row_, int column_, int n)
{
for (int i = 0; i< n; i++)
{
if (wDeck[i].row == row_ && wDeck[i].column == column_)
return true;
}
return false;
}
//洗牌
void shuffle(deck wDeck[])
{
int row_;
int column_;
for (int card = 0; card< 52; card++)
{
do
{
row_ = rand() % 4;
column_ = rand() % 13;
} while (judge(wDeck, row_, column_, card));
wDeck[card].row = row_;
wDeck[card].column = column_;
}
}
//将牌按从大到小排序
void selectSort(deck wDeck[])
{
int i, j, index;
deck temp;
for (i = 0; i< 52; i++)
{
index = i;
for (j = index + 4; j< 52; j += 4)
{
if (wDeck[j].column >wDeck[index].column)
index = j;
}
if (index != i)
{
temp = wDeck[i];
wDeck[i] = wDeck[index];
wDeck[index] = temp;
}
}
}
//发牌
void deal(deck wDeck[], const char* wFace[], const char* wSuit[])
{
//排序,让每个玩家的牌按从大到小的顺序显示
selectSort(wDeck);
//打印
cout<< setw(12)<< "Player1"<< setw(24)<< "Player2"
<< setw(24)<< "Player3"<< setw(25)<< "Player4\n";
for (int i = 0; i< 52; i++)
{
cout<< setw(6)<
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧