queen-attack: initial commit

This commit is contained in:
Dmitry Kokorin 2016-05-11 19:47:22 +03:00
parent 0f23771e94
commit 92b98cf850
4 changed files with 310 additions and 0 deletions

View file

@ -0,0 +1,59 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
# Basic CMake project
cmake_minimum_required(VERSION 2.8.11)
# Name the project after the exercise
project(${exercise} CXX)
# Locate Boost libraries: unit_test_framework, date_time and regex
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.55 REQUIRED COMPONENTS unit_test_framework date_time regex)
# Enable C++11 features on gcc/clang
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set(CMAKE_CXX_FLAGS "-std=c++11")
endif()
# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
add_definitions(-DEXERCISM_RUN_ALL_TESTS)
endif()
# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})
# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()
# Include a test helper header if it exists
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/require_equal_containers.h)
set(test_helper require_equal_containers.h)
else()
set(test_helper "")
endif()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${test_helper} ${exercise_cpp} ${file}.h)
# We need boost includes
target_include_directories(${exercise} PRIVATE ${Boost_INCLUDE_DIRS})
# We need boost libraries
target_link_libraries(${exercise} ${Boost_LIBRARIES})
# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()
# Run the tests on every build
add_custom_command(TARGET ${exercise} POST_BUILD COMMAND ${exercise})

View file

@ -0,0 +1,61 @@
# Queen Attack
Write a program that positions two queens on a chess board and indicates 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:
```plain
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ 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](http://exercism.io/languages/cpp) on
exercism.io. 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](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
Create just enough structure by declaring namespaces, functions, classes,
etc., 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,
uncomment the next test by moving the following line past the next test.
```C++
#if defined(EXERCISM_RUN_ALL_TESTS)
```
This may result in compile errors as new constructs may be invoked that
you haven't yet declared or defined. Again, fix the compile errors minimally
to get a failing test, then change the code minimally to pass the test,
refactor your implementation for readability and expressiveness and then
go on to the next test.
Try to use standard C++11 facilities in preference to writing your own
low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/)
is a wiki reference to the C++ language and standard library. If you
are new to C++, but have programmed in C, beware of
[C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers).
## Source
J Dalbey's Programming Practice problems [http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html](http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html)

View file

@ -0,0 +1,102 @@
#include "queen_attack.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include "require_equal_containers.h"
BOOST_AUTO_TEST_CASE(queens_in_default_positions)
{
const queen_attack::chess_board board;
BOOST_REQUIRE_EQUAL(std::make_pair(0, 3), board.white());
BOOST_REQUIRE_EQUAL(std::make_pair(7, 3), board.black());
}
#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(initialized_with_specific_positions)
{
const auto white = std::make_pair(3, 7);
const auto black = std::make_pair(6, 1);
const queen_attack::chess_board board{white, black};
BOOST_REQUIRE_EQUAL(white, board.white());
BOOST_REQUIRE_EQUAL(black, board.black());
}
BOOST_AUTO_TEST_CASE(queen_positions_must_be_distinct)
{
const auto pos = std::make_pair(3, 7);
BOOST_REQUIRE_THROW((queen_attack::chess_board{pos, pos}), std::domain_error);
}
BOOST_AUTO_TEST_CASE(string_representation)
{
const queen_attack::chess_board board{std::make_pair(2, 4), std::make_pair(6, 6)};
const std::string expected{
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ W _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ B _\n"
"_ _ _ _ _ _ _ _\n"};
BOOST_REQUIRE_EQUAL(expected, static_cast<std::string>(board));
}
BOOST_AUTO_TEST_CASE(queens_cannot_attack)
{
const queen_attack::chess_board board{std::make_pair(2, 3), std::make_pair(4, 7)};
BOOST_REQUIRE(!board.can_attack());
}
BOOST_AUTO_TEST_CASE(queens_can_attack_when_they_are_on_the_same_row)
{
const queen_attack::chess_board board{std::make_pair(2, 4), std::make_pair(2, 7)};
BOOST_REQUIRE(board.can_attack());
}
BOOST_AUTO_TEST_CASE(queens_can_attack_when_they_are_on_the_same_column)
{
const queen_attack::chess_board board{std::make_pair(5, 4), std::make_pair(2, 4)};
BOOST_REQUIRE(board.can_attack());
}
BOOST_AUTO_TEST_CASE(queens_can_attack_diagonally)
{
const queen_attack::chess_board board{std::make_pair(1, 1), std::make_pair(6, 6)};
BOOST_REQUIRE(board.can_attack());
}
BOOST_AUTO_TEST_CASE(queens_can_attack_another_diagonally)
{
const queen_attack::chess_board board{std::make_pair(0, 6), std::make_pair(1, 7)};
BOOST_REQUIRE(board.can_attack());
}
BOOST_AUTO_TEST_CASE(queens_can_attack_yet_another_diagonally)
{
const queen_attack::chess_board board{std::make_pair(4, 1), std::make_pair(6, 3)};
BOOST_REQUIRE(board.can_attack());
}
BOOST_AUTO_TEST_CASE(queens_can_attack_on_the_nw_so_diagonal)
{
const queen_attack::chess_board board{std::make_pair(1, 6), std::make_pair(6, 1)};
BOOST_REQUIRE(board.can_attack());
}
BOOST_AUTO_TEST_CASE(queens_cannot_attack_if_not_on_same_row_column_or_diagonal)
{
const queen_attack::chess_board board{std::make_pair(1, 1), std::make_pair(3, 7)};
BOOST_REQUIRE(!board.can_attack());
}
#endif

View file

@ -0,0 +1,88 @@
#if !defined(REQUIRE_EQUAL_CONTAINERS_H)
#define REQUIRE_EQUAL_CONTAINERS_H
#include <utility>
#include <vector>
#include <boost/version.hpp>
#if BOOST_VERSION >= 105900
namespace boost
{
namespace test_tools
{
namespace tt_detail
{
// teach Boost.Test how to print std::vector<T>
template <typename T>
inline std::ostream &operator<<(std::ostream &str, std::vector<T> const &items)
{
str << '[';
bool first = true;
for (auto const& element : items) {
str << (!first ? "," : "") << element;
first = false;
}
return str << ']';
}
// teach Boost.Test how to print std::pair<K,V>
template <typename K, typename V>
inline std::ostream &operator<<(std::ostream &str, std::pair<K, V> const& item)
{
return str << '<' << item.first << ',' << item.second << '>';
}
} // namespace tt_detail
} // namespace test_tools
} // namespace boost
#else // BOOST_VERSION < 105900
namespace boost
{
// teach Boost.Test how to print std::vector to wrap_stringstream
template <typename T>
inline wrap_stringstream&
operator<<(wrap_stringstream& wrapped, std::vector<T> const& item)
{
wrapped << '[';
bool first = true;
for (auto const& element : item) {
wrapped << (!first ? "," : "") << element;
first = false;
}
return wrapped << ']';
}
// teach Boost.Test how to print std::pair<K,V> to wrap_stringstream
template <typename K, typename V>
inline wrap_stringstream &operator<<(wrap_stringstream &str, std::pair<K, V> const& item)
{
return str << '<' << item.first << ',' << item.second << '>';
}
namespace test_tools
{
// teach Boost.Test how to print std::pair with BOOST_REQUIRE_EQUAL
template<>
struct print_log_value<std::pair<int, int>>
{
void operator()(std::ostream& ostr, std::pair<int, int> const& item)
{
ostr << '<' << item.first << ',' << item.second << '>';
}
};
} // namespace test_tools
} // namespace boost
#endif // BOOST_VERSION
#define REQUIRE_EQUAL_CONTAINERS(left_, right_) \
BOOST_REQUIRE_EQUAL_COLLECTIONS(left_.begin(), left_.end(), right_.begin(), right_.end())
#endif