1
exercism fetch elixir queen-attack

queen_attack_test.exs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("queen_attack.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule QueenAttackTest do
  use ExUnit.Case

  # @tag :pending
  test "default positions" do
    queens = Queens.new()
    assert queens.white == {0, 3}
    assert queens.black == {7, 3}
  end

  @tag :pending
  test "specific placement" do
    queens = Queens.new({3, 7}, {6, 1})
    assert queens.white == {3, 7}
    assert queens.black == {6, 1}
  end

  @tag :pending
  test "cannot occupy same space" do
    assert_raise ArgumentError, fn ->
      Queens.new({2, 4}, {2, 4})
    end
  end

  @tag :pending
  test "string representation" do
    queens = Queens.new({2, 4}, {6, 6})

    board =
      String.trim("""
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ W _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ B _
      _ _ _ _ _ _ _ _
      """)

    assert Queens.to_string(queens) == board
  end

  @tag :pending
  test "another string representation" do
    queens = Queens.new({7, 1}, {0, 0})

    board =
      String.trim("""
      B _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ W _ _ _ _ _ _
      """)

    assert Queens.to_string(queens) == board
  end

  @tag :pending
  test "yet another string representation" do
    queens = Queens.new({4, 3}, {3, 4})

    board =
      String.trim("""
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ B _ _ _
      _ _ _ W _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      """)

    assert Queens.to_string(queens) == board
  end

  @tag :pending
  test "queen placed on the bottom right corner" do
    queens = Queens.new({4, 3}, {7, 7})

    board =
      String.trim("""
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ W _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ B
      """)

    assert Queens.to_string(queens) == board
  end

  @tag :pending
  test "queen placed on the edge of the board" do
    queens = Queens.new({4, 3}, {2, 7})

    board =
      String.trim("""
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ B
      _ _ _ _ _ _ _ _
      _ _ _ W _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _
      """)

    assert Queens.to_string(queens) == board
  end

  @tag :pending
  test "cannot attack" do
    queens = Queens.new({2, 3}, {4, 7})
    refute Queens.can_attack?(queens)
  end

  @tag :pending
  test "can attack on same row" do
    queens = Queens.new({2, 4}, {2, 7})
    assert Queens.can_attack?(queens)
  end

  @tag :pending
  test "can attack on same column" do
    queens = Queens.new({5, 4}, {2, 4})
    assert Queens.can_attack?(queens)
  end

  @tag :pending
  test "can attack on diagonal" do
    queens = Queens.new({1, 1}, {6, 6})
    assert Queens.can_attack?(queens)
  end

  @tag :pending
  test "can attack on other diagonal" do
    queens = Queens.new({0, 6}, {1, 7})
    assert Queens.can_attack?(queens)
  end

  @tag :pending
  test "can attack on yet another diagonal" do
    queens = Queens.new({4, 1}, {6, 3})
    assert Queens.can_attack?(queens)
  end

  @tag :pending
  test "can attack on a diagonal slanted the other way" do
    queens = Queens.new({6, 1}, {1, 6})
    assert Queens.can_attack?(queens)
  end
end