C++ 扑克牌发牌程序

版权声明:转载请注明出处 https://blog.csdn.net/qq_42292831/article/details/84959792

问题描述

一副扑克有52张牌,打桥牌时应将牌分给四个人。请设计一个程序完成自动发牌的工作。要求:黑桃用S(Spaces)表示;红桃用H(Hearts)表示;方块用D(Diamonds)表示;梅花用C(Clubs)表示。

提示:

黑桃标记 的ASCII码为6, 红桃标记的ASCII码为3, 方块标记的ASCII码为4,梅花标记的ASCII码为5。要求将测试序列结果存入1.txt文件中。

 

*Additional:切换编码VS2015

****************************************************************************************************************************************

 一:运行结果

(图形在我的PC上更换编码也无法显示)

<  文件存储路径:C:\\Users\\dell\\Desktop\\111.txt  >

二:源码 

(初始版本:冗余部分未优化)

1> Auto_Send.h

#include <iostream>
#include <string>
#include <time.h>
#include <algorithm>
#define _CRT_SECURE_NO_WARNINGS


class Person
{
	private:
		int card_code_1[13];
		int card_code_2[13];
		int card_code_3[13];
		int card_code_4[13];
	public:
		friend class Auto_Process;
		Person();
		void Show_code();
};

class Auto_Process
{
	private:
		int pock_code[52];
		Person a,b,c,d;
	public:
		void Init_pock_code();
		void Show_pock_code();
		void Spread_code_a();
		void Spread_code_b();
		void Spread_code_c();
		void Spread_code_d();
		void Save_results();
		void Save_code_change(const int x);
		void Show();
};

void Change_Code_Output(const int);

void Openfile(FILE *);

2> Auto_Send.cpp

#include "Auto_Send.h"
using namespace std;

FILE *file;       //全局变量最好定义在这个地方,因为函数大部分定义都在这里,全局变量定义在头文件的话例如这个指针就会报错


void Auto_Process::Init_pock_code()
{
	for(int i=0;i<52;i++)
	{
		pock_code[i] = i+1;
	}
	for(int i =0;i<52;i++)    //randomly swap 52 times
	{
		swap(pock_code[rand()%52],pock_code[rand()%52]);
	}
}

void Auto_Process::Show_pock_code()
{
	for(int i =0;i<52;i++)
	{
		cout << pock_code[i] << endl;
	}
}

void Auto_Process::Spread_code_a()
{
	//pock_code[52];
	for(int i =0,j=0;i<13,j<13;i++,j++)
	{
		if(pock_code[i]>=1&&pock_code[i]<=13)
		{
			a.card_code_1[j] = pock_code[i];
		}
		else if(pock_code[i]>=14&&pock_code[i]<=26)
		{
			a.card_code_2[j] = pock_code[i];
		}
		else if(pock_code[i]>=27&&pock_code[i]<=39)
		{
			a.card_code_3[j] = pock_code[i];
		}
		else
		{
			a.card_code_4[j] = pock_code[i];
		}
	}
}

void Auto_Process::Spread_code_b()
{
	//pock_code[52];
	for(int i =13,j = 0;i<26,j<13;i++,j++)
	{
		if(pock_code[i]>=1&&pock_code[i]<=13)
		{
			b.card_code_1[j] = pock_code[i];
		}
		else if(pock_code[i]>=14&&pock_code[i]<=26)
		{
			b.card_code_2[j] = pock_code[i];
		}
		else if(pock_code[i]>=27&&pock_code[i]<=39)
		{
			b.card_code_3[j] = pock_code[i];
		}
		else
		{
			b.card_code_4[j] = pock_code[i];
		}
	}
}
void Auto_Process::Spread_code_c()
{
	//pock_code[52];
	for(int i =26,j = 0;i<39,j<13;i++,j++)
	{
		if(pock_code[i]>=1&&pock_code[i]<=13)
		{
			c.card_code_1[j] = pock_code[i];
		}
		else if(pock_code[i]>=14&&pock_code[i]<=26)
		{
			c.card_code_2[j] = pock_code[i];
		}
		else if(pock_code[i]>=27&&pock_code[i]<=39)
		{
			c.card_code_3[j] = pock_code[i];
		}
		else
		{
			c.card_code_4[j] = pock_code[i];
		}
	}
}
void Auto_Process::Spread_code_d()
{
	//pock_code[52];
	for(int i =39,j=0;i<52,j<13;i++,j++)
	{
		if(pock_code[i]>=1&&pock_code[i]<=13)
		{
			d.card_code_1[j] = pock_code[i];
		}
		else if(pock_code[i]>=14&&pock_code[i]<=26)
		{
			d.card_code_2[j] = pock_code[i];
		}
		else if(pock_code[i]>=27&&pock_code[i]<=39)
		{
			d.card_code_3[j] = pock_code[i];
		}
		else
		{
			d.card_code_4[j] = pock_code[i];
		}
	}
}
void Auto_Process::Save_code_change(const int x)
{
	if (x % 13 == 0&&x!=0)
	{
		fputc('K', file);
		fputc(' ', file);
	}
	else if (x % 13 == 1)
	{
		fputc('A', file);
		fputc(' ', file);
	}
	else if (x % 13 == 2)
	{
		fputc('2', file);
		fputc(' ', file);
	}
	else if (x % 13 == 3)
	{
		fputc('3', file);
		fputc(' ', file);
	}
	else if (x % 13 == 4)
	{
		fputc('4', file);
		fputc(' ', file);
	}
	else if (x % 13 == 5)
	{
		fputc('5', file);
		fputc(' ', file);
	}
	else if (x % 13 == 6)
	{
		fputc('6', file);
		fputc(' ', file);
	}
	else if (x % 13 == 7)
	{
		fputc('7', file);
		fputc(' ', file);
	}
	else if (x % 13 == 8)
	{
		fputc('8', file);
		fputc(' ', file);
	}
	else if (x % 13 == 9)
	{
		fputc('9', file);
		fputc(' ', file);
	}
	else if (x % 13 == 10)
	{
		fputc('10', file);
		fputc(' ', file);
	}
	else if (x % 13 == 11)
	{
		fputc('J', file);
		fputc(' ', file);
	}
	else if (x % 13 == 12)
	{
		fputc('Q', file);
		fputc(' ', file);
	}
}

void Auto_Process::Save_results()
{
	fopen_s(&file, "C:\\Users\\dell\\Desktop\\111.txt", "a+");
	fputc(3,file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(a.card_code_1[i]);
	}fputc('\n', file);	
	fputc(4, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(a.card_code_2[i]);
	}fputc('\n', file);	
	fputc(5, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(a.card_code_3[i]);
	}fputc('\n', file);	
	fputc(6, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(a.card_code_4[i]);
	}fputc('\n', file);
	fputc('\n', file);
	fputc(3, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(b.card_code_1[i]);
	}fputc('\n', file);
	fputc(4,file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(b.card_code_2[i]);
	}fputc('\n', file);
	fputc(5, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(b.card_code_3[i]);
	}fputc('\n', file);
	fputc(6, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(b.card_code_4[i]);
	}fputc('\n', file);
	fputc('\n', file);
	fputc(3, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(c.card_code_1[i]);
	}fputc('\n', file);
	fputc(4, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(c.card_code_2[i]);
	}fputc('\n', file);
	fputc(5, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(c.card_code_3[i]);
	}fputc('\n', file);
	fputc(6, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(c.card_code_4[i]);
	}fputc('\n', file);
	fputc('\n', file);
	fputc(3, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(d.card_code_1[i]);
	}fputc('\n', file);
	fputc(4, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(d.card_code_2[i]);
	}fputc('\n', file);
	fputc(5, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(d.card_code_3[i]);
	}fputc('\n', file);
	fputc(6, file);
	fputc(' ', file);
	for (int i = 0; i < 13; i++)
	{
		Save_code_change(d.card_code_4[i]);
	}fputc('\n', file);
	fclose(file);
}
void Change_Code_Output(const int x)
{
	if (x % 13 == 0)
	{
		cout << "K ";
	}
	else if (x % 13 == 1)
	{
		cout << "A ";
	}
	else if (x % 13 == 2)
	{
		cout << "2 ";
	}
	else if (x % 13 == 3)
	{
		cout << "3 ";
	}
	else if (x % 13 == 4)
	{
		cout << "4 ";
	}
	else if (x % 13 == 5)
	{
		cout << "5 ";
	}
	else if (x % 13 == 6)
	{
		cout << "6 ";
	}
	else if (x % 13 == 7)
	{
		cout << "7 ";
	}
	else if (x % 13 == 8)
	{
		cout << "8 ";
	}
	else if (x % 13 == 9)
	{
		cout << "9 ";
	}
	else if (x % 13 == 10)
	{
		cout << "10 ";
	}
	else if (x % 13 == 11)
	{
		cout << "J ";
	}
	else if (x % 13 == 12)
	{
		cout << "Q ";
	}
}

void Person::Show_code()
		{
			cout << (char)3 << " ";
			for(int i=0;i<13;i++)
			{
				if(!card_code_1[i])
				{
					
				}
				else
				{
					Change_Code_Output(card_code_1[i]);
				}
			}
			cout << endl << (char)4 << " ";
			for(int i=0;i<13;i++)
			{
				if(!card_code_2[i])
				{
					
				}
				else
				{
					Change_Code_Output(card_code_2[i]);
				}
			}
			cout << endl << (char)5 << " ";
			for(int i=0;i<13;i++)
			{
				if(!card_code_3[i])
				{
					
				}
				else
				{
					Change_Code_Output(card_code_3[i]);
				}
			}
			cout << endl << (char)6 << " ";
			for(int i=0;i<13;i++)
			{
				if(!card_code_4[i])
				{
					
				}
				else
				{
					Change_Code_Output(card_code_4[i]);
				}
			}
		}
void Auto_Process::Show()
{
	a.Show_code();
	cout << endl << endl;
	b.Show_code();
	cout << endl << endl;
	c.Show_code();
	cout << endl << endl;
	d.Show_code();
	cout << endl << endl;
}

Person::Person()
{
	for(int i =0;i<13;i++)
	{
		card_code_1[i] = 0; 
		card_code_2[i] = 0; 
		card_code_3[i] = 0; 
		card_code_4[i] = 0; 
	}
}


3> pock_send.cpp

#include "Auto_Send.h"
using namespace std;

int main()
{
	system("title Spread_Pock");
	//char a[5] = {3,4,5,6,'\0'};    //RedHeart.Square/Diamond.Flower/Clubs.BlackSpaces
	//cout << a << endl;
	srand(unsigned(time(NULL)));
	Auto_Process A;
	A.Init_pock_code();
	//A.Show_pock_code();    //Already randomly
	//system("pause");
	A.Spread_code_a();
	A.Spread_code_b();
	A.Spread_code_c();
	A.Spread_code_d();
	A.Show();
	A.Save_results();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42292831/article/details/84959792