<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="280853" data-post-id="280853">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zoedsoupe" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zoedsoupe/120/25602_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zoedsoupe
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>like that:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
defmodule War do
  @doc """
  The main module to the challenge.
  This module exposes a deal/1 function to play the game.
  """

  # prefers to use a weight as we don't
  # need to represent the card suite
  @ace_weight 14

  def deal(deck) do
    {p1, p2} =
      deck
      |&gt; Enum.map(&amp;apply_ace_weight/1)
      |&gt; deal_deck()

    winner = play_game(p1, p2)

    Enum.map(winner, &amp;remove_ace_weight/1)
  end

  def deal_deck(deck) do
    {Enum.take_every(deck, 2), Enum.drop_every(deck, 2)}
  end

  defp play_game(p1, p2, tied \\ [])

  defp play_game([], p2, tied), do: p2 ++ tied
  defp play_game(p1, [], tied), do: p1 ++ tied

  # War, cards tied
  defp play_game([c | xs], [c | ys], tied) do
    cards = Enum.sort([c, c] ++ tied, :desc)
    play_game(xs, ys, cards)
  end

  # Normal game turn
  defp play_game([x | xs], [y | ys], tied) do
    cards = Enum.sort([x, y] ++ tied, :desc)

    if x &gt; y do
      play_game(xs ++ cards, ys)
    else
      play_game(xs, ys ++ cards)
    end
  end

  defp apply_ace_weight(card) do
    (card == 1 &amp;&amp; @ace_weight) || card
  end

  defp remove_ace_weight(card) do
    (card == @ace_weight &amp;&amp; 1) || card
  end
end

</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="280853" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-280853" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="280853"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
    <div class="postbit" id="280854" data-post-id="280854">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Are you sure this matches the requirements given?<br>
did you try to step through this with a really small deck?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="280854" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-280854" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="280854"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #32"></div>
  </section>
</div>
    <div class="postbit" id="280893" data-post-id="280893">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zoedsoupe" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zoedsoupe/120/25602_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zoedsoupe
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yeah, I will pass through each requirement and show my code.</p>
<p>the full requirement is here:</p>
<aside class="quote no-group quote-modified" data-username="zoedsoupe" data-post="13" data-topic="54134">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zoedsoupe/48/25602_2.png" class="avatar"> zoedsoupe:</div>
<blockquote>
<p>Preamble</p>
<blockquote>
<p>In this project, you will simulate a game of War. War is a simple card game between two players. A description of the game and its rules can be found here:<br>
<a href="https://bicyclecards.com/how-to-play/war" rel="noopener nofollow ugc">War </a><br>
Given the above description, there are still some situations that are ambiguous. They will be clarified in the game description below.<br>
You will complete this project in three of the four languages we study in this course. The languages chosen are up to you. In addition to the general requirements of the project, each language comes with its own language-specific constraints. These specify the format of the input and output, as well as submission instructions for each language.<br>
Aside from these requirements, anything you do inside your program is up to you. Use as many helper functions or methods as you want, use any syntax you find useful whether we covered it in class or not. There is one exception to this: you may not use any functionality that is not part of the base installation of each language. No 3rd party libraries. Your submission will be tested using out-of-the-box installations of each language.</p>
</blockquote>
<p>Game description</p>
<blockquote>
<p>The game starts with a shuffled deck of cards. The deck will be passed into your program already shuffled (details below). The cards are dealt in an alternating fashion to each player, so that each player has 26 cards.<br>
In each round, both players reveal the top card of their pile. The player with the higher card (by rank) wins both cards, placing them at the bottom of their pile. Aces are considered high, meaning the card ranks in ascending order are 2-10, Jack, Queen, King, Ace.<br>
If the revealed cards are tied, there is war! Each player turns up one card face down followed by one card face up. The player with the higher face-up card takes both piles (six cards – the two original cards that were tied, plus the four cards from the war). If the turned-up cards are again the same rank, each player places another card face down and turns another card face up. The player with the higher card takes all 10 cards, and so on.<br>
When one player runs out of cards, they are the loser, and the other the winner. If, during a war, a player runs out of cards, this counts as a loss as well.</p>
</blockquote>
<p>Technical details:</p>
<blockquote>
<p>Input: The input to your program, representing a shuffled deck of cards, will be a permutation of 52 integers, where each integer between 1-13 occurs four times. The integers in this permutation correspond to cards according to the following table (four kings, four tens, four threes, and so on). Notice that we don’t bother representing the suit because the game of War doesn’t require it.<br>
The game: Your program will deal two piles from the input permutation. How you represent your piles is completely up to you. Once the piles are dealt, “play” the game in your program until one player runs out of cards. Once again, how you manage your piles during the game is completely up to you. Keep going until one player runs out of cards.<br>
When cards are added to the bottom of a player’s pile, they should be added in decreasing order by rank. That is, first place the highest ranked card on the bottom, then place the next highest ranked card beneath that. This is true of wars as well. If a player wins six cards as a result of a war, those cards should be added to the bottom starting with the highest rank and ending with the smallest. Ace has the highest rank, Two has the lowest.<br>
Output: Your program will return the pile of the winning player. This pile should contain all 52 integers from the original input permutation and be in the correct order according to how the game played out.</p>
</blockquote>
</blockquote>
</aside>
<p>So let’s dig it:</p>
<blockquote>
<p>The game starts with a shuffled deck of cards. The deck will be passed into your program already shuffled<br>
Ok, nothing to do here!</p>
</blockquote>
<blockquote>
<p>The cards are dealt in an alternating fashion to each player, so that each player has 26 cards.<br>
So, talking about code, the first player will get cards with a odd indexes and player two cards with even indexes, which results in:</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir"> def deal_deck(deck) do
    {Enum.take_every(deck, 2), Enum.drop_every(deck, 2)}
  end
</code></pre>
<blockquote>
<p>In each round, both players reveal the top card of their pile</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp play_game([x | xs], [y | ys], tied)
</code></pre>
<blockquote>
<p>The player with the higher card (by rank) wins both cards, placing them at the bottom of their pile</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">if x &gt; y do
  play_game(xs ++ cards, ys)
else
  play_game(xs, ys ++ cards)
end
</code></pre>
<blockquote>
<p>If the revealed cards are tied, there is war! Each player turns up one card face down followed by one card face up. The player with the higher face-up card takes both piles (six cards – the two original cards that were tied, plus the four cards from the war)</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir"># War, cards tied
  defp play_game([c | xs], [c | ys], tied) do
    cards = Enum.sort([c, c] ++ tied, :desc)
    play_game(xs, ys, cards)
  end
</code></pre>
<blockquote>
<p>When one player runs out of cards, they are the loser, and the other the winner. If, during a war, a player runs out of cards, this counts as a loss as well.</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp play_game([], p2, tied), do: p2 ++ tied
defp play_game(p1, [], tied), do: p1 ++ tied
</code></pre>
<blockquote>
<p>When cards are added to the bottom of a player’s pile, they should be added in decreasing order by rank. That is, first place the highest ranked card on the bottom, then place the next highest ranked card beneath that. This is true of wars as well.<br>
I sort all cards always in descending order by rank, both in war and normal rounds</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir"># War, cards tied
  defp play_game([c | xs], [c | ys], tied) do
    cards = Enum.sort([c, c] ++ tied, :desc)
    play_game(xs, ys, cards)
  end

  # Normal game turn
  defp play_game([x | xs], [y | ys], tied) do
    cards = Enum.sort([x, y] ++ tied, :desc)

    if x &gt; y do
      play_game(xs ++ cards, ys)
    else
      play_game(xs, ys ++ cards)
    end
  end
</code></pre>
<p>So it matches all the requirements, so I can’t see why those test cases are failing (considering they are right)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="280893" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-280893" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="280893"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #33"></div>
  </section>
</div>
    <div class="postbit" id="280895" data-post-id="280895">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="zoedsoupe" data-post="34" data-topic="54134">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zoedsoupe/48/25602_2.png" class="avatar"> zoedsoupe:</div>
<blockquote>
<p>If the revealed cards are tied, there is war! Each player turns up one card face down followed by one card face up</p>
</blockquote>
</aside>
<p>I understood this part differently. Where is your second card?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="280895" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-280895" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="280895"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #34"></div>
  </section>
</div>
    <div class="postbit" id="280900" data-post-id="280900">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zoedsoupe" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zoedsoupe/120/25602_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zoedsoupe
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>These face-up cards will be the next two to be compared, so I call <code>play_game/3</code> again so the next round will firstly be treated as a normal one</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="280900" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/36">Post #35</a>
	                </div>
	            </div>
              <div id="likers-container-280900" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="280900"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #35"></div>
  </section>
</div>
    <div class="postbit" id="280904" data-post-id="280904">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If one player wins the war in the first war-turn he will then get the two <code>tied</code> cards from before and the two currently compared. A total of four. The rules explicitly state that it should be six. You are missing two. I may also be wrong, I did this a long time ago. Nearly a week, who can understand code that he wrote a week ago, certainly not me.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="280904" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/37">Post #36</a>
	                </div>
	            </div>
              <div id="likers-container-280904" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="280904"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #36"></div>
  </section>
</div>
    <div class="postbit" id="281288" data-post-id="281288">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I had some time this morning to look into this again.</p>
<p>This is a great exercise, as you really have to read the instructions carefully and also have to apply some common knowledge. There was an error in the solution I posted: when there is WAR, the first card is face down, the <strong>second</strong> is compared.</p>
<p>When getting a shuffled deck and you only have to deal, you have to keep in mind, that the cards are dealt on top of each other (assumed common knowledge). So the <strong>last</strong> two cards in the deck are those, that are used in the first round (hint: <code>reverse</code>).<br>
Also, it is assumed, that player one is the dealer and so player 2 gets the first card!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281288" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/38">Post #37</a>
	                </div>
	            </div>
              <div id="likers-container-281288" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281288"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #37"></div>
  </section>
</div>
    <div class="postbit" id="281420" data-post-id="281420">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="waheed-fullstack-eng" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/waheed-fullstack-eng/120/25708_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  waheed-fullstack-eng
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Long time ago, I did some work on client card game project, I did something like that.</p>
<p>defmodule War do</p>
<p>def play_game(deck) do<br>
[player1, player2] = deal_cards(deck)</p>
<pre><code>result = play_rounds(player1, player2)
IO.inspect(result)
result
</code></pre>
<p>end</p>
<p>def deal_cards(deck) do<br>
deck |&gt; Enum.chunk_every(26)<br>
end</p>
<p>def play_rounds(player1, player2) do<br>
case {player1, player2} do<br>
{<span class="chcklst-box fa fa-square-o"></span>, <span class="chcklst-box fa fa-square-o"></span>} → <br>
{:tie, <span class="chcklst-box fa fa-square-o"></span>}</p>
<pre><code>  {[], player2_deck} -&gt;
    {:player2, player2_deck}

  {player1_deck, []} -&gt;
    {:player1, player1_deck}

  {[card1 | hand1_without_card], [card2 | hand2_without_card]} -&gt;
    case compare_cards(card1, card2) do
      :win -&gt;
        play_rounds(hand1_without_card ++ [card1, card2], hand2_without_card)

      :lose -&gt;
        play_rounds(hand1_without_card, hand2_without_card ++ [card1, card2])

      :tie -&gt;
        play_war(hand1_without_card, hand2_without_card, [card1, card2])
    end
end
</code></pre>
<p>end</p>
<h1><a name="p-281420-we-assume-both-cards-are-at-same-rank-for-a-war-1" class="anchor" href="#p-281420-we-assume-both-cards-are-at-same-rank-for-a-war-1" aria-label="Heading link" rel="nofollow"></a>We assume both cards are at same rank for a WAR.</h1>
<p>def compare_cards(rank1, rank2) when rank1 == rank2, do: :tie<br>
def compare_cards(1, <em>), do: :win<br>
def compare_cards(</em>, 1), do: :lose<br>
def compare_cards(rank1, rank2) when rank1 &gt; rank2 or rank1 == 1, do: :win<br>
def compare_cards(rank1, rank2) when rank1 &lt; rank2 or rank2 == 1, do: :lose</p>
<h1><a name="p-281420-when-both-the-players-have-only-one-card-in-their-hand-in-war-2" class="anchor" href="#p-281420-when-both-the-players-have-only-one-card-in-their-hand-in-war-2" aria-label="Heading link" rel="nofollow"></a>When both the players have only one card in their hand in war.</h1>
<p>def play_war([a], [b], _) when a == b and is_integer(a), do: {:tie, <span class="chcklst-box fa fa-square-o"></span>}<br>
def play_war([a], [b], _) when a &gt; b and is_integer(a), do: :win<br>
def play_war([a], [b], _) when a &lt; b and is_integer(a), do: :lose</p>
<h1><a name="p-281420-when-1-of-the-player-have-only-one-card-3" class="anchor" href="#p-281420-when-1-of-the-player-have-only-one-card-3" aria-label="Heading link" rel="nofollow"></a>When 1 of the player have only one card.</h1>
<p>def play_war([a], [b | _], _) when a &gt; b and is_integer(a), do: :win<br>
def play_war([a | _], [b], _) when a &lt; b and is_integer(a), do: :lose</p>
<h1><a name="p-281420-h-4" class="anchor" href="#p-281420-h-4" aria-label="Heading link" rel="nofollow"></a></h1>
<p>def play_war(<span class="chcklst-box fa fa-square-o"></span>, <span class="chcklst-box fa fa-square-o"></span>, <em>), do: {:tie, []}<br>
def play_war(</em>, <span class="chcklst-box fa fa-square-o"></span>, _), do: :win<br>
def play_war(<span class="chcklst-box fa fa-square-o"></span>, _, _), do: :lose</p>
<p>def play_war(hand1, hand2, pile) do<br>
[face_down_c1, card1 | rest1] = hand1<br>
[face_down_c2, card2 | rest2] = hand2</p>
<pre><code>face_down_cards = [face_down_c1, face_down_c2]

case compare_cards(card1, card2) do
  :win -&gt;
    play_rounds(rest1 ++ pile ++ face_down_cards ++ [card1, card2], rest2)

  :lose -&gt;
    play_rounds(rest1, rest2 ++ pile ++ [card1, card2] ++ face_down_cards)

  :tie -&gt;
    # Again a war.
    play_war(rest1, rest2, pile)
end
</code></pre>
<p>end<br>
end</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281420" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/39">Post #38</a>
	                </div>
	            </div>
              <div id="likers-container-281420" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281420"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #38"></div>
  </section>
</div>
    <div class="postbit" id="281421" data-post-id="281421">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Sebb" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Sebb/120/21818_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Sebb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>could you please fix your codeblocks?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281421" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/40">Post #39</a>
	                </div>
	            </div>
              <div id="likers-container-281421" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281421"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #39"></div>
  </section>
</div>
    <div class="postbit" id="281519" data-post-id="281519">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="waheed-fullstack-eng" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/waheed-fullstack-eng/120/25708_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  waheed-fullstack-eng
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>defmodule War do<br>
def play_game(deck) do<br>
[player1, player2] = deal_cards(deck)<br>
play_rounds(player1, player2)<br>
end</p>
<p>def deal_cards(deck) do<br>
deck |&gt; Enum.chunk_every(26)<br>
end</p>
<p>def play_rounds(player1, player2) do<br>
case {player1, player2} do<br>
{<span class="chcklst-box fa fa-square-o"></span>, <span class="chcklst-box fa fa-square-o"></span>} → <br>
{:tie, <span class="chcklst-box fa fa-square-o"></span>}</p>
<pre><code>  {[], player2_deck} -&gt;
    {:player2, player2_deck}

  {player1_deck, []} -&gt;
    {:player1, player1_deck}

  {[card1 | hand1_without_card], [card2 | hand2_without_card]} -&gt;
    case compare_cards(card1, card2) do
      :win -&gt;
        play_rounds(hand1_without_card ++ [card1, card2], hand2_without_card)

      :lose -&gt;
        play_rounds(hand1_without_card, hand2_without_card ++ [card1, card2])

      :tie -&gt;
        play_war(hand1_without_card, hand2_without_card, [card1, card2])
    end
end
</code></pre>
<p>end</p>
<h1>We assume both cards are at same rank for a WAR.</h1>
<p>def compare_cards(rank1, rank2) when rank1 == rank2, do: :tie<br>
def compare_cards(1, <em>), do: :win<br>
def compare_cards(</em>, 1), do: :lose<br>
def compare_cards(rank1, rank2) when rank1 &gt; rank2 or rank1 == 1, do: :win<br>
def compare_cards(rank1, rank2) when rank1 &lt; rank2 or rank2 == 1, do: :lose</p>
<h1>When both the players have only one card in their hand in war.</h1>
<p>def play_war([a], [b], _) when a == b and is_integer(a), do: {:tie, <span class="chcklst-box fa fa-square-o"></span>}<br>
def play_war([a], [b], _) when a &gt; b and is_integer(a), do: :win<br>
def play_war([a], [b], _) when a &lt; b and is_integer(a), do: :lose</p>
<h1>When 1 of the player have only one card.</h1>
<p>def play_war([a], [b | _], _) when a &gt; b and is_integer(a), do: :win<br>
def play_war([a | _], [b], _) when a &lt; b and is_integer(a), do: :lose</p>
<h1></h1>
<p>def play_war(<span class="chcklst-box fa fa-square-o"></span>, <span class="chcklst-box fa fa-square-o"></span>, <em>), do: {:tie, []}<br>
def play_war(</em>, <span class="chcklst-box fa fa-square-o"></span>, _), do: :win<br>
def play_war(<span class="chcklst-box fa fa-square-o"></span>, _, _), do: :lose</p>
<p>def play_war(hand1, hand2, pile) do<br>
[face_down_c1, card1 | rest1] = hand1<br>
[face_down_c2, card2 | rest2] = hand2</p>
<pre><code>face_down_cards = [face_down_c1, face_down_c2]

case compare_cards(card1, card2) do
  :win -&gt;
    play_rounds(rest1 ++ pile ++ face_down_cards ++ [card1, card2], rest2)

  :lose -&gt;
    play_rounds(rest1, rest2 ++ pile ++ [card1, card2] ++ face_down_cards)

  :tie -&gt;
    # Again a war.
    play_war(rest1, rest2, pile)
end
</code></pre>
<p>end<br>
end</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281519" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/trying-to-implement-war-card-game-in-elixir/54134/41">Post #40</a>
	                </div>
	            </div>
              <div id="likers-container-281519" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281519"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #40"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/54134/load_more?page=5">Load more posts (1 remaining)</a>
</div></template></turbo-stream>