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*

Required fields*

Determine the winner of a game of War

The card gameWar is interesting in that the final outcome is entirely determined by the initial arrangement of the deck, so long as certain rules are followed for the order in which cards are picked up from the playing field and moved to decks. In this challenge, there will only be 2 players, simplifying things greatly.

The Game

  1. Each player is dealt a deck of 26 cards.
  2. Each player places the top card in their deck face-up. The player with the higher-ranking card (Ace > King > Queen > Jack > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2) wins the round, and places their card on top of their opponent's card, flips them over, and adds them to the bottom of their deck (so their winning card is on the bottom of the deck, and the other player's losing card is just above it). This is done until one of the players runs out of cards.
    • If the cards are of equal rank, then each player places the top 2 cards of their deck face-up on top of their previous card (so that the card that was on top of the deck is the second card in the stack, and the card that was second-from-top is on top). Then, the ranks (of the top card of each stack) are compared again, and the winner places their entire stack on top of the loser's entire stack, turns the stack upside-down, and places it on the bottom of their deck. If there is another tie, more cards are played in the same way, until a winner is chosen or one player runs out of cards.

If at any point one of the players needs to draw a card from their deck, but their deck is empty, they immediately lose the game.

The Challenge

Given two lists of cards in the players' decks, in any convenient format, output a truthy value if Player 1 wins, and a falsey value if Player 2 wins.

For convenience, a 10 card will be represented with aT, and face cards will be abbreviated (Ace -> A, King -> K, Queen -> Q, Jack -> J), so that all cards are one character long. Alternatively, ranks may be represented with decimal integers 2-14 (Jack -> 11, Queen -> 12, King -> 13, Ace -> 14) or hex digits 2-E (10 -> A, Jack -> B, Queen -> C, King -> D, Ace -> E). Since suits don't matter, suit information will not be given.

  • You may assume that all games will terminate at some point (though it may take a very long time), and one player will always run out of cards before the other.
  • Each player places cards simultaneously, and one card at a time, so there is never any ambiguity about which player ran out of cards first.

Test Cases

The test cases use23456789ABCDE to represent the ranks (in ascending order).

D58B35926B92C7C4C7E8D3DAA2, 8E47C38A2DEA43467EB9566B95 -> False669D9D846D4B3BA52452C2EDEB, E747CA988CC76723935A3B8EA5 -> False5744B95ECDC6D325B28A782A72, 68394D9DA96EBBA8533EE7C6C4 -> True87DB6C7EBC6C8D722389923DC6, E28435DBEBEA543AA47956594A -> False589EAB9DCD43E9EC264A5726A8, 48DC2577BD68AB9335263B7EC4 -> TrueE3698D7C46A739AE5BE2C49286, BB54B7D78954ED526A83C3CDA2 -> True32298B5E785DC394467D5C9CB2, 5ED6AAD93E873EA628B6A4BC47 -> TrueB4AB985B34756C624C92DE5E97, 3EDD5BA2A68397C26CE837AD48 -> False9A6D9A5457BB6ACBC5E8D7D4A9, 73E658CE2C3E289B837422D463 -> True96E64D226BC8B7D6C5974BAE32, 58DC7A8C543E35978AEBA34D29 -> TrueC2978A35E74D7652BA9762C458, 9A9BB332BE8C8DD44CE3DE66A5 -> FalseBEDB44E947693CD284923CEA82, 8CC3B75756255A683A6AB9E7DD -> FalseEEDDCCBBAA8877665544332299, EEDDCCBBAA9988776655443322 -> FalseEEDDCCBBAA9988776655443322, DDCCBBAA9988776655443E3E22 -> True

Reference Implementation

This reference implementation is written in Python 3, and takes input in the same format as the test cases (except separated by a newline instead of a comma and a space).

#!/usr/bin/env python3from collections import dequep1, p2 = [deque(s) for s in (input(),input())]print(''.join(p1))print(''.join(p2))try:    while p1 and p2:        p1s = [p1.popleft()]        p2s = [p2.popleft()]        while p1s[-1] == p2s[-1]:            p1s.append(p1.popleft())            p2s.append(p2.popleft())            p1s.append(p1.popleft())            p2s.append(p2.popleft())        if p1s[-1] > p2s[-1]:            p1.extend(p2s+p1s)        else:            p2.extend(p1s+p2s)except IndexError:    passfinally:    print(len(p1) > 0)

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
5
  • \$\begingroup\$I don't seem to get the right results when I try this with the test cases. Seejsfiddle.net/xbq5xzco\$\endgroup\$CommentedJun 7, 2016 at 5:52
  • \$\begingroup\$@ChuckMorris Sorry about that, I'd overlooked one of the rules. Should be fixed now.\$\endgroup\$CommentedJun 7, 2016 at 8:51
  • \$\begingroup\$@Mego Try again, I've just updated it.\$\endgroup\$CommentedJun 7, 2016 at 8:53
  • \$\begingroup\$Everything seems to check out now.\$\endgroup\$CommentedJun 7, 2016 at 9:46
  • \$\begingroup\$OK, now I'm impressed!\$\endgroup\$CommentedJun 7, 2016 at 22:51
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