Skip to content

Commit

Permalink
Add queen-attack
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed May 19, 2024
1 parent 6480f30 commit abd7184
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "queen-attack",
"name": "Queen Attack",
"uuid": "42c10a4c-140f-40d0-9a46-8373dd7d7a72",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "rna-transcription",
"name": "RNA Transcription",
Expand Down
21 changes: 21 additions & 0 deletions exercises/practice/queen-attack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions exercises/practice/queen-attack/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"queen-attack.rkt"
],
"test": [
"queen-attack-test.rkt"
],
"example": [
".meta/example.rkt"
]
},
"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"
}
27 changes: 27 additions & 0 deletions exercises/practice/queen-attack/.meta/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#lang racket

(provide create-queen
can-attack?)

(struct queen (row column))

(define (grid-position? n)
(and (exact-nonnegative-integer? n)
(> 8 n)))

(define/contract (create-queen row column)
(-> grid-position? grid-position? queen?)
(queen row column))

(define/contract (can-attack? white-queen black-queen)
(-> queen? queen? boolean?)
(match (list white-queen black-queen)
[(list (queen r _) (queen r _))
#t]
[(list (queen _ c) (queen _ c))
#t]
[(list (queen wr wc) (queen br bc))
#:when (= (abs (- wr br))
(abs (- wc bc)))
#t]
[_ #f]))
49 changes: 49 additions & 0 deletions exercises/practice/queen-attack/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
64 changes: 64 additions & 0 deletions exercises/practice/queen-attack/queen-attack-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#lang racket/base

(require "queen-attack.rkt")

(module+ test
(require rackunit rackunit/text-ui)

(define suite
(test-suite
"queen-attack tests"

(test-not-exn "queen with a valid position"
(lambda () (create-queen 2 2)))

(test-exn "queen must have positive row"
exn:fail?
(lambda () (create-queen -2 2)))

(test-exn "queen must have row on board"
exn:fail?
(lambda () (create-queen 8 4)))

(test-exn "queen must have positive column"
exn:fail?
(lambda () (create-queen 2 -2)))

(test-exn "queen must have column on board"
exn:fail?
(lambda () (create-queen 4 8)))

(test-false "cannot attack"
(can-attack? (create-queen 2 4)
(create-queen 6 6)))

(test-true "can attack on same row"
(can-attack? (create-queen 2 4)
(create-queen 2 6)))

(test-true "can attack on same column"
(can-attack? (create-queen 4 5)
(create-queen 2 5)))

(test-true "can attack on first diagonal"
(can-attack? (create-queen 2 2)
(create-queen 0 4)))

(test-true "can attack on second diagonal"
(can-attack? (create-queen 2 2)
(create-queen 3 1)))

(test-true "can attack on third diagonal"
(can-attack? (create-queen 2 2)
(create-queen 1 1)))

(test-true "can attack on fourth diagonal"
(can-attack? (create-queen 1 7)
(create-queen 0 6)))

(test-false "cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal"
(can-attack? (create-queen 4 1)
(create-queen 2 5)))))

(run-tests suite))

10 changes: 10 additions & 0 deletions exercises/practice/queen-attack/queen-attack.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#lang racket

(provide create-queen
can-attack?)

(define (create-queen row column)
(error "Not implemented yet"))

(define (can-attack? white-queen black-queen)
(error "Not implemented yet"))

0 comments on commit abd7184

Please sign in to comment.