Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Exercise]: Queen Attack #242

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,18 @@
"practices": [],
"prerequisites": [],
"difficulty": 4
}
},
{
"slug": "queen-attack",
"name": "Queen Attack",
"uuid": "d116b6c3-9a79-4b03-9dbb-0499d9e6d112",
"practices": [
"classes",
"string"
],
"prerequisites": [],
"difficulty": 4
}
]
},
"concepts": [],
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/queen-attack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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:

```text
a b c d e f g h
8 _ _ _ _ _ _ _ _ 8
7 _ _ _ _ _ _ _ _ 7
6 _ _ _ _ _ _ _ _ 6
5 _ _ W _ _ _ _ _ 5
4 _ _ _ _ _ _ _ _ 4
3 _ _ _ _ _ _ _ _ 3
2 _ _ _ _ _ B _ _ 2
1 _ _ _ _ _ _ _ _ 1
a b c d e f g h
```

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.
85 changes: 85 additions & 0 deletions exercises/practice/queen-attack/.meta/QueenAttack.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<#
.SYNOPSIS
Given the positions of two queens on a chess board,
indicate whether or not they are positioned so that they can attack each other.

.DESCRIPTION
In a chessboard represented by an 8 by 8 array, check if a queen can attack another queen based on their positions.
In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.
If there are no position provided, queens will be placed at their starting positions: White at bottom (7,3), Black at top (0,3).

.EXAMPLE
#Positions provided:
$whitePosition = @(2, 2)
$blackPosition = @(5, 6)
$board = [ChessBoard]::new($whitePosition, $blackPosition)
$board.CanAttack() => False
$board.DrawBoard()
@"
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ W _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
"@

#No positions provided:
$board2 = [ChessBoard]::new()
$board2.CanAttack() => True
$board.DrawBoard()
@"
_ _ _ B _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
"@
#>
Class ChessBoard {
[int[]]$White
[int[]]$Black

ChessBoard() {
$this.White = @(7,3)
$this.Black = @(0,3)
}

ChessBoard([int[]]$WhitePos,[int[]]$BlackPos) {
[ChessBoard]::ValidateQueens($WhitePos, $BlackPos)
$this.White = $WhitePos
$this.Black = $BlackPos
}

static [void] ValidateQueens([int[]]$WhitePos, [int[]]$BlackPos) {
if ($WhitePos | Where-Object {$_ -gt 7 -or $_ -lt 0 }) {
Throw "White queen must be placed on the board"
}
if ($BlackPos | Where-Object {$_ -gt 7 -or $_ -lt 0 }) {
Throw "Black queen must be placed on the board"
}
if (-not (Compare-Object $WhitePos $BlackPos)) {
Throw 'Queens can not share the same space'
}
}

[bool] CanAttack() {
$row_check = $this.White[0] -eq $this.Black[0]
$col_check = $this.White[1] -eq $this.Black[1]
$diag_check = [Math]::Abs($this.White[0] - $this.Black[0]) -eq [Math]::Abs($this.White[1] - $this.Black[1])
return $row_check -or $col_check -or $diag_check
}

[string] DrawBoard() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this task is also brought from another track.
Which track to be specific?
It doesn't seem to be Python.

Copy link
Contributor Author

@glaxxie glaxxie Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey meatball, I did mention that it was from the javascript track in the comment above. Although I swear I did try to add and do the print out on my own when I did this problem in Elixir. I saw the drawn board in introduction, and thought it was good practice to add it myself for debugging and working with string. I actually used ♕ and ♛ (U+2655 and U+265B) instead of W and B.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, and sorry I missed that comment at the top. I have seen the drawing part on multiple tracks so just wanted to know where specific this version is taken from.

$empty_row = @('_') * 8
$Board = @(0..7 | ForEach-Object {,$empty_row.Clone()})
$Board[$this.White[0]][$this.White[1]] = 'W'
$Board[$this.Black[0]][$this.Black[1]] = 'B'
return ($Board | ForEach-Object {$_ -join " "}) -join "`r`n"
}
}
17 changes: 17 additions & 0 deletions exercises/practice/queen-attack/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["glaxxie"],
"files": {
"solution": [
"QueenAttack.ps1"
],
"test": [
"QueenAttack.tests.ps1"
],
"example": [
".meta/QueenAttack.example.ps1"
]
},
"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"
}
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"
48 changes: 48 additions & 0 deletions exercises/practice/queen-attack/QueenAttack.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<#
.SYNOPSIS
Given the positions of two queens on a chess board,
indicate whether or not they are positioned so that they can attack each other.

.DESCRIPTION
In a chessboard represented by an 8 by 8 array, check if a queen can attack another queen based on their positions.
In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.
If there are no position provided, queens will be placed at their starting positions: White at bottom (7,3), Black at top (0,3).

.EXAMPLE
#Positions provided:
$whitePosition = @(2, 2)
$blackPosition = @(5, 6)
$board = [ChessBoard]::new($whitePosition, $blackPosition)
$board.CanAttack() => False
$board.DrawBoard()
@"
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ W _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
"@

#No positions provided:
$board2 = [ChessBoard]::new()
$board2.CanAttack() => True
$board.DrawBoard()
@"
_ _ _ B _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
"@
#>
Class ChessBoard {
ChessBoard() {
Throw "Please implement this class"
}
}