继前两篇算法的基础上,然后整合测试,成功测试出了斗地主的随机发牌效果,我这里测试截取了几张运行效果图,果真还是挺随机的!哈哈
Code:
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include <netdb.h> #include <pthread.h> using namespace cocos2d; using namespace CocosDenshion; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } int socketHandle = 0; //创建BSD连接 //ip and port int HelloWorld::connect(const char *ip, unsigned short port) { struct sockaddr_in sa; struct hostent * hp; hp = gethostbyname(ip); if (!hp) { return -1; } memset(&sa,0,sizeof(sa)); memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); sa.sin_family=hp->h_addrtype; sa.sin_port = HTONS(port); socketHandle = socket(sa.sin_family, SOCK_STREAM, 0); if(socketHandle<0) { printf("failed to create socket\n"); return -1; } if (::connect(socketHandle, (sockaddr *)&sa, sizeof(sa))) { printf("failed to connect socket\n"); return -1; } CCLog("Client connect OK! IP:%s:%d",ip,port); return 0; } //创建线程 int HelloWorld::threadStart() { int errCode = 0; do { pthread_attr_t tAttr; errCode = pthread_attr_init(&tAttr); CC_BREAK_IF(errCode!=0); errCode = pthread_attr_setdetachstate(&tAttr, PTHREAD_CREATE_DETACHED); if (errCode!=0) { pthread_attr_destroy(&tAttr); break; } //errCode = pthread_create(&threadHimi, &tAttr, thread_function, this); } while (0); return errCode; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } // char * ip = "192.168.1.24"; // unsigned short port = 5005; // // int con = HelloWorld::connect(ip, port); // if (con>0) { // printf("error"); // } // else // { // // printf("登陆成功"); // } // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite * bg = CCSprite::create("bg.jpg"); bg->setAnchorPoint(CCPointMake(0, 1)); bg->setPosition(CCPointMake(0, size.height)); bg->setScaleX(0.58); this->addChild(bg); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("card.plist"); CCSpriteBatchNode * batchNode = CCSpriteBatchNode::create("card.pvr.ccz"); batchNode->setPosition(CCPointZero); this->addChild(batchNode); // for (int i=1; i<=13; i++) // { // CCString *name = CCString::createWithFormat("Hong%d.png",i); // CCSprite*card1=CCSprite::createWithSpriteFrameName(name->getCString()); // card1->setScale(0.7); // card1->setPosition(CCPointMake(140 + i*15,size.height/2-95)); // batchNode->addChild(card1); // } CCSprite * role = CCSprite::createWithSpriteFrameName("role.png"); role->setScale(0.6); role->setPosition(CCPointMake(60, 80)); batchNode->addChild(role); CCSprite * role1 = CCSprite::createWithSpriteFrameName("role1.png"); role1->setScale(0.5); role1->setPosition(CCPointMake(60, 220)); batchNode->addChild(role1); CCSprite * role2 = CCSprite::createWithSpriteFrameName("role1.png"); role2->setScale(0.5); role2->setPosition(CCPointMake(420,220)); batchNode->addChild(role2); //左边牌 for (int i=2; i<=13; i++) { CCSprite*card1=CCSprite::createWithSpriteFrameName("SmallCardBack1.png"); card1->setScale(0.6); card1->setRotation(-82); card1->setPosition(CCPointMake(115+i ,size.height/2-13+i*11)); batchNode->addChild(card1); } //右边牌 for (int i=2; i<=13; i++) { CCSprite*card1=CCSprite::createWithSpriteFrameName("SmallCardBack1.png"); card1->setScale(0.6); card1->setRotation(82); card1->setPosition(CCPointMake(365-i,size.height/2-13+i*11)); batchNode->addChild(card1); } //播放音乐 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width/2, size.height-20) ); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); //绘制牌 xipai(); //调用洗牌算法 //分牌 for (int i=0; i<17; i++) { pai[i]=data[i]; } printf("\n------"); for (int i=0; i<17; i++) { printf("%d ",pai[i]); } int * p = pai; Sort(p, 17); printf("\n------"); for (int i=0; i<17; i++) { printf("%d ",pai[i]); } //绘制牌 for (int i=0; i<17; i++) { //大王 if (pai[i]==53) { // printf("111"); CCString *name = CCString::createWithFormat("BigJoker.png"); CCSprite*card1=CCSprite::createWithSpriteFrameName(name->getCString()); card1->setScale(0.7); card1->setPosition(CCPointMake(140 + i*15,size.height/2-95)); batchNode->addChild(card1); } //小王 else if (pai[i] == 52) { // printf("222"); CCString *name = CCString::createWithFormat("smlJoker.png"); CCSprite*card1=CCSprite::createWithSpriteFrameName(name->getCString()); card1->setScale(0.7); card1->setPosition(CCPointMake(140 + i*15,size.height/2-95)); batchNode->addChild(card1); } else { int huase = pai[i]/13+1; int value = pai[i]%13+1; //牌值 char * s = NULL; switch (huase) { case 1: //CCString *name = CCString::createWithFormat("Hong%d.png",i); s = "Hong"; break; case 2: //CCString *name = CCString::createWithFormat("Fan%d.png",i); s = "Fan"; break; case 3: //CCString *name = CCString::createWithFormat("Mei%d.png",i); s = "Mei"; break; case 4: //CCString *name = CCString::createWithFormat("Hei%d.png",i); s = "Hei"; break; default: break; } printf("%s%d.png\n",s,value); // printf("%d,%d\n",huase,value); CCString *name = CCString::createWithFormat("%s%d.png",s,value); CCSprite*card1=CCSprite::createWithSpriteFrameName(name->getCString()); card1->setScale(0.7); card1->setPosition(CCPointMake(140 + i*15,size.height/2-95)); batchNode->addChild(card1); } } bgMusic = true; return true; } //播放音乐 void HelloWorld::menuCloseCallback(CCObject* pSender) { // CCDirector::sharedDirector()->end(); // //#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // exit(0); //#endif bgMusic=!bgMusic; if (bgMusic) { SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); } else { //播放场景音乐 SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bg.mp3", true); } } void HelloWorld::xipai() { for (int i=0; i<54; i++) { data[i] = i; } //随机数 //洗牌 for(int i=0;i<154;i++) { int t1 = arc4random()%54; int t2 = arc4random()%54; int temp = data[t1]; int data1 = data[t1]; int data2 = data[t2]; data[t1] = data2; data[t2] = temp; } printf("洗牌之后:\n"); for (int i =0; i<54; i++) { printf("%d ",data[i]); } } void HelloWorld::Sort(int *array,int length) { int temp; bool hasExchangeAction; for (int i=0; i<length-1; i++) { hasExchangeAction = false; for (int j=0; j<length-i-1; j++) { int p1 = array[j]%13; int p2 = array[j + 1] % 13; if (p1 == 0) p1 = 14; if (p2 == 0) p2 = 14; if (p1 == 1) p1 = 15; if (p2 == 1) p2 = 15; if (array[j] == 52) p1 = 16; if (array[j + 1] == 52) p2 = 16; if (array[j] == 53) p1 = 17; if (array[j + 1] == 53) p2 = 17; if (p1 < p2) //相邻两个数进行比较,如果前面的数大于后面的数,则将这相邻的两个数进行互换 { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; hasExchangeAction = true; //发生过互换 } } if (!hasExchangeAction) { break; } } }
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 相互学习,共同进步 ===================
转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/17668009