简易c++斗地主源码,简单的斗地主不需要ui,只要能发牌,随机决定地主,把牌按大小顺序排列

html-css031

简易c++斗地主源码,简单的斗地主不需要ui,只要能发牌,随机决定地主,把牌按大小顺序排列,第1张

#include <time.h>

#include <iostream>

#include <string>

#include <vector>

enum em_CardClass_t

{

_CC_hearts = 0, // 红桃

_CC_Spade, // 黑桃

_CC_Clubs, // 梅花

_CC_Diamonds, // 方块

_CC_King // 大小王

}

struct st_Card_t

{

em_CardClass_t _emCardClass

UINT _uCardIndex // _emCardClass为_CC_King时,有效取值[1,2]

// _emCardClass非_CC_King时,有效取值[1,13]

}

using namespace std

void InitCard(vector<st_Card_t>&vtCard)

{

st_Card_t card

for( UINT i=0i<_CC_Kingi++ )

{

card._emCardClass = (em_CardClass_t)i

for( UINT j=1j<14j++ )

{

card._uCardIndex = j

vtCard.push_back(card)

}

}

card._emCardClass = _CC_King

card._uCardIndex = 1

vtCard.push_back(card)

card._uCardIndex = 2

vtCard.push_back(card)

}

// 分牌

void DealCard(vector<st_Card_t>&vtCard, vector<st_Card_t>&vtPlayer1,

vector<st_Card_t>&vtPlayer2, vector<st_Card_t>&vtPlayer3)

{

srand( (unsigned)time( NULL ) )

for( int i = 0i <51i++ )

{

int iCard = rand() % vtCard.size()

switch( i % 3 )

{

case 0:

vtPlayer1.push_back(vtCard[iCard])

break

case 1:

vtPlayer2.push_back(vtCard[iCard])

break

default:

vtPlayer3.push_back(vtCard[iCard])

break

}

if( iCard != vtCard.size()-1 )

{

vtCard[iCard] = vtCard[vtCard.size()-1]

}

vtCard.pop_back()

}

}

// 叫牌

int Bid(vector<st_Card_t>&vtCard, vector<st_Card_t>&vtPlayer1,

vector<st_Card_t>&vtPlayer2, vector<st_Card_t>&vtPlayer3)

{

srand( (unsigned)time( NULL ) )

int iBider = rand() % 3 // 叫牌者,也就是地主

vector<st_Card_t>* pPlayer = NULL

switch( iBider )

{

case 0:

pPlayer = &vtPlayer1

break

case 1:

pPlayer = &vtPlayer2

break

default:

pPlayer = &vtPlayer3

break

}

for( UINT i = 0i <vtCard.size()i++ )

{

pPlayer->push_back(vtCard[i])

}

return iBider

}

// 整牌(按红桃、黑桃、梅花、方块、小大王的花色顺序整理,同一花色再按从小到大的顺序整理)

void SortCard(vector<st_Card_t>&vtPlayer)

{

st_Card_t tmp

for( UINT i=0i<vtPlayer.size()-1i++ )

{

for( UINT j=i+1j<vtPlayer.size()j++ )

{

if( vtPlayer[i]._emCardClass <vtPlayer[j]._emCardClass )

{

continue

}

if( vtPlayer[i]._emCardClass == vtPlayer[j]._emCardClass &&

vtPlayer[i]._uCardIndex <vtPlayer[j]._uCardIndex )

{

continue

}

tmp = vtPlayer[i]

vtPlayer[i] = vtPlayer[j]

vtPlayer[j] = tmp

}

}

}

void PrintPlayerCard(const string&sPlayerName,

const vector<st_Card_t>&vtPlayer)

{

cout <<sPlayerName <<" cards:" <<endl

for( UINT i=0i<vtPlayer.size()i++ )

{

switch( vtPlayer[i]._emCardClass )

{

case _CC_hearts:

cout <<"红桃"

break

case _CC_Spade:

cout <<"黑桃"

break

case _CC_Clubs:

cout <<"梅花"

break

case _CC_Diamonds:

cout <<"方块"

break

default:

cout <<"King"

break

}

cout <<vtPlayer[i]._uCardIndex<<" "

}

cout <<endl <<endl

}

void main()

{

vector<st_Card_t>vtCard

vector<st_Card_t>vtPlayer1

vector<st_Card_t>vtPlayer2

vector<st_Card_t>vtPlayer3

InitCard(vtCard)

DealCard(vtCard, vtPlayer1, vtPlayer2, vtPlayer3)

int iBider = Bid(vtCard, vtPlayer1, vtPlayer2, vtPlayer3)

SortCard(vtPlayer1)

SortCard(vtPlayer2)

SortCard(vtPlayer3)

PrintPlayerCard("Player1", vtPlayer1)

PrintPlayerCard("Player2", vtPlayer2)

PrintPlayerCard("Player3", vtPlayer3)

cout <<"地主是Player"<<iBider+1 <<endl <<endl

}

先设计类啦,类的内部在嵌套一个牌类,牌类的成员无非就那几个:花色、大小、名称

类有那些判断牌类型的函数,比如判断是对子,三代,联队,连子,飞机,炸弹。。。。。。

你要压过别人的牌,就首先得判断牌类型,函数已经定义好了,还得判断你的牌是否打过对方,这又需要函数来解决,具体思路你按自己打牌的规矩来设计呗

希望采纳

Araneid 蜘蛛纸牌游戏

详细参考工程主页: http://pub.idr.gov.cn/dujid/projects/araneid

源代码下载(或附件):

http://pub.idr.gov.cn/dujid/projects/araneid/download/araneid-1.1.0-src.zip

二进制下载:

http://pub.idr.gov.cn/dujid/projects/araneid/download/araneid-1.1.0.zip

运行时请先解压缩,然后执行 classes 目录下的 start.bat