Compare commits
No commits in common. "ee9f5e11870182a97ec7d517402b63db961a1ed6" and "03af54b32f7ed174b1d9dc4a9d5997589002750c" have entirely different histories.
ee9f5e1187
...
03af54b32f
@ -1,6 +1,6 @@
|
|||||||
defmodule Utils do
|
defmodule Utils do
|
||||||
|
|
||||||
@min_print_level 3
|
@min_print_level 1
|
||||||
|
|
||||||
def safecast(p, m) when p == nil, do: IO.puts('Trying to safecast #{m} with p as nil')
|
def safecast(p, m) when p == nil, do: IO.puts('Trying to safecast #{m} with p as nil')
|
||||||
def safecast(p, m) when is_pid(p), do: send(p, m)
|
def safecast(p, m) when is_pid(p), do: send(p, m)
|
||||||
|
395
lib/server.ex
395
lib/server.ex
@ -103,9 +103,7 @@ defmodule Server do
|
|||||||
state
|
state
|
||||||
|
|
||||||
{:get_game_state, game_id, pid_to_inform} ->
|
{:get_game_state, game_id, pid_to_inform} ->
|
||||||
state = get_game_state(state, game_id, pid_to_inform)
|
get_game_state(state, game_id, pid_to_inform)
|
||||||
set_modifed(state, game_id)
|
|
||||||
|
|
||||||
|
|
||||||
{:make_move, game_id, move, pid_to_inform} ->
|
{:make_move, game_id, move, pid_to_inform} ->
|
||||||
try_to_play_checks(state, game_id, move, pid_to_inform)
|
try_to_play_checks(state, game_id, move, pid_to_inform)
|
||||||
@ -139,29 +137,14 @@ defmodule Server do
|
|||||||
safecast(pid_to_inform, {:make_move, game_id, :player_moved_before, game.game_state, game.hand})
|
safecast(pid_to_inform, {:make_move, game_id, :player_moved_before, game.game_state, game.hand})
|
||||||
set_modifed(state, game_id)
|
set_modifed(state, game_id)
|
||||||
true ->
|
true ->
|
||||||
cond do
|
try_to_play(state, game_id, move, pid_to_inform)
|
||||||
not is_number(move) ->
|
|
||||||
safecast(pid_to_inform, {:make_move, game_id, :invalid_move})
|
|
||||||
state
|
|
||||||
move >= length(game.game_state) ->
|
|
||||||
safecast(pid_to_inform, {:make_move, game_id, :invalid_move})
|
|
||||||
state
|
|
||||||
true ->
|
|
||||||
try_to_play(state, game_id, move, pid_to_inform)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp try_to_play(state, game_id, move, pid_to_inform) do
|
defp try_to_play(state, game_id, move, pid_to_inform) do
|
||||||
name = state.name
|
name = state.name
|
||||||
|
new_hand = get_hand_for_game_state(state.games[game_id].game_state)
|
||||||
new_hand = if state.games[game_id].hand[state.name] == :- do
|
|
||||||
state.games[game_id].game_state[move]
|
|
||||||
else
|
|
||||||
get_hand_for_game_state(state.games[game_id].game_state)
|
|
||||||
end
|
|
||||||
|
|
||||||
try_propose {:make_move, game_id, name, move, new_hand}
|
try_propose {:make_move, game_id, name, move, new_hand}
|
||||||
do
|
do
|
||||||
{:decision, {:make_move, ^game_id, ^name, ^move, ^new_hand}} ->
|
{:decision, {:make_move, ^game_id, ^name, ^move, ^new_hand}} ->
|
||||||
@ -224,15 +207,13 @@ defmodule Server do
|
|||||||
|
|
||||||
defp get_hand_for_game_state(game_state) do
|
defp get_hand_for_game_state(game_state) do
|
||||||
r1 = Enum.random(0..100)
|
r1 = Enum.random(0..100)
|
||||||
cond do
|
if r1 <= 10 do
|
||||||
r1 <= 1 -> :b
|
:+
|
||||||
r1 <= 5 -> :-
|
else
|
||||||
r1 <= 20 -> :+
|
mx = game_state |> Enum.filter(fn m -> m != :+ end) |> Enum.max()
|
||||||
true ->
|
mn = max(mx - 20, 1)
|
||||||
mx = game_state |> Enum.filter(fn m -> m != :+ end) |> Enum.max()
|
mx = max(mx - 2, 4)
|
||||||
mn = max(mx - 20, 1)
|
Enum.random(mn..mx)
|
||||||
mx = max(mx - 2, 4)
|
|
||||||
Enum.random(mn..mx)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -274,7 +255,7 @@ defmodule Server do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp set_modifed(state, game, val \\ false) do
|
defp set_modifed(state, game, val \\ false) do
|
||||||
or_state not is_finished(state, game) and state.games[game] != :not_playing_in_game and state.games[game] != nil do
|
or_state not is_finished(state, game) do
|
||||||
%{state | games: Map.put(state.games, game, %{state.games[game] | modified: val})}
|
%{state | games: Map.put(state.games, game, %{state.games[game] | modified: val})}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -308,7 +289,6 @@ defmodule Server do
|
|||||||
if b == a do
|
if b == a do
|
||||||
case b do
|
case b do
|
||||||
:+ -> simplify_game_state_pluses(tl, indexed_game_state)
|
:+ -> simplify_game_state_pluses(tl, indexed_game_state)
|
||||||
:b -> simplify_game_state_pluses(tl, indexed_game_state)
|
|
||||||
n ->
|
n ->
|
||||||
list =
|
list =
|
||||||
indexed_game_state |>
|
indexed_game_state |>
|
||||||
@ -330,33 +310,6 @@ defmodule Server do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp simplify_game_state_pluses([{:b, i} | tl], indexed_game_state) do
|
|
||||||
before_i = get_index(indexed_game_state, i, -1)
|
|
||||||
after_i = get_index(indexed_game_state, i, 1)
|
|
||||||
|
|
||||||
if before_i != after_i do
|
|
||||||
{b, b_i} = Enum.at(indexed_game_state, before_i)
|
|
||||||
{a, a_i} = Enum.at(indexed_game_state, after_i)
|
|
||||||
|
|
||||||
a = if is_atom(a) do 1 else a end
|
|
||||||
b = if is_atom(b) do 1 else b end
|
|
||||||
|
|
||||||
list =
|
|
||||||
indexed_game_state |>
|
|
||||||
Enum.map(fn {x, ti} -> if ti == i, do: {{:merged, trunc(:math.floor((a + b) / 2))}, i}, else: {x, ti} end) |>
|
|
||||||
Enum.filter(fn {x, ti} -> cond do
|
|
||||||
b_i == ti -> false
|
|
||||||
a_i == ti -> false
|
|
||||||
true -> true
|
|
||||||
end
|
|
||||||
end) |>
|
|
||||||
reindex()
|
|
||||||
{true, expand_merge(list)}
|
|
||||||
else
|
|
||||||
simplify_game_state_pluses(tl, indexed_game_state)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp is_merged(item) do
|
defp is_merged(item) do
|
||||||
case item do
|
case item do
|
||||||
{:merged, _} -> true
|
{:merged, _} -> true
|
||||||
@ -379,7 +332,7 @@ defmodule Server do
|
|||||||
{:merged, _} -> indexed_game_state
|
{:merged, _} -> indexed_game_state
|
||||||
_ ->
|
_ ->
|
||||||
indexed_game_state |>
|
indexed_game_state |>
|
||||||
Enum.map(fn {x, ti} -> if ti == i, do: {{:merged, max(n, a) + 1}, i}, else: {x, ti} end) |>
|
Enum.map(fn {x, ti} -> if ti == i, do: {{:merged, n + 1}, i}, else: {x, ti} end) |>
|
||||||
Enum.filter(fn {x, ti} -> cond do
|
Enum.filter(fn {x, ti} -> cond do
|
||||||
b_i == ti -> false
|
b_i == ti -> false
|
||||||
a_i == ti -> false
|
a_i == ti -> false
|
||||||
@ -409,16 +362,24 @@ defmodule Server do
|
|||||||
|
|
||||||
defp remove_merged([], rec, _), do: rec
|
defp remove_merged([], rec, _), do: rec
|
||||||
|
|
||||||
defp remove_merged([{:merged, n} | tl], rec, add),
|
defp remove_merged([{:merged, n} | tl], rec, add) do
|
||||||
do: if add, do: remove_merged(tl, rec ++ [n], false),
|
if add do
|
||||||
else: remove_merged(tl, rec, false)
|
remove_merged(tl, rec ++ [n], false)
|
||||||
|
else
|
||||||
|
remove_merged(tl, rec, false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp remove_merged([n | tl], rec, add), do:
|
defp remove_merged([n | tl], rec, add), do:
|
||||||
remove_merged(tl, rec ++ [n], add)
|
remove_merged(tl, rec ++ [n], add)
|
||||||
|
|
||||||
defp remove_merged(list), do: remove_merged(list, [], true)
|
defp remove_merged(list) do
|
||||||
|
log("#{inspect(list)}")
|
||||||
|
remove_merged(list, [], true)
|
||||||
|
end
|
||||||
|
|
||||||
defp simplify_game_state(game_state) do
|
defp simplify_game_state(game_state) do
|
||||||
|
log("game_state: #{inspect(game_state)}")
|
||||||
indexed_game_state =
|
indexed_game_state =
|
||||||
game_state |>
|
game_state |>
|
||||||
reindex(false)
|
reindex(false)
|
||||||
@ -427,12 +388,13 @@ defmodule Server do
|
|||||||
indexed_game_state |>
|
indexed_game_state |>
|
||||||
Enum.filter(fn x -> case x do
|
Enum.filter(fn x -> case x do
|
||||||
{:+, _} -> true
|
{:+, _} -> true
|
||||||
{:b, _} -> true
|
|
||||||
_ -> false
|
_ -> false
|
||||||
end
|
end
|
||||||
end) |>
|
end) |>
|
||||||
simplify_game_state_pluses(indexed_game_state)
|
simplify_game_state_pluses(indexed_game_state)
|
||||||
|
|
||||||
|
log("game_state2: #{inspect(indexed_game_state)}")
|
||||||
|
|
||||||
if repeat do
|
if repeat do
|
||||||
indexed_game_state |>
|
indexed_game_state |>
|
||||||
Enum.map(fn {v, _} -> v end) |>
|
Enum.map(fn {v, _} -> v end) |>
|
||||||
@ -452,23 +414,14 @@ defmodule Server do
|
|||||||
%{state | instance: state.instance + 1 }
|
%{state | instance: state.instance + 1 }
|
||||||
game ->
|
game ->
|
||||||
game_state = game.game_state
|
game_state = game.game_state
|
||||||
game_state = if game.hand[player_name] == :- do
|
{b, e} = Enum.split(game_state, pos_move)
|
||||||
List.delete_at(game_state, pos_move)
|
game_state = b ++ [ game.hand[player_name] ] ++ e
|
||||||
else
|
|
||||||
{b, e} = Enum.split(game_state, pos_move)
|
|
||||||
b ++ [ game.hand[player_name] ] ++ e
|
|
||||||
end
|
|
||||||
game_state = simplify_game_state(game_state)
|
game_state = simplify_game_state(game_state)
|
||||||
hand = Map.put(game.hand, player_name, new_hand)
|
hand = Map.put(game.hand, player_name, new_hand)
|
||||||
game = %{game| hand: hand, game_state: game_state, modified: true }
|
game = %{game| hand: hand, game_state: game_state }
|
||||||
|
|
||||||
if length(game.game_state) > 16 do
|
if length(game.game_state) > 15 do
|
||||||
%{state| games: Map.put(state.games, game_id, {
|
%{state| games: Map.put(state.games, game_id, {:finished, Enum.sum(game.game_state)}), instance: state.instance + 1}
|
||||||
:finished,
|
|
||||||
game.game_state |>
|
|
||||||
Enum.filter(fn x -> not is_atom(x) end) |>
|
|
||||||
Enum.sum()}), instance: state.instance + 1
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
%{state| games: Map.put(state.games, game_id, game), instance: state.instance + 1}
|
%{state| games: Map.put(state.games, game_id, game), instance: state.instance + 1}
|
||||||
end
|
end
|
||||||
@ -518,12 +471,15 @@ defmodule Server do
|
|||||||
{:game_state, ^v, :not_playing} ->
|
{:game_state, ^v, :not_playing} ->
|
||||||
log("Not Playing in that game")
|
log("Not Playing in that game")
|
||||||
{:not_playing}
|
{:not_playing}
|
||||||
{:game_state, ^v, :game_finished, score} ->
|
|
||||||
log("Game finsihed, #{score}")
|
|
||||||
{:game_finished, score}
|
|
||||||
{:game_state, ^v, game_state, hand} ->
|
{:game_state, ^v, game_state, hand} ->
|
||||||
log("Got game state, #{inspect(game_state)}, hand: #{inspect(hand)}")
|
log("Got game state, #{inspect(game_state)}, hand: #{inspect(hand)}")
|
||||||
{:state, game_state, hand}
|
{:state, game_state, hand}
|
||||||
|
{:game_state, ^v, :not_playing} ->
|
||||||
|
log("Not Playing in that game")
|
||||||
|
{:not_playing}
|
||||||
|
{:game_state, ^v, :game_finished, score} ->
|
||||||
|
log("Game finsihed, #{score}")
|
||||||
|
{:game_finished, score}
|
||||||
{:game_state, ^v, :game_does_not_exist} ->
|
{:game_state, ^v, :game_does_not_exist} ->
|
||||||
log("Got game does not exist")
|
log("Got game does not exist")
|
||||||
{:not_exists}
|
{:not_exists}
|
||||||
@ -547,9 +503,6 @@ defmodule Server do
|
|||||||
{:make_move, ^v, :player_moved_before, game_state, hand} ->
|
{:make_move, ^v, :player_moved_before, game_state, hand} ->
|
||||||
log("Player moved_before, #{inspect(game_state)} #{inspect(hand)}")
|
log("Player moved_before, #{inspect(game_state)} #{inspect(hand)}")
|
||||||
{:player_moved_before, game_state, hand}
|
{:player_moved_before, game_state, hand}
|
||||||
{:make_move, ^v, :invalid_move} ->
|
|
||||||
log("Invalid Move")
|
|
||||||
{:invalid_move}
|
|
||||||
{:make_move, ^v, game_state, hand} ->
|
{:make_move, ^v, game_state, hand} ->
|
||||||
log("Got game state, #{inspect(game_state)}, hand: #{inspect(hand)}")
|
log("Got game state, #{inspect(game_state)}, hand: #{inspect(hand)}")
|
||||||
{:state, game_state, hand}
|
{:state, game_state, hand}
|
||||||
@ -581,233 +534,87 @@ defmodule Client do
|
|||||||
create_log 3
|
create_log 3
|
||||||
|
|
||||||
def play_game(process, game_id) do
|
def play_game(process, game_id) do
|
||||||
game = Server.get_game_state(process, game_id)
|
|
||||||
|
|
||||||
cont = case game do
|
|
||||||
:timeout ->
|
|
||||||
log("Could to not comunicate with the server")
|
|
||||||
true
|
|
||||||
{:not_exists} ->
|
|
||||||
log("Game does not exist")
|
|
||||||
true
|
|
||||||
{:not_playing} ->
|
|
||||||
log("Not Playing in that game")
|
|
||||||
true
|
|
||||||
{:game_finished, score} ->
|
|
||||||
log("Game finsihed, #{score}")
|
|
||||||
true
|
|
||||||
{:game_does_not_exist} ->
|
|
||||||
log("Got game does not exist")
|
|
||||||
true
|
|
||||||
{:state, game_state, hand} ->
|
|
||||||
game_state |>
|
|
||||||
to_name() |>
|
|
||||||
interpolate() |>
|
|
||||||
fill() |>
|
|
||||||
printpt(to_name(hand))
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
if not cont do
|
|
||||||
to_play = IO.gets("Type the number you want to play or q to exit: ")
|
|
||||||
to_play = to_play |> String.trim("\"") |> String.trim()
|
|
||||||
|
|
||||||
case to_play do
|
|
||||||
"q" ->
|
|
||||||
log("Bye Bye")
|
|
||||||
v ->
|
|
||||||
try do
|
|
||||||
{n, _} = Integer.parse(v)
|
|
||||||
|
|
||||||
res = Server.make_move(process, game_id, n)
|
|
||||||
case res do
|
|
||||||
:timeout ->
|
|
||||||
log("Could to not comunicate with the server")
|
|
||||||
{:not_exists} -> log("Game does not exist")
|
|
||||||
{:not_playing} -> log("Not Playing in that game")
|
|
||||||
{:game_finished, score} -> log("Game finsihed, #{score}")
|
|
||||||
{:game_does_not_exist} -> log("Got game does not exist")
|
|
||||||
{:player_moved_before, _, _} ->
|
|
||||||
log("Player Moved before you did please check the map and try again")
|
|
||||||
play_game(process, game_id)
|
|
||||||
{:invalid_move} -> raise "Invalid Move"
|
|
||||||
{:state, _, _} -> play_game(process, game_id)
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
_ ->
|
|
||||||
log("Please provide a valid number")
|
|
||||||
play_game(process, game_id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def control_game(process, game_id) do
|
|
||||||
to_play = IO.gets("Type the number you want to play or q to exit: ")
|
|
||||||
to_play = to_play |> String.trim("\"") |> String.trim()
|
|
||||||
|
|
||||||
case to_play do
|
|
||||||
"q" ->
|
|
||||||
log("Bye Bye")
|
|
||||||
v ->
|
|
||||||
try do
|
|
||||||
{n, _} = Integer.parse(v)
|
|
||||||
|
|
||||||
res = Server.make_move(process, game_id, n)
|
|
||||||
case res do
|
|
||||||
:timeout -> log("Could to not comunicate with the server")
|
|
||||||
{:not_exists} -> control_game(process, game_id)
|
|
||||||
{:not_playing} -> control_game(process, game_id)
|
|
||||||
{:game_finished, _} -> control_game(process, game_id)
|
|
||||||
{:player_moved_before, _, _} ->
|
|
||||||
log("Player Moved before you did please check the map and try again")
|
|
||||||
control_game(process, game_id)
|
|
||||||
{:invalid_move} -> raise "Invalid Move"
|
|
||||||
{:state, _, _} -> control_game(process, game_id)
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
_ ->
|
|
||||||
log("Please provide a valid number")
|
|
||||||
control_game(process, game_id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def display_game(process, game_id, temp_game \\ [], temp_hand \\ []) do
|
|
||||||
game = Server.get_game_state(process, game_id)
|
|
||||||
|
|
||||||
cont = case game do
|
|
||||||
:timeout -> log("Could to not comunicate with the server")
|
|
||||||
{:not_exists} -> log("Game does not exist")
|
|
||||||
{:not_playing} -> log("Not Playing in that game")
|
|
||||||
{:game_finished, score} -> log("Game finsihed, #{score}")
|
|
||||||
{:game_does_not_exist} -> log("Got game does not exist")
|
|
||||||
{:state, game_state, hand} ->
|
|
||||||
if temp_game != game_state or temp_hand != hand do
|
|
||||||
# IO.ANSI.clear()
|
|
||||||
game_state |>
|
|
||||||
to_name() |>
|
|
||||||
interpolate() |>
|
|
||||||
fill() |>
|
|
||||||
printpt(to_name(hand))
|
|
||||||
Process.sleep(1000)
|
|
||||||
display_game(process, game_id, game_state, hand)
|
|
||||||
else
|
|
||||||
display_game(process, game_id, temp_game, temp_hand)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_name(list) when is_list(list), do: list |> Enum.map(fn x -> to_name(x) end)
|
def to_name(list) when is_list(list), do: list |> Enum.map(fn x -> to_name(x) end)
|
||||||
def to_name(atom) when atom == :+, do: IO.ANSI.color_background(9) <> IO.ANSI.color(15) <> " + " <> IO.ANSI.reset()
|
|
||||||
def to_name(atom) when atom == :-, do: IO.ANSI.color_background(21) <> IO.ANSI.color(15) <> " - " <> IO.ANSI.reset()
|
|
||||||
def to_name(atom) when atom == :b, do: IO.ANSI.color_background(232) <> IO.ANSI.color(15) <> " b " <> IO.ANSI.reset()
|
|
||||||
def to_name(atom) when is_atom(atom), do: atom
|
def to_name(atom) when is_atom(atom), do: atom
|
||||||
def to_name(i) do
|
def to_name(i) do
|
||||||
letter = [ "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og" ] |> Enum.at(i - 1)
|
[ "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og" ] |>
|
||||||
color = [46,45,138,19,11,140,8,47,57,159,72,48,55,35,251,188,107,110,21,11,156,134,128,179,140,234,14,90,206,7,249,209,253,123,192,165,234,136,198,208,43,34,215,127,23,250,177,237,124,202,229,
|
Enum.at(i - 1)
|
||||||
63,206,220,224,109,202,113,253,7,243,26,160,65,39,112,57,75, 252,82,213,186,68,243,134,100,226,48,90,134,208,102,25,106,72, 242,26,59,166,26,187,54,194,165,97,219,186,130,7,154,233,85, 130,67,43,200,90,60,148,49,161,110,247,116,223,159,132,132] |> Enum.at(i - 1) |> IO.ANSI.color()
|
|
||||||
color <> String.pad_leading("#{letter}", 3, " ") <> IO.ANSI.reset()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def interpolate(list) do
|
def interpolate(list) do
|
||||||
[list, 0..length(list) - 1] |>
|
[list, 0..length(list) - 1] |>
|
||||||
Enum.zip() |>
|
Enum.zip() |>
|
||||||
Enum.reduce([], fn {v, i}, acc -> acc ++ [String.pad_leading("#{i}", 3, " "), v] end)
|
Enum.reduce([], fn {v, i}, acc -> acc ++ [i, v] end) |>
|
||||||
|
Enum.map(fn x -> String.pad_leading("#{x}", 2, " ") end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def grow_empty_list(t, i, acc) when i == 0, do: t ++ acc
|
def printpt(game_state) do
|
||||||
def grow_empty_list([], i, acc), do: grow_empty_list(acc, i, [])
|
rad = 8;
|
||||||
def grow_empty_list([h | tl], i, acc), do:
|
printpt1(game_state, 0, rad)
|
||||||
grow_empty_list(tl, i - 1, acc ++ [ h ++ [" "] ])
|
|
||||||
|
|
||||||
def fill(list) do
|
|
||||||
to_fill = 32 - length(list)
|
|
||||||
to_add = grow_empty_list(Enum.map(list, fn _ -> [] end), to_fill, [])
|
|
||||||
fill(list, to_add, [])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fill([], _, acc), do: acc
|
defp count_char(str, char) do
|
||||||
def fill([hd | tail], [add_hd | add_tail], acc) do
|
String.split(str, "") |> Enum.reduce(0, fn x, acc -> if x == char do acc + 1 else acc end end)
|
||||||
fill(tail, add_tail ,acc ++ [hd] ++ add_hd)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def printpt(game_state, hand), do: printpt(game_state, hand, 0)
|
def printpt1(_, i, rad) when i > rad * 2, do: nil
|
||||||
def printpt(_, _, i) when i > 16, do: nil
|
def printpt1(game_state, i, rad) do
|
||||||
def printpt(game_state, hand, i) do
|
res = printpt2(game_state, i, -5, rad, "")
|
||||||
res = case i do
|
|
||||||
0 ->
|
" xxx \n"
|
||||||
" xxx \n" |>
|
" xxx yyy \n"
|
||||||
String.replace("xxx", Enum.at(game_state, 1))
|
" xxx yyy \n"
|
||||||
1 ->
|
" xxx yyy \n"
|
||||||
" xxx yyy \n" |>
|
" xxx yyy \n"
|
||||||
String.replace("xxx", Enum.at(game_state, 0)) |>
|
" xxx yyy \n"
|
||||||
String.replace("yyy", Enum.at(game_state, 2))
|
" xxx yyy \n"
|
||||||
2 ->
|
" xxx yyy \n"
|
||||||
" xxx yyy \n" |>
|
" xxx yyy \n"
|
||||||
String.replace("xxx", Enum.at(game_state, 31)) |>
|
" xxx yyy \n"
|
||||||
String.replace("yyy", Enum.at(game_state, 3))
|
" xxx yyy \n"
|
||||||
3 ->
|
" xxx yyy \n"
|
||||||
" xxx yyy \n" |>
|
" xxx yyy \n"
|
||||||
String.replace("xxx", Enum.at(game_state, 30)) |>
|
" xxx yyy \n"
|
||||||
String.replace("yyy", Enum.at(game_state, 4))
|
" xxx yyy \n"
|
||||||
4 ->
|
" xxx \n"
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 29)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 5))
|
|
||||||
5 ->
|
|
||||||
" xxx yyy \n" |>
|
if i != 15 do
|
||||||
String.replace("xxx", Enum.at(game_state, 28)) |>
|
res = case i do
|
||||||
String.replace("yyy", Enum.at(game_state, 6))
|
0 ->
|
||||||
6 ->
|
x = count_char(res, "x")
|
||||||
" xxx yyy \n" |>
|
log("#{x}")
|
||||||
String.replace("xxx", Enum.at(game_state, 27)) |>
|
#TODO
|
||||||
String.replace("yyy", Enum.at(game_state, 7))
|
res
|
||||||
7 ->
|
16 ->
|
||||||
" xxx yyy \n" |>
|
#TODO
|
||||||
String.replace("xxx", Enum.at(game_state, 26)) |>
|
res
|
||||||
String.replace("yyy", Enum.at(game_state, 8))
|
v ->
|
||||||
8 ->
|
#TODO
|
||||||
" xxx zzz yyy \n" |>
|
res
|
||||||
String.replace("xxx", Enum.at(game_state, 25)) |>
|
end
|
||||||
String.replace("yyy", Enum.at(game_state, 9)) |>
|
IO.write(" #{res} \n")
|
||||||
String.replace("zzz", hand)
|
|
||||||
9 ->
|
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 24)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 10))
|
|
||||||
10 ->
|
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 23)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 11))
|
|
||||||
11 ->
|
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 22)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 12))
|
|
||||||
12 ->
|
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 21)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 13))
|
|
||||||
13 ->
|
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 20)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 14))
|
|
||||||
14 ->
|
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 19)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 15))
|
|
||||||
15 ->
|
|
||||||
" xxx yyy \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 18)) |>
|
|
||||||
String.replace("yyy", Enum.at(game_state, 16))
|
|
||||||
16 ->
|
|
||||||
" xxx \n" |>
|
|
||||||
String.replace("xxx", Enum.at(game_state, 17))
|
|
||||||
end
|
end
|
||||||
|
printpt1(game_state, i + 1, rad)
|
||||||
|
end
|
||||||
|
|
||||||
IO.write("#{res}")
|
def printpt2(_, _, j, rad, res) when j > rad * 4 + 10, do: res
|
||||||
printpt(game_state, hand, i + 1)
|
def printpt2(game_state, i, j, rad, res) do
|
||||||
|
dist = :math.sqrt((i - rad)*(i - rad) + (j / 2 - rad)*(j / 2 - rad));
|
||||||
|
v = if (dist > rad - 1 and dist < rad + 1) do
|
||||||
|
case i do
|
||||||
|
0 -> "x"
|
||||||
|
16 -> "x"
|
||||||
|
_ -> if j < rad * 2 do "x" else "y" end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
" "
|
||||||
|
end
|
||||||
|
printpt2(game_state, i, j + 1, rad, res <> v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
191
notes
191
notes
@ -1,191 +0,0 @@
|
|||||||
procs = Enum.to_list(1..3) |> Enum.map(fn m -> :"p#{m}")
|
|
||||||
pids = Enum.map(procs, fn p -> ShopServer.start(p, procs))
|
|
||||||
pids |> Enum.map(fn p -> Process.exit(p, :kill) end)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
max = 100
|
|
||||||
|
|
||||||
1..max |> Enum.to_list() |> Enum.reduce(%{}, fn n, acc -> Map.put(acc, n, :not_reseverd) end) |>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
" # "
|
|
||||||
" #1# "
|
|
||||||
" # "
|
|
||||||
" # # "
|
|
||||||
" #0# #2# "
|
|
||||||
" # # "
|
|
||||||
" # "
|
|
||||||
" #4# "
|
|
||||||
" # "
|
|
||||||
|
|
||||||
|
|
||||||
" # "
|
|
||||||
" #1# "
|
|
||||||
" # "
|
|
||||||
" # # "
|
|
||||||
" #0# #2# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #7# #3# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #6# #4# "
|
|
||||||
" # # "
|
|
||||||
" # "
|
|
||||||
" #5# "
|
|
||||||
" # "
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
" # "
|
|
||||||
" #1# "
|
|
||||||
" # "
|
|
||||||
" # # "
|
|
||||||
" #0# #2# "
|
|
||||||
" # # "
|
|
||||||
" #11 #3# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #10 #4# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #9# #5# "
|
|
||||||
" # # "
|
|
||||||
" #8# #6# "
|
|
||||||
" # # "
|
|
||||||
" # "
|
|
||||||
" #7# "
|
|
||||||
" # "
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
" # "
|
|
||||||
" #1# "
|
|
||||||
" # "
|
|
||||||
" # # "
|
|
||||||
" #0# #2# "
|
|
||||||
" #15 #3# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #14 #4# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #13 #5# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #12 #6# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #11 #7# "
|
|
||||||
" #10 #8# "
|
|
||||||
" # # "
|
|
||||||
" # "
|
|
||||||
" #9# "
|
|
||||||
" # "
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
" # "
|
|
||||||
" #1# "
|
|
||||||
" # "
|
|
||||||
" # # "
|
|
||||||
" #0# #2# "
|
|
||||||
" #19 #3# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #18 #4# "
|
|
||||||
" #17 #5# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #16 #6# "
|
|
||||||
" # # "
|
|
||||||
" #15 #7# "
|
|
||||||
" # # "
|
|
||||||
" #14 #8# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #13 #9# "
|
|
||||||
" #12 #10 "
|
|
||||||
" # # "
|
|
||||||
" # "
|
|
||||||
" #11 "
|
|
||||||
" # "
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
" # "
|
|
||||||
" #1# "
|
|
||||||
" # "
|
|
||||||
" # # "
|
|
||||||
" #0# #2# "
|
|
||||||
" #23 #3# "
|
|
||||||
" # # "
|
|
||||||
" #22 #4# "
|
|
||||||
" #21 #5# "
|
|
||||||
" # # "
|
|
||||||
" #20 #6# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #19 #7# "
|
|
||||||
" # # "
|
|
||||||
" # # "
|
|
||||||
" #18 #8# "
|
|
||||||
" # # "
|
|
||||||
" #17 #9# "
|
|
||||||
" #16 #10 "
|
|
||||||
" # # "
|
|
||||||
" #15 #11 "
|
|
||||||
" #14 #12 "
|
|
||||||
" # # "
|
|
||||||
" # "
|
|
||||||
" #13 "
|
|
||||||
" # "
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user