1.声明一个扑克牌类,拥有方法:传入花色和点数生成扑克牌、打印扑克牌。
2、声明一个玩家类,每人拥有18张扑克牌。拥有方法:增加手牌(摸牌)、展示手牌,其中展示手牌要求降序排序展示。
完成程序:
1、随机生成18张扑克牌,当做一个玩家的手牌。
2、用54张不同的扑克牌构成牌堆,发给3个玩家。
在class.h中声明类:
#pragma once
#include<iostream>
using namespace std;
enum {
SPADES,//黑桃
HEARTS,//红桃
CLUBS,//梅花
DIAMONDS,//方片
JOKER //大小王
};
class ALL;
class Player;
class Poker {
private:
int type;
int point;
public:
void input(int T, int P);
void output();
char Type() { return type; }
int Point() { return point; }
};
class ALL {
private:
Poker src[54];
int size;
public:
ALL() {
size = 53;
}
void InitPoker();//初始化一副牌, 洗牌
void allprint();
Poker* fapai();//发牌
void draw1(ALL& A, Player& p1, Player& p2, Player& p3);
};
class Player {
private:
Poker a[18];
int size;
public:
Player() {
size = 0;
}
void Draw1_(ALL& A);//手动摸牌, 一次取一张摸牌
void Draw18(ALL& A);//自动发牌,一次18张
void print();//
bool cmppoker(Poker a, Poker b);
void InsertSort();//排序
};
在class.cpp中定义类的成员函数:
#include"class.h"
#include<ctime>
void Swap(int& a, int& b) {
int tmp = a;
a = b;
b = tmp;
}
void Poker::input(int T, int P) {
type = T;
point = P;
if (type == JOKER) {
point += 13;
}
}
void Poker::output() {
const char* Type[5] = { "黑桃","梅花","红桃","方片" ,"" };
const char* Point[16] = { "","A","2","3","4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "小王", "大王" };
cout << Type[type] << Point[point] << " ";
}
void ALL::InitPoker() {//初始化,洗牌
int rn[54];
srand((unsigned int)time(0));
for (int i = 0; i < 54; ++i) {
rn[i] = i;
}
for (int i = 0; i < 50; ++i) {
Swap(rn[rand() % 54], rn[rand() % 54]);
}
for (int n = 0,i = 0; n < 4; ++n) {
for (int m = 1; m < 14; ++m) {
src[rn[i++]].input(n, m);
}
}
src[rn[52]].input(4, 1);
src[rn[53]].input(4, 2);
}
void ALL::allprint() {
for (auto i : src) {
i.output();
}
cout << endl<<endl;
}
Poker* ALL::fapai() {
if (size >= 0) {
return &src[size--];
}
else {
cout << "牌发完了!";
return nullptr;
}
}
void Player::Draw1_(ALL& A) {
if (size < 18) {
a[size++] = *(A.fapai());
}
else {
cout << "取牌结束\n";
}
}
void Player::Draw18(ALL& A) {
for (auto& i : a) {
i = *(A.fapai());
}
}
void ALL::draw1(ALL& A, Player& p1, Player& p2, Player& p3) {
for (int i = 0; i < 18; ++i) {
p1.Draw1_(A);
p2.Draw1_(A);
p3.Draw1_(A);
}
}
void Player::print() {
for (auto i : a) {
i.output();
}
cout << endl << endl << endl;
}
bool Player::cmppoker(Poker a, Poker b) {
return (a.Point() > b.Point()) ||
(a.Point() == b.Point() && a.Type() < b.Type());
}
void Player::InsertSort() {
int i, j;
Poker tmp;
for (i = 1; i < 18; i++)
{
tmp = a[i];
for (j = i; j > 0 && cmppoker(tmp, a[j - 1]); j--)
{
a[j] = a[j - 1];
}
a[j] = tmp;
}
}
在main.cpp中调试
#include"class.h"
int main() {
ALL a;
cout << "洗牌后整副牌\n";
a.InitPoker();
a.allprint();//输出洗后的牌
Player p[3];
#if 1
//一人一张顺序抽取
a.draw1(a, p[0], p[1], p[2]);
#else
//给三个玩家随机生成18张牌
for (int i = 0; i < 3; ++i) {
p[i].Draw18(a);
}
#endif
for (int i = 0; i < 3; ++i) {
cout << "玩家" << i + 1 << "抽取的牌\n";
p[i].print();
}
for (int i = 0; i < 3; ++i) {
p[i].InsertSort();//排序
}
for (int i = 0; i < 3; ++i) {
cout << "整牌后玩家" << i + 1 << "抽取的牌\n";
p[i].print();
}
system("pause");
}
依次给三个玩家发18张牌:
三个玩家各一次抽取18张牌: