src/canonical_cards.cpp
// This version does not resort the cards
// Returns true if a change was made
This version does not resort the cards
Returns true if a change was made
This version does not resort the cards Returns true if a change was made
bool CanonicalCards::ToCanon2(const Card *cards, int num_cards, int suit_groups,
			      Card *canon_cards) {
  int num_suits = Game::NumSuits();
  bool change_made = false;
  if (num_cards <= 0) return false;
  int *rank_used = new int[num_suits];
  int nsg = suit_groups;
  for (int i = 0; i != num_suits; ++i) rank_used[i] = 0;
  if (canon_cards != cards) {
    for (int i = 0; i < num_cards; ++i) {
      canon_cards[i] = cards[i];
    }
  }
  for (int i = 0; i < num_cards; ++i) {
    int old_suit = Suit(canon_cards[i]);
    int new_suit = ((char *)&nsg)[old_suit];
    int rank = Rank(canon_cards[i]);
    rank_used[new_suit] |= 1 << rank;
    if (new_suit != old_suit) {
      canon_cards[i] = MakeCard(rank, new_suit);
      change_made = true;
      for (int j = i + 1; j < num_cards; ++j ) {
	if (Suit(canon_cards[j]) == old_suit) {
	  rank = Rank(canon_cards[j]);
	  canon_cards[j] = MakeCard(rank, new_suit);
	} else if (Suit(canon_cards[j]) == new_suit) {
	  rank = Rank(canon_cards[j]);
	  canon_cards[j] = MakeCard(rank, old_suit);
	}
      }
    }
    for (int m = 0; m < num_suits; ++m) {
      int n;
      for (n = 0; n < m; ++n) {
	if (((unsigned char *)&suit_groups)[n] ==
	    ((unsigned char *)&suit_groups)[m] &&
	    rank_used[n] == rank_used[m]) {
	  break;
	} else if (((unsigned char *)&nsg)[n] ==
		   ((unsigned char *)&nsg)[m] &&
		   rank_used[n] == rank_used[m]) {
	  break;
	}
      }
      ((unsigned char *)&nsg)[m] = n;
    }
  }
  delete [] rank_used;
  return change_made;
}
bool CanonicalCards :: ToCanon2 ( const Card * cards , int num_cards , int suit_groups , Card * canon_cards ) { int num_suits = Game :: NumSuits ( ) ; bool change_made = false ; if ( num_cards <= 0 ) return false ; int * rank_used = new int [ num_suits ] ; int nsg = suit_groups ; for ( int i = 0 ; i != num_suits ; ++ i ) rank_used [ i ] = 0 ; if ( canon_cards != cards ) { for ( int i = 0 ; i < num_cards ; ++ i ) { canon_cards [ i ] = cards [ i ] ; } } for ( int i = 0 ; i < num_cards ; ++ i ) { int old_suit = Suit ( canon_cards [ i ] ) ; int new_suit = ( ( char * ) & nsg ) [ old_suit ] ; int rank = Rank ( canon_cards [ i ] ) ; rank_used [ new_suit ] |= 1 << rank ; if ( new_suit != old_suit ) { canon_cards [ i ] = MakeCard ( rank , new_suit ) ; change_made = true ; for ( int j = i + 1 ; j < num_cards ; ++ j ) { if ( Suit ( canon_cards [ j ] ) == old_suit ) { rank = Rank ( canon_cards [ j ] ) ; canon_cards [ j ] = MakeCard ( rank , new_suit ) ; } else if ( Suit ( canon_cards [ j ] ) == new_suit ) { rank = Rank ( canon_cards [ j ] ) ; canon_cards [ j ] = MakeCard ( rank , old_suit ) ; } } } for ( int m = 0 ; m < num_suits ; ++ m ) { int n ; for ( n = 0 ; n < m ; ++ n ) { if ( ( ( unsigned char * ) & suit_groups ) [ n ] == ( ( unsigned char * ) & suit_groups ) [ m ] && rank_used [ n ] == rank_used [ m ] ) { break ; } else if ( ( ( unsigned char * ) & nsg ) [ n ] == ( ( unsigned char * ) & nsg ) [ m ] && rank_used [ n ] == rank_used [ m ] ) { break ; } } ( ( unsigned char * ) & nsg ) [ m ] = n ; } } delete [ ] rank_used ; return change_made ; }
This version does not resort the cards
Returns true if a change was made
This version does not resort the cards Returns true if a change was made
/* Get the current suit of the card */ /* Get the new suit */ /* Change every other card of the same suit */ /* Update suit_groups */ // Were the suits in the same group before, and they // now have the same cards again?  If so, recombine them. // Have we found a new suit that is isomorphic, given the  // cards we've seen so far?