📄 cardpile.cpp
字号:
}//-----------------------------------------------------------------------------/// Sorts the cards in the pile according the past function//-----------------------------------------------------------------------------void ICardPile::sort (CMPFUNC fnSort) { std::sort (begin (), end (), fnSort);}//-----------------------------------------------------------------------------/// Finds the first card being equal or bigger than the past one/// \param nr: Number of card (2, 3, 4, ... Ace) to search for/// \returns \c int: Position of card in pile (or -1, if none found)/// \pre Cards must be sorted (as the search is binary)//-----------------------------------------------------------------------------int ICardPile::findFirstEqualOrBigger (CardWidget::NUMBERS nr) const { unsigned int first (0), last (size ()); unsigned int middle (0); TRACE8 ("ICardPile::findFirstEqualOrBigger (CardWidget::NUMBERS) - Searching for " << nr << " in " << size () << " cards"); while ((last - first) > 0 ) { middle = first + ((last - first) >> 1); TRACE5 ("ICardPile::findFirstEqualOrBigger (CardWidget::NUMBERS) - Data = [" << first << "-(" << middle << ")-" << last << ") = " << *operator[] (middle)); Check3 (operator[] (first)); Check3 (operator[] (middle)); Check3 (operator[] (first)->number () <= operator[] (middle)->number ()); Check3 ((last == size ()) ? 1 : operator[] (last) && (operator[] (middle)->number () <= operator[] (last)->number ())); if (operator[] (middle)->number () < nr) first = middle + 1; else last = middle; Check3 (middle <= size ()); Check3 (last <= size ()); Check3 (first <= last); Check3 (middle <= last); }#if TRACELEVEL > 4 TRACE ("ICardPile::findFirstEqualOrBigger (CardWidget::NUMBERS) - End = [" << first << "-(" << middle << ")-" << last << ')'); if (first < size ()) TRACE ("\t-> " << *operator[] (first)) else TRACE ("\t-> Not found");#endif return ((first < size ()) && ((operator[] (first)->number () >= nr)) ? static_cast<int> (first) : -1);}//-----------------------------------------------------------------------------/// Finds the first card being equal or bigger than the past one/// \param nr: Colour of card (club, diamond, ...) to search for/// \returns \c int: Position of card in pile (or -1, if none found)/// \pre Cards must be sorted (as the search is binary)//-----------------------------------------------------------------------------int ICardPile::findFirstEqualOrBiggerColour (CardWidget::COLOURS col) const { unsigned int first (0), last (size ()); unsigned int middle (0); TRACE8 ("ICardPile::findFirstEqualOrBiggerColour (CardWidget::COLOURS) - Searching for " << col << " in " << size () << " cards"); while ((last - first) > 0 ) { middle = first + ((last - first) >> 1); TRACE5 ("ICardPile::findFirstEqualOrBiggerColour (CardWidget::COLOURS) - Data = [" << first << "-(" << middle << ")-" << last << ") = " << *operator[] (middle)); Check3 (operator[] (first)); Check3 (operator[] (middle)); Check3 (operator[] (first)->colour () <= operator[] (middle)->colour ()); Check3 ((last == size ()) ? 1 : operator[] (last) && (operator[] (middle)->colour () <= operator[] (last)->colour ())); if (operator[] (middle)->colour () < col) first = middle + 1; else last = middle; Check3 (middle <= size ()); Check3 (last <= size ()); Check3 (first <= last); Check3 (middle <= last); }#if TRACELEVEL > 4 TRACE ("ICardPile::findFirstEqualOrBigger (CardWidget::COLOURS) - End = [" << first << "-(" << middle << ")-" << last << ')'); if (first < size ()) TRACE ("\t-> " << *operator[] (first)) else TRACE ("\t-> Not found");#endif return ((first < size ()) && ((operator[] (first)->colour () >= col)) ? static_cast<int> (first) : -1);}//-----------------------------------------------------------------------------/// Finds the last card having an equal number as the passed card/// \param pos: Card whose (equal) number has to be found/// \returns \c int: Position of card in pile/// \pre Cards must be sorted//-----------------------------------------------------------------------------int ICardPile::findLastEqual (unsigned int pos) const { Check3 (pos < size ()); CardWidget::NUMBERS nr (operator[] (pos)->number ()); while (++pos < size ()) { if (operator[] (pos)->number () != nr) break; } TRACE5 ("ICardPile::findLastEqual (unsigned int) - Card " << *operator[] (pos - 1) << " at position " << pos - 1); return pos - 1;}//-----------------------------------------------------------------------------/// Finds the last card having an equal colour as the passed card/// \param pos: Card whose (equal) colour has to be found/// \returns \c int: Position of card in pile/// \pre Cards must be sorted//-----------------------------------------------------------------------------int ICardPile::findLastEqualColour (unsigned int pos) const { Check3 (pos < size ()); CardWidget::COLOURS col (operator[] (pos)->colour ()); while (++pos < size ()) { if (operator[] (pos)->colour () != col) break; } TRACE5 ("ICardPile::findLastEqualColour (unsigned int) - Card " << *operator[] (pos - 1) << " at position " << pos - 1); return pos - 1;}//-----------------------------------------------------------------------------/// Finds the first card having an equal number as the passed card/// \param pos: Card whose (equal) number has to be found/// \returns \c int: Position of card in pile/// \pre Cards must be sorted//-----------------------------------------------------------------------------int ICardPile::findFirstEqual (unsigned int pos) const { TRACE5 ("ICardPile::findFirstEqual (unsigned int) - Checking card " << pos); Check1 (pos < size ()); Check3 (operator[] (pos)); CardWidget::NUMBERS nr (operator[] (pos)->number ()); while (pos--) { TRACE9 ("ICardPile::findFirstEqual (unsigned int) - Checking card" << " at " << pos << " = " << *operator[] (pos)); if (operator[] (pos)->number () != nr) break; } TRACE5 ("ICardPile::findFirstEqual (unsigned int) - Card at position " << (pos + 1) << " = " << *operator[] (pos + 1)); return pos + 1;}//-----------------------------------------------------------------------------/// Finds the first card having an equal colour as the passed card/// \param pos: Card whose (equal) colour has to be found/// \returns \c int: Position of card in pile/// \pre Cards must be sorted//-----------------------------------------------------------------------------int ICardPile::findFirstEqualColour (unsigned int pos) const { TRACE5 ("ICardPile::findFirstEqualColour (unsigned int) - Checking card " << pos); Check1 (pos < size ()); Check3 (operator[] (pos)); CardWidget::COLOURS col (operator[] (pos)->colour ()); while (pos--) { TRACE9 ("ICardPile::findFirstEqualColour (unsigned int) - Checking card" << " at " << pos << " = " << *operator[] (pos)); if (operator[] (pos)->colour () != col) break; } TRACE5 ("ICardPile::findFirstEqualColour (unsigned int) - Card at position " << (pos + 1) << " = " << *operator[] (pos + 1)); return pos + 1;}//-----------------------------------------------------------------------------/// Makes the staple display either back or faces of the cards/// \param show: Option of how to display the cards//-----------------------------------------------------------------------------void ICardPile::setShowOption (ShowOpt show) { showOpt = show; if (showOpt < DONT_CHANGE) { std::vector<CardWidget*>::iterator i; for (i = begin (); i != end (); ++i) { Check3 (*i); (*i)->showFace (showOpt); } }}//-----------------------------------------------------------------------------/// Moves the card at pos source to pos dest/// \param source: Position of card to move/// \param dest: New position of card//-----------------------------------------------------------------------------void ICardPile::move (unsigned int dest, unsigned int source) { TRACE5 ("ICardPile::move (unsigned int, unsigned int) - Card from pos " << source << " to " << dest); insert (remove (source), dest);}//-----------------------------------------------------------------------------/// Searches for the first card having the passed number/// \param nr: Number to search for/// \param start: Position of start of search/// \returns \c int: Offset of found card or -1//-----------------------------------------------------------------------------int ICardPile::find (CardWidget::NUMBERS nr, unsigned int start) const { for (; start < size (); ++start) if (operator[] (start)->number () == nr) return start; return -1;}//-----------------------------------------------------------------------------/// Searches for the first card having the passed colour/// \param colour: Colour to search for/// \param start: Position of start of search/// \returns \c int: Offset of found card or -1//-----------------------------------------------------------------------------int ICardPile::find (CardWidget::COLOURS colour, unsigned int start) const { for (; start < size (); ++start) if (operator[] (start)->colour () == colour) return start; return -1;}//-----------------------------------------------------------------------------/// Searches for the first card having the passed id/// \param id: ID of card to search for/// \param id: ID of card to search for/// \returns \c int: Offset of found card or -1//-----------------------------------------------------------------------------int ICardPile::find (unsigned int id, unsigned int start) const { Check1 (start < size ()); for (const_iterator i (begin () + start); i != end (); ++i) if ((*i)->id () == id) return i - begin (); return -1;}//-----------------------------------------------------------------------------/// Resizing of a card in the pile/// \param CardWidget&: Card to resize/// \param PileStyle: Style of pile//-----------------------------------------------------------------------------void ICardPile::resize (CardWidget&, PileStyle) {}//-----------------------------------------------------------------------------/// Checks if the passed pile contains a pair matching the passed card/// \param card: Card where to find a pair to/// \param cmp: Method to compare two cards. This method gets the two cards to/// compare as input as must return an integer describing their/// difference (0: Equal). A pair can have a difference of at most/// [-2 - 2]/// \param doubles: True, if the same card (id) can be included more than once/// \returns \c True, if the pile contains a matching pair//-----------------------------------------------------------------------------bool ICardPile::hasFittingPair (const CardWidget& card, CMPFUNC2 cmp, bool doubles) const { TRACE3 ("ICardPile::pileHasFittingPair (const CardWidget*, CMPFUNC2, bool) - " << card); unsigned int nrs (0); unsigned int bCols (0); std::vector<unsigned int> foundCards (4); if (!doubles) foundCards.push_back (card.id ());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -