-
-
Notifications
You must be signed in to change notification settings - Fork 18
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 exercise #183
Closed
Closed
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,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. |
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,2 @@ | ||
*.o | ||
tests |
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": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"queen_attack.asm" | ||
], | ||
"test": [ | ||
"queen_attack_test.c" | ||
], | ||
"example": [ | ||
".meta/example.asm" | ||
] | ||
}, | ||
"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,41 @@ | ||
section .text | ||
global can_create | ||
; rdi - row | ||
; rsi - column | ||
; rax - result | ||
; rdx - 7 | ||
can_create: | ||
xor rax, rax | ||
mov rdx, 7 | ||
cmp rdi, rdx | ||
ja .return | ||
cmp rsi, rdx | ||
ja .return | ||
inc rax | ||
.return: | ||
ret | ||
|
||
; rdi - white row | ||
; rsi - white column | ||
; rdx - black row | ||
; rcx - black column | ||
global can_attack | ||
can_attack: | ||
xor rax, rax | ||
sub rdi, rdx ; row difference | ||
jz .attacks | ||
sub rsi, rcx ; column difference | ||
jz .attacks | ||
cmp rdi, rsi | ||
jz .attacks | ||
add rdi, rsi | ||
jz .attacks | ||
ret | ||
|
||
.attacks: | ||
inc rax | ||
ret | ||
|
||
%ifidn __OUTPUT_FORMAT__,elf64 | ||
section .note.GNU-stack noalloc noexec nowrite progbits | ||
%endif |
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,46 @@ | ||
AS = nasm | ||
|
||
CFLAGS = -g -Wall -Wextra -pedantic -Werror | ||
LDFLAGS = | ||
ASFLAGS = -g -F dwarf -Werror | ||
|
||
ifeq ($(shell uname -s),Darwin) | ||
ifeq ($(shell sysctl -n hw.optional.arm64 2>/dev/null),1) | ||
ALL_CFLAGS = -target x86_64-apple-darwin | ||
endif | ||
ALL_LDFLAGS = -Wl,-pie -Wl,-fatal_warnings | ||
ALL_ASFLAGS = -f macho64 --prefix _ | ||
else | ||
ALL_LDFLAGS = -pie -Wl,--fatal-warnings | ||
ALL_ASFLAGS = -f elf64 | ||
endif | ||
|
||
ALL_CFLAGS += -std=c99 -fPIE -m64 $(CFLAGS) | ||
ALL_LDFLAGS += $(LDFLAGS) | ||
ALL_ASFLAGS += $(ASFLAGS) | ||
|
||
C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c)) | ||
AS_OBJS = $(patsubst %.asm,%.o,$(wildcard *.asm)) | ||
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o) | ||
|
||
CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $< | ||
|
||
all: tests | ||
@./$< | ||
|
||
tests: $(ALL_OBJS) | ||
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS) | ||
|
||
%.o: %.asm | ||
@$(AS) $(ALL_ASFLAGS) -o $@ $< | ||
|
||
%.o: %.c | ||
@$(CC_CMD) | ||
|
||
vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h | ||
@$(CC_CMD) | ||
|
||
clean: | ||
@rm -f *.o vendor/*.o tests | ||
|
||
.PHONY: all clean |
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,14 @@ | ||
section .text | ||
global can_create | ||
can_create: | ||
; Provide your implementation here | ||
ret | ||
|
||
global can_attack | ||
can_attack: | ||
; Provide your implementation here | ||
ret | ||
|
||
%ifidn __OUTPUT_FORMAT__,elf64 | ||
section .note.GNU-stack noalloc noexec nowrite progbits | ||
%endif |
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,97 @@ | ||
// Version: 1.0.0 | ||
|
||
#include "vendor/unity.h" | ||
|
||
// Returns 1 if queen can be created, otherwise 0. | ||
extern int can_create(int row, int column); | ||
|
||
// Returns 1 if queens attack, otherwise 0. | ||
extern int can_attack(int white_row, int white_column, int black_row, int black_column); | ||
|
||
void setUp(void) { | ||
} | ||
|
||
void tearDown(void) { | ||
} | ||
|
||
void test_queen_with_a_valid_position(void) { | ||
TEST_ASSERT_TRUE(can_create(2, 2)); | ||
} | ||
|
||
void test_queen_must_have_positive_row(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_FALSE(can_create(-2, 2)); | ||
} | ||
|
||
void test_queen_must_have_row_on_board(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_FALSE(can_create(8, 4)); | ||
} | ||
|
||
void test_queen_must_have_positive_column(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_FALSE(can_create(2, -2)); | ||
} | ||
|
||
void test_queen_must_have_column_on_board(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_FALSE(can_create(4, 8)); | ||
} | ||
|
||
void test_cannot_attack(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_FALSE(can_attack(2, 4, 6, 6)); | ||
} | ||
|
||
void test_can_attack_on_same_row(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_TRUE(can_attack(2, 4, 2, 6)); | ||
} | ||
|
||
void test_can_attack_on_same_column(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_TRUE(can_attack(4, 5, 2, 5)); | ||
} | ||
|
||
void test_can_attack_on_first_diagonal(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_TRUE(can_attack(2, 2, 0, 4)); | ||
} | ||
|
||
void test_can_attack_on_second_diagonal(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_TRUE(can_attack(2, 2, 3, 1)); | ||
} | ||
|
||
void test_can_attack_on_third_diagonal(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_TRUE(can_attack(2, 2, 1, 1)); | ||
} | ||
|
||
void test_can_attack_on_fourth_diagonal(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_TRUE(can_attack(1, 7, 0, 6)); | ||
} | ||
|
||
void test_cannot_attack_if_falling_diagonals_are_only_the_same_when_reflected_across_the_longest_falling_diagonal(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_FALSE(can_attack(4, 1, 2, 5)); | ||
} | ||
|
||
int main(void) { | ||
UNITY_BEGIN(); | ||
RUN_TEST(test_queen_with_a_valid_position); | ||
RUN_TEST(test_queen_must_have_positive_row); | ||
RUN_TEST(test_queen_must_have_row_on_board); | ||
RUN_TEST(test_queen_must_have_positive_column); | ||
RUN_TEST(test_queen_must_have_column_on_board); | ||
RUN_TEST(test_cannot_attack); | ||
RUN_TEST(test_can_attack_on_same_row); | ||
RUN_TEST(test_can_attack_on_same_column); | ||
RUN_TEST(test_can_attack_on_first_diagonal); | ||
RUN_TEST(test_can_attack_on_second_diagonal); | ||
RUN_TEST(test_can_attack_on_third_diagonal); | ||
RUN_TEST(test_can_attack_on_fourth_diagonal); | ||
RUN_TEST(test_cannot_attack_if_falling_diagonals_are_only_the_same_when_reflected_across_the_longest_falling_diagonal); | ||
return UNITY_END(); | ||
} |
Oops, something went wrong.
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.
Two possible alternatives:
Alternative One:
Alternative Two with no create function (adapted from C track):