public void SortByValue(int nums)
 {
     CardHand temphand = new CardHand();
     for (int i = 0; i < nums; i++)
     {
         temphand.AddCard(m_Cards[m_Cards.Length - nums + i]);
     }
     Array.Sort(temphand.Cards, new CardValueComparer(true));
     for (int i = 0; i < nums; i++)
     {
         m_Cards[m_Cards.Length - nums + i] = temphand.Cards[i];
     }
 }
		public static HandResult ScoreHand(CardHand hand, CardHand communitycards)
		{
			CardHand totalcards = new CardHand();
			CardHand handcheck = new CardHand();
			HandResult besthand = null;

			foreach (Card card in hand.Cards)
				totalcards.AddCard(card);
			foreach (Card card in communitycards.Cards)
				totalcards.AddCard(card);

			for (int i = 0; i < totalcards.Cards.Length; i++)
				for (int j = 0; j < totalcards.Cards.Length; j++)
				{
					handcheck.Clear();
					int number = 0;
					for (int k = 0; k < totalcards.Cards.Length; k++)
						if (k != i && k != j && i != j)
						{
							handcheck.AddCard(totalcards.Cards[k]);
							number++;
						}
					if (number > 4)
					{
						int handid = 0;
						int x = 0;
						int count = 0;
						int[] match = new int[5] { 0, 0, 0, 0, 0 };
						int pairs = 0;
						int[] aP = new int[2] { -1, -1 };
						bool flush = false;
						bool acehigh = false;
						bool straight = false;

						handcheck.SortByValue(false);
						if (handcheck.Cards[0].Value - 1 == handcheck.Cards[1].Value && handcheck.Cards[1].Value - 1 == handcheck.Cards[2].Value && handcheck.Cards[2].Value - 1 == handcheck.Cards[3].Value && handcheck.Cards[3].Value - 1 == handcheck.Cards[4].Value)
							straight = true;

						if (handcheck.Cards[0].Color == handcheck.Cards[1].Color && handcheck.Cards[0].Color == handcheck.Cards[2].Color && handcheck.Cards[0].Color == handcheck.Cards[3].Color && handcheck.Cards[0].Color == handcheck.Cards[4].Color)
							flush = true;

						handcheck.SortByValue(true);
						if ((handcheck.Cards[0].Value == 1 ? 14 : handcheck.Cards[0].Value) - 1 == handcheck.Cards[1].Value && handcheck.Cards[1].Value - 1 == handcheck.Cards[2].Value && handcheck.Cards[2].Value - 1 == handcheck.Cards[3].Value && handcheck.Cards[3].Value - 1 == handcheck.Cards[4].Value)
							acehigh = true;

						for (int m = 0; m < 5; m++)
						{
							for (int k = 0; k < 5; k++)
							{
								int temp = handcheck.Cards[k].Value;
								if (handcheck.Cards[m].Value == temp && k != m)
									match[m]++;
							}
							count += match[m];
						}

						for (int k = 0; k < 4; k++)
						{
							if (handcheck.Cards[k].Value == handcheck.Cards[k + 1].Value && (count == 2 || count == 4))
							{
								aP[pairs++] = k;
								k++;
							}
						}

						if (flush && (acehigh || straight))
						{//9 royal, 8 straight flush
							if (acehigh)
								x = handcheck.Cards[4].Value == 10 ? 9 : 8;
							else
							{
								x = 8;
								handcheck.SortByValue(false);
							}
						}
						else if (count == 12)
						{//four of a kind
							x = 7;
							if (handcheck.Cards[0].Value != handcheck.Cards[1].Value)
								handcheck.FlipCards(0, 4);
						}
						else if (count == 8)
						{//full house
							x = 6;
							if (handcheck.Cards[0].Value != handcheck.Cards[2].Value)
							{
								handcheck.FlipCards(0, 3);
								handcheck.FlipCards(1, 4);
							}
						}
						else if (flush)
							x = 5;
						else if (straight || acehigh)
						{
							x = 4;
							if (straight)
								handcheck.SortByValue(false);
						}
						else if (count == 6)
						{//three of a kind
							x = 3;
							if (handcheck.Cards[2].Value == handcheck.Cards[3].Value && handcheck.Cards[3].Value != handcheck.Cards[4].Value)
								handcheck.FlipCards(0, 3);
							if (handcheck.Cards[0].Value != handcheck.Cards[2].Value)
							{
								handcheck.FlipCards(0, 3);
								handcheck.FlipCards(1, 4);
							}
						}
						else if (count == 4)
						{//two pair
							x = 2;
							handcheck.FlipCards(0, aP[0]);
							handcheck.FlipCards(1, aP[0] + 1);
							handcheck.FlipCards(2, aP[1]);
							handcheck.FlipCards(3, aP[1] + 1);
						}
						else if (count == 2)
						{//pair
							x = 1;
							handcheck.FlipCards(0, aP[0]);
							handcheck.FlipCards(1, aP[0] + 1);
							handcheck.SortByValue(3);
						}
						else//high card
							x = 0;
						handid |= x << (4 * 5);
						handid |= (handcheck.Cards[0].Value == 1 ? 14 : handcheck.Cards[0].Value) << (4 * 4);
						handid |= (handcheck.Cards[1].Value == 1 ? 14 : handcheck.Cards[1].Value) << (4 * 3);
						handid |= (handcheck.Cards[2].Value == 1 ? 14 : handcheck.Cards[2].Value) << (4 * 2);
						handid |= (handcheck.Cards[3].Value == 1 ? 14 : handcheck.Cards[3].Value) << (4 * 1);
						handid |= (handcheck.Cards[4].Value == 1 ? 14 : handcheck.Cards[4].Value) << (4 * 0);

						HandResult result = new HandResult(handcheck, handid);
						if (besthand == null || result.HandID > besthand.HandID)
							besthand = result;
					}
				}
			return besthand;
		}
 public void DrawCards(CardHand hand, int x, int y, bool player)
 {
     if (hand.Cards != null)
         for (int i = 0; i < hand.Cards.Length; i++)
             hand.Cards[i].Draw(this, x + 30 * i, y, player);
 }
		public HandResult( CardHand hand, int handid )
		{
			m_Hand = new CardHand();
			foreach ( Card card in hand.Cards )
				m_Hand.AddCard( card );
            m_HandID = handid;
		}