Those test cases handle only the winner’s deck, so I modified your code a little
Here’s the code:
defmodule War do
import Enum
@trace false
defp play(decks) do
round_(:normal, decks, [])
end
defp round_(state, {deck_1, deck_2}, pot) do
if @trace, do: dbg({state, {deck_1, deck_2}, pot})
round(state, {deck_1, deck_2}, pot)
end
defp round(_, {[], []}, _), do: :draw
defp round(_, {p1, []}, _), do: p1
defp round(_, {[], p2}, _), do: p2
defp round(:war, {[c, c1 | d1], [c, c2 | d2]}, pot) do
round_(:war, {d1, d2}, pot ++ [c, c1, c, c2])
end
defp round(:war, {[c11, c12 | d1], [c21, c22 | d2]}, pot) do
round_(:normal, decks(c11, c21, d1, d2, pot ++ [c11, c12, c21, c22]), [])
end
defp round(_, {[c | d1], [c | d2]}, pot) do
round_(:war, {d1, d2}, pot ++ [c, c])
end
defp round(_, {[c1 | d1], [c2 | d2]}, pot) do
round_(:normal, decks(c1, c2, d1, d2, [c1, c2]), pot)
end
defp decks(c1, c2, d1, d2, pot) do
pot = sort(pot, :desc)
if c1 > c2, do: {d1 ++ pot, d2}, else: {d1, d2 ++ pot}
end
defp apply_ace_weight(card) do
(card == 1 && 14) || card
end
defp remove_ace_weight(card) do
(card == 14 && 1) || card
end
def deal(deck) do
deck
|> Enum.map(&apply_ace_weight/1)
|> split(length(deck) * 2)
|> play()
|> case do
xs when is_list(xs) -> Enum.map(xs, &remove_ace_weight/1)
r -> r
end
end
end
ExUnit.start()
defmodule WarTest do
use ExUnit.Case
describe "War" do
test "deal_1" do
t1 = [1,1,1,1,13,13,13,13,11,11,11,11,12,12,12,12,10,10,10,10,9,9,9,9,7,7,7,7,8,8,8,8,6,6,6,6,5,5,5,5,4,4,4,4,3,3,3,3,2,2,2,2]
r1 = [1,1,1,1,13,13,13,13,12,12,12,12,11,11,11,11,10,10,10,10,9,9,9,9,8,8,8,8,7,7,7,7,6,6,6,6,5,5,5,5,4,4,4,4,3,3,3,3,2,2,2,2]
assert War.deal(t1) == r1
end
test "deal_2" do
t2 = [1,13,1,13,1,13,1,13,12,11,12,11,12,11,12,11,10,9,10,9,10,9,10,9,8,7,8,7,8,7,8,7,6,5,6,5,6,5,6,5,4,3,4,3,4,3,4,3,2,2,2,2]
r2 = [4,3,2,2,2,2,4,3,4,3,4,3,6,5,6,5,6,5,6,5,8,7,8,7,8,7,8,7,10,9,10,9,10,9,10,9,12,11,12,11,12,11,12,11,1,13,1,13,1,13,1,13]
assert War.deal(t2) == r2
end
test "deal_3" do
t3 = [13,1,13,1,13,1,13,1,11,12,11,12,11,12,11,12,9,10,9,10,9,10,9,10,7,8,7,8,7,8,7,8,5,6,5,6,5,6,5,6,3,4,3,4,3,4,3,4,2,2,2,2]
r3 = [4,3,2,2,2,2,4,3,4,3,4,3,6,5,6,5,6,5,6,5,8,7,8,7,8,7,8,7,10,9,10,9,10,9,10,9,12,11,12,11,12,11,12,11,1,13,1,13,1,13,1,13]
assert War.deal(t3) == r3
end
test "deal_4" do
t4 = [10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9]
r4 = [1,1,13,12,9,5,11,4,9,3,8,7,7,2,13,10,12,5,10,4,9,6,8,3,1,1,13,12,7,5,11,4,9,3,8,6,7,2,13,10,12,5,11,11,10,8,6,4,6,3,2,2]
assert War.deal(t4) == r4
end
test "deal_5" do
t5 = [1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13]
r5 = [1,10,13,8,11,9,8,7,11,8,13,7,13,6,12,6,9,5,8,5,7,4,7,4,11,6,12,10,6,3,2,2,12,5,9,3,10,4,9,2,10,3,5,2,1,1,1,13,12,11,4,3]
assert War.deal(t5) == r5
end
defp create_deck do
deck =
for n <- 1..13, _ <- 1..4 do
n
end
Enum.shuffle(deck)
end
test "should return the same number of cards after deal" do
deck = create_deck()
assert length(deck) == 52
assert length(War.deal(deck)) == 52
end
test "should remove the ace weight after a win" do
deck = create_deck()
assert Enum.all?(War.deal(deck), &(&1 != 14))
end
end
end
And running with elixir war.ex:
1) test War deal_5 (WarTest)
war.ex:90
Assertion with == failed
code: assert War.deal(t5) == r5
left: [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13
]
right: [
1,
10,
13,
8,
11,
9,
8,
7,
11,
8,
13,
7,
13,
6,
12,
6,
9,
5,
8,
5,
7,
4,
7,
4,
11,
6,
12,
10,
6,
3,
2,
2,
12,
5,
9,
3,
10,
4,
9,
2,
10,
3,
5,
2,
1,
1,
1,
13,
12,
11,
4,
3
]
stacktrace:
war.ex:93: (test)
2) test War deal_2 (WarTest)
war.ex:72
Assertion with == failed
code: assert War.deal(t2) == r2
left: [
1,
13,
1,
13,
1,
13,
1,
13,
12,
11,
12,
11,
12,
11,
12,
11,
10,
9,
10,
9,
10,
9,
10,
9,
8,
7,
8,
7,
8,
7,
8,
7,
6,
5,
6,
5,
6,
5,
6,
5,
4,
3,
4,
3,
4,
3,
4,
3,
2,
2,
2,
2
]
right: [
4,
3,
2,
2,
2,
2,
4,
3,
4,
3,
4,
3,
6,
5,
6,
5,
6,
5,
6,
5,
8,
7,
8,
7,
8,
7,
8,
7,
10,
9,
10,
9,
10,
9,
10,
9,
12,
11,
12,
11,
12,
11,
12,
11,
1,
13,
1,
13,
1,
13,
1,
13
]
stacktrace:
war.ex:75: (test)
3) test War deal_3 (WarTest)
war.ex:78
Assertion with == failed
code: assert War.deal(t3) == r3
left: [
13,
1,
13,
1,
13,
1,
13,
1,
11,
12,
11,
12,
11,
12,
11,
12,
9,
10,
9,
10,
9,
10,
9,
10,
7,
8,
7,
8,
7,
8,
7,
8,
5,
6,
5,
6,
5,
6,
5,
6,
3,
4,
3,
4,
3,
4,
3,
4,
2,
2,
2,
2
]
right: [
4,
3,
2,
2,
2,
2,
4,
3,
4,
3,
4,
3,
6,
5,
6,
5,
6,
5,
6,
5,
8,
7,
8,
7,
8,
7,
8,
7,
10,
9,
10,
9,
10,
9,
10,
9,
12,
11,
12,
11,
12,
11,
12,
11,
1,
13,
1,
13,
1,
13,
1,
13
]
stacktrace:
war.ex:81: (test)
.
4) test War deal_1 (WarTest)
war.ex:66
Assertion with == failed
code: assert War.deal(t1) == r1
left: [
1,
1,
1,
1,
13,
13,
13,
13,
11,
11,
11,
11,
12,
12,
12,
12,
10,
10,
10,
10,
9,
9,
9,
9,
7,
7,
7,
7,
8,
8,
8,
8,
6,
6,
6,
6,
5,
5,
5,
5,
4,
4,
4,
4,
3,
3,
3,
3,
2,
2,
2,
2
]
right: [
1,
1,
1,
1,
13,
13,
13,
13,
12,
12,
12,
12,
11,
11,
11,
11,
10,
10,
10,
10,
9,
9,
9,
9,
8,
8,
8,
8,
7,
7,
7,
7,
6,
6,
6,
6,
5,
5,
5,
5,
4,
4,
4,
4,
3,
3,
3,
3,
2,
2,
2,
2
]
stacktrace:
war.ex:69: (test)
5) test War deal_4 (WarTest)
war.ex:84
Assertion with == failed
code: assert War.deal(t4) == r4
left: [
10,
11,
12,
13,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
1,
2,
3,
4,
5,
6,
7,
8,
9
]
right: [
1,
1,
13,
12,
9,
5,
11,
4,
9,
3,
8,
7,
7,
2,
13,
10,
12,
5,
10,
4,
9,
6,
8,
3,
1,
1,
13,
12,
7,
5,
11,
4,
9,
3,
8,
6,
7,
2,
13,
10,
12,
5,
11,
11,
10,
8,
6,
4,
6,
3,
2,
2
]
stacktrace:
war.ex:87: (test)
Finished in 0.03 seconds (0.03s on load, 0.00s async, 0.00s sync)
7 tests, 5 failures






















