WKYNEKO

博客园 首页 新随笔 联系 订阅 管理

 

 

 

分为8个部分,也是8个文件

头文件2个:

All.h                     包含主程序所需的所有头文件

Class.h                   包含所有的类,并卷入All.h

cpp文件6:

main 函数.cpp                主调函数

Deck.cpp                     包含Deck类的函数

Per_Deck.cpp                 包含Per_Deck类的函数

Play_Deck.cpp                 包含Play_Deck类的函数

Playing_Card.cpp              包含Playing_Card类的函数

main 函数调用的函数.cpp       包含主调函数调用的函数

一、  详细设计

All.h

#pragma once

#include <iostream>

#include <string.h>

#include <string>

#include <conio.h>

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

 

 

Class.h

#pragma once

#include"All.h"

using namespace std;

class Per_Deck;

class Playing_Card//扑克类                                     

{

private:

     int m_Value;//扑克实际面值           

     char m_Face[3];//扑克字符串A2345678910JQK                                  

     char m_Suit;//扑克花色(黑、红、梅、方)                              

public:

     friend class Per_Deck;

     Playing_Card();//将对象清零                                              

     void Show_Card();//显示面值花色        

     void Card_Make(int);//给每一张扑克赋值

};

 

 

class Deck//一副扑克(54张)                                                              

{

private:

     Playing_Card m_Card_Array[54];                         

     int m_Last_Delt;                                             

public:

     Deck();

     void Make_Deck();//52张牌清零(没有大小王)                           

     void Deal_One();//删除开头一张牌                             

     void Show_Deck();//依次逐张显示这副牌                          

     void Deck_Shuff(int);//用于洗牌,参数是洗牌次数                   

     void Remove_Card();//清零删掉的那张牌                         

};

 

 

class Per_Deck                                           

{

private:

     Playing_Card m_Card[20];                                    

     int m_Number;                                              

public:

     Per_Deck(){}                                                   

     void Make_Per_Deck(Deck &, int);                                

     void Show();

     void Sort_Face();                                                

     void Sort_Suit();                                               

     int Delete(int n);

};

 

 

class Play_Deck                                               

{

private:

     Per_Deck m_Deck[4];                                               

public:

     Play_Deck(){}                                                      

     void Play_Card(Deck &);                                            

     void Show();

     int Delete_One(int row, int column);                            

     int Delete_All(int, int, int, int);                                

     void Sort_Deck_Face(int);                                                     

     void Sort_Deck_Suit(int);                                               

     void s(int);

};

 

 

void Program_Init();//初始化,并输出信息                      

void Center_Text(char Shell_Text[80]);//居中显示                                           

int Get_Number();//键入整数型                                     

char Get_Key();  //键入字符串型                                    

void Deck_Make_Driver();//主要算法(生成扑克,洗牌,删除等)             

int Get_Rand_Int(int min, int max);//minmax间选随机值                   

void Prog_Close();//暂停,供用户查看中间运行结果  

 

 

Deck.cpp

     #include"Class.h"

#include<iostream>

using std::ostream;

using namespace std;

     void Deck::Make_Deck()//对生成的52张牌逐个赋值                       

     {

            m_Last_Delt = 53;//51                                       

            while (m_Last_Delt > -1)                                       

            {

                   m_Card_Array[m_Last_Delt].Card_Make(m_Last_Delt);                      

                   m_Last_Delt--;                                                      

            }

     }

 

 

     void Deck::Show_Deck()//显示完整一副扑克                          

     {

            char a[13] = { "完整一副牌" };

            int m_Index = m_Last_Delt + 1;                                  

            int m_New_Line = 0;                                           

            Center_Text(a);                                             

            cout << endl;

            while (m_Index <= 53)//51                                    

            {

                   if (m_New_Line % 11 == 0)cout << endl;                             

                   m_Card_Array[m_Index].Show_Card();                                     

                   m_New_Line++;                                                               

                   m_Index++;                                                                

            }

     }

 

 

     void Deck::Deck_Shuff(int times)//洗牌算法                                 

     {

            char a[11] = { "洗牌......" };

            int x, m_Split;                                                  

            Center_Text(a);                                                 

            cout << endl;

            for (x = 0; x <= times; x++)

            {

                   m_Split = Get_Rand_Int(20, 35);                                

                   Deck Top_Deck;                                               

                   Deck Bottom_Deck;                                      

                   int i;

                   int m_Bottom_Number = 1;                                     

                   int m_Top_Number = 1;                                           

                   for (i = 0; i <= m_Split - 1;)                         

                   {

                          Top_Deck.m_Card_Array[m_Top_Number] = this->m_Card_Array[i];                    

                          m_Top_Number++;                                    

                          i++;

                   }

                   for (i = (m_Split); i < 54;)//52                       

                   {

                          Bottom_Deck.m_Card_Array[m_Bottom_Number] = this->m_Card_Array[i];                 

                          m_Bottom_Number++;                                                     

                          i++;

                   }

                   int m_Out_Number = 0;                                    

                   int m_Number_To_Top;                                          

                   int m_Number_To_Bottom;                                           

                   int j;

                   int h = 0;

                   m_Bottom_Number = 54 - m_Split;//52                                      

                   m_Top_Number = m_Split;                                                             

                   while (m_Out_Number < 53)//51                                     

                   {

                          m_Number_To_Top = Get_Rand_Int(2, 7);                                  

                          m_Number_To_Bottom = Get_Rand_Int(2, 7);                            

                          for (j = 0; j <= m_Number_To_Bottom; j++)                        

                          {

                                 if (m_Bottom_Number>0)                                 

                                 {

                                        this->m_Card_Array[m_Out_Number] = Bottom_Deck.m_Card_Array[m_Bottom_Number];          

                                        m_Out_Number++;                       

                                        m_Bottom_Number--;                     

                                 }

                                 for (h = 0; h <= m_Number_To_Top; h++)                       

                                 {

                                        if ((m_Top_Number>0) && (m_Out_Number <= 54))//52                 

                                        {

                                               this->m_Card_Array[m_Out_Number] = Top_Deck.m_Card_Array[m_Top_Number];         

                                               m_Out_Number++;                          

                                               m_Top_Number--;                          

                                        }

                                 }

                          }

                   }

            }

            this->m_Last_Delt = -1;                        

     }

 

 

     void Deck::Remove_Card()//将删除的那张牌清零                                

     {

            m_Card_Array[m_Last_Delt] = Playing_Card();//清零                          

     }

 

 

     void Deck::Deal_One()//从一副扑克牌中删除一张牌                      

     {

            if (m_Last_Delt != 53)//51                                             

            {

                   m_Last_Delt++;                                            

                   cout.width(5);

                   cout << endl;

                   cout << "删除牌:";

                   m_Card_Array[m_Last_Delt].Show_Card();                         

                   Remove_Card();                                                   

            }

            else

            {

                   cout << "这副牌全都删除完了";

                   Prog_Close();                                       

            }

     }

 

 

     Deck::Deck()//初始化一副扑克牌,将52张扑克牌均清零

     {

            int Last_Delt = 0;                                           

            for (int i = 0; i <= 53; i++)//51

            {

                   m_Card_Array[i] = Playing_Card();                               

            }

     }  

 

 

main 函数调用的函数.cpp

#include"Class.h"

#include<iostream>

using namespace std;

void Program_Init()//程序一开始的提示信息                         

{

     char a[11] = { "扑克牌游戏" };

     char b[25] = { "单击<Eeturn>键开始......" };

     Center_Text(a);                                                      

     cout << endl << '\n';

     Center_Text(b);                                                            

     cin.get();

}

 

 

char Get_Key()//从键盘接收一个字符,将其作为函数返回值                     

{

     char x;

     x = cin.get();

     cout << endl;

     return x;

}

 

 

void Center_Text(char Shell_Text[80])//居中算法                       

{

     int m_Length;                                        

     int m_Center;                                          

     m_Length = strlen(Shell_Text);                              

     m_Center = (80 - m_Length) / 2;                              

     for (; m_Center != 0; m_Center--)                       

     {

            cout<<" ";

     }

     cout<<Shell_Text;         

}

 

 

int Get_Number()//从键盘接收一个整数值,作为函数返回值                

{

     char a[40]={"请输入一个0-51之间的整数,输入52退出"};

     int m_Input_Integer = 0;                                      

     Center_Text(a);          

     cout << endl;

     cin >> m_Input_Integer;                                     

     return m_Input_Integer;                                    

}

 

 

void Prog_Close()//程序暂停,按任意键继续                        

{

     cout << endl << endl;

     cout << "单击<return>键继续......" << endl;

     cout << endl << endl;

     Get_Key();                                                  

     cin.get();

}

 

 

void Deck_Make_Driver()                                             

{

     Deck Deck_1;

     Deck_1.Make_Deck();                                          

     Deck_1.Show_Deck();                                            

     Get_Key();                                                           

     Deck_1.Deck_Shuff(250);                                        

     Deck_1.Show_Deck();                                               

     cout << endl << endl << endl;

     char y;

     Play_Deck F;                                                

     F.Play_Card(Deck_1);                                              

     cout << endl << endl << endl;

     F.Show();

     int g = 0;

     do

     {

            char a[9] = { "游戏规则" };

            char b[23] = { "删除一位玩家的牌输入1" };

            char c[23] = { "删除四位玩家的牌输入2" };

            char d[23] = { "按牌的面值排序   输入3" };

            char e[23] = { "按牌的花色排序   输入4" };

            char f[23] = { "显示四位玩家手牌输入5" };

            char q[23] = { "    N(n)   退出游戏" };

            Center_Text(a);                                   

            cout << endl;

            Center_Text(b);

            cout << endl;

            Center_Text(c);

            cout << endl;

            Center_Text(d);

            cout << endl;

            Center_Text(e);

            cout << endl;

            Center_Text(f);

            cout << endl;

            Center_Text(q);

            cout << endl;

            cin >> g;

            if (g == 1)

            {

                   int j, k;

                   char h;

                   cout << "请输入删除玩家的编号和牌号(从左往右数的地第N张)" << endl;

                   cin >> j >> k;

                   F.Delete_One(j, k);                                                       

                   cout << endl << "是(Y)否(N)显示删除后的手牌?" << endl;

                   cin >> h;

                   h = toupper(h);

                   if (h == 'Y')F.s(j);

                   cout << endl;

            }

            if (g == 2)

            {

                   int w, x, h, z;

                   char y;

                   cout << "请依次输入欲删除各玩家的序号" << endl;

                   cin >> w >> x >> h >> z;

                   F.Delete_All(w, x, h, z);

                   cout << endl << "是(Y)否(N)显示删除后玩家的牌?" << endl;

                   cin >> y;

                   y = toupper(y);

                   if (y == 'Y')F.Show();

                   cout << endl;

            }

            if (g == 3)

            {

                   int h;

                   char y;

                   cout << "请输入需要排列的玩家" << endl;

                   cin >> h;

                   y = toupper(h);

                   cout << "是(Y)否(N)显示排序后的牌?" << endl;

                   cin >> y;

                   y = toupper(y);

                   if (y == 'Y')F.s(h);

            }

            if (g == 4)

            {

                   int x;

                   char y;

                   cout << "请输入需要排列玩家的序号" << endl;

                   cin >> x;

                   F.Sort_Deck_Suit(x);

                   cout << "是(Y)否(N)显示排序后的牌?" << endl;

                   cin >> y;

                   y = toupper(y);

                   if (y == 'Y')F.s(x);

            }

            if (g == 5)

            {

                   F.Show();

            }

            if (g == 78 || g == 110)

            {

                   goto loop;

            }

            cout << "是(Y)否(N)回到游戏规则菜单?" << endl;

            cout << endl;

            cin >> y;

            y = toupper(y);

     } while (y == 'y');

loop:;

}

 

 

int Get_Rand_Int(int m_Min, int m_Max)//返回minmax之间的任意一值(实际上是"洗牌算法"s的一部分)

{

     int m_Number_To_Return;

     m_Number_To_Return = rand();

     m_Number_To_Return = m_Number_To_Return % (m_Max - m_Min + 1) + m_Min;

     return m_Number_To_Return;

}

 

 

main 函数.cpp

#include"Class.h"

#include<iostream>

using namespace std;

int main(void)

{

     system("chcp 437");

     srand((unsigned)time(NULL));

     int m_Card_Number = 0;                                      

     Program_Init();                                                          

     Deck_Make_Driver();                                                

     Prog_Close();                                                

     return 1;

}

 

 

Per_Deck.cpp

#include<iostream>

#include"Class.h"

using namespace std;

void Per_Deck::Make_Per_Deck(Deck &d, int a)

{

     m_Number = a;

     int i = 0;

     while (i < a&&d.m_Last_Delt != 53)

     {

            m_Card[i++] = d.m_Card_Array[++d.m_Last_Delt];

     }

}

 

 

void Per_Deck::Show()

{

     char a[20] = { "该玩家的牌:" };

     int m_New_Line = 0;

     Center_Text(a);

     cout << endl;

     while (m_New_Line<m_Number)

     {

            if (m_New_Line % 5 == 0)

                   cout << endl;

            m_Card[m_New_Line++].Show_Card();

     }

     cout << endl;

}

 

 

void Per_Deck::Sort_Face()//冒泡

{

     Playing_Card k;

     for (int i = 0; i < m_Number; i++)

     for (int j = 0; j + i<m_Number - 1; j++)

     if (m_Card[j].m_Value>m_Card[j + 1].m_Value)

     {

            k = m_Card[j];

            m_Card[j] = m_Card[j + 1];

            m_Card[j + 1] = k;

     }

}

 

 

void Per_Deck::Sort_Suit()

{

     Playing_Card k;

     for (int i = 0; i < m_Number; i++)

     for (int j = 0; j + i < m_Number - 1; j++)

     if (m_Card[j].m_Suit>m_Card[j + 1].m_Suit)

     {

            k = m_Card[j];

            m_Card[j] = m_Card[j + 1];

            m_Card[j + 1] = k;

     }

}

 

 

int Per_Deck::Delete(int n)

{

     if (n > m_Number)

     {

            cout << "超过总牌数!" << endl;

            return m_Number;

     }

     else

     {

            cout.widen(5);

            cout << "删除牌:";

            m_Card[n - 1].Show_Card();

            while (n < m_Number + 1)

            {

                   m_Card[n - 1] = m_Card[n++];

            }

            return --m_Number;

     }

}

 

 

Play_Deck.cpp

#include"Class.h"

#include<iostream>

using namespace std;

void Play_Deck::Play_Card(Deck &k)                                     

{

     int a, b, c, d;

     char g[29] = { "依次输入四位玩家手里的牌数:" };

     Center_Text(g);                                               

     cout << endl;

     cin >> a >> b >> c >> d;

     if (a + b + c + d > 54)

     {

            char g[] = { "超过牌的张数,退出游戏" };

                   Center_Text(g);                                       

            exit(0);

     }

     m_Deck[0].Make_Per_Deck(k, a);                                      

     m_Deck[1].Make_Per_Deck(k, b);                                        

     m_Deck[2].Make_Per_Deck(k, c);                                       

     m_Deck[3].Make_Per_Deck(k, d);                                    

}

 

 

void Play_Deck::Show()                                                

{

     char a[6] = { "玩家1" };

     char b[6] = { "玩家2" };

     char c[6] = { "玩家3" };

     char d[6] = { "玩家4" };

 

     Center_Text(a);                                                    

     cout << endl;

     m_Deck[0].Show();                                                   

 

     Center_Text(b);                                                   

     cout << endl;

     m_Deck[1].Show();                                                   

 

     Center_Text(c);                                                   

     cout << endl;

     m_Deck[2].Show();                                              

 

     Center_Text(d);                                                   

     cout << endl;

     m_Deck[3].Show();                                                 

}

 

 

int Play_Deck::Delete_One(int row, int column)                             

{

     m_Deck[row - 1].Delete(column);                                    

     return 1;

}

 

 

int Play_Deck::Delete_All(int a, int b, int c, int d)                         

{

     m_Deck[0].Delete(a);                                                           

     m_Deck[1].Delete(b);                                                          

     m_Deck[2].Delete(c);                                                         

     m_Deck[3].Delete(d);                                                      

     return 1;

}

 

 

void Play_Deck::Sort_Deck_Face(int b)                             

{

     m_Deck[b - 1].Sort_Face();                                         

}

 

 

void Play_Deck::Sort_Deck_Suit(int b)                                 

{

     m_Deck[b - 1].Sort_Suit();                                            

}

 

 

void Play_Deck::s(int a)                                                    

{

     m_Deck[a - 1].Show();                                                

}

 

 

Playing_Card.cpp

#include"Class.h"

#include<iostream>

using namespace std;

Playing_Card::Playing_Card()//扑克牌对象的构造函数

{

     for (int i = 1; i <= 3;)

     {

            m_Face[i] = ' ';

            i++;

     }

     m_Suit = ' ';

     m_Value = 0;

}

 

 

void Playing_Card::Show_Card()//在屏幕上显示一张扑克牌的面值及花色

{

     cout << "   ";

     cout << m_Face;

     cout.width(1);

     cout << m_Suit;

     cout << "   ";

}

 

 

void Playing_Card::Card_Make(int m_Number)//根据参数m_Number生成一张扑克牌,m_Number不同,生成的面值或花色不同

{

     int i = 0;

     char j;

     int m_Face_Number = m_Number % 13;

     if (m_Number == 52)

     {

            strcpy(m_Face, "S");

            goto loop;

     }

     if (m_Number == 53)

     {

            strcpy(m_Face, "B");

            goto loop;

     }

     switch (m_Face_Number)

     {

     case 0: strcpy(m_Face, "A"); break;

     case 9: strcpy(m_Face, "10"); break;

     case 10: strcpy(m_Face, "J"); break;

     case 11: strcpy(m_Face, "Q"); break;

     case 12: strcpy(m_Face, "K"); break;

     default:

            j = char(m_Face_Number + 49);

            if (i<3)

            {

                   m_Face[i] = ' '; i++;

                   m_Face[i] = j; i++;

                   m_Face[i] = NULL;

                   break;

            }

     }

loop:;

     if (m_Number <= 12)m_Suit = 6;//红桃

     if (m_Number >12 && m_Number <= 25)m_Suit = 3;//黑桃

     if (m_Number >25 && m_Number <= 38)m_Suit = 5;//梅花

     if (m_Number >38 && m_Number <= 51)m_Suit = 4;//方块

     if (m_Face_Number <= 9)m_Value = m_Face_Number + 1;

     if (m_Face_Number >= 10)m_Value = 10;

     if (m_Number == 52)

     {

            m_Value = 11;

            m_Suit = 49;

     }

     if (m_Number == 53)

     {

            m_Value = 12;

            m_Suit = 50;

     }

}

 

posted on 2023-05-14 23:26  BKYNEKO  阅读(27)  评论(0编辑  收藏  举报