C ++ landlords (distribution licensing algorithm)

Source: Internet
Author: User

C ++ landlords (distribution licensing algorithm)

The previous article introduced the general licensing ideas of landlords. Today I will share some difficult distribution and licensing algorithms. Thank you for your comments!

The requirement is as follows: at least one of three players in each game has a bomb and it is randomly issued.

The solution is as follows:

In fact, 54 cards can be defined as an array as follows (the element is a hexadecimal number, and the bracket is a decimal number)

Static int card_arr [] = {
0x01 (1 square A), 0x11 (17 plum blossom), 0x21 (33 peach), 0x31 (49 peach), //
0x02 (2 square 2), 0x12 (18 plum blossom), 0x22 (34 peach), 0x32 (50 peach), // 2
0x03 (3 square 3), 0x13 (19 plum blossom), 0x23 (35 peach), 0x33 (51 peach), // 3
0x04 (4 square 4), 0x14 (20 plum blossom), 0x24 (36 peach), 0x34 (52 peach), // 4
0x05 (5 blocks 5), 0x15 (21 plum blossom), 0x25 (37 peach), 0x35 (53 peach), // 5
0x06 (6 blocks 6), 0x16 (22 plum blossom), 0x26 (38 peach), 0x36 (54 peach), // 6
0x07 (7 square 7), 0x17 (23 plum blossom), 0x27 (39 peach), 0x37 (55 peach), // 7
0x08 (8 square 8), 0x18 (24 plum blossom), 0x28 (40 peach), 0x38 (56 peach), // 8
0x09 (9 square 9), 0x19 (25 plum blossom), 0x29 (41 peach), 0x39 (57 peach), // 9
0x0A (10 square 10), 0x1A (26 plum blossom), 0x2A (42 peach), 0x3A (58 peach), // 10
0x0B (11 square J), 0x1B (27 plum blossom), 0x2B (43 peach), 0x3B (59 peach), // J
0x0C (12 square Q), 0x1C (28 plum blossom), 0x2C (44 peach), 0x3C (60 peach), // Q
0x0D (13 square K), 0x1D (29 plum blossom), 0x2D (45 peach), 0x3D (61 peach), // K
0x0E (14 John), 0x0F (15 kings)
};

54 cards have, below we for such a deck can be seen: 1-13 numbers can represent the A-K, so we can use this base, leave the base of the bomb card, the other three cards with the same value will also be available.

In other words: first, 1-N bombs are randomly generated, and the base number of bombs is reserved. Then, the number of bombs that each player should allocate is randomly displayed, and the bombs are sent to the players, other cards are randomly issued.

See the specific code below:

/*************************************** **********************************//**** **************************** ************//************************** **************************************** * ******/# Include "stdafx. h "# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include // definition in this standard header file random_shuffle () is defined in the standard header file. // All STL algorithms are declared in the namespace std. Random_shuffle () has two parameters. The first parameter is the iterator pointing to the first element of the sequence. The second parameter is the next position of the last element of the sequence. Using namespace std; void main () {// store 54 card arrays int card_arr [54] =, }; srand (time (NULL) + 1); // initialize the random number transmitter to set the starting value of the random function int bomb_num = rand () % 3; if (bomb_num = 0) {bomb_num = 2;} // create three vector containers to hold the cards. Note that the containers are first-in-first-out. Vector
      
        New_arr_vc; vector
       
         Other_card_vc; vector
        
          Last_all_cards; new_arr_vc.clear (); // The bomb base card other_card_vc.clear (); // The card last_all_cards.clear () other than the bomb (); // pack all the cards that have been washed. // new_arr_vc 0-12 a total of 13 digits (player bomb base) for (int I = 0; I <13; I ++) {new_arr_vc.push_back (I);} random_shuffle (new_arr_vc.begin (), new_arr_vc.end (); // random function for (int I = 12; I> = 0; I --) {cout <
         
           = 17 & last_all_cards.size () <34) {// This judgment aims to reserve the location of the bomb if (last_all_cards.size () <B _num2*4 + 17) {// condition allocation bomb for (int j = 0; j <4; j ++) {last_all_cards.push_back (card_arr [new_arr_vc [bomb_tmp] * 4 + j]); cout <
          
            = 34 & last_all_cards.size () <51) {// This judgment aims to reserve the location of the bomb if (last_all_cards.size () <B _num3*4 + 34) {// condition allocation bomb for (int j = 0; j <4; j ++) {last_all_cards.push_back (card_arr [new_arr_vc [bomb_tmp] * 4 + j]); cout <
           
            


The test results are as follows: If you have any questions, you can ask me. If you have any better methods, you can talk about them. Thank you!


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.