From b33599c1b4ad74ad0b29f4661f93aae95abf545c Mon Sep 17 00:00:00 2001 From: Angelika Cathor Date: Sat, 2 May 2026 08:38:38 +0200 Subject: [PATCH 1/2] Rename :black and :white to :X and :O --- exercises/practice/connect/.meta/example.ex | 28 +++++++++---------- exercises/practice/connect/lib/connect.ex | 4 +-- .../practice/connect/test/connect_test.exs | 26 ++++++++--------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/exercises/practice/connect/.meta/example.ex b/exercises/practice/connect/.meta/example.ex index 08b00a89aa..00f80118e2 100644 --- a/exercises/practice/connect/.meta/example.ex +++ b/exercises/practice/connect/.meta/example.ex @@ -1,52 +1,50 @@ defmodule Connect do @doc """ Calculates the winner (if any) of a board - using "O" as the white player - and "X" as the black player """ - @spec result_for([String.t()]) :: :none | :black | :white + @spec result_for([String.t()]) :: :none | :X | :O def result_for(board) do cond do - black_wins?(board) -> :black - white_wins?(board) -> :white + x_wins?(board) -> :X + o_wins?(board) -> :O true -> :none end end - defp black_wins?(board) do + defp x_wins?(board) do board |> Enum.with_index() |> Enum.any?(fn {row, index} -> - String.first(row) == "X" && black_wins?(board, [{index, 0}]) + String.first(row) == "X" && x_wins?(board, [{index, 0}]) end) end - defp black_wins?(board, [{_, y} | _]) when y + 1 == byte_size(hd(board)), do: true + defp x_wins?(board, [{_, y} | _]) when y + 1 == byte_size(hd(board)), do: true - defp black_wins?(board, history = [last_loc | _]) do + defp x_wins?(board, history = [last_loc | _]) do last_loc |> locs_next_to(history) |> Enum.filter(&(get_loc(board, &1) == "X")) - |> Enum.any?(&black_wins?(board, [&1 | history])) + |> Enum.any?(&x_wins?(board, [&1 | history])) end - defp white_wins?(board) do + defp o_wins?(board) do board |> hd |> String.graphemes() |> Enum.with_index() |> Enum.any?(fn {spot, index} -> - spot == "O" && white_wins?(board, [{0, index}]) + spot == "O" && o_wins?(board, [{0, index}]) end) end - defp white_wins?(board, [{x, _} | _]) when x + 1 == length(board), do: true + defp o_wins?(board, [{x, _} | _]) when x + 1 == length(board), do: true - defp white_wins?(board, history = [last_loc | _]) do + defp o_wins?(board, history = [last_loc | _]) do last_loc |> locs_next_to(history) |> Enum.filter(&(get_loc(board, &1) == "O")) - |> Enum.any?(&white_wins?(board, [&1 | history])) + |> Enum.any?(&o_wins?(board, [&1 | history])) end defp locs_next_to({x, y}, history) do diff --git a/exercises/practice/connect/lib/connect.ex b/exercises/practice/connect/lib/connect.ex index 0819d8cb99..718a831175 100644 --- a/exercises/practice/connect/lib/connect.ex +++ b/exercises/practice/connect/lib/connect.ex @@ -1,10 +1,8 @@ defmodule Connect do @doc """ Calculates the winner (if any) of a board - using "O" as the white player - and "X" as the black player """ - @spec result_for([String.t()]) :: :none | :black | :white + @spec result_for([String.t()]) :: :none | :X | :O def result_for(board) do end end diff --git a/exercises/practice/connect/test/connect_test.exs b/exercises/practice/connect/test/connect_test.exs index 41d86bae92..8f95b23ba7 100644 --- a/exercises/practice/connect/test/connect_test.exs +++ b/exercises/practice/connect/test/connect_test.exs @@ -6,7 +6,7 @@ defmodule ConnectTest do end # @tag :pending - test "empty board has no winner" do + test "an empty board has no winner" do board = remove_spaces([ ". . . . .", @@ -20,15 +20,15 @@ defmodule ConnectTest do end @tag :pending - test "1x1 board with black stone" do + test "X can win on a 1x1 board" do board = ["X"] - assert Connect.result_for(board) == :black + assert Connect.result_for(board) == :X end @tag :pending - test "1x1 board with white stone" do + test "O can win on a 1x1 board" do board = ["O"] - assert Connect.result_for(board) == :white + assert Connect.result_for(board) == :O end @tag :pending @@ -73,7 +73,7 @@ defmodule ConnectTest do end @tag :pending - test "black wins crossing from left to right" do + test "X wins crossing from left to right" do board = remove_spaces([ ". O . .", @@ -83,11 +83,11 @@ defmodule ConnectTest do " . O X ." ]) - assert Connect.result_for(board) == :black + assert Connect.result_for(board) == :X end @tag :pending - test "white wins crossing from top to bottom" do + test "O wins crossing from top to bottom" do board = remove_spaces([ ". O . .", @@ -97,11 +97,11 @@ defmodule ConnectTest do " . O X ." ]) - assert Connect.result_for(board) == :white + assert Connect.result_for(board) == :O end @tag :pending - test "black wins using a convoluted path" do + test "X wins using a convoluted path" do board = remove_spaces([ ". X X . .", @@ -111,11 +111,11 @@ defmodule ConnectTest do " O O O O O" ]) - assert Connect.result_for(board) == :black + assert Connect.result_for(board) == :X end @tag :pending - test "black wins using a spiral path" do + test "X wins using a spiral path" do board = remove_spaces([ "O X X X X X X X X", @@ -129,6 +129,6 @@ defmodule ConnectTest do " X X X X X X X X O" ]) - assert Connect.result_for(board) == :black + assert Connect.result_for(board) == :X end end From 82c2431c9edc18ee66523630c2096c1b1f5a89f8 Mon Sep 17 00:00:00 2001 From: Angelika Cathor Date: Sat, 2 May 2026 08:41:10 +0200 Subject: [PATCH 2/2] Add two new tests --- exercises/practice/connect/.meta/tests.toml | 19 +++++++++++--- .../practice/connect/test/connect_test.exs | 26 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/exercises/practice/connect/.meta/tests.toml b/exercises/practice/connect/.meta/tests.toml index 59ec615e39..951b87e5c4 100644 --- a/exercises/practice/connect/.meta/tests.toml +++ b/exercises/practice/connect/.meta/tests.toml @@ -1,6 +1,13 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. [6eff0df4-3e92-478d-9b54-d3e8b354db56] description = "an empty board has no winner" @@ -23,6 +30,12 @@ description = "nobody wins crossing adjacent angles" [cd61c143-92f6-4a8d-84d9-cb2b359e226b] description = "X wins crossing from left to right" +[495e33ed-30a9-4012-b46e-d7c4d5fe13c3] +description = "X wins with left-hand dead end fork" + +[ab167ab0-4a98-4d0f-a1c0-e1cddddc3d58] +description = "X wins with right-hand dead end fork" + [73d1eda6-16ab-4460-9904-b5f5dd401d0b] description = "O wins crossing from top to bottom" diff --git a/exercises/practice/connect/test/connect_test.exs b/exercises/practice/connect/test/connect_test.exs index 8f95b23ba7..e1c5e46747 100644 --- a/exercises/practice/connect/test/connect_test.exs +++ b/exercises/practice/connect/test/connect_test.exs @@ -86,6 +86,32 @@ defmodule ConnectTest do assert Connect.result_for(board) == :X end + @tag :pending + test "X wins with left-hand dead end fork" do + board = + remove_spaces([ + ". . X .", + " X X . .", + " . X X X", + " O O O O" + ]) + + assert Connect.result_for(board) == :X + end + + @tag :pending + test "X wins with right-hand dead end fork" do + board = + remove_spaces([ + ". . X X", + " X X . .", + " . X X .", + " O O O O" + ]) + + assert Connect.result_for(board) == :X + end + @tag :pending test "O wins crossing from top to bottom" do board =