Data update

This commit is contained in:
Ingy döt Net 2024-07-13 15:19:22 -07:00
parent 29a5eea0d4
commit 5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions

View file

@ -15,4 +15,3 @@ Related tasks:
* [[Poker hand_analyser]]
* [[Go Fish]]

View file

@ -0,0 +1,146 @@
-- Play the card game War
-- J. Carter 2024 Jul
-- Uses the PragmAda Reusable Components (https://github.com/jrcarter/PragmARC)
with Ada.Text_IO;
with PragmARC.Ansi_Tty_Control;
with PragmARC.Cards.Decks.US;
with PragmARC.Cards.US;
procedure War is
package Ansi renames PragmARC.Ansi_Tty_Control;
package Cards renames PragmARC.Cards.US;
package Decks renames PragmARC.Cards.Decks.US;
use type Cards.Rank_Id;
procedure Display (Hand1 : in Decks.Deck_52; Hand2 : in Decks.Deck_52);
-- Display the state of the game represented by the 2 hands
function ">" (Left : in Cards.Card_Info; Right : in Cards.Card_Info) return Boolean is
(if Left.Rank = Cards.Ace then
Right.Rank /= Cards.Ace
elsif Right.Rank = Cards.Ace then
False
else
Left.Rank > Right.Rank);
procedure Display (Hand1 : in Decks.Deck_52; Hand2 : in Decks.Deck_52) is
function Image (Card : in Cards.Card_Info) return Character is
(Cards.Image (Card) (1) );
procedure Display (Hand : in Decks.Deck_52);
-- Puts the ranks of the cards in Hand in a row
procedure Display (Hand : in Decks.Deck_52) is
-- Empty
begin -- Display
All_Cards : for I in 1 .. Hand.Size loop
Ada.Text_IO.Put (Item => Image (Hand.Value (I) ) );
end loop All_Cards;
end Display;
begin -- Display
Ada.Text_IO.Put (Item => Ansi.Clear_Screen);
Display (Hand => Hand1);
Ada.Text_IO.Put (Item => Ansi.Position (2, 1) );
Display (Hand => Hand2);
Ada.Text_IO.Put (Item => Ansi.Position (3, 1) );
end Display;
Hand1 : Decks.Deck_52; -- Player 1
Hand2 : Decks.Deck_52; -- Player 2
Card1 : Cards.Card_Info; -- Player 1
Card2 : Cards.Card_Info; -- Player 2
War1 : Decks.Deck_52; -- Player 1
War2 : Decks.Deck_52; -- Player 2
begin -- War
Decks.Standard_Deck (Item => Hand1);
Hand1.Shuffle;
Deal : for I in 1 .. Hand1.Size / 2 loop -- Each player gets half the deck
Hand1.Deal (To => Card1);
Hand2.Add (Item => Card1);
end loop Deal;
Play : loop
Display (Hand1 => Hand1, Hand2 => Hand2);
exit Play when Hand1.Size = 0 or Hand2.Size = 0;
delay 0.5;
Hand1.Deal (To => Card1);
Hand2.Deal (To => Card2);
if Card1.Rank /= Card2.Rank then
if Card1 > Card2 then
Hand1.Add (Item => Card1);
Hand1.Add (Item => Card2);
else
Hand2.Add (Item => Card2);
Hand2.Add (Item => Card1);
end if;
else -- War!
War1.Add (Item => Card1);
War2.Add (Item => Card2);
All_Tries : loop
Ada.Text_IO.Put (Item => "W^^");
delay 0.5;
Deal1 : for I in 1 .. 2 loop
exit Deal1 when Hand1.Is_Empty;
Hand1.Deal (To => Card1);
War1.Add (Item => Card1);
end loop Deal1;
Deal2 : for I in 1 .. 2 loop
exit Deal2 when Hand2.Is_Empty;
Hand2.Deal (To => Card2);
War2.Add (Item => Card2);
end loop Deal2;
if War1.Value (War1.Size) > War2.Value (War2.Size) then -- 1 wins
Deal11 : loop
exit Deal11 when War1.Is_Empty;
War1.Deal (To => Card1);
Hand1.Add (Item => Card1);
end loop Deal11;
Deal12 : loop
exit Deal12 when War2.Is_Empty;
War2.Deal (To => Card1);
Hand1.Add (Item => Card1);
end loop Deal12;
exit All_Tries;
elsif War2.Value (War2.Size) > War1.Value (War1.Size) then -- 2 Wins
Deal22 : loop
exit Deal22 when War2.Is_Empty;
War2.Deal (To => Card2);
Hand2.Add (Item => Card2);
end loop Deal22;
Deal21 : loop
exit Deal21 when War1.Is_Empty;
War1.Deal (To => Card2);
Hand2.Add (Item => Card2);
end loop Deal21;
exit All_Tries;
else
null; -- The war continues
end if;
end loop All_Tries;
end if;
end loop Play;
Ada.Text_IO.Put (Item => "Player " & (if Hand1.Size = 0 then '2' else '1') & " wins");
end War;

View file

@ -0,0 +1,113 @@
### Generic functions
# Set .popped to the first item of the specified array, and remove it therefrom
@ Usage example: {a:[1,2]} | pop(.a; .x)
def pop(array; popped):
popped = array[0]
| array |= .[1:];
# like while/2 but emit the final term rather than the first one
def whilst(cond; update):
def _whilst:
if cond then update | (., _whilst) else empty end;
_whilst;
# Output: a prn in range(0;.)
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
def knuthShuffle:
length as $n
| if $n <= 1 then .
else {i: $n, a: .}
| until(.i == 0;
.i += -1
| (.i + 1 | prn) as $j
| .a[.i] as $t
| .a[.i] = .a[$j]
| .a[$j] = $t)
| .a
end;
### War Card Game
def suits: ["♣", "♦", "♥", "♠"];
def faces: ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" ];
def cards: [range( 0; 52) | "\(faces[.%13])\(suits[(./13)|floor])"];
def ranks: [range( 0; 52) | . % 13];
def war:
cards as $cards
| ranks as $ranks
| ([range(0; 52)] | knuthShuffle) as $deck
| reduce range(0; 26) as $i ({};
.hand1 |= [$deck[2*$i]] + .
| .hand2 |= [$deck[2*$i+1]] + .)
| .numPlayed=0
| whilst( .numPlayed == 0 and
(.hand1|length) > 0 and (.hand2|length) > 0;
pop(.hand1; .card1)
| pop(.hand2; .card2)
| .played1 = [.card1]
| .played2 = [.card2]
| .numPlayed = 2
| whilst(.numPlayed > 0 ;
.emit = "\($cards[.card1])\t\($cards[.card2])\t"
| if $ranks[.card1] > $ranks[.card2]
then .hand1 += .played1 + .played2
| .emit += "Player 1 takes the \(.numPlayed) cards. Now has \(.hand1|length)."
| .numPlayed = 0
elif $ranks[.card1] < $ranks[.card2]
then .hand2 += .played2 + .played1
| .emit += "Player 2 takes the \(.numPlayed) cards. Now has \(.hand2|length)."
| .numPlayed = 0
else .emit += "War!\n"
| if (.hand1|length) < 2
then .emit += "Player 1 has insufficient cards left."
| .hand2 = .hand2 + .played2 + .played1 + .hand1
| .hand1 = []
| .numPlayed = 0
elif .hand2|length < 2
then .emit += "Player 2 has insufficient cards left."
| .hand1 = .hand1 + .played1 + .played2 + .hand2
| .hand2 = []
| .numPlayed = 0
else
pop(.hand1; .card1) # face down card
| .played1 += [.card1]
| pop(.hand1; .card1) # face up card
| .played1 += [.card1]
| pop( .hand2; .card2) # face down card 2
| .played2 += [.card2]
| pop(.hand2; .card2) # face up card 2
| .played2 += [.card2]
| .numPlayed += 4
| .emit += "? \t?\tFace down cards."
end
end
)
)
| .emit,
(if .hand1|length == 52
then "Player 1 wins the game!"
elif .hand2|length == 52
then "Player 2 wins the game!"
else empty
end) ;
war