/ ************************************************** ************** * File Name: Hu.cpp * * * * Function: Demonstrate a simple recursive function - judgment [Mahjong] and card. * * *: This function does not judge seven pairs and thirteen, readers are not difficult to judge; * * At the same time, due to the different rules of mahjong, please add and modify it yourself. * * * * 2. Other games similar to mahjong, such as [word] (also known as run beard, * * two seventy), etc., you can also use a similar judgment function. * * * * Environment: VC 6.0, but comply with ANSI C standard, casually transplant ^ * * * * author: ShyWorm (afraid) * * E_mail: ShyWorm@sina.com * ****** *********************************************************** ******* / # include
INT Hu (int Pai [38]); int Remain (int Pai [38]);
INT main () {// put a panel in the array below, can be filled in the number to test the function correctly or not. / / For convenience, PAI [0], PAI [10], PAI [20], PAI [30] is not used, and must // is set to 0, pay attention! INT PAI [38] = {0, 1, 1, 1, 0, 1, 1, 1, 0, 0, // PAI [1- 9] 万 万 ~ 0, 0, 0, 0 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, / / PAI [21-29] The number of 0, 0, 1, 1, 1, 0, 0, 0 //, northwest of the southeast is};
// Please exclude "seven pairs" and "thirteen", because it is simple, so no // if (qidui (pai)) ... // IF (SHISANYAO (PAI)) ...
IF (HU (PAI) Printf ("Ha! I and A"); Else Printf ("Hey, and Not! / N"); Return 1;}
// Judgment the recursive function of the card, do not consider "seven pairs" and "thirteen". Because if // puts "seven pairs" and "thirteen" judgment in the recursive function, it will be lost. INT Hu (int Pai [38]) {static int jiang = 0; // will "two" in the brand "three three three three two"
IF (! Remain (PAI) RETURN 1; // Removing exit condition: If there is no left, it returns to the card.
For (int i = 1;! Pai [i] && i <38; i ); // Find a place to have a card, i is the current card, PAI [i] is a number
Printf ("i =% D / N", I); // Tracking information
// 4 combination (bar) IF (PAI [I] == 4) // If the current number is equal to 4 {PAI [i] = 0; // All 4 card IF (Hu (PAI) Return 1; // If the remaining card combination is successful, the card PAI [i] = 4; / / Otherwise, cancel 4 combinations}
// 3 combination (large pair) IF (PAI [i]> = 3) // If the current card is not less than 3 {PAI [i] - = 3; // minus 3 card IF (Hu (PAI) )) RETURN 1; // If the remaining card combination is successful, the card PAI [i] = 3; // Cancel 3 combination}
// 2 combinations (billboard) IF (! Jiang && pai [i]> = 2) // If there is no card before, and the current card is not less than 2 {jiang = 1; // Set the card sign PAI [I] - = 2; // minus 2 cards IF (Hu (PAI)) Return 1; // If the remaining card combination is successful, the card PAI [i] = 2; // Cancel 2 combination Jiang = 0; // Clear the signing sign}
IF (i> 30) Return 0; // "White" in the northwest of southeastern "without successful combination, not
// Shun card combination, pay attention to the combination of going back! IF (i% 10! = 8 && i% 10! = 9 && // Exclusion of the value of 8 and 9 PAI [i 1] && Pai [i 2]) // If there is two consecutive cards { PAI [I] -; PAI [i 1] -; PAI [i 2] -; // Dimension of each brand 1 if (hu (pai)) Return 1; // If the remaining card combination is successful , Harmonic PAI [i] ; PAI [i 1] ; PAI [i 2] ; // Restore the number of copies} // Unable to all combination, not,! Return 0;}
/ / Check the remaining brand INT Remain (int Pai [38]) {int sum = 0; for (int i = 1; i <38; i ) SUM = PAI [I]; return sum;