Movatterモバイル変換


[0]ホーム

URL:


Sorry, we no longer support your browser
Please upgrade toMicrosoft Edge,Google Chrome, orFirefox. Learn more about ourbrowser support.
Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities includingStack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Loading…
Code Golf

You are notlogged in. Your edit will be placed in a queue until it ispeer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Snap (card game)

When I was a child I used to play a variation ofSnap to stake soccer trading cards.
"Game" is an overstatement because the only role of the players is to secretly prepare their starting piles. As the game starts there are no choices other than just perform the game algorithm.

Rules

There is no maximum number of players and they all start with a fixed number of cards.
Players in turn play the top card of their piles on a common pile (initially empty).

If the current player has no cards, the common pile bottom card will be played.

When a player happens to play an equal card to that on top of the common pile:

  • This player will take the common pile and append it face-down to his own pile. (Players' piles are face-down but the common pile is face-up, so the pair of equal cards should end up at the bottom)
  • Anyone with no cards is out of the game.
  • This player has to resume the game replenish the common pile with their top card as usual.

The game ends in one of three scenarios:

  • one player has all the cards (they win)
  • all the cards are in the common pile, the next player rotates it but doesn't form a pair (the common pile can't be taken)
  • one previous state of the game reoccurs (the game gets stuck in a loop)

Step by step game examples

1) Initial configuration: "abba abca"     p1      p2      common pile 1   abba    abca 2   bba     abca    a 3   bba     bca     aa 4           TAKES 5   bba     bcaaa 6   bba     caaa    b 7   ba      caaa    bb 8   TAKES 9   babb    caaa10   abb     caaa    b11   abb     aaa     bc12   bb      aaa     bca13   bb      aa      bcaa14           TAKES15   bb      aabcaa16   bb      abcaa   a17   b       abcaa   ab18   b       bcaa    aba19           bcaa    abab20           caa     ababb21   OUT     TAKES22           caaababb============ p2 wins ============2) Initial configuration: "abba acab"      p1      p2      common pile  1   abba    acab  2   bba     acab    a  3   bba     cab     aa  4           TAKES  5   bba     cabaa  6   bba     abaa    c  7   ba      abaa    cb  8   ba      baa     cba  9   a       baa     cbab 10   a       aa      cbabb 11           TAKES 12   a       aacbabb 13   a       acbabb  a 14           acbabb  aa 15   TAKES 16   aa      acbabb 17   a       acbabb  a 18   a       cbabb   aa 19           TAKES 20   a       cbabbaa 21   a       babbaa  c 22           babbaa  ca 23           abbaa   cab 24           abbaa   abc 25           bbaa    abca 26           bbaa    bcaa 27   TAKES 28   bcaa    bbaa 29   caa     bbaa    b 30   caa     baa     bb 31           TAKES 32   caa     baabb 33   caa     aabb    b 34   aa      aabb    bc 35   aa      abb     bca 36   a       abb     bcaa 37   TAKES 38   abcaa   abb 39   bcaa    abb     a 40   bcaa    bb      aa 41           TAKES 42   bcaa    bbaa 43   bcaa    baa     b 44   caa     baa     bb 45   TAKES 46   caabb   baa 47   aabb    baa     c 48   aabb    aa      cb 49   abb     aa      cba 50   abb     a       cbaa 51           TAKES 52   abb     acbaa 53   abb     cbaa    a 54   bb      cbaa    aa 55   TAKES 56   bbaa    cbaa 57   baa     cbaa    b 58   baa     baa     bc 59   aa      baa     bcb 60   aa      aa      bcbb 61           TAKES 62   aa      aabcbb 63   aa      abcbb   a 64   a       abcbb   aa 65   TAKES 66   aaa     abcbb 67   aa      abcbb   a 68   aa      bcbb    aa 69           TAKES 70   aa      bcbbaa 71   aa      cbbaa   b 72   a       cbbaa   ba 73   a       bbaa    bac 74           bbaa    baca 75           baa     bacab 76           baa     acabb 77   TAKES 78   acabb   baa 79   cabb    baa     a 80   cabb    aa      ab 81   abb     aa      abc 82   abb     a       abca 83   bb      a       abcaa 84   TAKES 85   bbabcaa a 86   babcaa  a       b 87   babcaa          ba 88   abcaa           bab 89   abcaa           abb 90           TAKES 91   abcaa   abb 92   abcaa   bb      a 93   bcaa    bb      aa 94   TAKES 95   bcaaaa  bb 96   caaaa   bb      b 97   caaaa   b       bb 98           TAKES 99   caaaa   bbb100   caaaa   bb      b101   aaaa    bb      bc102   aaaa    b       bcb103   aaa     b       bcba104   aaa             bcbab105   aa              bcbaba106   aa              cbabab107   a               cbababa108   a               bababac109                   bababaca110                   ababacab // common pile can't be taken============ p1, p2 in game ============3) Initial configuration: "bdad acbc abba"     p1          p2          p3          common pile 1   bdad        acbc        abba 2   dad         acbc        abba        b 3   dad         cbc         abba        ba 4   dad         cbc         bba         baa 5                           TAKES 6   dad         cbc         bbabaa 7   dad         cbc         babaa       b 8   ad          cbc         babaa       bd 9   ad          bc          babaa       bdc10   ad          bc          abaa        bdcb11   d           bc          abaa        bdcba12   d           c           abaa        bdcbab13   d           c           baa         bdcbaba14               c           baa         bdcbabad15                           baa         bdcbabadc16                           aa          bdcbabadcb17                           aa          dcbabadcbb18   TAKES       OUT19   dcbabadcbb              aa20   cbabadcbb               aa          d21   cbabadcbb               a           da22   babadcbb                a           dac23   babadcbb                            daca24   abadcbb                             dacab25   abadcbb                             acabd26   badcbb                              acabda27   badcbb                              cabdaa28                           TAKES29   badcbb                  cabdaa30   badcbb                  abdaa       c31   adcbb                   abdaa       cb32   adcbb                   bdaa        cba33   dcbb                    bdaa        cbaa34   TAKES35   dcbbcbaa                bdaa36   cbbcbaa                 bdaa        d37   cbbcbaa                 daa         db38   bbcbaa                  daa         dbc39   bbcbaa                  aa          dbcd40   bcbaa                   aa          dbcdb41   bcbaa                   a           dbcdba42   cbaa                    a           dbcdbab43   cbaa                                dbcdbaba44   baa                                 dbcdbabac45   baa                                 bcdbabacd46   aa                                  bcdbabacdb47   aa                                  cdbabacdbb48                           TAKES49   aa                      cdbabacdbb50   aa                      dbabacdbb   c51   a                       dbabacdbb   ca52   a                       babacdbb    cad53                           babacdbb    cada54                           abacdbb     cadab55                           abacdbb     adabc56                           bacdbb      adabca57                           bacdbb      dabcaa58   TAKES59   dabcaa                  bacdbb60   abcaa                   bacdbb      d61   abcaa                   acdbb       db62   bcaa                    acdbb       dba63   bcaa                    cdbb        dbaa64                           TAKES65   bcaa                    cdbbdbaa66   bcaa                    dbbdbaa     c67   caa                     dbbdbaa     cb68   caa                     bbdbaa      cbd69   aa                      bbdbaa      cbdc70   aa                      bdbaa       cbdcb71   a                       bdbaa       cbdcba72   a                       dbaa        cbdcbab73                           dbaa        cbdcbaba74                           baa         cbdcbabad75                           baa         bdcbabadc76                           aa          bdcbabadcb77                           aa          dcbabadcbb78   TAKES79   dcbabadcbb              aa                       // loop (line 19)============ p1, p3 in game ============

N.B. game states are inviduated by piles configuration and current player. As you can see in example2) lines 28, 42 are not the same game state.

Input

A list of players' piles (top to bottom) as:

  • an array of strings["code", "golf", "fold"], or
  • a matrix of positive integers[[1,2,3,4],[5,2,6,7],[7,2,6,3]]

Players order is implied by piles order.

Output

A number indicating the player who wins or a list of numbers for the players who reach the end of the game.
You decide whether players are 0 or 1 indexed.

I/O Examples (1-indexed players):

"abca abba"      -> 1"abba abca"      -> 2"abba acab"      -> 1 2"bdad acbc abba" -> 1 3"fizz buzz"      -> 2"robinhooda ndlittlejo hnwalkingt hroughthef orestlaugh ingbackand forthatwha ttheothero nehastosay" -> 9

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
Cancel
    Add a comment | 
    How to Edit
    • Correct minor typos or mistakes
    • Clarify meaning without changing it
    • Add related resources or links
    • Always respect the author’s intent
    • Don’t use edits to reply to the author
    How to Format
    • create code fences with backticks ` or tildes ~
      ```
      like so
      ```
    • add language identifier to highlight code
      ```python
      def function(foo):
          print(foo)
      ```
    • put returns between paragraphs
    • for linebreak add 2 spaces at end
    • _italic_ or**bold**
    • indent code by 4 spaces
    • backtick escapes`like _so_`
    • quote by placing > at start of line
    • to make links (use https whenever possible)
      <https://example.com>
      [example](https://example.com)
      <a href="https://example.com">example</a>
    • MathJax equations\$\sin^2 \theta\$

    MathJax help »

    How to Tag

    A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.

    • complete the sentence:my question is about...
    • use tags that describe things or concepts that areessential, not incidental to your question
    • favor usingexisting popular tags
    • read the descriptions that appear below the tag

    If your question is primarily about a topic for which you can't find a tag:

    popular tags »



    [8]ページ先頭

    ©2009-2025 Movatter.jp