// ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <windows.h>
#define DA 0
#define XIAO 1
#define HEI 2
#define HONG 3
#define MEI 4
#define FANG 5
struct PAI {
int zhi;
int se;
};
const char* se[] = { "大","小","黑桃","红桃","梅花","方块" };
const char* zhi[] = { "3","4","5","6","7","8","9","10","j","q","k","1","2","王" };
void shuchu(PAI pai) {
/*if(pai.se==WANG){
std::cout<<(pai.zhi==1?"大":"小")<<se[pai.se]<<"\n";
}
else*/
std::cout << se[pai.se] << " " << zhi[pai.zhi] << "\n";
}
class Doudizhu
{
PAI* pai;
int len;
public:
Doudizhu() {
srand(time(0));
pai = new PAI[54];
Xipai();
}
//洗牌
void Xipai() {
CSH();
len = 54;
for (int i = 0; i<54; ++i) {
int j = rand() % 54;
PAI p = pai[i];
pai[i] = pai[j];
pai[j] = p;
}
}
//初始化
void CSH() {
int zhi = 14;
int se = DA;
for (int i = 0; i<54; ++i) {
pai[i].zhi = --zhi;
pai[i].se = se;
if (se == DA) {
se++;
zhi = 14;
}
else if (se == XIAO) {
se++;
zhi = 13;
}
else if (zhi == 0) {
zhi = 13;
if (se == HEI)
se = HONG;
else if (se == HONG)
se = MEI;
else if (se == MEI)
se = FANG;
else if (se == FANG)
return;
}
}
}
//排序
void Sort(PAI* pai, int len, bool(*bj)(PAI&, PAI&)) {
for (int i = 0; i<13; ++i) {
for (int j = 0; j<13 - 1 - i; ++j) {
if (!bj(pai[j], pai[j + 1])) {
PAI p = pai[j];
pai[j] = pai[j + 1];
pai[j + 1] = p;
}
}
}
}
//vip发牌
void fapaiVIP(int shu, PAI* p) {
int maxlen = len;
if (shu<4)
return;
int plen = 0;
int k;
while (1) {
k = rand() % 54;
int jishu = 0;
for (int i = 0; i<len; ++i) {
if (pai[i].zhi == pai[k].zhi)
{
jishu++;
}
}
if ((pai[k].zhi == 13 && jishu == 2) || (pai[k].zhi != 13 && jishu == 4))
break;
}
PAI pk = pai[k];
//随机其他炸弹
int zhi2 = -1;
int zhi3 = -1;
if (len>20) {
do {
zhi2 = rand() % 14;
} while (zhi2 == pk.zhi);
}
if (len>41) {
do {
zhi3 = rand() % 14;
} while (zhi3 == pk.zhi || zhi3 == zhi2);
}
p[plen++] = pk;
for (int i = k; i<len - 1; ++i) {
pai[i] = pai[i + 1];
}
len--;
//找炸弹
for (int i = 0; i<len; ++i) {
if (pai[i].zhi == pk.zhi && pai[i].se != pk.se) {
p[plen++] = pai[i];
for (int j = i; j<len - 1; ++j) {
pai[j] = pai[j + 1];
}
len--;
i--;
}
}
//随机其他的牌
while (len != maxlen - shu) {
do {
k = rand() % len;
} while (zhi3 == k || k == zhi2);
PAI pk = pai[k];
p[plen++] = pk;
for (int i = k; i<len - 1; ++i) {
pai[i] = pai[i + 1];
}
len--;
}
}
//普通发牌
void fapai(int shu, PAI* p) {
int maxlen = len;
int k;
int plen = 0;
//随机其他炸弹
int zhi2 = -1;
int zhi3 = -1;
if (len>20) {
zhi2 = rand() % 14;
}
if (len>41) {
do {
zhi3 = rand() % 14;
} while (zhi3 == zhi2);
}
//随机其他的牌
while (len != maxlen - shu) {
do {
k = rand() % len;
} while (zhi3 == k || k == zhi2);
PAI pk = pai[k];
p[plen++] = pk;
for (int i = k; i<len - 1; ++i) {
pai[i] = pai[i + 1];
}
len--;
}
}
//显示
void show() {
for (int i = 0; i<54; ++i) {
shuchu(pai[i]);
}
}
//找顺子
bool shunzi(PAI* pai, int len, int* s) {
int jishu = 0;
int zhi = -1;
for (int i = len - 1; i >= 0; --i) {
if (jishu>0) {
if (jishu == 5)
return true;
if (zhi == pai[i].zhi) {
jishu--;
}
else if (zhi + 1 != pai[i].zhi)
{
zhi = -1;
memset(s, 0, 4 * jishu);
jishu = 0;
}
}
if (pai[i].zhi<12) {
zhi = pai[i].zhi;
s[jishu++] = i;
}
}
return false;
}
};
bool pd(PAI& a, PAI& b) {
return (a.zhi == b.zhi ? a.se<b.se : a.zhi>b.zhi);
}
void main() {
{
Doudizhu d;
//d.show();
int a = 13;
PAI pai[13];
d.fapai(a, pai);
std::cout << "---------------------\n";
d.Sort(pai, a, pd);
for (int i = 0; i<a; ++i) {
shuchu(pai[i]);
}
int xiab[5];
if (d.shunzi(pai, a, xiab)) {
for (int i = 0; i<5; ++i) {
std::cout << zhi[pai[xiab[i]].zhi] << " ";
}
}
std::cout << "\n";
PAI* pai2 = pai;
d.fapai(a, pai2);
std::cout << "---------------------\n";
d.Sort(pai2, a, pd);
for (int i = 0; i<a; ++i) {
shuchu(pai2[i]);
}
if (d.shunzi(pai, a, xiab)) {
for (int i = 0; i<5; ++i) {
std::cout << zhi[pai[xiab[i]].zhi] << " ";
}
}
std::cout << "\n";
PAI* pai3 = pai;
d.fapai(a, pai3);
std::cout << "---------------------\n";
d.Sort(pai3, a, pd);
for (int i = 0; i<a; ++i) {
shuchu(pai3[i]);
}
if (d.shunzi(pai, a, xiab)) {
for (int i = 0; i<5; ++i) {
std::cout << zhi[pai[xiab[i]].zhi] << " ";
}
}
std::cout << "\n";
}
system("pause");
}