cocos2dx 製作單機麻將(二),cocos2dx麻將
cocos2dx 製作單機麻將(二)
打亂麻將順序2
前面講解了如何打亂初始給定的麻將牌堆, 還有一種是打亂任意給定的麻將牌堆
//混亂撲克2
void RandAppointCardData(BYTE cbCardData[],BYTE cbMaxCount,BYTE OriginalData[]/*源牌堆資料*/)
{
//混亂撲克
BYTE cbRandCount=0,cbPosition=0;
do
{
cbPosition=rand()%(cbMaxCount-cbRandCount);
cbCardData[cbRandCount++]=OriginalData[cbPosition];
OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount];
} while (cbRandCount<cbMaxCount);
return;
}
下面是完整控制台代碼
//// main.cpp// MajiangLogicTest//// Created by TinyUlt on 14-8-16.// Copyright (c) 2014年 TinyUlt. All rights reserved.//#include <iostream>using namespace std;#define MAX_REPERTORY 144typedef unsigned char BYTE;typedef unsigned short WORD;//數組維數#ifndef CountArray#define CountArray(Array) (sizeof(Array)/sizeof(Array[0]))#endifconst BYTE m_cbCardDataArray[MAX_REPERTORY]={ 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x31,0x32,0x33,0x34,//風牌 0x31,0x32,0x33,0x34,//風牌 0x31,0x32,0x33,0x34,//風牌 0x31,0x32,0x33,0x34,//風牌 0x41,0x42,0x43,//箭牌 0x41,0x42,0x43,//箭牌 0x41,0x42,0x43,//箭牌 0x41,0x42,0x43,//箭牌 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,//花牌 };//混亂撲克static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount){ //混亂準備 BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//為什麼直接用MAX_REPERTORY? 因為這樣無耦合 memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷貝一份到臨時牌數組中 //混亂撲克(關鍵的核心打亂代碼) BYTE cbRandCount=0,cbPosition=0; do { cbPosition=rand()%(cbMaxCount-cbRandCount); cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition]; cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount]; } while (cbRandCount<cbMaxCount); return; }//混亂撲克2void RandAppointCardData(BYTE cbCardData[],BYTE cbMaxCount,BYTE OriginalData[]/*源牌堆資料*/){ //混亂撲克 BYTE cbRandCount=0,cbPosition=0; do { cbPosition=rand()%(cbMaxCount-cbRandCount); cbCardData[cbRandCount++]=OriginalData[cbPosition]; OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount]; } while (cbRandCount<cbMaxCount); return;}int main(int argc, const char * argv[]) { // insert code here... /*第一種混亂*/ //建立一個空牌堆 BYTE _cardData1[MAX_REPERTORY]; //把在該函數中建立並打亂牌堆,然後把指標傳給_cardData; RandCardData(_cardData1, MAX_REPERTORY); //輸出牌資料 for (int i = 0 ; i < MAX_REPERTORY; i++) { cout<<hex<<int(_cardData1[i])<<" "; } cout<<endl; /*第二種混亂*/ //建立一個空牌堆 BYTE _cardData2[MAX_REPERTORY]; //把在該函數中建立並打亂牌堆,然後把指標傳給_cardData; RandAppointCardData(_cardData2, MAX_REPERTORY,_cardData1); //輸出牌資料 for (int i = 0 ; i < MAX_REPERTORY; i++) { cout<<hex<<int(_cardData2[i])<<" "; } cout<<endl; return 0;}輸出:
25 13 1 3 21 43 54 14 9 12 13 8 31 24 13 31 6 4 28 31 34 18 7 27 15 18 51 11 42 12 28 2 57 25 16 4 33 15 18 21 42 33 29 41 25 3 23 55 14 41 27 22 34 21 2 9 29 19 43 23 22 22 19 34 16 15 32 58 6 28 17 21 18 8 43 28 33 32 6 33 2 25 14 11 29 19 26 13 4 24 53 52 16 15 27 3 27 31 9 1 26 22 3 32 17 26 26 7 12 42 41 32 17 8 7 9 34 8 7 16 17 41 19 5 29 2 23 6 4 24 42 24 1 56 11 1 12 5 23 11 14 43 5 5
16 56 21 7 28 14 41 12 16 24 43 21 31 26 3 53 52 7 12 34 51 14 9 29 23 33 12 31 14 6 16 18 54 21 25 58 19 5 7 28 32 34 1 27 27 33 6 14 9 17 25 33 28 11 17 24 43 2 22 6 23 3 11 42 2 18 3 4 42 4 18 55 25 42 22 32 4 15 8 29 24 13 6 26 19 9 41 25 7 8 1 13 11 15 41 43 57 16 33 18 32 27 1 8 12 31 4 5 27 22 26 23 31 2 5 17 26 13 19 43 17 21 42 5 3 19 23 15 28 15 8 24 9 29 13 32 34 2 34 41 11 29 22 1
Program ended with exit code: 0
麻將邏輯3. 初始化手牌
麻將的邏輯前提是有資料的支援, 所有有良好的資料存放區方式是很有必要的.
麻將的牌記錄一般採取比較通用的方法,即一個一維的數組, 長度是牌型的數量, 元素的值為牌的數量
例如
#define MAX_INDEX 42//最大索引
BYTE cbCardIndex[MAX_INDEX]
因為牌的類型共有42種 1萬-9萬 , 1筒-9筒, 1索-9索, 東南西北中發白(7),花牌 (春夏秋冬)梅蘭竹菊(8)
9+9+9+7+8 = 42.
如果每摸到一張 1萬 只要 cbCardIndex[0]++, 摸到 3萬 cbCardIndex[2]++ , 摸到東風就cbCardIndex[0x31]++嗎?
你會發現牌值是用16進位表示的(回顧第一講), 所有我們不能用cbCardIndex[牌值] 來表示該類型牌的數量
比如 我想得到手中1筒的數量 不能用cbCardIndex[0x11], 應該用cbCardIndex[9]. 所有我們應該要有個可以讓牌型值和索引互相轉換的函數
即 實現這樣的功能 cbCardIndex[func(9)] == cbCardIndex[0x11], 這樣我們就可以用牌型值來取該類型牌的數量了
直接上代碼了 不早了 該睡覺了
//// main.cpp// MajiangLogicTest//// Created by TinyUlt on 14-8-17.// Copyright (c) 2014年 TinyUlt. All rights reserved.//#include <iostream>using namespace std;#define MAX_REPERTORY 144typedef unsigned char BYTE;typedef unsigned short WORD;//數組維數#ifndef CountArray#define CountArray(Array) (sizeof(Array)/sizeof(Array[0]))#endif//邏輯掩碼#defineMASK_COLOR0xF0//花色掩碼#defineMASK_VALUE0x0F//數值掩碼#define MAX_INDEX42//最大索引#define MAX_COUNT14//最大數目const BYTE m_cbCardDataArray[MAX_REPERTORY]={ 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//萬子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子 0x31,0x32,0x33,0x34,//風牌 0x31,0x32,0x33,0x34,//風牌 0x31,0x32,0x33,0x34,//風牌 0x31,0x32,0x33,0x34,//風牌 0x41,0x42,0x43,//箭牌 0x41,0x42,0x43,//箭牌 0x41,0x42,0x43,//箭牌 0x41,0x42,0x43,//箭牌 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,//花牌 };//混亂撲克static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount){ //混亂準備 BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//為什麼直接用MAX_REPERTORY? 因為這樣無耦合 memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷貝一份到臨時牌數組中 //混亂撲克(關鍵的核心打亂代碼) BYTE cbRandCount=0,cbPosition=0; do { cbPosition=rand()%(cbMaxCount-cbRandCount); cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition]; cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount]; } while (cbRandCount<cbMaxCount); return; }//混亂撲克2void RandAppointCardData(BYTE cbCardData[],BYTE cbMaxCount,BYTE OriginalData[]/*源牌堆資料*/){ //混亂撲克 BYTE cbRandCount=0,cbPosition=0; do { cbPosition=rand()%(cbMaxCount-cbRandCount); cbCardData[cbRandCount++]=OriginalData[cbPosition]; OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount]; } while (cbRandCount<cbMaxCount); return;}//撲克轉換(索引->牌值)BYTE SwitchToCardData(BYTE cbCardIndex){ //assert(cbCardIndex<42); if(cbCardIndex<31) return ((cbCardIndex/9)<<4)|(cbCardIndex%9+1); if(cbCardIndex>=31&&cbCardIndex<=33) return(((cbCardIndex/7)<<4)+cbCardIndex%10 ); if(cbCardIndex>33) return(cbCardIndex+0x2F); //assert(false); return 0;}//撲克轉換(牌型->索引)BYTE SwitchToCardIndex(BYTE cbCardData){ // ASSERT(IsValidCard(cbCardData)); if((cbCardData&MASK_COLOR)<=0x30) return (((cbCardData&MASK_COLOR)>>4)*9+(cbCardData&MASK_VALUE)-1); if((cbCardData&MASK_COLOR)==0x40) return (31+(cbCardData&MASK_VALUE)-1); if((cbCardData&MASK_COLOR)==0x50) return (34+(cbCardData&MASK_VALUE)-1); //ASSERT(false); return 0;}int main(int argc, const char * argv[]) { // insert code here... /*第一種混亂髮*/ //建立一個空牌堆 BYTE _cardData1[MAX_REPERTORY]; //把在該函數中建立並打亂牌堆,然後把指標傳給_cardData; RandCardData(_cardData1, MAX_REPERTORY); //輸出牌資料 cout<<"混亂初始牌堆"<<endl; for (int i = 0 ; i < MAX_REPERTORY; i++) { cout<<hex<<"0x"<<int(_cardData1[i])<<" "; } cout<<endl; cout<<endl; /*第二種混亂髮*/ //建立一個空牌堆 BYTE _cardData2[MAX_REPERTORY]; //把在該函數中建立並打亂牌堆,然後把指標傳給_cardData; RandAppointCardData(_cardData2, MAX_REPERTORY,_cardData1); //輸出牌資料 cout<<"混亂指定牌堆"<<endl; for (int i = 0 ; i < MAX_REPERTORY; i++) { cout<<"0x"<<int(_cardData2[i])<<" "; } cout<<endl; cout<<endl; //虛擬一副手牌 開始遊戲時,每人13張手牌,然後庄家再摸一張牌即14張 //我們使用上面初始化好的牌堆,進行摸牌,假設只有一個玩家 BYTE cbCardIndex[MAX_INDEX]; for (int i = 0; i < MAX_COUNT; i++) { BYTE _cardValue = _cardData2[i];//得到牌堆中的牌 int _index = SwitchToCardIndex(_cardValue);//得到該牌對應的索引 cbCardIndex[_index]++;//該牌型加一 } cout<<"輸出手牌中所有牌型對應的數量"<<endl; for (int i = 0; i< MAX_INDEX; i++) { cout<<"0x"<<hex<<int(SwitchToCardData(i))<<":"<<dec<<(int)cbCardIndex[i]<<" ";//輸出手牌中所有牌型對應的數量 } cout<<endl; cout<<endl; return 0;}
輸出:
混亂初始牌堆
0x25 0x13 0x1 0x3 0x21 0x43 0x54 0x14 0x9 0x12 0x13 0x8 0x31 0x24 0x13 0x31 0x6 0x4 0x28 0x31 0x34 0x18 0x7 0x27 0x15 0x18 0x51 0x11 0x42 0x12 0x28 0x2 0x57 0x25 0x16 0x4 0x33 0x15 0x18 0x21 0x42 0x33 0x29 0x41 0x25 0x3 0x23 0x55 0x14 0x41 0x27 0x22 0x34 0x21 0x2 0x9 0x29 0x19 0x43 0x23 0x22 0x22 0x19 0x34 0x16 0x15 0x32 0x58 0x6 0x28 0x17 0x21 0x18 0x8 0x43 0x28 0x33 0x32 0x6 0x33 0x2 0x25 0x14 0x11 0x29 0x19 0x26 0x13 0x4 0x24 0x53 0x52 0x16 0x15 0x27 0x3 0x27 0x31 0x9 0x1 0x26 0x22 0x3 0x32 0x17 0x26 0x26 0x7 0x12 0x42 0x41 0x32 0x17 0x8 0x7 0x9 0x34 0x8 0x7 0x16 0x17 0x41 0x19 0x5 0x29 0x2 0x23 0x6 0x4 0x24 0x42 0x24 0x1 0x56 0x11 0x1 0x12 0x5 0x23 0x11 0x14 0x43 0x5 0x5
混亂指定牌堆
0x16 0x56 0x21 0x7 0x28 0x14 0x41 0x12 0x16 0x24 0x43 0x21 0x31 0x26 0x3 0x53 0x52 0x7 0x12 0x34 0x51 0x14 0x9 0x29 0x23 0x33 0x12 0x31 0x14 0x6 0x16 0x18 0x54 0x21 0x25 0x58 0x19 0x5 0x7 0x28 0x32 0x34 0x1 0x27 0x27 0x33 0x6 0x14 0x9 0x17 0x25 0x33 0x28 0x11 0x17 0x24 0x43 0x2 0x22 0x6 0x23 0x3 0x11 0x42 0x2 0x18 0x3 0x4 0x42 0x4 0x18 0x55 0x25 0x42 0x22 0x32 0x4 0x15 0x8 0x29 0x24 0x13 0x6 0x26 0x19 0x9 0x41 0x25 0x7 0x8 0x1 0x13 0x11 0x15 0x41 0x43 0x57 0x16 0x33 0x18 0x32 0x27 0x1 0x8 0x12 0x31 0x4 0x5 0x27 0x22 0x26 0x23 0x31 0x2 0x5 0x17 0x26 0x13 0x19 0x43 0x17 0x21 0x42 0x5 0x3 0x19 0x23 0x15 0x28 0x15 0x8 0x24 0x9 0x29 0x13 0x32 0x34 0x2 0x34 0x41 0x11 0x29 0x22 0x1
輸出牌堆中所有牌型對應的數量
0x1:0 0x2:0 0x3:0 0x4:0 0x5:0 0x6:0 0x7:1 0x8:0 0x9:0 0x11:0 0x12:1 0x13:0 0x14:1 0x15:0 0x16:2 0x17:0 0x18:0 0x19:0 0x21:2 0x22:0 0x23:0 0x24:1 0x25:0 0x26:1 0x27:0 0x28:1 0x29:0 0x31:1 0x32:0 0x33:0 0x34:0 0x41:1 0x42:0 0x43:1 0x51:0 0x52:0 0x53:0 0x54:0 0x55:0 0x56:1 0x57:0 0x58:0
Program ended with exit code: 0
天水麻將或者二報麻將網上可以玩?在什地方有下載的?
找到了,終於找到了,天水麻將(二報麻將)可以登陸到盛唐遊戲的官方網站上www.sntan.com進行註冊,就可以找到天水麻將的遊戲了。玩的人還不少。還送唐豆呢,真不錯@@@
這個單機麻將遊戲叫什?
是 麻將大師2
希望採納