7
\$\begingroup\$

Beggar my neighbor is a deterministic game in which 2 players get dealt half of a 52 card deck, and the objective is to obtain the entire deck.

On their turn, each player places the top card of their deck on the table. If that card is a face card (the ace is a face card), the opposite player then must turn over a certain number of cards from the top of their deck until they reveal a face card as well (at which point the roles reverse). If they can't, the player that last played a face card takes the pile of cards on the table, and adds it to the bottom of their deck. the winning player then continues the game.

The game ends when a player has obtained all the cards in the deck. Note that if a player has all the face cards, their victory is guaranteed, but it is not achieved yet and they need to take all the cards from their opponent to properly end the game.

The number of tries someone has to turn over a face card in response to a face card depends on the face card:

  • Jack : 1 card
  • Queen: 2 cards
  • King : 3 cards
  • Ace : 4 cards

Here's an example trick:

I turn over a 10.
They turn over an ace.
I turn over a 5, a 7, and a Jack.
They turn over a Queen
I turn over a 2 and a 9.
They win this trick, sweep the table and it is their turn next.

Hopefully by now it's becoming clear that this game is completely deterministic and that there is no strategy; the entire game is decided by how the cards are shuffled and dealt. The longest known terminating game is 1164 tricks long, but it is possible for a game to go on forever (you won't need to handle this here)

The task

Given a deck of cards, output the number of tricks and cards it takes to reach the end of the game.

IO

You may take the deck however you see fit. You may forgo taking the cards color as input, as it has no bearing on play. Since numbered cards also are fungible, a standard way of representing a deck is as a pair of strings like so:

---K---Q-KQAJ-----AAJ--J-- and----------Q----KQ-J-----KA (this game will be infinite, don't use it as a test case!)

You may choose to represent face cards as the number of cards they ask for. Generally speaking, within reason, feel free to use a format convenient for you as long as it doesn't trivialize the meat of the challenge (simulating the game and how the cards rotate). This isn't an input parsing challenge.

Assume the game is finite. You may assume that a game cannot go on for longer than the record 1164 tricks and 8344 cards.

Output format flexible.

Test cases

[Q--J----K--K-J---Q---A---A, ---K-K--JA-QA--J-----Q----] -> 713 tricks, 5104 cards[K-KK----K-A-----JAA--Q--J-, ---Q---Q-J-----J------AQ--] -> 1007 tricks, 7157 cards[---AK-Q--J----J--QKJ-Q----, ------JK-----A--K--Q---AA-] -> 1014 tricks, 7259 cards[---AJ--Q---------QAKQJJ-QK, -----A----KJ-K--------A---] -> 1164 tricks, 8344 cards

You may find additional test cases and resources atthis link

This is.

asked21 hours ago
Themoonisacheese's user avatar
\$\endgroup\$
2
  • 2
    \$\begingroup\$Beggar-my-neighbour, also known as strip jack naked, beat your neighbour out of doors, or beat jack out of doors, or beat your neighbour. Those are funny names.\$\endgroup\$Commented19 hours ago
  • \$\begingroup\$The game ends when a player has obtained all the cards in the deck. If I do exactly that, I get a significantly different result for the 3rd test case. If I stop as soon as a player has no more cards, the results are much closer to yours (at ±1 trick for some other reason).\$\endgroup\$Commented44 mins ago

2 Answers2

2
\$\begingroup\$

Python3, 311 bytes

def f(A,B): t,p,T,M=0,[],0,0 while A and B:  p+=[c:=[A,B][t].pop(0)]  M+=1  if c:   l=t;T+=1   while 1:    F=1    for _ in range(c):     if[]==(K:=[A,B][not t]):return T,M     p+=[C:=K.pop(0)];M+=1     if C:F=0;l=not t;t=not t;c=C;break    if F:[A,B][l]+=p;p=[];t=l;break  else:t=not t return T,M

Try it online!

answered5 hours ago
Ajax1234's user avatar
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 79 bytes

≔⮌E²⪪⮌S¹θ≔⟦⟧η≔⁰ζW⌊θ«→≔⮌θθ≔⊟§θ⁰ι⊞υι¿№αι≔⊕⌕JQKAιζ¿ζ«≦⊖ζ¿ζ≔⮌θθ⊞η⊞Oθ⁺E⮌υ⊟υ⊟θ»»I⟦Lηⅈ

Try it online! Link is to verbose version of code. Explanation:

≔⮌E²⪪⮌S¹θ

Input the two decks, but reverse the cards in each deck, and also reverse the decks, so that the first player's deck is last.

≔⟦⟧η

Keep track of the number of tricks.

≔⁰ζ

Keep track of the tries left.

W⌊θ«

Repeat while both players have cards.

Keep track of the number of cards played.

≔⮌θθ

Switch the decks around so that the player whose turn it is is first.

≔⊟§θ⁰ι

Remove their next card from their deck.

⊞υι

Add it to the table.

¿№αι

If it's a picture card, then...

≔⊕⌕JQKAιζ

... get the number of tries their opponent has.

¿ζ«

Otherwise if they have tries left, then:

≦⊖ζ

Decrement the number of tries left.

¿ζ

If they still have tries left, then...

≔⮌θθ

...switch the players around, so that the current player will have to play again on the next loop.

⊞η⊞Oθ⁺E⮌υ⊟υ⊟θ

Otherwise sweep the cards from the table into the other player's deck.

»»I⟦Lηⅈ

Output the number of tricks and cards played.

answered6 hours ago
Neil's user avatar
\$\endgroup\$

Your Answer

More generally…

  • …Please make sure to answer the question and provide sufficient detail.

  • …Avoid asking for help, clarification or responding to other answers (use comments instead).

Draft saved
Draft discarded

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.