-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Add queen-attack
#599
Merged
+243
−0
Merged
Add queen-attack
#599
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"lib/queen_attack.dart" | ||
], | ||
"test": [ | ||
"test/queen_attack_test.dart" | ||
], | ||
"example": [ | ||
".meta/lib/example.dart" | ||
] | ||
}, | ||
"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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Queen { | ||
final int row; | ||
final int column; | ||
|
||
Queen(int row, int column) | ||
: assert(row >= 0, 'row not positive'), | ||
assert(row <= 7, 'row not on board'), | ||
assert(column >= 0, 'column not positive'), | ||
assert(column <= 7, 'column not on board'), | ||
this.row = row, | ||
this.column = column; | ||
|
||
bool canAttack(Queen other) => | ||
this.row == other.row || | ||
this.column == other.column || | ||
(this.row - other.row).abs() == (this.column - other.column).abs(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
analyzer: | ||
strong-mode: | ||
implicit-casts: false | ||
implicit-dynamic: false | ||
errors: | ||
unused_element: error | ||
unused_import: error | ||
unused_local_variable: error | ||
dead_code: error | ||
|
||
linter: | ||
rules: | ||
# Error Rules | ||
- avoid_relative_lib_imports | ||
- avoid_types_as_parameter_names | ||
- literal_only_boolean_expressions | ||
- no_adjacent_strings_in_list | ||
- valid_regexps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Queen { | ||
// Put your code here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: 'queen_attack' | ||
environment: | ||
sdk: '>=3.2.0 <4.0.0' | ||
dev_dependencies: | ||
test: '<2.0.0' |
103 changes: 103 additions & 0 deletions
103
exercises/practice/queen-attack/test/queen_attack_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import 'package:queen_attack/queen_attack.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Queen Attack', () { | ||
group('Test creation of Queens with valid and invalid positions', () { | ||
test('Queens with a valid position', () { | ||
expect(() => Queen(2, 2), returnsNormally); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't seen |
||
}, skip: false); | ||
|
||
test('Queens must have positive row value', () { | ||
expect( | ||
() => Queen(-2, 2), | ||
throwsA( | ||
predicate((e) => e is AssertionError && e.message == 'row not positive'), | ||
)); | ||
}, skip: true); | ||
|
||
test('Queens must have row value on board', () { | ||
expect( | ||
() => Queen(8, 4), | ||
throwsA( | ||
predicate((e) => e is AssertionError && e.message == 'row not on board'), | ||
)); | ||
}, skip: true); | ||
|
||
test('Queens must have positive column value', () { | ||
expect( | ||
() => Queen(2, -2), | ||
throwsA( | ||
predicate((e) => e is AssertionError && e.message == 'column not positive'), | ||
)); | ||
}, skip: true); | ||
|
||
test('Queens must have column value on board', () { | ||
expect( | ||
() => Queen(4, 8), | ||
throwsA( | ||
predicate((e) => e is AssertionError && e.message == 'column not on board'), | ||
)); | ||
}, skip: true); | ||
}); | ||
|
||
group('Test the ability of one queen to attack another', () { | ||
test('cannot attack', () { | ||
final Queen white = Queen(2, 4); | ||
final Queen black = Queen(6, 6); | ||
|
||
expect(white.canAttack(black), isFalse); | ||
}, skip: true); | ||
|
||
test('can attack on same row', () { | ||
final Queen white = Queen(2, 4); | ||
final Queen black = Queen(2, 6); | ||
|
||
expect(white.canAttack(black), isTrue); | ||
}, skip: true); | ||
|
||
test('can attack on same column', () { | ||
final Queen white = Queen(4, 5); | ||
final Queen black = Queen(2, 5); | ||
|
||
expect(white.canAttack(black), isTrue); | ||
}, skip: true); | ||
|
||
test('can attack on first diagonal', () { | ||
final Queen white = Queen(2, 2); | ||
final Queen black = Queen(0, 4); | ||
|
||
expect(white.canAttack(black), isTrue); | ||
}, skip: true); | ||
|
||
test('can attack on second diagonal', () { | ||
final Queen white = Queen(2, 2); | ||
final Queen black = Queen(3, 1); | ||
|
||
expect(white.canAttack(black), isTrue); | ||
}, skip: true); | ||
|
||
test('can attack on third diagonal', () { | ||
final Queen white = Queen(2, 2); | ||
final Queen black = Queen(1, 1); | ||
|
||
expect(white.canAttack(black), isTrue); | ||
}, skip: true); | ||
|
||
test('can attack on fourth diagonal', () { | ||
final Queen white = Queen(1, 7); | ||
final Queen black = Queen(0, 6); | ||
|
||
expect(white.canAttack(black), isTrue); | ||
}, skip: true); | ||
|
||
test('cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal', | ||
() { | ||
final Queen white = Queen(4, 1); | ||
final Queen black = Queen(2, 5); | ||
|
||
expect(white.canAttack(black), isFalse); | ||
}, skip: true); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, with the understanding that asserts are compiled out of production code.