Avatar of vlzware

vlzware's solution

to Queen Attack in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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're told the white queen is at (2, 3) and the black queen at (5, 6), then you'd know you've got a set-up like so:

_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _

You'd also be 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.

Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_queen_attack.c

#include "vendor/unity.h"
#include "../src/queen_attack.h"

void test_invalid_if_on_same_position(void)
{
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 2;
   white_queen.row = 5;
   black_queen.column = white_queen.column;
   black_queen.row = white_queen.row;

   TEST_ASSERT_EQUAL(INVALID_POSITION, can_attack(white_queen, black_queen));
}

void test_white_queen_must_have_row_on_board(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 2;
   white_queen.row = 8;
   black_queen.column = 0;
   black_queen.row = 0;

   TEST_ASSERT_EQUAL(INVALID_POSITION, can_attack(white_queen, black_queen));
}

void test_white_queen_must_have_column_on_board(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 8;
   white_queen.row = 2;
   black_queen.column = 0;
   black_queen.row = 0;

   TEST_ASSERT_EQUAL(INVALID_POSITION, can_attack(white_queen, black_queen));
}

void test_black_queen_must_have_row_on_board(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 0;
   white_queen.row = 0;
   black_queen.column = 2;
   black_queen.row = 10;

   TEST_ASSERT_EQUAL(INVALID_POSITION, can_attack(white_queen, black_queen));
}

void test_black_queen_must_have_column_on_board(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 0;
   white_queen.row = 0;
   black_queen.column = 9;
   black_queen.row = 4;

   TEST_ASSERT_EQUAL(INVALID_POSITION, can_attack(white_queen, black_queen));
}

void test_can_not_attack(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 4;
   white_queen.row = 2;
   black_queen.column = 6;
   black_queen.row = 6;

   TEST_ASSERT_EQUAL(CAN_NOT_ATTACK, can_attack(white_queen, black_queen));
}

void test_can_attack_on_same_row(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 4;
   white_queen.row = 2;
   black_queen.column = 6;
   black_queen.row = 2;

   TEST_ASSERT_EQUAL(CAN_ATTACK, can_attack(white_queen, black_queen));
}

void test_can_attack_on_same_column(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 5;
   white_queen.row = 4;
   black_queen.column = 5;
   black_queen.row = 2;

   TEST_ASSERT_EQUAL(CAN_ATTACK, can_attack(white_queen, black_queen));
}

void test_can_attack_on_first_diagonal(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 2;
   white_queen.row = 2;
   black_queen.column = 4;
   black_queen.row = 0;

   TEST_ASSERT_EQUAL(CAN_ATTACK, can_attack(white_queen, black_queen));
}

void test_can_attack_on_second_diagonal(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 2;
   white_queen.row = 2;
   black_queen.column = 1;
   black_queen.row = 3;

   TEST_ASSERT_EQUAL(CAN_ATTACK, can_attack(white_queen, black_queen));
}

void test_can_attack_on_third_diagonal(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 2;
   white_queen.row = 2;
   black_queen.column = 1;
   black_queen.row = 1;

   TEST_ASSERT_EQUAL(CAN_ATTACK, can_attack(white_queen, black_queen));
}

void test_can_attack_on_fourth_diagonal(void)
{
   TEST_IGNORE();
   position_t white_queen;
   position_t black_queen;

   white_queen.column = 2;
   white_queen.row = 2;
   black_queen.column = 5;
   black_queen.row = 5;

   TEST_ASSERT_EQUAL(CAN_ATTACK, can_attack(white_queen, black_queen));
}

int main(void)
{
   UnityBegin("test/test_queen_attack.c");

   RUN_TEST(test_invalid_if_on_same_position);
   RUN_TEST(test_white_queen_must_have_row_on_board);
   RUN_TEST(test_white_queen_must_have_column_on_board);
   RUN_TEST(test_black_queen_must_have_row_on_board);
   RUN_TEST(test_black_queen_must_have_column_on_board);
   RUN_TEST(test_can_not_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);

   UnityEnd();
   return 0;
}

src/queen_attack.c

#include "queen_attack.h"

#define WIDTH 8

static int abs(int x)
{
	return (x < 0) ? -x : x;
}

static int same_diag(int a_1, int a_2, int b_1, int b_2)
{
	return abs(b_2 - a_2) == abs(b_1 - a_1);
}

attack_status_t can_attack(position_t queen_1, position_t queen_2)
{
	if (
		queen_1.column >= WIDTH || queen_1.row >= WIDTH ||
		queen_2.column >= WIDTH || queen_2.row >= WIDTH ||
		(
			(queen_1.column == queen_2.column) &&
			(queen_1.row == queen_2.row)
		)
	)
		return INVALID_POSITION;

	if (
		(queen_1.column == queen_2.column) ||
		(queen_1.row == queen_2.row)
	)
		return CAN_ATTACK;

	return
		same_diag(queen_1.column, queen_1.row, queen_2.column,
			queen_2.row)
		? CAN_ATTACK : CAN_NOT_ATTACK;
}

src/queen_attack.h

#ifndef QUEEN_ATTACK_H
#define QUEEN_ATTACK_H

#include <stdint.h>

typedef enum {
   CAN_NOT_ATTACK,
   CAN_ATTACK,
   INVALID_POSITION
} attack_status_t;

typedef struct {
   uint8_t row;
   uint8_t column;
} position_t;

attack_status_t can_attack(position_t queen_1, position_t queen_2);

#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?