Advertisement
mixster

mixster

May 15th, 2009
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.25 KB | None | 0 0
  1. program Poker;
  2. type
  3.   TPoker = record
  4.     suit: Integer;
  5.     value: Integer;
  6.   end;
  7. var
  8.   hand, table: array of TPoker;
  9.  
  10. procedure Write(input: TVariantArray);
  11. var
  12.   s: string;
  13.   i: Integer;
  14. begin
  15.   s := '';
  16.   for i := 0 to High(input) do
  17.     s := s + input[i];
  18.   Writeln(s);
  19. end;
  20.  
  21. procedure WriteHand(input: array of TPoker);
  22. var
  23.   s: string;
  24.   i: Integer;
  25. begin
  26.   s := '';
  27.   for i := 0 to High(input) do
  28.   begin
  29.     case input[i].suit of
  30.       0: s := s + '(H)';
  31.       1: s := s + '(D)';
  32.       2: s := s + '(C)';
  33.       3: s := s + '(S)';
  34.     end;
  35.     if (input[i].value < 10) then
  36.       s := s + IntToStr(input[i].value + 1)
  37.     else
  38.       case input[i].value of
  39.         10: s := s + 'J';
  40.         11: s := s + 'Q';
  41.         12: s := s + 'K';
  42.       end;
  43.     s := s + '; ';
  44.   end;
  45.   Writeln(s);
  46. end;
  47.  
  48. function Card(suit, value: Integer): TPoker;
  49. begin
  50.   Result.suit := suit;
  51.   Result.value := value;
  52. end;
  53.  
  54. function GroupSuits(input: array of TPoker): array of array of TPoker;
  55. var
  56.   i: Integer;
  57.   l: TIntegerArray;
  58. begin
  59.   SetLength(Result, 4);
  60.   l := [0, 0, 0, 0];
  61.   for i := 0 to High(input) do
  62.     l[input[i].suit] := l[input[i].suit] + 1;
  63.   for i := 0 to 3 do
  64.     SetLength(Result[i], l[i]);
  65.   l := [0, 0, 0, 0];
  66.   for i := 0 to High(input) do
  67.   begin
  68.     Result[input[i].suit][l[input[i].suit]] := input[i];
  69.     l[input[i].suit] := l[input[i].suit] + 1;
  70.   end;
  71. end;
  72.  
  73. procedure SizeOrder(var input: array of TPoker);
  74. var
  75.   i, ii: Integer;
  76.   ordered: array of TPoker;
  77. begin
  78.   SetLength(ordered, Length(input));
  79.   for i := 0 to High(input) do
  80.   begin
  81.     ordered[0] := input[i];
  82.     for ii := 1 to High(ordered) do
  83.       if (ordered[ii - 1].value > ordered[ii].value) then
  84.         Swap(ordered[ii], ordered[ii - 1])
  85.       else
  86.         break;
  87.   end;
  88.   input := ordered;
  89. end;
  90.  
  91. function GroupPairs(input: array of TPoker): array of array of TPoker;
  92. var
  93.   i, ii, l, r: Integer;
  94. begin
  95.   l := 1;
  96.   r := 0;
  97.   for i := High(input) downto 1 do
  98.     if (input[i].value = input[i - 1].value) then
  99.       l := l + 1
  100.     else
  101.     begin
  102.       if (l >= 2) then
  103.       begin
  104.         SetLength(Result, r + 1);
  105.         SetLength(Result[r], l);
  106.         for ii := l - 1 downto 0 do
  107.           Result[r][ii] := input[i + ii];
  108.         r := r + 1;
  109.         case l of
  110.           2: Write(['Found a pair!']);
  111.           3: Write(['Found a three-of-a-kind!']);
  112.           4: Write(['Found a four-of-a-kind!']);
  113.           5: Write(['Found a five-of-a-kind error!']);
  114.         end;
  115.       end;
  116.       l := 1;
  117.     end;
  118. end;
  119.  
  120. function CreateHands(input: array of array of TPoker): array of array of TPoker;
  121. var
  122.   i, ii, l, h: Integer;
  123.   allCards: array of TPoker;
  124.   suits: array of array of TPoker;
  125.   pairs: array of array of TPoker;
  126. begin
  127.   for i := 0 to High(input) do
  128.     l := l + Length(input[i]);
  129.   SetLength(allCards, l);
  130.   l := 0;
  131.   for i := 0 to High(input) do
  132.     for ii := 0 to High(input[i]) do
  133.     begin
  134.       allCards[l] := input[i][ii];
  135.       l := l + 1;
  136.     end;
  137.    
  138.   SizeOrder(allCards);
  139.   suits := GroupSuits(allCards);
  140.   for i := 0 to 3 do
  141.   begin
  142.     if (Length(suits[i]) < 5) then
  143.       break;
  144.     l := 1;
  145.     for ii := High(suits[i]) downto 1 do
  146.       if (suits[i][ii - 1].value = suits[i][ii].value - 1) then
  147.         l := l + 1
  148.       else if (l >= 5) then
  149.       begin
  150.         h := ii + 4;
  151.         break;
  152.       end
  153.       else
  154.         l := 1;
  155.     if (l >= 5) then
  156.     begin
  157.       Write(['Found a straight!'])
  158.       l := Length(Result);
  159.       SetLength(Result, l + 1);
  160.       SetLength(Result[l], 5);
  161.       for ii := 4 downto 0 do
  162.         Result[l][ii] := suits[i][h - ii];
  163.     end
  164.     else
  165.       Write(['No straight']);
  166.   end;
  167.   pairs := GroupPairs(allCards);
  168.   l := Length(Result);
  169.   h := Length(pairs);
  170.   SetLength(Result, l + h);
  171.   l := l;
  172.   h := h - 1;
  173.   for i := 0 to h do
  174.     for ii := 0 to High(pairs[i]) do
  175.     begin
  176.       SetLength(Result[l + i], Length(pairs[ii]));
  177.       Result[l + i][ii] := pairs[i][ii];
  178.     end;
  179.  
  180.   for i := 0 to High(Result) do
  181.     WriteHand(Result[i]);
  182. end;
  183.  
  184. begin
  185.   Write(['Begin']);
  186.   hand := [Card(1, 5), Card(3, 7)];
  187.   table := [Card(1, 6), Card(1, 4), Card(0,7), Card(2,2), Card(3, 6)];
  188.   CreateHands([hand, table]);
  189.   Write(['End']);
  190. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement