Licensing and shuffling of landlords Algorithms

Source: Internet
Author: User

Implementation of Basic Algorithms for landlords

By-wojiushi3344


QQ: 513670524

Reprinted, please describe the source


Source code download

PS: First of all, I wish my friends a happy day !! I have nothing to worry about. Today I want to write about the basic implementation of the landlords game. It is not well written. Don't try it !! For specific implementation, see the source code. If you have better suggestions, you can leave a message on my blog. Thank you!

Blog: http://blog.csdn.net/wojiushi3344

 Licensing Animation:

Principle: The timer is updated after a while, and then the picture is updated to different locations to implement the landlord licensing animation.

Specific implementation: Use the junior high school mathematics knowledge to determine a straight line (y = kx + B) at two points ).

We can see that P points and A, B, and C points are connected to three different straight lines. To implement the licensing animation, we need to first move the cards from point P to A, B, and C. Then, update the coordinates of the points according to the direction of the arrows on the way, and execute them in sequence until three cards are generated, and the animation ends.

Define three vectors to store the coordinates of the issued cards.

Vector <card_coor> player_a;

Vector <card_coor> player_ B;

Vector <card_coor> player_c;

 Specific Code implementation:

Void cgame: calculatetwopoint (float X1, float Y1, float X2, float Y2) // calculate the line segment between two points {m_k = (y1-y2)/(x1-x2 ); m_ B = y1-x1 * m_k ;}

Calculate the values of K and B Based on the coordinates of the two points.

Plotting uses Y coordinates to represent X coordinates. The advantage is that when we update Y coordinates, X coordinates are also updated to achieve the desired effect.

m_dcBuffer.TransparentBlt((m_coor_y-m_b)/m_k,m_coor_y,80,105,&m_dcImage,80*2,4*105,80,105,RGB(255,0,255));

Finally, we only need to set a timer to update the value of Y at intervals.

When p arrives at any point A, B, and C during the update, the coordinates of the cards are saved to the corresponding vector, you can draw a three-party card based on the value of the vector.

 Landlord shuffling implementation:

First, upload an image.

Do you know there are 54 cards for landlords? If you know, that's good. You can go to the following link. Analyze the card splitting ideas.

First, we can define a one-dimensional array with 54 cards.

Int cardvaluearray [54];

Note: a detailed understanding of the meaning below is very important to the sub-card. We use 54 elements to represent different cards.

Cardvaluearray [0--------12]: Block A --------- block K

Cardvaluearray [13--------25]: plum blossom a --------- plum blossom K

Cardvaluearray [26-------38-]: red heart a --------- Red Heart K

Cardvaluearray [39--------51]: Black peach A --------- black peach K

Cardvaluearray [52] Little Devil cardvaluearray [53] Big ghost

I think you should understand what I should do next! ()

We initialize cardvaluearray [54] To 0------53 in sequence. Then, we disrupt the array value and divide the array into four parts. Three copies of 17 images, one copy of 3 (GUN landlord card ). Distribute three 17 cards to three different players in sequence.

The difficulty here is to assign values to the array.

There are many methods.

First:

One of the most direct ways we can think of is also the most efficient method. It is also the method used in my program. (Oh, It's stupid !)

Defines a one-dimensional array with 54 elements assigned a value of-1. Then the number between 0------54 is randomly generated, and then the generated number is determined whether it already exists in the array. If it does not exist, it is saved to the array. if it already exists, it is regenerated until all the 54 numbers appear.

Second:

For example, define a one-dimensional number with 54 elements, assign values to 1------53 in sequence, and then random two numbers ranging from 0 to 53, after comparing the numbers at these two locations, the efficiency is not particularly high, but it is better than the first one.

Third approach:

Define a one-dimensional number with 54 elements. Values are assigned to 1------53 at random for 54 times. A number is randomly generated at a time and exchanged with the number at the position I.

   int  CardValueArray[54];   for(int i=0;i<54;i++)   {    CardValueArray[i]=i;   }   for (int i=0;i<54;i++)    {   swap( CardValueArray[i], CardValueArray[(rand()%54)]);  }

For example, first random out of a position, exchange with the first digit, then random out of a location, and exchange with the second digit for example, first random out of 10, then the tenth digit, exchange with the first number, then randomly generate a 12-12th number, exchange with the first number, and then randomly exchange a number with the third one.

Fourth:

Use rand_shuffle to randomize sequence elements.

C ++ provides a better solution, that is, the random_shuffle () algorithm. Don't worry. I will show you how to use this algorithm to generate different types of random numbers.

The best way to generate a random element set within a specified range is to create an ordered sequence (that is, a vector or a built-in array) that contains all values in the specified range. For example, if you want to generate 54 numbers between 0 and 54, create a vector and fill the vector with 54 numbers in ascending order:

# Include <vector> Using STD: vector; int main () {vector <int> VI; For (INT I = 0; I <10; I ++) {vi. push_back (I);/* The current vector contains 54 integers between 0 and 54 in ascending order */}

After filling the vector, use the random_shuffle () algorithm to disrupt the order of elements. Random_shuffle () is defined in the standard header file <algorithm. h>. Because

All STL algorithms are declared in the namespace STD:, so pay attention to correct data types. Random_shuffle () has two parameters. The first parameter is the iterator pointing to the first element of the sequence, and the second parameter is pointing to the next position of the last element of the sequence. The following code segment uses the random_shuffle () algorithm to disrupt elements previously filled in the vector:

# Include <algorithm> Using STD: random_shuffle; random_shuffle (VI. Begin (), VI. End ();/* disrupt elements */

You can select either of the above four methods to generate 54 unique and plural numbers between 0--54.

In this way, the values in the cardvaluearray array are assigned a value through the above method. Now we only need to use the values in the array to cut the corresponding card in the big image.

We can use a formula to represent the X and Y coordinates of each card in the big image:

X = cardvaluearray [I] % 13 * width of each card

Y = cardvaluearray [I]/13 * Height of each card

 

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.