PHP array for details. With the development of the times, we have more and more programming technologies. for PHP, everyone must be familiar with it. let's introduce the PHP array here. PHP array is the most common batch data storage. with the development of the times, more and more programming technologies are available. for PHP, you must be familiar with it. let's introduce the PHP array here. PHP arrays are the most common batch data storage structures. Because arrays are stored in sequence, they can be stored randomly conveniently. In many languages, arrays are static and must be specified before use. in PHP, you do not have to worry about this problem. arrays are completely dynamic. You can even use an array like this.
- PHP CODE
- // DEMO of Array
- demo_array[] = 1;
- demo_array[] = 2;
Next we will use the PHP array to simulate the shuffling of mahjong, a sports favorite of the majority of Chinese working people. Here we will popularize the Mahjong rules, which will be useful when serving MM's mother later :). First of all, mahjong is intended for 4 people, but it cannot be played by 3 p. There are a total of 108 cards. after you touch the cards, there are 14 at the dealer's house, and 13 at the other three, with 55 cards left. (There are N ways to play mahjong in China. here we are talking about Easy home games. it is the simplest way to play mahjong games that I have ever seen .) Then let's analyze how to use a random number to simulate this process. this kind of practice can be done, because extracting a mathematical model from a specific example is the basic skill of programming. First, we use an array with a length of 108 to store these 108 cards. during initialization, we may assume they are discharged sequentially.
Then we will simulate the shuffling process, which is actually a process of two card exchanges several times. We generate two ~ The random number between 108, and then the value of the array element with these two numbers as index is exchanged. We can define this process as a function for multiple exchanges. Next, we will distribute the 108 cards to four people. In fact, as long as the number of exchanges on the front side is large enough, it will be OK to cut them into five parts directly. However, in order to better simulate the reality and present the characteristics of the array, we adopt a circular distribution method. That is, each person has four images at a time, one for each person, and two for the banker. Well, the whole process is like this. Below we will explain some more details through the program.
- PHP CODE
-
Php
- /*************************************** ***********************
- * An example of using arrays to simulate mahjong shuffling and card issuing processes
- * @ Easy@bjpeu.edu.cn
- * @ 2003-6-3
- **************************************** ***********************/
-
- // Card swap function
- Function swap ()
- {
- // Define $ majiangArray as a global variable
- // You can perform operations in the function.
- Global $ majiangArray;
-
- // Generate two random numbers ranging from 1 to 108
- $Index1=Rand(1,108 );
- $Index2=Rand(1,108 );
-
- // Exchange the values of corresponding array elements
- $Tmp= $ MajiangArray [$ index1];
- $ MajiangArray [$ index1] = $ majiangArray [$ index2];
- $ MajiangArray [$ index2] = $ tmp;
- }
-
- // Functions that represent the array content with the corresponding image
- Function showArray ($ arrayName)
- {
- $CountCount= Count ($ arrayName );
- For ($I=0; $ I<$ Count; $ I ++)
- {
- Echo'<Img Src="Img/'. $ arrayName [$ I].'">'; // Display the corresponding image
- }
- Echo'<P>'; // Line feed
- }
-
- // The main program starts.
-
- // Initialize the array
- Unset ($ majiangArray );
- $ MajiangArray [] = '0'; // data is stored from 1, skipping 0
- For ($I=1; $ I<= 3; $ I ++) // There are three colors in total
- {
- For ($J=1; $ J<= 9; $ j ++) // There are nine cards in a suit
- {
- For ($K=1; $ K<= 4; $ k ++) // Each card has four
- {
- $ MajiangArray [] = $ I. '_'. $ j. '.gif ';
- }
- }
- }
-
- // Start shuffling
- For ($I=1; $ I<= 1000; $ I ++) // The cyclic variable I is re-assigned and reused.
- {
- Swap ();
- }
-
- // Initialize four users
- Unset ($ user1 );
- Unset ($ user2 );
- Unset ($ user3 );
- Unset ($ user4 );
-
- // $ Seek is the progress pointer. The record should be licensed from there.
- $Seek=1;
-
- For ($I=1; $ I<= 3; $ I ++) // three times for a total of four
- {
- For ($J=1; $ J<= 4; $ j ++) // four people
- {
- For ($K=1; $ K<= 4; $ k ++) // Four at a time
- {
- $Name='User'. $ J;
- $ {$ Name} [] = $ majiangArray [$ seek ++];
- }
- }
- }
-
- // Then one person
- For ($I=1; $ I<= 4; $ I ++)
- {
- $Name='User'. $ I;
- $ {$ Name} [] = $ majiangArray [$ seek ++];
- }
-
- // The Banker finally finds one more
- $ User4 [] = $ majiangArray [$ seek];
-
- // The result of the sub-card ending Display
- ShowArray ($ user1 );
- ShowArray ($ user2 );
- ShowArray ($ user3 );
- ShowArray ($ user4 );
-
- ?>
Bytes. PHP array is the most common batch data storage...