C++ 撲克牌發牌程式
阿新 • • 發佈:2018-12-15
問題描述:
一副撲克有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;
}