Skip to content

Queen Attack

Place two queens on a chessboard and say whether they can attack each other.

Adapted from Exercism’s problem specification (MIT). Listed under Games & Simulation.

// FILE: queen_attack.nomi
//# Whether two queens on a chessboard can attack each other.

/// A queen on an 8 by 8 board, at a zero-based row and column.
pub opaque struct Queen
    row:    Int
    column: Int
end

impl Queen
    /// Places a queen, or says which coordinate is off the board.
    pub fn new(row: Int, column: Int) -> Result<Queen, Error>
        case
            row < 0    -> Err(.RowNotPositive)
            row > 7    -> Err(.RowNotOnBoard)
            column < 0 -> Err(.ColumnNotPositive)
            column > 7 -> Err(.ColumnNotOnBoard)
            _          -> Ok({row:, column:})
        end
    end

    /// Reports whether two queens share a row, a column or a diagonal.
    pub fn can_attack(white: Queen, black: Queen) -> Bool
        rows = Int.abs(white.row - black.row)
        columns = Int.abs(white.column - black.column)
        rows == 0 or columns == 0 or rows == columns
    end
end

/// Why a queen cannot be placed.
pub enum Error
    RowNotPositive
    RowNotOnBoard
    ColumnNotPositive
    ColumnNotOnBoard
end

// FILE: queen_attack_test.nomi
import queen_attack.{Error, Queen}

group "Test creation of Queens with valid and invalid positions"
    test "queen with a valid position"
        assert Queen.new(2, 2)
    end

    test "invalid positions"
        [
            ("queen must have positive row",    -2, 2,  Error.RowNotPositive),
            ("queen must have row on board",    8,  4,  .RowNotOnBoard),
            ("queen must have positive column", 2,  -2, .ColumnNotPositive),
            ("queen must have column on board", 4,  8,  .ColumnNotOnBoard),
        ]
        |> subtests (description, row, column, error), description
            assert Queen.new(row, column) == Err(error)
        end
    end
end

test "Test the ability of one queen to attack another"
    [
        ("cannot attack",                 (2, 4), (6, 6), False),
        ("can attack on same row",        (2, 4), (2, 6), True),
        ("can attack on same column",     (4, 5), (2, 5), True),
        ("can attack on first diagonal",  (2, 2), (0, 4), True),
        ("can attack on second diagonal", (2, 2), (3, 1), True),
        ("can attack on third diagonal",  (2, 2), (1, 1), True),
        ("can attack on fourth diagonal", (1, 7), (0, 6), True),
        (
            "cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal",
            (4, 1),
            (2, 5),
            False,
        ),
    ]
    |> subtests (
        description,
        (white_row, white_column),
        (black_row, black_column),
        expected
    ), description
        assert Ok(white) = Queen.new(white_row, white_column)
        assert Ok(black) = Queen.new(black_row, black_column)
        assert Queen.can_attack(white, black) == expected
    end
end
  • Queen is a pub opaque struct, so Queen.new is the only way to build one and an off-board queen cannot exist.
  • Queen.new validates with a subject-less case, one arm per canonical error, and answers Result<Queen, Error>.
  • can_attack compares row and column distances, each Int.abs of a difference: equal rows, equal columns or equal distances.
  • The valid-position case is its own test (assert Queen.new(2, 2)), since an opaque Queen cannot be written as an expected value outside its file.
  • The attack table builds each queen with assert Ok(white) = Queen.new(...); ? cannot be used in a subtests body, and the error saying so suggests this form.