⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2004254205.cpp

📁 是一个简单的扑克牌游戏
💻 CPP
字号:
#include<iostream>
#include<string>
#include<ctime>
#include<iomanip>
#include<conio.h>
using namespace std;
void ProgramInit();                 
void DeckMakeDriver();              
int GetRandInt(int min,int max);    
void PlayCard();                   
int nCount;                   
class CCard                         
{ 
private:
	char szFace[3];                
	char chSuit;                    
	int nNumber;                   
public:
	CCard();
    void ShowCard();
    void CardMake(int);
	int GetCardNum();
};
CCard::CCard()                     
{
	int i;
	for(i=1;i<3;)                                               
	{
		szFace[i]=' ';
		i++;
	}

	chSuit=' ';                     
}
int CCard::GetCardNum(){return nNumber;}
void CCard::ShowCard()
{
	cout<<setw(3)<<szFace<<setw(2)<<chSuit<<" ";
}
void CCard::CardMake(int num)
{
	int i=0;
	char j;
	int face_num=num%13;           
	nNumber=face_num;
	switch(face_num)
	{
	case 0:  strcpy(szFace,"A");break;  
	case 9:  strcpy(szFace,"10");break;
	case 10: strcpy(szFace,"J");break;
	case 11: strcpy(szFace,"Q");break;
	case 12: strcpy(szFace,"K");break;
    default:
		j=char(face_num +49);
		if(i<3)
		{
			szFace[i]=' '; i++;
			szFace[i]=j; i++;
			szFace[i]=NULL;
			break;
		}
	}
	if(num<=12)chSuit=6;
	if(num>12&&num<=25)chSuit=3;
	if(num>25&&num<=38)chSuit=5;
	if(num>38&&num<=51)chSuit=4;


}
class CDeck                 
{
protected:
	CCard CardArray[52];                
	int nLastDelt;
public:
	CDeck(){}                           
	void MakeDeck();                   
	void DealOne();                     
	void ShowDeck();                    
	void DeckShuff(int);                
};
void CDeck::MakeDeck()                  
{
	nLastDelt=51;
	while(nLastDelt>-1)
	{
		CardArray[nLastDelt].CardMake(nLastDelt);
		nLastDelt--;
	}
}
void CDeck::ShowDeck()                  
{
	int nIndex=nLastDelt+1;
	int newline=0;
	cout<<endl;
	while(nIndex<=51)
	{
		if(newline%13==0)cout<<endl;        
		CardArray[nIndex].ShowCard();       
		newline++;
		nIndex++;
	}
}
void CDeck::DeckShuff(int times)          
{
	int split;                             
	cout<<"\n\t\t洗牌后......\n";
	for(int i=0;i<=times;i++)               
	{
		split=GetRandInt(1,1);
		CDeck topdeck;                    
		CDeck bottomdeck;
		int bottomdeckindex=1;
		int topdeckindex=1;
		for(int j=0;j<=split-1;j++)        
		{
			topdeck.CardArray[topdeckindex]=this->CardArray[j];
			topdeckindex++;
		}
		for(int k=(split);k<52;k++)        
		{
			bottomdeck.CardArray[bottomdeckindex]=this->CardArray[k];
			bottomdeckindex++;
		}
		int deckoutindex=0;
		int numcardstomovetop;
		int numcardstomovebottom;
		bottomdeckindex=52-split;
		topdeckindex=split;
		while(deckoutindex<=51)
		{
			numcardstomovetop=GetRandInt(2,7);    
			numcardstomovebottom=GetRandInt(2,7);
			for(j=0;j<=numcardstomovebottom;j++)
			{
				if(bottomdeckindex>0)
				{
					this->CardArray[deckoutindex]=bottomdeck.CardArray[bottomdeckindex];
					deckoutindex++;
					bottomdeckindex--;
				}
				for(int l=0;l<=numcardstomovetop;l++)
				{
					if((topdeckindex>0)&&(deckoutindex<=52))
					{
						this->CardArray[deckoutindex]=CardArray[topdeckindex];
						deckoutindex++;
						topdeckindex--;
					}
				}
			}
		}
	}

		this->nLastDelt=-1;
	}
void CDeck::DealOne()                    
{
	   

	  if(nLastDelt!=12)
	  {
		  cout<<"第一个玩家要删除的牌是:(请输入要删除牌的序号,序号从0~12)\n";
		  cin>>nLastDelt;
		  cout<<"\n已经删除:";
		  CardArray[nLastDelt].ShowCard();
	  }

	  if(nLastDelt!=25)
	  {
		  cout<<endl<<"第二个玩家要删除的牌是:(请输入要删除牌的序号,序号从13~25)\n";
		  cin>>nLastDelt;
		  cout<<"\n已经删除:";
		  CardArray[nLastDelt].ShowCard();
	  }
	  
	  if(nLastDelt!=38)
	  {
		  cout<<endl<<"第三个玩家要删除的牌是:(请输入要删除牌的序号,序号从26~38)\n";
		  cin>>nLastDelt;
		  cout<<"\n已经删除:";
		  CardArray[nLastDelt].ShowCard();
	  }
	  if(nLastDelt!=51)
	  {
		  cout<<endl<<"第四个玩家要删除的牌是:(请输入要删除牌的序号,序号从39~51)\n";
		  cin>>nLastDelt;
		  cout<<"\n已经删除:";
		  CardArray[nLastDelt].ShowCard();
	  }
	else
		cout<<"这副牌已全部被删完\n";
}
class CSuperDeck:public CDeck

{
private:
	int nLastCard;
public:
	CSuperDeck(){nLastCard=52;}
	int GetLastCard();                           
	void DeleteDeckCard(int nNum);               
	void ShowOneCard(int nNum);                  
	int GetOneCard(int nNum){return CardArray[nNum-1].GetCardNum();}
};
int CSuperDeck::GetLastCard(){return nLastCard;}
void CSuperDeck::ShowOneCard(int nNum)
{
	CardArray[nNum-1].ShowCard();
		CardArray[nNum].ShowCard();
}
void CSuperDeck::DeleteDeckCard(int nNum)
{
	for(int i=nNum-1;i<nLastCard-1;i++)
		CardArray[i]=CardArray[i+1];
	nLastCard--;
}
int main()
{
	srand((unsigned)time(NULL));             
	ProgramInit();                           
	DeckMakeDriver();                        
	PlayCard();                              
	cout<<"\n\n\t\t 谢谢使用本游戏,再见!\n\n";
	return 1;
}
void ProgramInit()                           
{
	cout<<"\n\n\t\t\t欢迎进入扑克牌游戏!\n\n";
	cout<<"\t\t\t本游戏完成洗牌和对抗游戏的功能\n\n";
	cout<<"\t\t\t程序修改员:04信息(2)    刘宏锵   学号:2004254208\n\n"<<endl;                  
	system("pause");
}
void DeckMakeDriver()
{
	CDeck deck;                                  
	deck.MakeDeck();                              
	deck.ShowDeck();                              

	cout<<endl<<endl;
	cout<<setw(15)<<"Joker"<<setw(15)<<"joker"<<endl;
	system("pause");
	deck.DeckShuff(250);                          
	deck.ShowDeck();                              
	cout<<endl<<endl;
	system("pause");
	string strChoice;
	cout<<"是否删除1张牌(Y/N)?";
    cin>>strChoice;

	while(strChoice=="Y"||strChoice=="y")
	{
		deck.DealOne();                         
		cout<<"\n\n\t余牌显示:";
		deck.ShowDeck();                         
		cout<<endl<<endl<<"是否继续删除(Y/N)?";
		cin>>strChoice;
	
	}
}
int GetRandInt(int min,int max)                  
{
	int numToReturn;
	numToReturn=rand();
	numToReturn=numToReturn%(max-min+1)+min;
	return numToReturn;
}
void PlayCard()                                                                                            
{
	CSuperDeck deck;                                 
	deck.MakeDeck();                           
	deck.ShowDeck();                           
	cout<<endl<<endl;
	system("pause");
	deck.DeckShuff(250);                      
	deck.ShowDeck();                          
	cout<<endl<<endl;
	system("pause");
	string strChoice="y";
	int nPlayer,nComputer;                 
	int nPlayerNum,nComputerNum;              
    cout<<"请设置你的总赌资\n";
	cin >>nCount;
	cout<<"对抗游戏\n";
    cout <<"您的总赌资为:"<<nCount<<endl;
	system("pause");
	do
	{   cout <<"请在对抗前下注.\n";
	    cout <<"您的下注是:"<<endl;
	    int m;	 
        cin >>m ;
	    nPlayer=GetRandInt(1,deck.GetLastCard()); 
		cout<<"你抽取的牌是:";
		deck.ShowOneCard(nPlayer);                
		nPlayerNum=deck.GetOneCard(nPlayer);      
		deck.DeleteDeckCard(nPlayer);            



        cout<<"是否高级玩家(Y/N)?[高级玩家可以查看自己下一张牌!]";    
        cin>>strChoice;                                 
	    while(strChoice=="Y"||strChoice=="y")
		{cout <<"请输入你的密码:"<<endl;
	    cin>>strChoice;
        if(strChoice=="H"||strChoice=="h")
Loop2:	{
		nPlayer=GetRandInt(1,deck.GetLastCard()); 
        cout<<"你将得到的下一张牌是:";
	    deck.ShowOneCard(nPlayer);                
	    nPlayerNum=deck.GetOneCard(nPlayer);     
	    deck.DeleteDeckCard(nPlayer);             
        cout<<"是否满意这张牌(Y/N)?";
        cin>>strChoice;
	    while(strChoice=="Y"||strChoice=="y")
		{goto Loop;} ; 
		goto Loop2;
		}
		}






Loop:        cout<<"需要查看电脑的牌吗(Y/N)?";          
        cin>>strChoice;
		while(strChoice=="Y"||strChoice=="y")
		{cout <<"请输入偷看密匙:"<<endl;
		cin>>strChoice;
        if(strChoice=="C"||strChoice=="c")
        {nComputer=GetRandInt(1,deck.GetLastCard()); 
		cout<<"\n计算机抽取的牌是:";
	
		deck.ShowOneCard(nComputer);                
		nComputerNum=deck.GetOneCard(nComputer);    
		deck.DeleteDeckCard(nComputer);
	
        cout<<"是否更改你的下注(Y/N)?";
        cin>>strChoice;
		while(strChoice=="Y"||strChoice=="y")
		{cout <<"请输入你的新赌注:"<<endl;
	    int z;	 
        cin >>z;
			m=z;
        goto Loop3;
		}goto Loop3;
		}

		}




        cout <<"是否加注.(Y/N)?";
		cin>>strChoice;
		while(strChoice=="Y"||strChoice=="y")
		{
		cout <<"请输入你的加注金额:";
		int x;
		cin >>x;
		m+=x;
		break;
		}
		system("pause");

		nComputer=GetRandInt(1,deck.GetLastCard()); 
		cout<<"\n计算机抽取的牌是:";
		deck.ShowOneCard(nComputer);               
		nComputerNum=deck.GetOneCard(nComputer);    
		deck.DeleteDeckCard(nComputer);             
Loop3:	if(nComputerNum>nPlayerNum)                 
		{nCount-=m;
		cout<<"\n对不起,你输了,下次努力吧\n\n";}
	    if(nComputerNum==nPlayerNum)
		{nCount=nCount;
		cout<<"\n平局!\n\n";}
		if(nComputerNum<nPlayerNum)
		{nCount+=m;
		cout<<"\n恭喜你,你赢了!\n\n";}
		cout <<"您的输赢的结果是:"<<nCount<<endl; 
		cout<<"是否继续对抗(Y/N)?";
		cin>>strChoice;		
	}
	while ((strChoice=="Y"||strChoice=="y")&&(deck.GetLastCard()!=0));
{
		if(deck.GetLastCard()==0)
		cout<<"\n\n\t\t牌已经发完,欢迎下次继续!\n";	
	    cout<<"\n\t\t对抗结束!\n";

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -