Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1131,13 +1131,22 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "flower-field",
"name": "Flower Field",
"uuid": "d17d040b-b214-4079-ad3e-b518f776aeeb",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "3753a72a-78b7-429f-b79a-f68d55a00387",
"practices": [],
"prerequisites": [],
"difficulty": 5
"difficulty": 5,
"status": "deprecated"
},
{
"slug": "sublist",
Expand Down
26 changes: 26 additions & 0 deletions exercises/practice/flower-field/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Instructions

Your task is to add flower counts to empty squares in a completed Flower Field garden.
The garden itself is a rectangle board composed of squares that are either empty (`' '`) or a flower (`'*'`).

For each empty square, count the number of flowers adjacent to it (horizontally, vertically, diagonally).
If the empty square has no adjacent flowers, leave it empty.
Otherwise replace it with the count of adjacent flowers.

For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen):

```text
·*·*·
··*··
··*··
·····
```

Which your code should transform into this:

```text
1*3*1
13*31
·2*2·
·111·
```
7 changes: 7 additions & 0 deletions exercises/practice/flower-field/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

[Flower Field][history] is a compassionate reimagining of the popular game Minesweeper.
The object of the game is to find all the flowers in the garden using numeric hints that indicate how many flowers are directly adjacent (horizontally, vertically, diagonally) to a square.
"Flower Field" shipped in regional versions of Microsoft Windows in Italy, Germany, South Korea, Japan and Taiwan.

[history]: https://web.archive.org/web/20020409051321fw_/http://rcm.usr.dsi.unimi.it/rcmweb/fnm/
22 changes: 22 additions & 0 deletions exercises/practice/flower-field/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"vaeng"
],
"contributors": [
"BNAndras"
],
"files": {
"solution": [
"flower_field.cpp",
"flower_field.h"
],
"test": [
"flower_field_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Mark all the flowers in a garden."
}
54 changes: 54 additions & 0 deletions exercises/practice/flower-field/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "flower_field.h"

namespace flower_field {
int score(const std::vector<std::string>& garden, size_t row, size_t column) {
int flowers{};
// north
if (row > 0 && garden.at(row - 1).at(column) == '*') ++flowers;
// north-east
if (row > 0 && column < garden.at(0).size() - 1 &&
garden.at(row - 1).at(column + 1) == '*')
++flowers;
// north-west
if (row > 0 && column > 0 && garden.at(row - 1).at(column - 1) == '*')
++flowers;
// east
if (column < garden.at(0).size() - 1 &&
garden.at(row).at(column + 1) == '*')
++flowers;
// west
if (column > 0 && garden.at(row).at(column - 1) == '*') ++flowers;
// south
if (row < garden.size() - 1 && garden.at(row + 1).at(column) == '*')
++flowers;
// south-east
if (row < garden.size() - 1 && column < garden.at(0).size() - 1 &&
garden.at(row + 1).at(column + 1) == '*')
++flowers;
// south-west
if (row < garden.size() - 1 && column > 0 &&
garden.at(row + 1).at(column - 1) == '*')
++flowers;
return flowers;
}

std::vector<std::string> annotate(const std::vector<std::string>& garden) {
std::vector<std::string> annotation{};
for (size_t row{}; row < garden.size(); ++row) {
std::string annotated{};
for (size_t column{}; column < garden.at(0).size(); ++column) {
if (garden.at(row).at(column) == '*') {
annotated += '*';
} else {
int score_number = score(garden, row, column);
char score_char = (score_number == 0)
? ' '
: static_cast<char>(score_number) + '0';
annotated += score_char;
}
}
annotation.push_back(std::move(annotated));
}
return annotation;
}
} // namespace flower_field
8 changes: 8 additions & 0 deletions exercises/practice/flower-field/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <string>
#include <vector>

namespace flower_field {
std::vector<std::string> annotate(const std::vector<std::string>& garden);
} // namespace flower_field
46 changes: 46 additions & 0 deletions exercises/practice/flower-field/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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.

[237ff487-467a-47e1-9b01-8a891844f86c]
description = "no rows"

[4b4134ec-e20f-439c-a295-664c38950ba1]
description = "no columns"

[d774d054-bbad-4867-88ae-069cbd1c4f92]
description = "no flowers"

[225176a0-725e-43cd-aa13-9dced501f16e]
description = "garden full of flowers"

[3f345495-f1a5-4132-8411-74bd7ca08c49]
description = "flower surrounded by spaces"

[6cb04070-4199-4ef7-a6fa-92f68c660fca]
description = "space surrounded by flowers"

[272d2306-9f62-44fe-8ab5-6b0f43a26338]
description = "horizontal line"

[c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e]
description = "horizontal line, flowers at edges"

[a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5]
description = "vertical line"

[b40f42f5-dec5-4abc-b167-3f08195189c1]
description = "vertical line, flowers at edges"

[58674965-7b42-4818-b930-0215062d543c]
description = "cross"

[dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8]
description = "large garden"
67 changes: 67 additions & 0 deletions exercises/practice/flower-field/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# 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()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
# Treat warnings as errors
# Treat type conversion warnings C4244 and C4267 as level 4 warnings, i.e. ignore them in level 3
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS
COMPILE_FLAGS "/WX /w44244 /w44267")
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
7 changes: 7 additions & 0 deletions exercises/practice/flower-field/flower_field.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "flower_field.h"

namespace flower_field {

// TODO: add your solution here

} // namespace flower_field
7 changes: 7 additions & 0 deletions exercises/practice/flower-field/flower_field.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace flower_field {

// TODO: add your solution here

} // namespace flower_field
85 changes: 85 additions & 0 deletions exercises/practice/flower-field/flower_field_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "flower_field.h"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif

/*
The expected outputs are represented as arrays of strings to
improve readability in this JSON file.
Your track may choose whether to present the input as a single
string (concatenating all the lines) or as the list.
*/

TEST_CASE("no rows", "[237ff487-467a-47e1-9b01-8a891844f86c]") {
const std::vector<std::string> expected{};
REQUIRE(expected == flower_field::annotate({}));
}

#if defined(EXERCISM_RUN_ALL_TESTS)

TEST_CASE("no columns", "[4b4134ec-e20f-439c-a295-664c38950ba1]") {
const std::vector<std::string> expected{""};
REQUIRE(expected == flower_field::annotate({""}));
}

TEST_CASE("no flowers", "[d774d054-bbad-4867-88ae-069cbd1c4f92]") {
const std::vector<std::string> expected{" ", " ", " "};
REQUIRE(expected == flower_field::annotate({" ", " ", " "}));
}

TEST_CASE("garden full of flowers", "[225176a0-725e-43cd-aa13-9dced501f16e]") {
const std::vector<std::string> expected{"***", "***", "***"};
REQUIRE(expected == flower_field::annotate({"***", "***", "***"}));
}

TEST_CASE("flower surrounded by spaces",
"[3f345495-f1a5-4132-8411-74bd7ca08c49]") {
const std::vector<std::string> expected{"111", "1*1", "111"};
REQUIRE(expected == flower_field::annotate({" ", " * ", " "}));
}

TEST_CASE("space surrounded by flowers",
"[6cb04070-4199-4ef7-a6fa-92f68c660fca]") {
const std::vector<std::string> expected{"***", "*8*", "***"};
REQUIRE(expected == flower_field::annotate({"***", "* *", "***"}));
}

TEST_CASE("horizontal line", "[272d2306-9f62-44fe-8ab5-6b0f43a26338]") {
const std::vector<std::string> expected{"1*2*1"};
REQUIRE(expected == flower_field::annotate({" * * "}));
}

TEST_CASE("horizontal line, flowers at edges",
"[c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e]") {
const std::vector<std::string> expected{"*1 1*"};
REQUIRE(expected == flower_field::annotate({"* *"}));
}

TEST_CASE("vertical line", "[a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5]") {
const std::vector<std::string> expected{"1", "*", "2", "*", "1"};
REQUIRE(expected == flower_field::annotate({" ", "*", " ", "*", " "}));
}

TEST_CASE("vertical line, flowers at edges",
"[b40f42f5-dec5-4abc-b167-3f08195189c1]") {
const std::vector<std::string> expected{"*", "1", " ", "1", "*"};
REQUIRE(expected == flower_field::annotate({"*", " ", " ", " ", "*"}));
}

TEST_CASE("cross", "[58674965-7b42-4818-b930-0215062d543c]") {
const std::vector<std::string> expected{" 2*2 ", "25*52", "*****", "25*52",
" 2*2 "};
REQUIRE(expected == flower_field::annotate(
{" * ", " * ", "*****", " * ", " * "}));
}

TEST_CASE("large garden", "[dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8]") {
const std::vector<std::string> expected{"1*22*1", "12*322", " 123*2",
"112*4*", "1*22*2", "111111"};
REQUIRE(expected == flower_field::annotate({" * * ", " * ", " * ",
" * *", " * * ", " "}));
}

#endif
Loading