/// <summary>Constructor: Create a deck of cards (reverse sort order)</summary> public Deck(bool reverseOrder) { _numCards = 54; _cardArray = new Card[_numCards]; for (int suits = 0; suits <= 3; suits++) { for (int faces = 0; faces <= 12; faces++){ _cardArray[(suits * 13) + faces] = new Card(suits, faces + 1); } } // Add the jokers to the end of the deck _cardArray[52] = new Card(CardSuit.Joker, CardFace.Ace); _cardArray[53] = new Card(CardSuit.Joker, CardFace.Two); }
} // FindTop(Card) /// <summary>Return the position of the specified card if it can /// be found in the deck. Otherwise return a negative number. /// _cardArray[0] is the top of the Deck.</summary> /// <param name="findCard">Card</param> /// <returns>int [1 .. 54]</returns> public int FindTop(Card findCard) { int i = 0; while (i < _numCards ) { if (_cardArray[i] == findCard) { return i + 1; } // if (_cardArray[i] == findCard) i++; } // while (i <= UpperBound) return -1; } // FindBottom(Card)
} // PeekBottom(int) /// <summary>Return the position of the specified card if it can /// be found in the deck. Otherwise return a negative number. /// _cardArray[53] is the bottom of the Deck.</summary> /// <param name="findCard">Card</param> /// <returns>int [1 .. 54]</returns> public int FindBottom(Card findCard) { return (_numCards - FindTop(findCard)) + 1; // not base 0 } // FindTop(Card)
} // MoveDown(int) /// <summary>Switch cards below the specified position with /// the cards above, up to the specified limit. /// A position of 1 or limit will not change the deck.</summary> /// <param name="fromPosition">Where to start the cut. /// 0 or 54 will not cause any cut.</param> /// <param name="toPosition">Where to stop the cut</param> public void BasicCut(int fromPosition, int toPosition) { if ((fromPosition > 0) && (fromPosition < toPosition)) { // number of elements must be +1 because // position 9 requires an array size // of 10 (don't forget position 0). Card[] buffer = new Card[toPosition+1]; // First move the lower cards to the top // [0 .. from_position] -> [to_position-from_position ... ] for (int i = 0; i < fromPosition; i++) { buffer[i + (toPosition - fromPosition)] = _cardArray[i]; } // for i // Then move the upper cards down. // [ from_position .. to_position ] -> [ 0 .. ] for (int i = fromPosition; i < toPosition; i++) { buffer[i - fromPosition] = _cardArray[i]; } // for i // Then replace the range in the original deck. for (int i = 0; i < toPosition; i++) { _cardArray[i] = buffer[i]; } // for i } // if positionInDeck } // BasicCut(int, int)
} // PontifexSolitaire(String) /// <summary>Performs the basic stream encryption operation // and returns the next element of the key stream.</summary> /// <returns>int [ 1 ... 26 ] to be converted to a character</returns> public int NextKeyStream() { Card JokerA = new Card(CardSuit.Joker, CardFace.Ace); Card JokerB = new Card(CardSuit.Joker, CardFace.Two); // Step one: Move Joker A one card down. _keyDeck.MoveDown(_keyDeck.FindTop(JokerA)); // Step two: Move Joker B two cards down. _keyDeck.MoveDown(_keyDeck.FindTop(JokerB), 2); Debug.WriteLine ("\n\nAfter Step 1,2:<br>" + _keyDeck.ToString()); // Step three: Perform a triple cut. int iJokerAPos = _keyDeck.FindTop(JokerA); int iJokerBPos = _keyDeck.FindTop(JokerB); _keyDeck.TripleCut(Math.Min(iJokerAPos, iJokerBPos), Math.Max (iJokerAPos, iJokerBPos)); Debug.WriteLine ("\n\nAfter Step 3:<br>" + _keyDeck.ToString()); // Step four: Perform a count cut (value of bottom card) from // the top (ensure the bottom card remains at the bottom). Card count = _keyDeck.PeekBottom(1); _keyDeck.CutTop(count.ID); _keyDeck.MoveDown(_keyDeck.FindTop(count), _keyDeck.Count() - _keyDeck.FindTop(count)); Debug.WriteLine ("\n\nAfter Step 4:<br>" + _keyDeck.ToString()); // Step five: Find the output card. // Peek at the top card to find it's value, then count down // and peek at the card *after* that position (hence +1). Card output = _keyDeck.PeekTop( _keyDeck.PeekTop(1).ID + 1 ); Debug.WriteLine ("\n\nAfter Step 5:<br>" + _keyDeck.ToString()); // Step six: Convert output card to a number. int result = output.ID; //getCardValue(output); Debug.WriteLine ("\n\nAfter Step 6:<br>" + _keyDeck.ToString()); Debug.WriteLine ("\n\n==== RESULT: " + result); // If Joker, repeat process if (result == 53 | result == 54) { _output += " (" + result + ")"; return NextKeyStream(); } else if (result > 26) { _output += " " + result; result = result - 26; } return result; } // NextKeyStream()