War Card Game

Simulate the card game War. Use the Bicycle playing card manufacturer's rules. Show a game as played. User input is optional.

References:

 Bicycle card company: War game site
 Wikipedia: War (card game)
Related tasks:

 Playing cards
 Card shuffles
 Deal cards for FreeCell
 Poker hand_analyser
 Go Fish


char    Deck(52),               \initial card deck (low 2 bits = suit)
        Stack(2, 52);           \each player's stack of cards (52 maximum)
int     Inx(2),                 \index to last card (+1) for each stack
        Top,                    \index to compared cards, = stack top if not war
        Card, N, I, J, P, T;
char    Suit, Rank;
 
proc    MoveCard(To, From);     \Move top card From Stack to bottom of To Stack
int     To, From;
int     Card, I;
[Card:= Stack(From, 0);         \take top Card from From Stack
for I:= 0 to Inx(From)-2 do     \shift remaining cards over
        Stack(From, I):= Stack(From, I+1);
if Inx(From) > 0 then           \remove From card from its Stack
        Inx(From):= Inx(From)-1;
Stack(To, Inx(To)):= Card;      \add Card to bottom of To Stack
if Inx(To) < 52 then            \remove From card from its Stack
        Inx(To):= Inx(To)+1;
];
 
[\\Suit:= "^C^D^E^F ";          \IBM OEM card symbols aren't displayable on RC
Suit:= "HDCS ";
Rank:= "23456789TJQKA ";        \T = 10
for Card:= 0 to 52-1 do         \make a complete deck of cards
        Deck(Card):= Card;
for N:= 0 to 10_000 do          \shuffle the deck by swapping random locations
        [I:= Ran(52);  J:= Ran(52);
        T:= Deck(I);  Deck(I):= Deck(J);  Deck(J):= T;
        ];
for N:= 0 to 52-1 do            \deal deck into two stacks
        [Card:= Deck(N);
        I:= N/2;
        P:= rem(0);
        Stack(P, I):= Card;
        ];
Inx(0):= 52/2;  Inx(1):= 52/2;  \set indexes to last card +1
 
loop    [for P:= 0 to 1 do      \show both stacks of cards
                [for I:= 0 to Inx(P)-1 do
                        [Card:= Stack(P, I);  ChOut(0, Rank(Card>>2))];
                CrLf(0);
                for I:= 0 to Inx(P)-1 do
                        [Card:= Stack(P, I);  ChOut(0, Suit(Card&3))];
                CrLf(0);
                ];
        if Inx(0)=0 or Inx(1)=0 then quit;      \game over
 
        Top:= 0;                \compare card ranks (above 2-bit suits)
        loop    [if Stack(0, Top)>>2 = Stack(1, Top)>>2 then
                        [Text(0, "War!");  CrLf(0);
                        Top:= Top+2;            \play a card down and a card up
                        ]
                else if Stack(0, Top)>>2 > Stack(1, Top)>>2 then
                        [for I:= 0 to Top do    \move cards to Stack 0
                                [MoveCard(0, 0);  MoveCard(0, 1)];
                        quit;
                        ]
                else    [for I:= 0 to Top do    \move cards to Stack 1
                                [MoveCard(1, 1);  MoveCard(1, 0)];
                        quit;
                        ];
                ];
        T:= ChIn(1);            \wait for keystroke (no key echo)
        CrLf(0);
        ];
]
]