完整代码:
#include <iostream>
#include <string>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
using namespace std;
class Playing_Card //扑克类
{
//private:
public:
int m_Value; //1~10 10 20 30
char m_Face[3]; //扑克的数字大小 A\2\3\4\5\6\7\8\9\10\J\Q\K
char m_Suit; //扑克的花色(黑、红、梅、方、大王、小王) 1,2,3,4,5,6
friend class PerDeck;
public:
Playing_Card(); //构造初始化
void showcard(); //打印扑克
void Card_Make(int);//扑克制作
};
class Deck //一副扑克(52张)加两张大小王
{
//private:
public:
Playing_Card m_cardarray[54];
int m_lastdelt;
friend class PerDeck;
public:
Deck();
void MakeDeck();
void Deal_One();
void ShowDeck();
void DeckShuff(int);
void Remove_Card();
};
//某个人拿的牌
class PerDeck
{
//private:
public:
Playing_Card m_cards[14]; // 每个人最多13张牌 + 1张可能的大小王
int m_NumCards=14;
public:
// 排序方法,按照花色排序
void SortBySuit()
{
//sort(m_cards, m_cards+ m_NumCards, [](const Playing_Card& a, const Playing_Card& b) {
// return a.m_Suit < b.m_Suit; // 假设花色字符 '1' 到 '6' 是递增的
// });
for (int i = 0; i < 14-1; i++)
{
for (int j = 0; j < 14 - i - 1; j++)
{
if (m_cards[j].m_Suit > m_cards[j + 1].m_Suit)//这是升序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
{
Playing_Card tem = m_cards[j];
m_cards[j] = m_cards[j + 1];
m_cards[j + 1] = tem;
}
}
}
}
// 排序方法,按照面值排序
void SortByFace()
{
for (int i = 0; i < 14 - 1; i++)
{
for (int j = 0; j < 14 - i - 1; j++)
{
if (m_cards[j].m_Face[1] > m_cards[j + 1].m_Face[1])//这是升序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
{
Playing_Card tem = m_cards[j];
m_cards[j] = m_cards[j + 1];
m_cards[j + 1] = tem;
}
}
}
}
};
//四个人玩牌的过程
class PlayDeck
{
//private:
public:
Deck m_deck;
PerDeck m_players[4];
int AllCards = 54;
int cnt = 0;
public:
void DealCards(); //给四个人发牌
void DisplayDeck();// 显示每个人的牌
void RemoveCard(int playerIndex, int cardIndex);// 从指定玩家手中删除指定牌
void RemoveCardBySuitAndValue(char suit, char face);
void RemoveFourCard(int index);//删除一轮的牌
// 花色排序
void DisplaySortedDeck()
{
//for (int i = 0; i < 4; ++i) {
// m_players[i].SortBySuit(); // 对每个玩家的牌进行排序
// std::cout << "Player " << (i + 1) << " cards (sorted by suit):\n";
// for (int j = 0; j < m_players[i].m_NumCards; ++j) {
// m_players[i].m_cards[j].showcard(); // 显示排序后的牌
// std::cout << std::endl;
// }
// std::cout << std::endl;
//}
for (int i = 0; i < 4; i++)
{
m_players[i].SortBySuit(