面向对象C++程序设计课程设计报告
实验一 扑克牌游戏
一、问题描述
创建一副扑克,并完成洗牌等操作。
二、基本要求
1.增加大小王。
2.删去一张指定的扑克后,余牌显示。
3.创建一副扑克,显示一副扑克,洗牌,依次分发给4个人并显示。
- 需求分析
1.输入形式与范围是按照提示语句输入相应的数字或是(Y)否(N)的英文字母。
2.输出形式即为提示语句所说明的内容。
3.该程序基本任务是完成扑克洗牌,显示玩家手牌,按花色或牌面整理手牌(排序)。
4.测试数据为 54 张扑克牌面值以及花色,无需手动设置。
5.正确的输入将使得程序按说明运行,错误输入默认直接退出程序。
四、概要设计
cpp 文件,共 6 个,说明如下:
main 函数 主调函数
deck 包含 deck类的函数
perdeck 包含 perdeck类的函数
playdeck 包含 playdeck类的函数
PlayingCard 包含playingCard 类的函数
main 函数调用的函数
五、详细设计
#include <iostream>
#include <string>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
void Program_Init();
int main();
void Center_Text(char[]);
int get_number();
char Get_Key();
void DeckMakeDriver();
int getRandInt(int min, int max);
void prog_close();
class PerDeck;
class Playing_Card //扑克类
{
private:
int m_Value;
char m_Face[3]; //扑克的数字大小
char m_Suit; //扑克的花色(黑、红、梅、方)
public:
Playing_Card(); // CONSTRUCTOR
void showcard(); // Displays an object of class Playing_Card
void Card_Make(int);
friend class PerDeck;
};
class Deck //一副扑克(54张)
{
private:
Playing_Card m_cardarray[54]; // Object array of class Playing_Card
int m_lastdelt;
public:
Deck(); // CONSTRUCTOR initializes empty Deck object
void MakeDeck(); // makes a complete object of class Deck
void Deal_One(); // Deals a card from top of the deck
void ShowDeck(); // Displays a Deck object to the screen
void DeckShuff(int); // "Shuffles" a Deck object for int times
void Remove_Card();
friend class PerDeck;// Removes used card from Deck Prevents
};
// mistakenly using a nonexistant card
class PerDeck
{
Playing_Card mCard[20];
int nNum;
public:
PerDeck() { nNum = 0; }
void MakePerDeck(Deck&, int);
void Show();
void SortFace();
void SortSuit();
int Delete(int n);
};
class PlayDeck
{
PerDeck mDeck[4];
public:
PlayDeck() {}
void PlayCard(Deck&);
void Show();
int DeleteOne(int row, int column);
int DeleteAll(int, int, int, int);
void SortDeckFace(int);
void SortDeckSuit(int);
void s(int);
};
void PerDeck::MakePerDeck(Deck & d, int a)
{
nNum = a;
int i = 0;
while (i < a && d.m_lastdelt != 53)
mCard[i++] = d.m_cardarray[++d.m_lastdelt];
}
void PerDeck::Show()
{
int nNewLine = 0;
Center_Text("此玩家的牌:");
cout << endl;
while (nNewLine < nNum)
{
if (nNewLine % 5 == 0)
cout << endl;
mCard[nNewLine++].showcard();
}
cout << endl;
}
void PerDeck::SortFace()
{
Playing_Card k;
for (int i = 0; i < nNum; i++)
for (int j = 0; j < nNum-i-1; j++)
if (mCard[j].m_Value > mCard[j + 1].m_Value)
{
k = mCard[j]; mCard[j] = mCard[j + 1]; mCard[j + 1] = k;
}
}
void PerDeck::SortSuit()
{
Playing_Card k;
for (int i = 0; i < nNum; i++)
for (int j = 0; j < nNum - i - 1; j++)
if (mCard[j].m_Suit > mCard[j + 1].m_Suit)
{
k = mCard[j];
mCard[j] = mCard[j + 1];
mCard[j + 1] = k;
}
}
int PerDeck::Delete(int n)
{
if (n > nNum)
{
cout << "超出这副牌数" << endl;
return nNum;
}
else
{
cout.width(5);
cout << "删除牌:";
mCard[n - 1].showcard();
while (n - 1 < nNum) mCard[n - 1] = mCard[n++];
}
return --nNum;
}
void PlayDeck::PlayCard(Deck& k)
{
int a, b, c, d;
Center_Text("请依次输入四位玩家总牌数(小于等于54)");
cout << endl;
cin >> a >> b >> c >> d;
if (a + b + c + d > 54)
{
Center_Text("张数超过牌总张数,退出游戏");
exit(0);
}
mDeck[0].MakePerDeck(k, a);
mDeck[1].MakePerDeck(k, b);
mDeck[2].MakePerDeck(k, c);
mDeck[3].MakePerDeck(k, d);
}
void PlayDeck::Show()
{
Center_Text("玩家1");
cout << endl;
mDeck[0].Show();
Center_Text("玩家2");
cout << endl;
mDeck[1].Show();
Center_Text("玩家3");
cout << endl;
mDeck[2].Show();
Center_Text("玩家4");
cout << endl;
mDeck[3].Show();
}
int PlayDeck::DeleteOne(int row, int column)
{
mDeck[row - 1].Delete(column);
return 1;
}
int PlayDeck::DeleteAll(int a, int b, int c, int d)
{
mDeck[0].Delete(a);
mDeck[1].Delete(b);
mDeck[2].Delete(c);
mDeck[3].Delete(d);
return 1;
}
void PlayDeck::SortDeckFace(int b)
{
mDeck[b - 1].SortFace();
}
void PlayDeck::SortDeckSuit(int b)
{
mDeck[b - 1].SortSuit();
}
void PlayDeck::s(int a)
{
mDeck[a - 1].Show();
}
//****************************Main*************************************
int main()
{
srand((unsigned)time(NULL)); // Seeds GetRandInt
int Card_Number = 0;
Program_Init(); // Showd title etc.
DeckMakeDriver(); // The Main Function Driver Tests Deck and
// Playing_Card classes
prog_close(); // pauses screen output
return 1;
}
Playing_Card::Playing_Card() // CONSTRUCTOR
{
int i;
for (i = 1; i <= 3;) // inits string variable to blanks
{
m_Face[i] = ' ';
i++;
}
m_Suit = ' '; // inits char variable to blank
m_Value = 0;
}
void Program_Init()
{
Center_Text("扑克牌游戏");
cout << endl << "\n";
Center_Text("By Frank ");
Center_Text("Hit the <Return> key to Continue..");
cin.get();
}
char Get_Key()
{
char x;
x = cin.get();
cout << endl;
return x;
}
void Playing_Card::showcard()
{
cout << " ";
cout << m_Face;
cout.width(1);
cout << m_Suit;
cout << " ";
}
void Center_Text(char ShellText[80])
{
int length;
int center;
length = strlen(ShellText);
center = (80 - length) / 2;
for (; center != 0; center--)
{
cputs(" ");
}
cputs(ShellText);
cout << endl;
}
int get_number()
{
int Input_Integer = 0;
Center_Text("Please enter an integer between 0 and 51. 52 to quit.");
cout << endl;
cin >> Input_Integer;
return Input_Integer;
}
void Playing_Card::Card_Make(int num)
{
int i = 0;
char j;
int face_num = num % 13;
if (num / 13 == 4)
{
switch (face_num)
{
case 0:strcpy(m_Face, "S"); break;
case 1:strcpy(m_Face, "B"); break;
}
}
else
switch (face_num) // Assigns a Face value for string cards
{
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(face_num + 49); // Fills string with number mod 13
if (i < 3)
{
m_Face[i] = ' '; i++;
m_Face[i] = j; i++;
m_Face[i] = NULL;
break;
}
}
if (num <= 12)m_Suit = 6; // assigns suit use ascii values for
if (num > 12 && num <= 25)m_Suit = 3; // card symbols
if (num > 25 && num <= 38)m_Suit = 5;
if (num > 38 && num <= 51)m_Suit = 4;
if (num == 52)m_Suit = 1;
if (num == 53)m_Suit = 2;
if (num > 51)m_Value = 14;
else
{
m_Value = face_num + 1;
}
}
void DeckMakeDriver()
{
Deck deck_1;
deck_1.MakeDeck(); // Creates the Deck.
deck_1.ShowDeck(); // Displays deck to screen.
Get_Key();
// Pauses Program.
int b;
Center_Text("请输入洗牌次数。");
cin >> b;
deck_1.DeckShuff(b); // Shuffles the deck 250 times
deck_1.ShowDeck(); // Displays deck to screen.
cout << endl << endl << endl;
char y;
PlayDeck four;
four.PlayCard(deck_1);
cout << endl << endl;
four.Show();
int a = 0;
do
{
Center_Text("****************");
Center_Text("主菜单");
Center_Text("删除玩家一张牌请输入 1");
Center_Text("删除每位玩家一张牌请输入 2");
Center_Text("按玩家的牌面值顺序请输入 3");
Center_Text("按玩家的牌花色排序请输入 4");
Center_Text("显示各玩家的牌面请输入 5");
Center_Text("****************");
cin >> a;
if (a == 1)
{
int b=0, c=0;
char y;
cout << "请输入要删除第几位玩家的第几张牌。" << endl;
four.DeleteOne(b, c);
cout << endl << "是(Y)否(N)显示删除玩家后的牌?";
cin >> y;
y = toupper(y);
if (y == 'Y')four.s(b);
cout << endl;
}
if (a == 2)
{
int b, c, d, e;
char y;
cout << "请一次输入要删除玩家牌的序号。" << endl;
cin >> b >> c >> d >> e;
four.DeleteAll(b, c, d, e);
cout << endl << "是(Y)否(N)显示删除玩家后的牌?";
cin >> y;
y = toupper(y);
if (y == 'Y')four.Show();
cout << endl;
}
if (a == 3)
{
int b;
char y;
cout << "请输入要对的玩家排序" << endl;
cin >> b;
four.SortDeckFace(b);
cout << "是(Y)否(N)显示删除玩家后的牌?" << endl;
cin >> y;
y = toupper(y);
if (y == 'Y')four.s(b);
}
if (a == 4)
{
int b;
char y;
cout << "请输入要对玩家的排序" << endl;
cin >> b;
four.SortDeckSuit(b);
cout << "是(Y)否(N)显示删除玩家后的牌?" << endl;
cin >> y;
y = toupper(y);
if (y == 'Y')four.s(b);
}
if (a == 5)
{
four.Show();
}
cout << "是(Y)否(N)回到菜单?" << endl;
cin >> y;
y = toupper(y);
}
while (y == 'Y');
}
void Deck::MakeDeck() // creates a full deck not a construct
{
m_lastdelt = 53; // Set Deck to empty
while (m_lastdelt > -1) // iterate until deck is full.
{
m_cardarray[m_lastdelt].Card_Make(m_lastdelt); // call card make for every card object
m_lastdelt--; // inside the deck object ie cardarray
}
}
void Deck::ShowDeck()
{
int index = m_lastdelt + 1;
int newline = 0;
Center_Text("Current Deck of Cards from top to bottom");
cout << endl;
while (index <= 53)
{
if (newline % 11 == 0) cout << endl;
m_cardarray[index].showcard(); // calls showcard for every card in
newline++; // the cardarray object
index++;
}
}
int getRandInt(int min, int max)
{
int numToReturn;
numToReturn = rand();
numToReturn = numToReturn % (max - min + 1) + min;
return numToReturn;
}
void Deck::DeckShuff(int times) //洗牌次数
{
int x, split; //split是分开成两部分的位置,如上部分、下部分
Center_Text("Shuffling Deck");
cout << endl;
for (x = 0; x <= times; x++) // iterate input number of times
{
split = getRandInt(20, 35); // Get split location
Deck topdeck; // Create 2 new unfilled decks
Deck bottomdeck;
int i;
int bottomdeckindex = 1;
int topdeckindex = 1;
for (i = 0; i <= split - 1;)
{
topdeck.m_cardarray[topdeckindex] = this->m_cardarray[i];
topdeckindex++;
i++;
}
for (i = (split); i < 54;) // move remaining cards to bottom deck
{
bottomdeck.m_cardarray[bottomdeckindex] = this->m_cardarray[i];
bottomdeckindex++;
i++;
}
int deckoutindex = 0; // set deck to fill's index to zero
int numcardstomovetop;
int numcardstomovebottom;
int j;
int h = 0;
bottomdeckindex = 54 - split; // set index to num cards in bottom
topdeckindex = split; // set index to num cards in top
while (deckoutindex <= 53)
{
numcardstomovetop = getRandInt(2, 7);
//从上部分抽取的张数,是2-7之间的随机数
numcardstomovebottom = getRandInt(2, 7);
// Move a random number of cards(2-7)
for (j = 0; j <= numcardstomovebottom; j++) // from bottomdeck to original deck
{
if (bottomdeckindex > 0) // check for available cards
{
this->m_cardarray[deckoutindex] = bottomdeck.m_cardarray[bottomdeckindex];
deckoutindex++;
bottomdeckindex--;
}
for (h = 0; h <= numcardstomovetop; h++)
// Move a random number of cards (2 to 7)
{ // from topdeck to original deck.
if ((topdeckindex > 0) && (deckoutindex <= 52))
// check for available cards
{ // and slots
this->m_cardarray[deckoutindex] = topdeck.m_cardarray[topdeckindex];
deckoutindex++;
topdeckindex--;
}
}
}
}
}
this->m_lastdelt = -1; // Return a complete shuffled deck
}
void prog_close()
{
cout << endl << endl;
cout << " Hit the <Return> key to Continue.." << endl;
cout << endl << endl;
Get_Key(); // Necesary for clear input.
cin.get();
}
void Deck::Remove_Card()
{
m_cardarray[m_lastdelt] = Playing_Card(); // reinits used card prevents mistakes
}
void Deck::Deal_One()
{
if (m_lastdelt != 53) // Checks for available cards
{
m_lastdelt++;
cout.width(5);
cout << " Card delt";
m_cardarray[m_lastdelt].showcard();
}
else
{
cout << "Out of range Error";
prog_close();
}
}
Deck::Deck()
{
int lastdelt = 0;
int i;
for (i = 0; i <= 53; i++)
{
m_cardarray[i] = Playing_Card();
}
}
#include <iostream>
#include <string>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
void Program_Init();
int main();
void Center_Text(char[]);
int get_number();
char Get_Key();
void DeckMakeDriver();
int getRandInt(int min, int max);
void prog_close();
class PerDeck;
class Playing_Card //扑克类
{
private:
int m_Value;
char m_Face[3]; //扑克的数字大小
char m_Suit; //扑克的花色(黑、红、梅、方)
public:
Playing_Card(); // CONSTRUCTOR
void showcard(); // Displays an object of class Playing_Card
void Card_Make(int);
friend class PerDeck;
};
class Deck //一副扑克(54张)
{
private:
Playing_Card m_cardarray[54]; // Object array of class Playing_Card
int m_lastdelt;
public:
Deck(); // CONSTRUCTOR initializes empty Deck object
void MakeDeck(); // makes a complete object of class Deck
void Deal_One(); // Deals a card from top of the deck
void ShowDeck(); // Displays a Deck object to the screen
void DeckShuff(int); // "Shuffles" a Deck object for int times
void Remove_Card();
friend class PerDeck;// Removes used card from Deck Prevents
};
// mistakenly using a nonexistant card
class PerDeck
{
Playing_Card mCard[20];
int nNum;
public:
PerDeck() { nNum = 0; }
void MakePerDeck(Deck&, int);
void Show();
void SortFace();
void SortSuit();
int Delete(int n);
};
class PlayDeck
{
PerDeck mDeck[4];
public:
PlayDeck() {}
void PlayCard(Deck&);
void Show();
int DeleteOne(int row, int column);
int DeleteAll(int, int, int, int);
void SortDeckFace(int);
void SortDeckSuit(int);
void s(int);
};
void PerDeck::MakePerDeck(Deck & d, int a)
{
nNum = a;
int i = 0;
while (i < a && d.m_lastdelt != 53)
mCard[i++] = d.m_cardarray[++d.m_lastdelt];
}
void PerDeck::Show()
{
int nNewLine = 0;
Center_Text("此玩家的牌:");
cout << endl;
while (nNewLine < nNum)
{
if (nNewLine % 5 == 0)
cout << endl;
mCard[nNewLine++].showcard();
}
cout << endl;
}
void PerDeck::SortFace()
{
Playing_Card k;
for (int i = 0; i < nNum; i++)
for (int j = 0; j < nNum-i-1; j++)
if (mCard[j].m_Value > mCard[j + 1].m_Value)
{
k = mCard[j]; mCard[j] = mCard[j + 1]; mCard[j + 1] = k;
}
}
void PerDeck::SortSuit()
{
Playing_Card k;
for (int i = 0; i < nNum; i++)
for (int j = 0; j < nNum - i - 1; j++)
if (mCard[j].m_Suit > mCard[j + 1].m_Suit)
{
k = mCard[j];
mCard[j] = mCard[j + 1];
mCard[j + 1] = k;
}
}
int PerDeck::Delete(int n)
{
if (n > nNum)
{
cout << "超出这副牌数" << endl;
return nNum;
}
else
{
cout.width(5);
cout << "删除牌:";
mCard[n - 1].showcard();
while (n - 1 < nNum) mCard[n - 1] = mCard[n++];
}
return --nNum;
}
void PlayDeck::PlayCard(Deck& k)
{
int a, b, c, d;
Center_Text("请依次输入四位玩家总牌数(小于等于54)");
cout << endl;
cin >> a >> b >> c >> d;
if (a + b + c + d > 54)
{
Center_Text("张数超过牌总张数,退出游戏");
exit(0);
}
mDeck[0].MakePerDeck(k, a);
mDeck[1].MakePerDeck(k, b);
mDeck[2].MakePerDeck(k, c);
mDeck[3].MakePerDeck(k, d);
}
void PlayDeck::Show()
{
Center_Text("玩家1");
cout << endl;
mDeck[0].Show();
Center_Text("玩家2");
cout << endl;
mDeck[1].Show();
Center_Text("玩家3");
cout << endl;
mDeck[2].Show();
Center_Text("玩家4");
cout << endl;
mDeck[3].Show();
}
int PlayDeck::DeleteOne(int row, int column)
{
mDeck[row - 1].Delete(column);
return 1;
}
int PlayDeck::DeleteAll(int a, int b, int c, int d)
{
mDeck[0].Delete(a);
mDeck[1].Delete(b);
mDeck[2].Delete(c);
mDeck[3].Delete(d);
return 1;
}
void PlayDeck::SortDeckFace(int b)
{
mDeck[b - 1].SortFace();
}
void PlayDeck::SortDeckSuit(int b)
{
mDeck[b - 1].SortSuit();
}
void PlayDeck::s(int a)
{
mDeck[a - 1].Show();
}
//****************************Main*************************************
int main()
{
srand((unsigned)time(NULL)); // Seeds GetRandInt
int Card_Number = 0;
Program_Init(); // Showd title etc.
DeckMakeDriver(); // The Main Function Driver Tests Deck and
// Playing_Card classes
prog_close(); // pauses screen output
return 1;
}
Playing_Card::Playing_Card() // CONSTRUCTOR
{
int i;
for (i = 1; i <= 3;) // inits string variable to blanks
{
m_Face[i] = ' ';
i++;
}
m_Suit = ' '; // inits char variable to blank
m_Value = 0;
}
void Program_Init()
{
Center_Text("扑克牌游戏");
cout << endl << "\n";
Center_Text("By Frank ");
Center_Text("Hit the <Return> key to Continue..");
cin.get();
}
char Get_Key()
{
char x;
x = cin.get();
cout << endl;
return x;
}
void Playing_Card::showcard()
{
cout << " ";
cout << m_Face;
cout.width(1);
cout << m_Suit;
cout << " ";
}
void Center_Text(char ShellText[80])
{
int length;
int center;
length = strlen(ShellText);
center = (80 - length) / 2;
for (; center != 0; center--)
{
cputs(" ");
}
cputs(ShellText);
cout << endl;
}
int get_number()
{
int Input_Integer = 0;
Center_Text("Please enter an integer between 0 and 51. 52 to quit.");
cout << endl;
cin >> Input_Integer;
return Input_Integer;
}
void Playing_Card::Card_Make(int num)
{
int i = 0;
char j;
int face_num = num % 13;
if (num / 13 == 4)
{
switch (face_num)
{
case 0:strcpy(m_Face, "S"); break;
case 1:strcpy(m_Face, "B"); break;
}
}
else
switch (face_num) // Assigns a Face value for string cards
{
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(face_num + 49); // Fills string with number mod 13
if (i < 3)
{
m_Face[i] = ' '; i++;
m_Face[i] = j; i++;
m_Face[i] = NULL;
break;
}
}
if (num <= 12)m_Suit = 6; // assigns suit use ascii values for
if (num > 12 && num <= 25)m_Suit = 3; // card symbols
if (num > 25 && num <= 38)m_Suit = 5;
if (num > 38 && num <= 51)m_Suit = 4;
if (num == 52)m_Suit = 1;
if (num == 53)m_Suit = 2;
if (num > 51)m_Value = 14;
else
{
m_Value = face_num + 1;
}
}
void DeckMakeDriver()
{
Deck deck_1;
deck_1.MakeDeck(); // Creates the Deck.
deck_1.ShowDeck(); // Displays deck to screen.
Get_Key();
// Pauses Program.
int b;
Center_Text("请输入洗牌次数。");
cin >> b;
deck_1.DeckShuff(b); // Shuffles the deck 250 times
deck_1.ShowDeck(); // Displays deck to screen.
cout << endl << endl << endl;
char y;
PlayDeck four;
four.PlayCard(deck_1);
cout << endl << endl;
four.Show();
int a = 0;
do
{
Center_Text("****************");
Center_Text("主菜单");
Center_Text("删除玩家一张牌请输入 1");
Center_Text("删除每位玩家一张牌请输入 2");
Center_Text("按玩家的牌面值顺序请输入 3");
Center_Text("按玩家的牌花色排序请输入 4");
Center_Text("显示各玩家的牌面请输入 5");
Center_Text("****************");
cin >> a;
if (a == 1)
{
int b=0, c=0;
char y;
cout << "请输入要删除第几位玩家的第几张牌。" << endl;
four.DeleteOne(b, c);
cout << endl << "是(Y)否(N)显示删除玩家后的牌?";
cin >> y;
y = toupper(y);
if (y == 'Y')four.s(b);
cout << endl;
}
if (a == 2)
{
int b, c, d, e;
char y;
cout << "请一次输入要删除玩家牌的序号。" << endl;
cin >> b >> c >> d >> e;
four.DeleteAll(b, c, d, e);
cout << endl << "是(Y)否(N)显示删除玩家后的牌?";
cin >> y;
y = toupper(y);
if (y == 'Y')four.Show();
cout << endl;
}
if (a == 3)
{
int b;
char y;
cout << "请输入要对的玩家排序" << endl;
cin >> b;
four.SortDeckFace(b);
cout << "是(Y)否(N)显示删除玩家后的牌?" << endl;
cin >> y;
y = toupper(y);
if (y == 'Y')four.s(b);
}
if (a == 4)
{
int b;
char y;
cout << "请输入要对玩家的排序" << endl;
cin >> b;
four.SortDeckSuit(b);
cout << "是(Y)否(N)显示删除玩家后的牌?" << endl;
cin >> y;
y = toupper(y);
if (y == 'Y')four.s(b);
}
if (a == 5)
{
four.Show();
}
cout << "是(Y)否(N)回到菜单?" << endl;
cin >> y;
y = toupper(y);
}
while (y == 'Y');
}
void Deck::MakeDeck() // creates a full deck not a construct
{
m_lastdelt = 53; // Set Deck to empty
while (m_lastdelt > -1) // iterate until deck is full.
{
m_cardarray[m_lastdelt].Card_Make(m_lastdelt); // call card make for every card object
m_lastdelt--; // inside the deck object ie cardarray
}
}
void Deck::ShowDeck()
{
int index = m_lastdelt + 1;
int newline = 0;
Center_Text("Current Deck of Cards from top to bottom");
cout << endl;
while (index <= 53)
{
if (newline % 11 == 0) cout << endl;
m_cardarray[index].showcard(); // calls showcard for every card in
newline++; // the cardarray object
index++;
}
}
int getRandInt(int min, int max)
{
int numToReturn;
numToReturn = rand();
numToReturn = numToReturn % (max - min + 1) + min;
return numToReturn;
}
void Deck::DeckShuff(int times) //洗牌次数
{
int x, split; //split是分开成两部分的位置,如上部分、下部分
Center_Text("Shuffling Deck");
cout << endl;
for (x = 0; x <= times; x++) // iterate input number of times
{
split = getRandInt(20, 35); // Get split location
Deck topdeck; // Create 2 new unfilled decks
Deck bottomdeck;
int i;
int bottomdeckindex = 1;
int topdeckindex = 1;
for (i = 0; i <= split - 1;)
{
topdeck.m_cardarray[topdeckindex] = this->m_cardarray[i];
topdeckindex++;
i++;
}
for (i = (split); i < 54;) // move remaining cards to bottom deck
{
bottomdeck.m_cardarray[bottomdeckindex] = this->m_cardarray[i];
bottomdeckindex++;
i++;
}
int deckoutindex = 0; // set deck to fill's index to zero
int numcardstomovetop;
int numcardstomovebottom;
int j;
int h = 0;
bottomdeckindex = 54 - split; // set index to num cards in bottom
topdeckindex = split; // set index to num cards in top
while (deckoutindex <= 53)
{
numcardstomovetop = getRandInt(2, 7);
//从上部分抽取的张数,是2-7之间的随机数
numcardstomovebottom = getRandInt(2, 7);
// Move a random number of cards(2-7)
for (j = 0; j <= numcardstomovebottom; j++) // from bottomdeck to original deck
{
if (bottomdeckindex > 0) // check for available cards
{
this->m_cardarray[deckoutindex] = bottomdeck.m_cardarray[bottomdeckindex];
deckoutindex++;
bottomdeckindex--;
}
for (h = 0; h <= numcardstomovetop; h++)
// Move a random number of cards (2 to 7)
{ // from topdeck to original deck.
if ((topdeckindex > 0) && (deckoutindex <= 52))
// check for available cards
{ // and slots
this->m_cardarray[deckoutindex] = topdeck.m_cardarray[topdeckindex];
deckoutindex++;
topdeckindex--;
}
}
}
}
}
this->m_lastdelt = -1; // Return a complete shuffled deck
}
void prog_close()
{
cout << endl << endl;
cout << " Hit the <Return> key to Continue.." << endl;
cout << endl << endl;
Get_Key(); // Necesary for clear input.
cin.get();
}
void Deck::Remove_Card()
{
m_cardarray[m_lastdelt] = Playing_Card(); // reinits used card prevents mistakes
}
void Deck::Deal_One()
{
if (m_lastdelt != 53) // Checks for available cards
{
m_lastdelt++;
cout.width(5);
cout << " Card delt";
m_cardarray[m_lastdelt].showcard();
}
else
{
cout << "Out of range Error";
prog_close();
}
}
Deck::Deck()
{
int lastdelt = 0;
int i;
for (i = 0; i <= 53; i++)
{
m_cardarray[i] = Playing_Card();
}
}
六、调试分析
VS 提示“未使用调试信息生成二进制文件”因此百度了一下
解决:
文档仅供参考
1.项目 属性 配置属性 链接器 调试 生成调试信息:是(/DEBUG)
2.项目 属性 配置属性 C/C++ 调试信息格式(/Zi)
3.项目 属性 配置属性 C/C++ 优化 优化:禁用(/Od)
Vs每次都报错 是vs本身系统问题
严重性 | 代码 | 说明 | 项目 | 文件 | 行 | 禁止显示状态 |
错误 | C4996 | 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. | Project1 | D:\vs源文件\Project1\Project1\源.cpp | 371 |
在CSDN中搜索出答案 尝试在项目属性中加入_CRT_SECURE_NO_WARNINGS.后报错消失。
严重性 | 代码 | 说明 | 项目 | 文件 | 行 | 禁止显示状态 |
警告 | C4267 | “=”: 从“size_t”转换到“int”,可能丢失数据 | Project1 | D:\vs源文件\Project1\Project1\源.cpp | 320 |
本身代码两边在vs编译器中转换类型不相同导致代码报错。
严重性 | 代码 | 说明 | 项目 | 文件 | 行 | 禁止显示状态 |
警告 | C26495 | 未初始化变量 Deck::m_lastdelt。始终初始化成员变量(type.6)。 | Project1 | D:\vs源文件\Project1\Project1\源.cpp | 721 |
因为未初始化变量导致代码报错。初始化变量后代码成功运行。
七、用户使用说明
1.两次回车 洗牌
2.依次输入四个玩家手牌个数
3.进入”游戏规则”菜单页,按提示输入一到五的阿拉伯数字
4.按N 退回菜单页
八、测试结果
九、课程设计总结
在设计 c++ 扑克牌课程的过程中,我遇到了一些问题,其中一些是技术性的问题,另一些是设计上的问题。
技术性问题:
如何定义类和其相关关系:在开始设计时,我遇到了如何定义类和它们之间的关系,例如玩家类如何与牌类进行关联,如何定义公共属性或方法等等。
设计上的问题:
1.程序代码逻辑:在编写代码逻辑的过程中,逻辑相对清晰的部分很快就完成了。但是当问题出现时,程序的结构就变得混乱。这时,我需要重新思考代码结构,并将其重构。
2.用户体验:为了使用户更愿意使用程序,我需要考虑一些用户体验上的问题,例如程序界面的友好性,以及界面交互是否合理。
我的课程设计进展还算顺利,对于程序设计我得到了很实际的经验。在实现整个项目时,我深入了解了 c++的面向对象编程,更好的理解和应用了 c++ 的基础语法和算法知识。在编写代码时,我更注重代码质量和可读性,希望其中每一行代码都有可读性深思熟虑并且是必不可少的。
在程序的优化方面,可以考虑以下几点:
1.代码效率:尽量将不必要的计算和操作排除在程序之外,使程序的运行速度更加快速。
2.内存使用:尽可能减少大量的内存使用,使用类型大小尽可能小的数据类型,可以有效减少内存的使用量。
3.运算符重载:使用运算符重载,使代码更加清晰,可读性更高,更具示意性。
总的来说,该课程设计让我了解了 c++这门编程语言的应用以及扩展领域的开发,也提高了我学习和解决问题的能力。
十、参考文献
列出参考的相关资料和书籍:
- CSDN (主要寻求一些bug的解答)
- 百度文库 (提升代码简略效率)
- C++ primer (对源代码进行参考)
#我删除了部分无用代码注释,以尽力提高代码可读性