From ad49a34275591f06876fecf73c81884e4b16ab2a Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Mon, 9 Dec 2024 12:47:44 +0100 Subject: [PATCH] Add `queen-attack` exercise --- config.json | 8 +++ .../queen-attack/.docs/instructions.append.md | 3 + .../queen-attack/.docs/instructions.md | 21 +++++++ .../practice/queen-attack/.meta/config.json | 19 ++++++ .../practice/queen-attack/.meta/example.ua | 7 +++ .../practice/queen-attack/.meta/tests.toml | 49 +++++++++++++++ .../practice/queen-attack/queen-attack.ua | 17 ++++++ exercises/practice/queen-attack/tests.ua | 59 +++++++++++++++++++ 8 files changed, 183 insertions(+) create mode 100644 exercises/practice/queen-attack/.docs/instructions.append.md 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.ua create mode 100644 exercises/practice/queen-attack/.meta/tests.toml create mode 100644 exercises/practice/queen-attack/queen-attack.ua create mode 100644 exercises/practice/queen-attack/tests.ua diff --git a/config.json b/config.json index b1bc389..fba6885 100644 --- a/config.json +++ b/config.json @@ -553,6 +553,14 @@ "practices": [], "prerequisites": [], "difficulty": 9 + }, + { + "slug": "queen-attack", + "name": "Queen Attack", + "uuid": "b1e4dd71-27e8-4ba6-af6b-5c0ad8a11df5", + "practices": [], + "prerequisites": [], + "difficulty": 3 } ] }, diff --git a/exercises/practice/queen-attack/.docs/instructions.append.md b/exercises/practice/queen-attack/.docs/instructions.append.md new file mode 100644 index 0000000..b308f20 --- /dev/null +++ b/exercises/practice/queen-attack/.docs/instructions.append.md @@ -0,0 +1,3 @@ +# Instructions append + +This exercises uses [modules as functions](https://www.uiua.org/tutorial/modules#modules-as-functions) for a nice way to group functions that work on the same type of data (queens on a chessboard in this case). diff --git a/exercises/practice/queen-attack/.docs/instructions.md b/exercises/practice/queen-attack/.docs/instructions.md new file mode 100644 index 0000000..97f22a0 --- /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 0000000..76e751d --- /dev/null +++ b/exercises/practice/queen-attack/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "queen-attack.ua" + ], + "test": [ + "tests.ua" + ], + "example": [ + ".meta/example.ua" + ] + }, + "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.ua b/exercises/practice/queen-attack/.meta/example.ua new file mode 100644 index 0000000..9765c98 --- /dev/null +++ b/exercises/practice/queen-attack/.meta/example.ua @@ -0,0 +1,7 @@ +┌─╴Queen + Valid ← ×⊓≥≤0,7 + New ← ⊟⊓(⍤"invalid row" Valid.|⍤"invalid column" Valid.) + Row ← ⊢ + Column ← ⊣ + CanAttack ← ↥↥⊃(=|⋅⋅=|=⊙(⌵-)⌵-) ⊃(∩Row|∩Column) +└─╴ diff --git a/exercises/practice/queen-attack/.meta/tests.toml b/exercises/practice/queen-attack/.meta/tests.toml new file mode 100644 index 0000000..e062412 --- /dev/null +++ b/exercises/practice/queen-attack/.meta/tests.toml @@ -0,0 +1,49 @@ +# 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" + +[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" + +[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.ua b/exercises/practice/queen-attack/queen-attack.ua new file mode 100644 index 0000000..9efac34 --- /dev/null +++ b/exercises/practice/queen-attack/queen-attack.ua @@ -0,0 +1,17 @@ +┌─╴Queen + # Create a new queen + # Queen ? Row Column + New ← |2 ⊙⋅(⍤ "Please implement New" 0) + + # Get a queen's row + # Row ? Queen + Row ← |1 ⊙(⍤ "Please implement Row" 0) + + # Get a queen's column + # Column ? Queen + Column ← |1 ⊙(⍤ "Please implement Row" 0) + + # Check if two queens can attack each other + # Validity ? Queen1 Queen2 + CanAttack ← |2 ⊙⋅(⍤ "Please implement CanAttack" 0) +└─╴ diff --git a/exercises/practice/queen-attack/tests.ua b/exercises/practice/queen-attack/tests.ua new file mode 100644 index 0000000..cbc4223 --- /dev/null +++ b/exercises/practice/queen-attack/tests.ua @@ -0,0 +1,59 @@ +~ "queen-attack.ua" ~ Queen + +# Queen must have positive row +⍤⤙≍ "invalid row" ⍣(Queen~New ¯2 2) + +# Queen must have row on board +⍤⤙≍ "invalid row" ⍣(Queen~New 8 4) + +# Queen must have positive column +⍤⤙≍ "invalid column" ⍣(Queen~New 2 ¯2) + +# Queen must have column on board +⍤⤙≍ "invalid column" ⍣(Queen~New 4 8) + +# Row of queen with a valid position +⍤⤙≍ 2 Queen~Row Queen~New 2 3 + +# Column of queen with a valid position +⍤⤙≍ 3 Queen~Column Queen~New 2 3 + +# Cannot attack +WhiteQueen ← Queen~New 2 4 +BlackQueen ← Queen~New 6 6 +⍤⤙≍ 0 Queen~CanAttack BlackQueen WhiteQueen + +# Can attack on same row +WhiteQueen ← Queen~New 2 4 +BlackQueen ← Queen~New 2 6 +⍤⤙≍ 1 Queen~CanAttack BlackQueen WhiteQueen + +# Can attack on same column +WhiteQueen ← Queen~New 4 5 +BlackQueen ← Queen~New 2 5 +⍤⤙≍ 1 Queen~CanAttack BlackQueen WhiteQueen + +# Can attack on first diagonal +WhiteQueen ← Queen~New 2 2 +BlackQueen ← Queen~New 0 4 +⍤⤙≍ 1 Queen~CanAttack BlackQueen WhiteQueen + +# Can attack on second diagonal +WhiteQueen ← Queen~New 2 2 +BlackQueen ← Queen~New 3 1 +⍤⤙≍ 1 Queen~CanAttack BlackQueen WhiteQueen + +# Can attack on third diagonal +WhiteQueen ← Queen~New 2 2 +BlackQueen ← Queen~New 1 1 +⍤⤙≍ 1 Queen~CanAttack BlackQueen WhiteQueen + +# Can attack on fourth diagonal +WhiteQueen ← Queen~New 1 7 +BlackQueen ← Queen~New 0 6 +⍤⤙≍ 1 Queen~CanAttack BlackQueen WhiteQueen + +# Cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal +WhiteQueen ← Queen~New 4 1 +BlackQueen ← Queen~New 2 5 +⍤⤙≍ 0 Queen~CanAttack BlackQueen WhiteQueen