From 75f1dd8c0772fbe8857706926463e4f568fead35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:29:16 -0700 Subject: [PATCH] Add `queen-attack` --- config.json | 8 +++ .../queen-attack/.docs/instructions.md | 21 ++++++++ .../practice/queen-attack/.meta/config.json | 19 +++++++ .../practice/queen-attack/.meta/example.jl | 24 +++++++++ .../practice/queen-attack/.meta/tests.toml | 51 +++++++++++++++++++ .../practice/queen-attack/queen-attack.jl | 11 ++++ exercises/practice/queen-attack/runtests.jl | 44 ++++++++++++++++ 7 files changed, 178 insertions(+) create mode 100644 exercises/practice/queen-attack/.docs/instructions.md create mode 100644 exercises/practice/queen-attack/.meta/config.json create mode 100644 exercises/practice/queen-attack/.meta/example.jl create mode 100644 exercises/practice/queen-attack/.meta/tests.toml create mode 100644 exercises/practice/queen-attack/queen-attack.jl create mode 100644 exercises/practice/queen-attack/runtests.jl diff --git a/config.json b/config.json index 088d565c..aa778fbd 100644 --- a/config.json +++ b/config.json @@ -653,6 +653,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "queen-attack", + "name": "Queen Attack", + "uuid": "e68041f6-a661-41dd-94fb-2000095803ae", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "reverse-string", "name": "Reverse String", diff --git a/exercises/practice/queen-attack/.docs/instructions.md b/exercises/practice/queen-attack/.docs/instructions.md new file mode 100644 index 00000000..97f22a0a --- /dev/null +++ b/exercises/practice/queen-attack/.docs/instructions.md @@ -0,0 +1,21 @@ +# Instructions + +Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other. + +In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal. + +A chessboard can be represented by an 8 by 8 array. + +So if you are told the white queen is at `c5` (zero-indexed at column 2, row 3) and the black queen at `f2` (zero-indexed at column 5, row 6), then you know that the set-up is like so: + +![A chess board with two queens. Arrows emanating from the queen at c5 indicate possible directions of capture along file, rank and diagonal.](https://assets.exercism.org/images/exercises/queen-attack/queen-capture.svg) + +You are also able to answer whether the queens can attack each other. +In this case, that answer would be yes, they can, because both pieces share a diagonal. + +## Credit + +The chessboard image was made by [habere-et-dispertire][habere-et-dispertire] using LaTeX and the [chessboard package][chessboard-package] by Ulrike Fischer. + +[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire +[chessboard-package]: https://github.com/u-fischer/chessboard diff --git a/exercises/practice/queen-attack/.meta/config.json b/exercises/practice/queen-attack/.meta/config.json new file mode 100644 index 00000000..7231bf4f --- /dev/null +++ b/exercises/practice/queen-attack/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "queen-attack.jl" + ], + "test": [ + "runtests.jl" + ], + "example": [ + ".meta/example.jl" + ] + }, + "blurb": "Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.", + "source": "J Dalbey's Programming Practice problems", + "source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html" +} diff --git a/exercises/practice/queen-attack/.meta/example.jl b/exercises/practice/queen-attack/.meta/example.jl new file mode 100644 index 00000000..943d5767 --- /dev/null +++ b/exercises/practice/queen-attack/.meta/example.jl @@ -0,0 +1,24 @@ +struct InvalidPosition <: Exception + message::String +end + +struct Queen + row::Int + col::Int + Queen(row::Int, col::Int) = + if row < 0 + throw(InvalidPosition("row must be positive")) + elseif row > 7 + throw(InvalidPosition("row not on board")) + elseif row < 0 + throw(InvalidPosition("column must be positive")) + elseif col > 7 + throw(InvalidPosition("column not on board")) + else + new(row, col) + end +end + +function canattack(white::Queen, black::Queen) + white.row == black.row || white.col == black.col || abs(white.row - black.row) == abs(white.col - black.col) +end diff --git a/exercises/practice/queen-attack/.meta/tests.toml b/exercises/practice/queen-attack/.meta/tests.toml new file mode 100644 index 00000000..c673d531 --- /dev/null +++ b/exercises/practice/queen-attack/.meta/tests.toml @@ -0,0 +1,51 @@ +# 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. + +[3ac4f735-d36c-44c4-a3e2-316f79704203] +description = "Test creation of Queens with valid and invalid positions -> queen with a valid position" + +[4e812d5d-b974-4e38-9a6b-8e0492bfa7be] +description = "Test creation of Queens with valid and invalid positions -> queen must have positive row" +include = false + +[f07b7536-b66b-4f08-beb9-4d70d891d5c8] +description = "Test creation of Queens with valid and invalid positions -> queen must have row on board" + +[15a10794-36d9-4907-ae6b-e5a0d4c54ebe] +description = "Test creation of Queens with valid and invalid positions -> queen must have positive column" +include = false + +[6907762d-0e8a-4c38-87fb-12f2f65f0ce4] +description = "Test creation of Queens with valid and invalid positions -> queen must have column on board" + +[33ae4113-d237-42ee-bac1-e1e699c0c007] +description = "Test the ability of one queen to attack another -> cannot attack" + +[eaa65540-ea7c-4152-8c21-003c7a68c914] +description = "Test the ability of one queen to attack another -> can attack on same row" + +[bae6f609-2c0e-4154-af71-af82b7c31cea] +description = "Test the ability of one queen to attack another -> can attack on same column" + +[0e1b4139-b90d-4562-bd58-dfa04f1746c7] +description = "Test the ability of one queen to attack another -> can attack on first diagonal" + +[ff9b7ed4-e4b6-401b-8d16-bc894d6d3dcd] +description = "Test the ability of one queen to attack another -> can attack on second diagonal" + +[0a71e605-6e28-4cc2-aa47-d20a2e71037a] +description = "Test the ability of one queen to attack another -> can attack on third diagonal" + +[0790b588-ae73-4f1f-a968-dd0b34f45f86] +description = "Test the ability of one queen to attack another -> can attack on fourth diagonal" + +[543f8fd4-2597-4aad-8d77-cbdab63619f8] +description = "Test the ability of one queen to attack another -> cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal" diff --git a/exercises/practice/queen-attack/queen-attack.jl b/exercises/practice/queen-attack/queen-attack.jl new file mode 100644 index 00000000..35cd5ecd --- /dev/null +++ b/exercises/practice/queen-attack/queen-attack.jl @@ -0,0 +1,11 @@ +struct InvalidPosition <: Exception + +end + +struct Queen + +end + +function canattack(white::Queen, black::Queen) + +end diff --git a/exercises/practice/queen-attack/runtests.jl b/exercises/practice/queen-attack/runtests.jl new file mode 100644 index 00000000..52d6f3ac --- /dev/null +++ b/exercises/practice/queen-attack/runtests.jl @@ -0,0 +1,44 @@ +using Test + +include("queen-attack.jl") + +@testset verbose = true "tests" begin + @testset "test creation of Queens" begin + @testset "Queen with a valid position" begin + @test Queen(2, 2) == Queen(2, 2) + end + @testset "Queen must have row on board" begin + @test_throws InvalidPosition Queen(8, 4) + end + @testset "Queen must have column on board" begin + @test_throws InvalidPosition Queen(4, 8) + end + end + + @testset "test the ability of one queen to attack another" begin + @testset "cannot attack" begin + @test !canattack(Queen(2, 4), Queen(6, 6)) + end + @testset "can attack on same row" begin + @test canattack(Queen(2, 4), Queen(2, 6)) + end + @testset "can attack on same column" begin + @test canattack(Queen(4, 5), Queen(2, 5)) + end + @testset "can attack on first diagonal" begin + @test canattack(Queen(2, 2), Queen(0, 4)) + end + @testset "can attack on second diagonal" begin + @test canattack(Queen(2, 2), Queen(3, 1)) + end + @testset "can attack on third diagonal" begin + @test canattack(Queen(2, 2), Queen(1, 1)) + end + @testset "can attack on fourth diagonal" begin + @test canattack(Queen(1, 7), Queen(0, 6)) + end + @testset "cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal" begin + @test !canattack(Queen(4, 1), Queen(2, 5)) + end + end +end \ No newline at end of file