Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
jrowah committed Jun 10, 2024
1 parent 2028a54 commit 49e6cfc
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 55 deletions.
23 changes: 23 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/_build/
/.elixir_ls/
/.git/
/config/
/cover/
/deps/
/doc/
/lib/
/priv/
/rel/
/screenshots/
/test/
.credo.exs
.dialyzer_ignore.exs
.dockerignore
.env
.env.prod.sample
.env.sample
.formatter.exs
.gitattributes
.gitignore
.iex.exs
.erl_crash.dump
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

36 changes: 15 additions & 21 deletions lib/battle_ship/board.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule BattleShip.Board do
@moduledoc false

alias BattleShip.{Coordinate, Ship}
alias BattleShip.Coordinate
alias BattleShip.Ship

@spec new() :: %{}
@doc """
Expand All @@ -28,12 +29,10 @@ defmodule BattleShip.Board do
"""
def position_ship(board, key, %Ship{} = ship) do
case overlaps_existing_ship?(board, key, ship) do
true ->
{:error, :overlapping_ship}

false ->
Map.put(board, key, ship)
if overlaps_existing_ship?(board, key, ship) do
{:error, :overlapping_ship}
else
Map.put(board, key, ship)
end
end

Expand Down Expand Up @@ -82,12 +81,10 @@ defmodule BattleShip.Board do
defp guess_response(:miss, board), do: {:miss, :none, :no_win, board}

defp sink_check(board, key) do
case sunk?(board, key) do
true ->
key

false ->
:none
if sunk?(board, key) do
key
else
:none
end
end

Expand All @@ -98,15 +95,12 @@ defmodule BattleShip.Board do
end

defp win_check(board) do
case all_sunk(board) do
true ->
:win

false ->
:no_win
if all_sunk(board) do
:win
else
:no_win
end
end

defp all_sunk(board),
do: Enum.all?(board, fn {_key, ship} -> Ship.sunk?(ship) end)
defp all_sunk(board), do: Enum.all?(board, fn {_key, ship} -> Ship.sunk?(ship) end)
end
3 changes: 1 addition & 2 deletions lib/battle_ship/coordinate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ defmodule BattleShip.Coordinate do
@spec new(integer(), integer()) ::
{:error, :invalid_coordinate}
| {:ok, t()}
def new(row, col) when row in @board_range and col in @board_range,
do: {:ok, %Coordinate{row: row, col: col}}
def new(row, col) when row in @board_range and col in @board_range, do: {:ok, %Coordinate{row: row, col: col}}

def new(_row, _col), do: {:error, :invalid_coordinate}
end
9 changes: 6 additions & 3 deletions lib/battle_ship/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ defmodule BattleShip.Game do
shutdown: 5000,
type: :worker

alias BattleShip.{Board, Coordinate, Guesses, Rules, Ship}
alias BattleShip.Board
alias BattleShip.Coordinate
alias BattleShip.Guesses
alias BattleShip.Rules
alias BattleShip.Ship

@players [:player1, :player2]

Expand Down Expand Up @@ -43,8 +47,7 @@ defmodule BattleShip.Game do
def position_ship(game, player, ship_key, row, col) when player in @players,
do: GenServer.call(game, {:position_ship, player, ship_key, row, col})

def set_ships(game, player) when player in @players,
do: GenServer.call(game, {:set_ships, player})
def set_ships(game, player) when player in @players, do: GenServer.call(game, {:set_ships, player})

@spec guess_coordinate(
pid(),
Expand Down
3 changes: 2 additions & 1 deletion lib/battle_ship/guesses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule BattleShip.Guesses do
@moduledoc """
This is the Guesses module.
"""
alias BattleShip.{Coordinate, Guesses}
alias BattleShip.Coordinate
alias BattleShip.Guesses

@enforce_keys [:hits, :misses]
defstruct [:hits, :misses]
Expand Down
13 changes: 5 additions & 8 deletions lib/battle_ship/rules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ defmodule BattleShip.Rules do
# sets ships for player and moves state if both player's ships are set
rules = Map.put(rules, player, :ships_set)

case both_players_ships_set?(rules) do
true ->
{:ok, %Rules{rules | state: :player1_turn}}

false ->
{:ok, rules}
if both_players_ships_set?(rules) do
{:ok, %Rules{rules | state: :player1_turn}}
else
{:ok, rules}
end
end

Expand All @@ -96,6 +94,5 @@ defmodule BattleShip.Rules do
# catch all state, eg when player1 tries to guess a coordinate when the state is in player2's turn
def check(_state, _action), do: :error

defp both_players_ships_set?(rules),
do: rules.player1 == :ships_set && rules.player2 == :ships_set
defp both_players_ships_set?(rules), do: rules.player1 == :ships_set && rules.player2 == :ships_set
end
21 changes: 8 additions & 13 deletions lib/battle_ship/ship.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule BattleShip.Ship do
@moduledoc """
This is the Ship module.
"""
alias BattleShip.{Coordinate, Ship}
alias BattleShip.Coordinate
alias BattleShip.Ship

@enforce_keys [:coordinates, :hit_coordinates]
defstruct [:coordinates, :hit_coordinates]
Expand All @@ -22,9 +23,6 @@ defmodule BattleShip.Ship do
with [_ | _] = offsets <- offset(type),
%MapSet{} = coordinates <- add_coordinates(offsets, upper_left) do
{:ok, %Ship{coordinates: coordinates, hit_coordinates: MapSet.new()}}
else
error ->
error
end
end

Expand Down Expand Up @@ -52,8 +50,7 @@ defmodule BattleShip.Ship do
end

@spec overlaps?(struct(), struct()) :: boolean()
def overlaps?(existing_ship, new_ship),
do: not MapSet.disjoint?(existing_ship.coordinates, new_ship.coordinates)
def overlaps?(existing_ship, new_ship), do: not MapSet.disjoint?(existing_ship.coordinates, new_ship.coordinates)

@spec guess(struct(), struct()) ::
:miss
Expand All @@ -64,13 +61,11 @@ defmodule BattleShip.Ship do
optional(any()) => any()
}}
def guess(ship, coordinate) do
case MapSet.member?(ship.coordinates, coordinate) do
true ->
hit_coordinates = MapSet.put(ship.hit_coordinates, coordinate)
{:hit, %{ship | hit_coordinates: hit_coordinates}}

false ->
:miss
if MapSet.member?(ship.coordinates, coordinate) do
hit_coordinates = MapSet.put(ship.hit_coordinates, coordinate)
{:hit, %{ship | hit_coordinates: hit_coordinates}}
else
:miss
end
end

Expand Down
23 changes: 20 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ defmodule BattleShip.MixProject do
{:excoveralls, "~> 0.18", only: :test},
{:sobelow, "~> 0.13", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:styler, "~> 1.0.0-rc.0", only: [:dev, :test], runtime: false}
{:styler, "~> 1.0.0-rc.0", only: [:dev, :test], runtime: false},
{:mix_audit, "~> 2.1", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.31", only: :dev, runtime: false}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
Expand All @@ -51,9 +53,24 @@ defmodule BattleShip.MixProject do
defp aliases do
[
ci: [
"ci.code_quality",
"ci.test"
"deps.unlock --check-unused",
"deps.audit",
"hex.audit",
"sobelow --config .sobelow-conf",
"format --check-formatted",
"cmd npx prettier -c .",
"compile --force --warnings-as-errors",
"credo --strict",
"dialyzer",
"cover.full"
],
"cover.full": [
"coveralls"
],
"cover.full_html": [
"coveralls.html"
],
prettier: ["cmd npx prettier -w ."],
"ci.deps_and_security": ["sobelow --config .sobelow-config"],
"ci.code_quality": [
"compile --force --warnings-as-errors",
Expand Down
9 changes: 9 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"credo": {:hex, :credo, "1.7.6", "b8f14011a5443f2839b04def0b252300842ce7388f3af177157c86da18dfbeea", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "146f347fb9f8cbc5f7e39e3f22f70acbef51d441baa6d10169dd604bfbc55296"},
"dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"},
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.34.0", "ab95e0775db3df71d30cf8d78728dd9261c355c81382bcd4cefdc74610bef13e", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "60734fb4c1353f270c3286df4a0d51e65a2c1d9fba66af3940847cc65a8066d7"},
"excoveralls": {:hex, :excoveralls, "0.18.1", "a6f547570c6b24ec13f122a5634833a063aec49218f6fff27de9df693a15588c", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d65f79db146bb20399f23046015974de0079668b9abb2f5aac074d078da60b8d"},
"file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.0", "6f0eff9c9c489f26b69b61440bf1b238d95badae49adac77973cbacae87e3c2e", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "ea7a9307de9d1548d2a72d299058d1fd2339e3d398560a0e46c27dab4891e4d2"},
"mix_audit": {:hex, :mix_audit, "2.1.3", "c70983d5cab5dca923f9a6efe559abfb4ec3f8e87762f02bab00fa4106d17eda", [:make, :mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:yaml_elixir, "~> 2.9", [hex: :yaml_elixir, repo: "hexpm", optional: false]}], "hexpm", "8c3987100b23099aea2f2df0af4d296701efd031affb08d0746b2be9e35988ec"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"sobelow": {:hex, :sobelow, "0.13.0", "218afe9075904793f5c64b8837cc356e493d88fddde126a463839351870b8d1e", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "cd6e9026b85fc35d7529da14f95e85a078d9dd1907a9097b3ba6ac7ebbe34a0d"},
"styler": {:hex, :styler, "1.0.0-rc.0", "977c702b91b11e86ae1995f0f699a372a43e8df175f4878d7e9cc1678d0d7513", [:mix], [], "hexpm", "031624294295d47af7859ef43595092f33b861f0a88e44fae6366a54f1736a1a"},
"yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"},
"yaml_elixir": {:hex, :yaml_elixir, "2.9.0", "9a256da867b37b8d2c1ffd5d9de373a4fda77a32a45b452f1708508ba7bbcb53", [:mix], [{:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "0cb0e7d4c56f5e99a6253ed1a670ed0e39c13fc45a6da054033928607ac08dfc"},
}
8 changes: 8 additions & 0 deletions prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const config = {
trailingComma: "es5",
tabWidth: 4,
semi: false,
singleQuote: true,
};

export default config;
3 changes: 2 additions & 1 deletion test/battle_ship/board_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule BattleShip.BoardTest do
use ExUnit.Case
doctest BattleShip

alias BattleShip.Board

doctest BattleShip

test "returns a new board" do
assert Board.new() == %{}
end
Expand Down
4 changes: 3 additions & 1 deletion test/battle_ship/coordinate_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule BattleShip.CoordinateTest do
use ExUnit.Case
doctest BattleShip

alias BattleShip.Coordinate

doctest BattleShip

# alias BattleShip.CoordinateFixtures

test "create new valid and invalid coordinate" do
Expand Down
3 changes: 2 additions & 1 deletion test/battle_ship/game_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule BattleShip.GameTest do
use ExUnit.Case
doctest BattleShip

alias BattleShip.Game

doctest BattleShip

test "starts a new game and ignore an already started game" do
assert {:ok, pid} = Game.start_link("test")

Expand Down
1 change: 1 addition & 0 deletions test/battle_ship/rules_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule BattleShip.RulesTest do
use ExUnit.Case

doctest BattleShip

test "create a new set of rules" do
Expand Down
3 changes: 2 additions & 1 deletion test/battle_ship/ship_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
defmodule BattleShip.ShipTest do
use ExUnit.Case
doctest BattleShip

alias BattleShip.Coordinate
alias BattleShip.Ship

doctest BattleShip

test "create new ship with valid coordinates" do
dot_ship_type = :dot
{:ok, valid_dot_coordinates} = Coordinate.new(1, 1)
Expand Down
1 change: 1 addition & 0 deletions test/battle_ship_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule BattleShipTest do
use ExUnit.Case

doctest BattleShip

test "greets the world" do
Expand Down

0 comments on commit 49e6cfc

Please sign in to comment.