Replace all cards in mahjong with numbers ：

ten thousand ~ ninety thousand    1~9

A tube ~ Nine tubes    11~19

One ~ Nine    21~29

east , south , west , north , in , hair , white    31~37

Types of Hu cards ： Yes, yes, Hu , Thirteen Yaohe 33332

The process of judging Hu cards （ Here are the rules , It's all about the hand, which has been sorted from small to large ）：

1, First, judge whether the total number of hands is 3n+2, If not, it's definitely not a Hu .

2, Start with something special , If the hand is 14 Zhang's time , It could be “ Yes, yes, Hu ” perhaps “ Thirteen one ”, First judge whether it is the two special Hu cards , If not , Continue with the following judgment .
Exclusion of special circumstances , So Hu , It can only be “33332”.

3, Traverse all hands , Find a number of cards >=2 Of , that “33332” Medium “2” It's possible to draw from this card 2 Zhang as this “2”, And then put this 2 Zhang Chuo , The rest of the hand is 3n Zhang , Judge the rest 3n Can a card form a straight or a dark cut , If you can make it all , That means the card is dead . Otherwise you need to find the next card as “2”, Repeat the above judgment , Until we find it , If you can't find it, you won't .

Judge the rest 3n Can a card form a straight or a dark cut , Is by judging the number of the first card ：

① If it is 1 Zhang or 2 Zhang , If you want a Hu card , It has to form an order with the cards behind it , No, you can't . if possible , Remove the cards that make up the order , Then deal with the rest of the cards .

② If it is 3 Zhang , There are two possibilities for a Hu card , One is to put 3 Zhang regarded it as a dark carving , The other is this 3 Zhang Du forms an order with the cards behind it . If you're just judging whether you're playing Hu or not , They're both the same , If you want to calculate the score , You have to separate yourself .

③ If it is 4 Zhang , If you want a Hu card , Yes 1 The card and the back of the card to form an order , The rest 3 Cards can go on ② operation .

The whole process can be called recursively through the function , One at a time , If you can't make it into a straight or dark engraving, you can't make it , Direct return error , If you can, continue to judge , Until the number of cards is 0, That's Hu , Return to success .

Here's what I use OC Hu card algorithm of writing , Preliminary verification is OK , There may be loopholes , It will be modified in the future , If you find a problem , You can also leave a message , I'll fix the update .

// Check the hand for Hu rules - (BOOL)checkHuWithHandCards:(NSArray *)handCards { //
Only the hand is 14 Zhang , It could be “ Yes, yes, Hu ” perhaps “ Thirteen one ” if (handCards.count == 14) { // Yes, yes, Hu BOOL isDuiDuiHu
= true; NSMutableArray *ArrM1 = [NSMutableArray arrayWithArray:handCards];
while (ArrM1.count && isDuiDuiHu) { if (ArrM1[0] == ArrM1[1]) { [ArrM1
removeObjectAtIndex:0]; [ArrM1 removeObjectAtIndex:0]; } else { isDuiDuiHu =
false; break; } } if (isDuiDuiHu) { return true; } // Thirteen one BOOL isShiSanYao =
false; NSArray *shiSanYaoArr = @[@1, @9, @11, @19, @21, @29, @31, @32, @33,
@34, @35, @36, @37]; NSMutableArray *arrM2 = [NSMutableArray
arrayWithArray:handCards]; for (int j = 0; j < handCards.count - 1; ++j) { if
(handCards[j] == handCards[j + 1]) { [arrM2 removeObjectAtIndex:j]; break; } }
for (int i = 0; i < shiSanYaoArr.count; ++i) { isShiSanYao = false; if
([arrM2[i] intValue] == [shiSanYaoArr[i] intValue]) { isShiSanYao = true; }
else { break; } } if (isShiSanYao) { return true; } } // Other Hu cards （3,3,3,3,2） return
[self checkHu33332WithCards:[self changeHandCards:handCards]]; } //
Other Hu cards （3,3,3,3,2） - (BOOL)checkHu33332WithCards:(NSArray *)cards { for (int i =
0; i < cards.count; ++i) { if ([cards[i][1] intValue] >= 2) { NSMutableArray
*arrM = [NSMutableArray arrayWithArray:cards]; // Find a pair of cards first , Remove and continue with the following judgment if
([cards[i][1] intValue] == 2) { [arrM removeObjectAtIndex:i]; } else { [arrM
replaceObjectAtIndex:i withObject:@[cards[i][0], [NSNumber
numberWithInt:[cards[i][1] intValue] - 2]]]; } if ((arrM.count && [self
checkForCards:arrM]) || !arrM.count) return true; } } return false; } //
According to the number of cards in the first card , Classification inspection - (BOOL)checkForCards:(NSArray *)cards { switch ([cards[0][1]
intValue]) { case 1: case 2: if (![self checkFirstCard1Or2:cards]) return
false; break; case 3: if (![self checkFirstCard3:cards]) return false; break;
case 4: if (![self checkFirstCard4:cards]) return false; break; default: break;
} return true; } // The number of the first card is 1 or 2 - (BOOL)checkFirstCard1Or2:(NSArray *)cards {
// It can only form an order with the cards behind it , Or it won't work if (cards.count >= 3 && [cards[0][0] intValue] ==
[cards[1][0] intValue] - 1 && [cards[0][0] intValue] == [cards[2][0] intValue]
- 2) { NSMutableArray *arrM = [NSMutableArray arrayWithArray:cards]; for (int i
= 2; i >= 0; --i) { if ([cards[i][1] intValue] > 1) { [arrM
replaceObjectAtIndex:i withObject:@[cards[i][0], [NSNumber
numberWithInt:[cards[i][1] intValue] - 1]]]; } else { [arrM
removeObjectAtIndex:i]; } } if ((arrM.count && [self checkForCards:arrM]) ||
!arrM.count) return true; } return false; } // The number of the first card is 3 -
(BOOL)checkFirstCard3:(NSArray *)cards { // Can be combined with the cards behind the order , Here for convenience , It's just a dark carving
NSMutableArray *arrM = [NSMutableArray arrayWithArray:cards]; [arrM
removeObjectAtIndex:0]; if ((arrM.count && [self checkForCards:arrM]) ||
!arrM.count) return true; return false; } // The number of the first card is 4 -
(BOOL)checkFirstCard4:(NSArray *)cards { // First 3 Zhang as a dark carving , Go on to the next step NSMutableArray
*arrM = [NSMutableArray arrayWithArray:cards]; [arrM replaceObjectAtIndex:0
withObject:@[cards[0][0], [NSNumber numberWithInt:1]]]; return [self
checkForCards:arrM]; } // Convert hand array to 2D array ,0, value 1, number - (NSArray
*)changeHandCards:(NSArray *)handCards { NSMutableArray *cardsArrM =
[NSMutableArray array]; int count = 1; for (int i = 0; i < handCards.count;
++i) { if (i == handCards.count - 1) { [cardsArrM addObject:@[handCards[i],
[NSNumber numberWithInt:count]]]; break; } if (handCards[i] == handCards[i +
1]) { count++; } else { [cardsArrM addObject:@[handCards[i], [NSNumber
numberWithInt:count]]]; count = 1; } } return cardsArrM; } // Count how many cards are in the array -
(int)cardsCount:(NSArray *)cards { int count = 0; for (int i = 0; i <
cards.count; ++i) { count = count + [cards[i][1] intValue]; } return count; }

Check the plate ：

NSArray *arr1 = @[@1, @9, @11, @19, @21, @21, @29, @31, @32, @33, @34, @35,
@36, @37]; NSArray *arr2 = @[@1, @1, @11, @11, @21, @21, @31, @31, @33, @33,
@35, @35, @37, @37]; NSArray *arr3 = @[@1, @1,
@21,@22,@23,@24,@25,@26,@27,@28,@29,@13,@14,@15]; NSArray *arr4 = @[@7, @8, @9,
@21, @22, @23, @23, @23, @24, @25, @26, @13, @14, @15]; NSLog(@"----%@", [self
checkHuWithHandCards:arr1]?@" Hu ":@" No "); NSLog(@"----%@", [self
checkHuWithHandCards:arr2]?@" Hu ":@" No "); NSLog(@"----%@", [self
checkHuWithHandCards:arr3]?@" Hu ":@" No "); NSLog(@"----%@", [self
checkHuWithHandCards:arr4]?@" Hu ":@" No ");

Technology
Daily Recommendation
views 5
views 2