Cocos2dx standalone Mahjong (6)

Source: Internet
Author: User

Cocos2dx standalone Mahjong (6)

Cocos2dx standalone Mahjong (6)

When the project is implemented, the project structure is not very beautiful. if you write in this structure-oriented mode, it will be very messy. in this chapter, we will discuss how to write mahjong in an object-oriented manner.

Standard Mahjong is divided into server module, client module, and logic module. the server module must be supported by the logic module. The client module rarely uses the logic module. Most of the results are the judgment results sent from the server. If necessary, the logic module is used. because the logical modules need to be shared, all interfaces called in the interface are called, and the result values required by the server and client are returned.

For example

Client trigger starts to happen to the server> server starts game> card heap compiled through logic module> server gets the player's hand card through card heap and rule calculation> sends the hand card data to the specified client> the client sends the desired card to the server based on the card data display card> User cards> the client sends the card to the server. The server uses the logic module to determine whether the card can be played and whether there are any operations such as eating or touching the bar., if you can play the game (if you have an operation card),> send the card that can be played to the client.> the client obtains the card and displays the card play animation.

Therefore, the client is only responsible for receiving the messages to be drawn, and the logic is generally handed over to the server. The client logic is generally used to determine which messages are sent by the server and draw the interface based on different messages.

Now think about the server module, client module, and logic module. Which of the following is the most difficult?

The most difficult part is the logic module, followed by the client, and the simplest part is the server. the simplest reason for the server is that it is only responsible for sending data. The logic judgment requires calling the logic module and storing the card data of all players. the client may also need to draw each player card, so it also needs to store the player card data.

Each time the project content is added, the server only needs to add a value to store the added content, and the logic and client need to change a lot.


In our project, because there is no server, we need to allow the client module to directly interact with the logic module (although the client is only a console window). First, we should separate the game logic module, in the logic class. the client module should be organized in an object-oriented manner

The client in this project is relatively simple. There is only one player object and a card heap object.

Currently, there are few attributes and methods in these objects.

Added several classes and files


/// Pai_sparrow.h // MajiangLogicTest /// Created by TinyUlt on 14-8-17. // Copyright (c) 2014 TinyUlt. all rights reserved. // # ifndef # define MAX_REPERTORY 144 typedef unsigned char BYTE; typedef unsigned short WORD; // Array dimension # ifndef CountArray # define CountArray (Array) (sizeof (Array) /sizeof (Array [0]) # endif // logical mask # defineMASK_COLOR0xF0 // Color Mask # defineMASK_VALUE0x0F // numerical mask # define MAX_INDEX42 // maximum index # define MAX_COUNT14 // maximum number # define GAME_PLAYER4 // number of players # endif

//// CPile. h // MajiangLogicTest /// Created by TinyUlt on 14-8-17. // Copyright (c) 2014 TinyUlt. all rights reserved. // # ifndef _ MajiangLogicTest _ CPile __# define _ MajiangLogicTest _ CPile __# include
 
  
# Include "pai_sparrow.h" class CPile {public: CPile (); private: // card heap BYTE m_cardPile [max_reperile]; // The position BYTE m_currentIndex; public: // obtain the card heap Data BYTE * getCardPile (); // obtain a Card BYTE takeOnCard (); // obtain the Card Data BYTE getCardDataByIndex (BYTE index) based on the index ); // obtain the number of cards in the current card heap. BYTE getCount () ;};# endif/* defined (_ MajiangLogicTest _ CPile __)*/
 

//// CPile. cpp // MajiangLogicTest /// Created by TinyUlt on 14-8-17. // Copyright (c) 2014 TinyUlt. all rights reserved. // # include "CPile. h "CPile: CPile () {m_currentIndex = 0;} BYTE * CPile: getCardPile () {return m_cardPile;} BYTE CPile: getCardDataByIndex (BYTE index) {return m_cardPile [index];} BYTE CPile: takeOnCard () {return m_cardPile [m_currentIndex ++];} BYTE CPile: getCount () {return max_reper1_- m_currentIndex ;}

/// CUser. h // MajiangLogicTest /// Created by TinyUlt on 14-8-17. // Copyright (c) 2014 TinyUlt. all rights reserved. // # ifndef _ MajiangLogicTest _ CUser __# define _ MajiangLogicTest _ CUser __# include
 
  
# Include "pai_sparrow.h" class CUser {public: // container for storing Card Data BYTE m_cardsIndexStore [MAX_INDEX] ;};# endif/* defined (_ MajiangLogicTest _ CUser __) */
 

//// CGameLogic. h // MajiangLogicTest /// Created by TinyUlt on 14-8-17. // Copyright (c) 2014 TinyUlt. all rights reserved. // # ifndef _ MajiangLogicTest _ CGameLogic __# define _ MajiangLogicTest _ CGameLogic __# include
 
  
# Include "pai_sparrow.h" class CGameLogic {// variable definition protected: static const BYTEm_cbCardDataArray [MAX_REPERTORY]; // poker data public: static const char * m_cbCardWordArray [MAX_INDEX]; // Chinese Poker public: // chaotic poker static void RandCardData (BYTE cbCardData [], BYTE cbMaxCount); // chaotic Poker 2 static void RandAppointCardData (BYTE cbCardData [], BYTE cbMaxCount, BYTE OriginalData []/* source card heap data */); // poker conversion (index-> card value) static BYTE SwitchToCardData (BYTE cbCardIndex ); // poker conversion (card type-> index) static BYTE SwitchToCardIndex (BYTE cbCardData ); // convert poker to static BYTE SwitchToCardData (BYTE cbCardIndex [MAX_INDEX]/* input table for counting the number of all cards */, BYTE cbCardData [MAX_COUNT]/* outgoing hand card data */); // Delete poker static bool RemoveCard (BYTE cbCardIndex [MAX_INDEX], BYTE cbRemoveCard); // obtain the card index static int getIndexByWord (const char * ch) based on the Chinese card ); // obtain the Chinese brand static const char * getWordByIndex (int index) based on the card index; // obtain the Chinese brand static const char * getWordByData (BYTE cbCardData) based on the card value ); // rotation player static int changePlayer (int id) ;};# endif/* defined (_ MajiangLogicTest _ CGameLogic __)*/
 
//// CGameLogic. cpp // MajiangLogicTest /// Created by TinyUlt on 14-8-17. // Copyright (c) 2014 TinyUlt. all rights reserved. // # include "CGameLogic. h "// poker data const BYTE CGameLogic: m_cbCardDataArray [max_reper1_] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, // Wanzi 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, // Wanzi 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, // Wanzi 0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x X 09, // x 19, // same as 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as 0x24, 0x25, 0x26, 0x27,0 x, 0x29, // suo Zi 0x24, 0x25, 0x26, 0x27,0x28, 0x29, // suo Zi 0x24, 0x25, 0x26, 0x27,0x28, 0x29, // suo Zi 0x24, 0x25, 0x26, 0x27, 0x 28, 0x29, // 0x32, 0x34, // 0x34, 0x34, // 0x34, // 0 x, // 0x43, // Wrigley 0x41,0x42,0x43, // Wrigley 0x41,0x42,0x43, // Wrigley 0x41,0x42,0x43, // Wrigley 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58, // card}; const char * CGameLogic :: m_cbCardWordArray [MAX_INDEX] = {"10 thousand", "20 thousand", "30 thousand", "40 thousand", "50 thousand", "60 thousand", "70 thousand", "80 thousand ", "90 thousand", "one cylinder", "two cylinder", "three cylinder", "Four Cylinder", "Five cylinder", "Six cylinder ", "Eight cylinder", "Nine cylinder", "one Suo", "Two Suo", "Three Suo" "," Four Suo "," Five Suo "," Six Suo "," Seven Suo "," eight Suo "," Nine Suo "," East "," South ", "West", "North", "medium", "fat", "White", "Spring", "Summer", "Autumn", "Winter", "Mei ", "Lan", "zhu", "Chrysanthemum"}; void CGameLogic: RandCardData (BYTE cbCardData [], BYTE cbMaxCount) {// confused preparation BYTE cbCardDataTemp [CountArray (m_cbCardDataArray)]; // Why directly use max_reperarray? Because of this non-coupling memcpy (cbCardDataTemp, m_cbCardDataArray, sizeof (m_cbCardDataArray); // copy a copy to the temporary card array // chaotic poker (key core disrupting code) BYTE cbRandCount = 0, cbPosition = 0; do {cbPosition = rand () % (cbMaxCount-cbRandCount); cbCardData [cbRandCount ++] = cbCardDataTemp [cbPosition]; cbCardDataTemp [cbPosition] = cbCardDataTemp [cbMaxCount-cbRandCount];} while (cbRandCount
 
  
Card value) BYTE CGameLogic: 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 ;}// poker conversion (card type-> index) BYTE CGameLogic :: 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 ;} // poker conversion BYTE CGameLogic: SwitchToCardData (BYTE cbCardIndex [MAX_INDEX]/* input table for counting the number of all cards */, BYTE cbCardData [MAX_COUNT]/* outgoing hand data */) {// convert poker BYTE cbPosition = 0; for (BYTE I = 0; I
  
   
0); // Delete poker if (cbCardIndex [cbRemoveIndex]> 0) {cbCardIndex [cbRemoveIndex] --; return true;} // failure validation // ASSERT (FALSE ); return false;} // rotation player int CGameLogic: changePlayer (int id) {id + = 1; if (id> = GAME_PLAYER) {id = 0;} return id ;}
  
 

//// Main. cpp // MajiangLogicTest /// Created by TinyUlt on 14-8-16. // Copyright (c) 2014 TinyUlt. all rights reserved. /// * the most basic Mahjong logic sets the number of mahjong players as needed. The basic process is to initialize the card heap, judge the banker's players 0 touch 13 cards players 1 touch 13 cards players 2 touch 13 cards Players 3 touch 13 card dealers touch a card dealer play a card touch the card and play the card at home. Touch the card at home. The dealer finds the card at home ....... the number of cards heap is 0. Other functions are terminated, such as uploading and listening. */# include
 
  
# Include "CGameLogic. h "# include" CPile. h "# include" CUser. h "using namespace std; // chaotic poker int main (int argc, const char * argv []) {// insert code here... // create a checkpoint heap CPile _ pile; // create a player CUser _ users [GAME_PLAYER]; // the number of times that a player is played. BYTE _ outCardTime = 0; // banker user index BYTE _ bankerId = rand () % GAME_PLAYER;/* first confusion Method * // obtain card heap data CGameLogic: RandCardData (_ pile. getCardPile (), max_reperile); // output card heap data cout <"chaotic initial card Heap" <
  
   
0) {// number of times a card is issued cout <"Total number of times a card is issued:" <
   <<(int)_outcardtime<
    
> Ch; int _ outCardIndex = CGameLogic: getIndexByWord (ch); if (_ outCardIndex =-1) {cout <"incorrect input. Please enter it again" <
    
     



<<(int)_outcardtime<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.