Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Poker;
- type
- TPoker = record
- suit: Integer;
- value: Integer;
- end;
- type
- THand = record
- cards: array of TPoker;
- 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: THand);
- var
- s: string;
- i: Integer;
- begin
- s := IntToStr(input.value) + ': ';
- for i := 0 to High(input.cards) do
- begin
- case input.cards[i].suit of
- 0: s := s + '(H)';
- 1: s := s + '(D)';
- 2: s := s + '(C)';
- 3: s := s + '(S)';
- end;
- if (input.cards[i].value < 10) then
- s := s + IntToStr(input.cards[i].value + 1)
- else
- case input.cards[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;
- ii := High(ordered);
- for i := 0 to ii do
- input[i] := ordered[ii - i];
- end;
- function FindFlush(input: array of TPoker): THand;
- var
- i: Integer;
- begin
- SetLength(Result.cards, 0);
- if (Length(input) < 5) then
- exit;
- SetLength(Result.cards, 5);
- for i := 0 to 4 do
- Result.cards[i] := input[i];
- Result.value := 4;
- end;
- procedure FilterSameCards(var input: array of TPoker);
- var
- i, l: Integer;
- store: array of TPoker;
- begin
- l := 0;
- SetLength(store, l + 1);
- store[l] := input[0];
- for i := 1 to High(input) do
- if (input[i].value <> store[l].value) then
- begin
- l := l + 1;
- SetLength(store, l + 1);
- store[l] := input[i];
- end;
- SetLength(input, l + 1);
- for i := 0 to l do
- input[i] := store[i];
- end;
- function FindStraight(input: array of TPoker): THand;
- var
- i, ii, l: Integer;
- begin
- SetLength(Result.cards, 0);
- FilterSameCards(input);
- if (Length(input) < 5) then
- exit;
- l := 1;
- for i := 1 to High(input) do
- begin
- if (input[i].value = input[i - 1].value - 1) then
- l := l + 1
- else
- l := 1;
- if (l >= 5) then
- begin
- Write(['Found a straight!']);
- SetLength(Result.cards, 5);
- for ii := 0 to 4 do
- Result.cards[4 - ii] := input[i - ii];
- Result.value := 5;
- end;
- end;
- end;
- function FindPairs(input: array of TPoker): array of THand;
- var
- i, ii, l, r: Integer;
- begin
- l := 1;
- r := 0;
- SetLength(Result, 0);
- for i := 1 to High(input) 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].cards, l);
- for ii := 0 to l - 1 do
- Result[r].cards[ii] := input[i - ii - 1];
- case l of
- 2: Result[r].value := 8;
- 3: Result[r].value := 6;
- 4: Result[r].value := 2;
- end;
- r := r + 1;
- end;
- l := 1;
- end;
- end;
- procedure AddHand(var curHands: array of THand; newHand: THand);
- var
- i, l, h: Integer;
- begin
- l := Length(curHands);
- SetLength(curHands, l + 1);
- h := High(newHand.cards);
- SetLength(curHands[l].cards, h + 1);
- for i := 0 to h do
- curHands[l].cards[i] := newHand.cards[i];
- curHands[l].value := newHand.value;
- end;
- function CreateHands(input: array of array of TPoker): array of THand;
- var
- i, ii, l, h: Integer;
- allCards: array of TPoker;
- suits: array of array of TPoker;
- pairs: array of THand;
- flush, straight: THand;
- 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
- flush := FindFlush(suits[i]);
- if (Length(flush.cards) <> 0) then
- AddHand(Result, flush);
- end;
- straight := FindStraight(allCards);
- if (Length(straight.cards) <> 0) then
- AddHand(Result, straight);
- pairs := FindPairs(allCards);
- h := High(pairs);
- for i := 0 to h do
- AddHand(Result, pairs[i]);
- 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