Skip to content

Commit d26b941

Browse files
Sync connect (#1631)
* Rename :black and :white to :X and :O * Add two new tests
1 parent 2a94e0b commit d26b941

4 files changed

Lines changed: 69 additions & 34 deletions

File tree

exercises/practice/connect/.meta/example.ex

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
11
defmodule Connect do
22
@doc """
33
Calculates the winner (if any) of a board
4-
using "O" as the white player
5-
and "X" as the black player
64
"""
7-
@spec result_for([String.t()]) :: :none | :black | :white
5+
@spec result_for([String.t()]) :: :none | :X | :O
86
def result_for(board) do
97
cond do
10-
black_wins?(board) -> :black
11-
white_wins?(board) -> :white
8+
x_wins?(board) -> :X
9+
o_wins?(board) -> :O
1210
true -> :none
1311
end
1412
end
1513

16-
defp black_wins?(board) do
14+
defp x_wins?(board) do
1715
board
1816
|> Enum.with_index()
1917
|> Enum.any?(fn {row, index} ->
20-
String.first(row) == "X" && black_wins?(board, [{index, 0}])
18+
String.first(row) == "X" && x_wins?(board, [{index, 0}])
2119
end)
2220
end
2321

24-
defp black_wins?(board, [{_, y} | _]) when y + 1 == byte_size(hd(board)), do: true
22+
defp x_wins?(board, [{_, y} | _]) when y + 1 == byte_size(hd(board)), do: true
2523

26-
defp black_wins?(board, history = [last_loc | _]) do
24+
defp x_wins?(board, history = [last_loc | _]) do
2725
last_loc
2826
|> locs_next_to(history)
2927
|> Enum.filter(&(get_loc(board, &1) == "X"))
30-
|> Enum.any?(&black_wins?(board, [&1 | history]))
28+
|> Enum.any?(&x_wins?(board, [&1 | history]))
3129
end
3230

33-
defp white_wins?(board) do
31+
defp o_wins?(board) do
3432
board
3533
|> hd
3634
|> String.graphemes()
3735
|> Enum.with_index()
3836
|> Enum.any?(fn {spot, index} ->
39-
spot == "O" && white_wins?(board, [{0, index}])
37+
spot == "O" && o_wins?(board, [{0, index}])
4038
end)
4139
end
4240

43-
defp white_wins?(board, [{x, _} | _]) when x + 1 == length(board), do: true
41+
defp o_wins?(board, [{x, _} | _]) when x + 1 == length(board), do: true
4442

45-
defp white_wins?(board, history = [last_loc | _]) do
43+
defp o_wins?(board, history = [last_loc | _]) do
4644
last_loc
4745
|> locs_next_to(history)
4846
|> Enum.filter(&(get_loc(board, &1) == "O"))
49-
|> Enum.any?(&white_wins?(board, [&1 | history]))
47+
|> Enum.any?(&o_wins?(board, [&1 | history]))
5048
end
5149

5250
defp locs_next_to({x, y}, history) do

exercises/practice/connect/.meta/tests.toml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[6eff0df4-3e92-478d-9b54-d3e8b354db56]
613
description = "an empty board has no winner"
@@ -23,6 +30,12 @@ description = "nobody wins crossing adjacent angles"
2330
[cd61c143-92f6-4a8d-84d9-cb2b359e226b]
2431
description = "X wins crossing from left to right"
2532

33+
[495e33ed-30a9-4012-b46e-d7c4d5fe13c3]
34+
description = "X wins with left-hand dead end fork"
35+
36+
[ab167ab0-4a98-4d0f-a1c0-e1cddddc3d58]
37+
description = "X wins with right-hand dead end fork"
38+
2639
[73d1eda6-16ab-4460-9904-b5f5dd401d0b]
2740
description = "O wins crossing from top to bottom"
2841

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
defmodule Connect do
22
@doc """
33
Calculates the winner (if any) of a board
4-
using "O" as the white player
5-
and "X" as the black player
64
"""
7-
@spec result_for([String.t()]) :: :none | :black | :white
5+
@spec result_for([String.t()]) :: :none | :X | :O
86
def result_for(board) do
97
end
108
end

exercises/practice/connect/test/connect_test.exs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule ConnectTest do
66
end
77

88
# @tag :pending
9-
test "empty board has no winner" do
9+
test "an empty board has no winner" do
1010
board =
1111
remove_spaces([
1212
". . . . .",
@@ -20,15 +20,15 @@ defmodule ConnectTest do
2020
end
2121

2222
@tag :pending
23-
test "1x1 board with black stone" do
23+
test "X can win on a 1x1 board" do
2424
board = ["X"]
25-
assert Connect.result_for(board) == :black
25+
assert Connect.result_for(board) == :X
2626
end
2727

2828
@tag :pending
29-
test "1x1 board with white stone" do
29+
test "O can win on a 1x1 board" do
3030
board = ["O"]
31-
assert Connect.result_for(board) == :white
31+
assert Connect.result_for(board) == :O
3232
end
3333

3434
@tag :pending
@@ -73,7 +73,7 @@ defmodule ConnectTest do
7373
end
7474

7575
@tag :pending
76-
test "black wins crossing from left to right" do
76+
test "X wins crossing from left to right" do
7777
board =
7878
remove_spaces([
7979
". O . .",
@@ -83,11 +83,37 @@ defmodule ConnectTest do
8383
" . O X ."
8484
])
8585

86-
assert Connect.result_for(board) == :black
86+
assert Connect.result_for(board) == :X
8787
end
8888

8989
@tag :pending
90-
test "white wins crossing from top to bottom" do
90+
test "X wins with left-hand dead end fork" do
91+
board =
92+
remove_spaces([
93+
". . X .",
94+
" X X . .",
95+
" . X X X",
96+
" O O O O"
97+
])
98+
99+
assert Connect.result_for(board) == :X
100+
end
101+
102+
@tag :pending
103+
test "X wins with right-hand dead end fork" do
104+
board =
105+
remove_spaces([
106+
". . X X",
107+
" X X . .",
108+
" . X X .",
109+
" O O O O"
110+
])
111+
112+
assert Connect.result_for(board) == :X
113+
end
114+
115+
@tag :pending
116+
test "O wins crossing from top to bottom" do
91117
board =
92118
remove_spaces([
93119
". O . .",
@@ -97,11 +123,11 @@ defmodule ConnectTest do
97123
" . O X ."
98124
])
99125

100-
assert Connect.result_for(board) == :white
126+
assert Connect.result_for(board) == :O
101127
end
102128

103129
@tag :pending
104-
test "black wins using a convoluted path" do
130+
test "X wins using a convoluted path" do
105131
board =
106132
remove_spaces([
107133
". X X . .",
@@ -111,11 +137,11 @@ defmodule ConnectTest do
111137
" O O O O O"
112138
])
113139

114-
assert Connect.result_for(board) == :black
140+
assert Connect.result_for(board) == :X
115141
end
116142

117143
@tag :pending
118-
test "black wins using a spiral path" do
144+
test "X wins using a spiral path" do
119145
board =
120146
remove_spaces([
121147
"O X X X X X X X X",
@@ -129,6 +155,6 @@ defmodule ConnectTest do
129155
" X X X X X X X X O"
130156
])
131157

132-
assert Connect.result_for(board) == :black
158+
assert Connect.result_for(board) == :X
133159
end
134160
end

0 commit comments

Comments
 (0)