Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Poker;
- type
- TPoker = record
- suit: Integer;
- value: Integer;
- end;
- var
- hand, table: array of TPoker;
- procedure Write(input: TVariantArray);
- var
- s: string;
- i: Integer;
- begin
- s := '';
- for i := 0 to High(input) do
- s := s + input[i];
- Writeln(s);
- end;
- procedure WriteHand(input: array of TPoker);
- var
- s: string;
- i: Integer;
- begin
- s := '';
- for i := 0 to High(input) do
- begin
- case input[i].suit of
- 0: s := s + '(H)';
- 1: s := s + '(D)';
- 2: s := s + '(C)';
- 3: s := s + '(S)';
- end;
- if (input[i].value < 10) then
- s := s + IntToStr(input[i].value + 1)
- else
- case input[i].value of
- 10: s := s + 'J';
- 11: s := s + 'Q';
- 12: s := s + 'K';
- end;
- s := s + '; ';
- end;
- Writeln(s);
- end;
- function Card(suit, value: Integer): TPoker;
- begin
- Result.suit := suit;
- Result.value := value;
- end;
- function GroupSuits(input: array of TPoker): array of array of TPoker;
- var
- i: Integer;
- l: TIntegerArray;
- begin
- SetLength(Result, 4);
- l := [0, 0, 0, 0];
- for i := 0 to High(input) do
- l[input[i].suit] := l[input[i].suit] + 1;
- for i := 0 to 3 do
- SetLength(Result[i], l[i]);
- l := [0, 0, 0, 0];
- for i := 0 to High(input) do
- begin
- Result[input[i].suit][l[input[i].suit]] := input[i];
- l[input[i].suit] := l[input[i].suit] + 1;
- end;
- end;
- procedure SizeOrder(var input: array of TPoker);
- var
- i, ii: Integer;
- ordered: array of TPoker;
- begin
- SetLength(ordered, Length(input));
- for i := 0 to High(input) do
- begin
- ordered[0] := input[i];
- for ii := 1 to High(ordered) do
- if (ordered[ii - 1].value > ordered[ii].value) then
- Swap(ordered[ii], ordered[ii - 1])
- else
- break;
- end;
- input := ordered;
- end;
- function GroupPairs(input: array of TPoker): array of array of TPoker;
- var
- i, ii, l, r: Integer;
- begin
- l := 1;
- r := 0;
- for i := High(input) downto 1 do
- if (input[i].value = input[i - 1].value) then
- l := l + 1
- else
- begin
- if (l >= 2) then
- begin
- SetLength(Result, r + 1);
- SetLength(Result[r], l);
- for ii := l - 1 downto 0 do
- Result[r][ii] := input[i + ii];
- r := r + 1;
- case l of
- 2: Write(['Found a pair!']);
- 3: Write(['Found a three-of-a-kind!']);
- 4: Write(['Found a four-of-a-kind!']);
- 5: Write(['Found a five-of-a-kind error!']);
- end;
- end;
- l := 1;
- end;
- end;
- function CreateHands(input: array of array of TPoker): array of array of TPoker;
- var
- i, ii, l, h: Integer;
- allCards: array of TPoker;
- suits: array of array of TPoker;
- pairs: array of array of TPoker;
- begin
- for i := 0 to High(input) do
- l := l + Length(input[i]);
- SetLength(allCards, l);
- l := 0;
- for i := 0 to High(input) do
- for ii := 0 to High(input[i]) do
- begin
- allCards[l] := input[i][ii];
- l := l + 1;
- end;
- SizeOrder(allCards);
- suits := GroupSuits(allCards);
- for i := 0 to 3 do
- begin
- if (Length(suits[i]) < 5) then
- break;
- l := 1;
- for ii := High(suits[i]) downto 1 do
- if (suits[i][ii - 1].value = suits[i][ii].value - 1) then
- l := l + 1
- else if (l >= 5) then
- begin
- h := ii + 4;
- break;
- end
- else
- l := 1;
- if (l >= 5) then
- begin
- Write(['Found a straight!'])
- l := Length(Result);
- SetLength(Result, l + 1);
- SetLength(Result[l], 5);
- for ii := 4 downto 0 do
- Result[l][ii] := suits[i][h - ii];
- end
- else
- Write(['No straight']);
- end;
- pairs := GroupPairs(allCards);
- l := Length(Result);
- h := Length(pairs);
- SetLength(Result, l + h);
- l := l;
- h := h - 1;
- for i := 0 to h do
- for ii := 0 to High(pairs[i]) do
- begin
- SetLength(Result[l + i], Length(pairs[ii]));
- Result[l + i][ii] := pairs[i][ii];
- end;
- for i := 0 to High(Result) do
- WriteHand(Result[i]);
- end;
- begin
- Write(['Begin']);
- hand := [Card(1, 5), Card(3, 7)];
- table := [Card(1, 6), Card(1, 4), Card(0,7), Card(2,2), Card(3, 6)];
- CreateHands([hand, table]);
- Write(['End']);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement