Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed example.ex format
  • Loading branch information
FraSanga committed Sep 10, 2025
commit 33eea8c08ba515e7b895763856923117e505df57
84 changes: 70 additions & 14 deletions exercises/practice/camicia/.meta/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,106 @@ defmodule Camicia do
do_simulate(format_deck(playerA), format_deck(playerB), MapSet.new(), 0, 0, :playerA)
end

defp do_simulate([], _playerB, _seen, cards, tricks, _current_player), do: {:finished, cards, tricks}
defp do_simulate(_playerA, [], _seen, cards, tricks, _current_player), do: {:finished, cards, tricks}
defp do_simulate([], _playerB, _seen, cards, tricks, _current_player),
do: {:finished, cards, tricks}

defp do_simulate(_playerA, [], _seen, cards, tricks, _current_player),
do: {:finished, cards, tricks}

defp do_simulate(playerA, playerB, seen, cards, tricks, current_player) do
state = {playerA, playerB}

if MapSet.member?(seen, state) do
{:loop, cards, tricks}
else
new_seen = MapSet.put(seen, state)
{winner, loser, new_cards, new_tricks, next_player} = case current_player do
:playerA -> round(playerA, playerB, cards, tricks, current_player)
:playerB -> round(playerB, playerA, cards, tricks, current_player)
end

{winner, loser, new_cards, new_tricks, next_player} =
case current_player do
:playerA -> round(playerA, playerB, cards, tricks, current_player)
:playerB -> round(playerB, playerA, cards, tricks, current_player)
end

case next_player do
:playerA -> do_simulate(winner, loser, new_seen, new_cards, new_tricks, next_player)
:playerB -> do_simulate(loser, winner, new_seen, new_cards, new_tricks, next_player)
end
end
end

defp round(player, opponent, cards, tricks, current_player), do:
do_round(player, opponent, cards, tricks, :number, [], current_player)
defp round(player, opponent, cards, tricks, current_player),
do: do_round(player, opponent, cards, tricks, :number, [], current_player)

defp do_round([], opponent, cards, tricks, _type, central_pile, current_player) do
{opponent ++ Enum.reverse(central_pile), [], cards, tricks + 1, change_player(current_player)}
end

defp do_round([card | rest], opponent, cards, tricks, :number, central_pile, current_player) do
case card do
"-" ->
do_round(opponent, rest, cards + 1, tricks, :number, [card | central_pile], change_player(current_player))
do_round(
opponent,
rest,
cards + 1,
tricks,
:number,
[card | central_pile],
change_player(current_player)
)

payment_card when payment_card in ["J", "Q", "K", "A"] ->
count = Map.get(@counts, payment_card)
do_round(opponent, rest, cards + 1, tricks, {:payment, count}, [card | central_pile], change_player(current_player))

do_round(
opponent,
rest,
cards + 1,
tricks,
{:payment, count},
[card | central_pile],
change_player(current_player)
)
end
end

defp do_round(player, opponent, cards, tricks, {:payment, 0}, central_pile, current_player) do
{opponent ++ Enum.reverse(central_pile), player, cards, tricks + 1, change_player(current_player)}
{opponent ++ Enum.reverse(central_pile), player, cards, tricks + 1,
change_player(current_player)}
end
defp do_round([card | rest], opponent, cards, tricks, {:payment, count}, central_pile, current_player) do

defp do_round(
[card | rest],
opponent,
cards,
tricks,
{:payment, count},
central_pile,
current_player
) do
case card do
"-" ->
do_round(rest, opponent, cards + 1, tricks, {:payment, count - 1}, [card | central_pile], current_player)
do_round(
rest,
opponent,
cards + 1,
tricks,
{:payment, count - 1},
[card | central_pile],
current_player
)

payment_card when payment_card in ["J", "Q", "K", "A"] ->
count = Map.get(@counts, payment_card)
do_round(opponent, rest, cards + 1, tricks, {:payment, count}, [card | central_pile], change_player(current_player))

do_round(
opponent,
rest,
cards + 1,
tricks,
{:payment, count},
[card | central_pile],
change_player(current_player)
)
end
end

Expand Down