From c2e39a6ded7eb97c0d6c2a129c8cbb3a65026f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 25 Jun 2025 21:28:04 -0700 Subject: [PATCH 1/2] Add `flower-field`, deprecating `minesweeper` --- config.json | 20 ++- .../flower-field/.docs/instructions.md | 26 ++++ .../flower-field/.docs/introduction.md | 7 + .../practice/flower-field/.meta/config.json | 22 +++ .../flower-field/.meta/lib/example.dart | 125 ++++++++++++++++++ .../practice/flower-field/.meta/tests.toml | 46 +++++++ .../flower-field/analysis_options.yaml | 18 +++ .../flower-field/lib/flower_field.dart | 3 + exercises/practice/flower-field/pubspec.yaml | 5 + .../flower-field/test/flower_field_test.dart | 66 +++++++++ 10 files changed, 337 insertions(+), 1 deletion(-) create mode 100644 exercises/practice/flower-field/.docs/instructions.md create mode 100644 exercises/practice/flower-field/.docs/introduction.md create mode 100644 exercises/practice/flower-field/.meta/config.json create mode 100644 exercises/practice/flower-field/.meta/lib/example.dart create mode 100644 exercises/practice/flower-field/.meta/tests.toml create mode 100644 exercises/practice/flower-field/analysis_options.yaml create mode 100644 exercises/practice/flower-field/lib/flower_field.dart create mode 100644 exercises/practice/flower-field/pubspec.yaml create mode 100644 exercises/practice/flower-field/test/flower_field_test.dart diff --git a/config.json b/config.json index 0f5a9547..4ea7b1e4 100644 --- a/config.json +++ b/config.json @@ -650,6 +650,23 @@ "loops" ] }, + { + "slug": "flower-field", + "name": "Flower Field", + "uuid": "b7b86bc1-b1f9-422e-b18e-4129bfe7a7d2", + "practices": [], + "prerequisites": [], + "difficulty": 5, + "topics": [ + "classes", + "games", + "lists", + "logic", + "loops", + "math", + "object_oriented_programming" + ] + }, { "slug": "minesweeper", "name": "Minesweeper", @@ -665,7 +682,8 @@ "loops", "math", "object_oriented_programming" - ] + ], + "status": "deprecated" }, { "slug": "diamond", diff --git a/exercises/practice/flower-field/.docs/instructions.md b/exercises/practice/flower-field/.docs/instructions.md new file mode 100644 index 00000000..bbdae0c2 --- /dev/null +++ b/exercises/practice/flower-field/.docs/instructions.md @@ -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· +``` diff --git a/exercises/practice/flower-field/.docs/introduction.md b/exercises/practice/flower-field/.docs/introduction.md new file mode 100644 index 00000000..af9b6153 --- /dev/null +++ b/exercises/practice/flower-field/.docs/introduction.md @@ -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/ diff --git a/exercises/practice/flower-field/.meta/config.json b/exercises/practice/flower-field/.meta/config.json new file mode 100644 index 00000000..d55c7097 --- /dev/null +++ b/exercises/practice/flower-field/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "devkabiir" + ], + "contributors": [ + "sisminnmaw", + "kytrinyx", + "BNAndras" + ], + "files": { + "solution": [ + "lib/flower_field.dart" + ], + "test": [ + "test/flower_field_test.dart" + ], + "example": [ + ".meta/lib/example.dart" + ] + }, + "blurb": "Mark all the flowers in a garden." +} diff --git a/exercises/practice/flower-field/.meta/lib/example.dart b/exercises/practice/flower-field/.meta/lib/example.dart new file mode 100644 index 00000000..f47ea044 --- /dev/null +++ b/exercises/practice/flower-field/.meta/lib/example.dart @@ -0,0 +1,125 @@ +class Cell { + /// Row this cell belongs to + final Row parent; + + /// The position of this cell column. + final int index; + + /// Wheter this cell is a flower + bool get isFlower => content == "*"; + + /// This is a private field so it can only be altered by [Row] and [FlowerField] + /// In other words, only elements of this file/library can alter this variable + int _nearbyFlowers = 0; + final String content; + + /// A single cell in the garden. + /// It can either be " " or "*" a number or empty. + Cell(this.parent, this.index, this.content); + + @override + String toString() { + if (isFlower) return "*"; + + /// If there are no columns in the garden, "" is expected. + if (content.isEmpty) return ""; + if (_nearbyFlowers == 0) return " "; + return "$_nearbyFlowers"; + } +} + +/// This contains all the cells in a single row. +class Row { + final List _cells = []; + + /// Access [index]th cell + Cell operator [](int index) => _cells[index]; + + /// This row in the garden + final int index; + + /// [content] is a single item from the input List. + Row(this.index, String content) { + /// If the entered string is empty + /// i.e. no columns. + if (content.isEmpty) { + _cells.add(new Cell(this, 0, "")); + } else { + _cells.addAll( + content.split("").map((content) => new Cell(this, _cells.length, content)), + ); + } + } + + @override + String toString() => _cells.join(); +} + +class FlowerField { + final List _garden = []; + + /// Access [index]th row + Row operator [](int index) => _garden[index]; + + /// Annoted version of garden + List get annotated => _garden.map((row) => "$row").toList(); + + /// Number of columns in this garden + int get columns => _garden.isEmpty ? 0 : _garden.first._cells.length; + + /// Number of rows in thie garden + int get rows => _garden.length; + + FlowerField(List garden) { + if (garden.isNotEmpty) { + _garden.addAll(garden.map((content) => new Row(_garden.length, content))); + + countFlowers(); + } + } + + /// This function counts flowers surrounding a space " ". + void countFlowers() { + /// Check for no rows. + if (rows == 0) return; + + /// Check for no columns. + if (columns == 0) return; + + /// Start looping through all rows. + for (int i = 0; i < rows; i++) { + /// For every row, loop through all of its columns. + for (int j = 0; j < columns; j++) { + /// If the current cell is not a flower. + if (!this[i][j].isFlower) { + /// A single Cell [C] has 8 neighbors [N] + /// 3 at its top, 3 at its bottom + /// 1 at its left side and 1 at its right. + /// "N N N" + /// "N C N" + /// "N N N" + /// Which is essentially a range of x,y co-ord. + /// from -1 to +1, assuming [C] is the center. + /// + /// To count nearby flowers, start iterating through + /// all possible neighbors [N] + for (int xOffset = -1; xOffset <= 1; xOffset++) { + for (int yOffset = -1; yOffset <= 1; yOffset++) { + /// This is the location of current neighbor + final neighborRow = this[i][j].parent.index + xOffset; + final neighborColumn = this[i][j].index + yOffset; + + /// This is to make sure the co-ord. are not out or bound. + if (neighborRow > -1 && neighborRow < rows && neighborColumn > -1 && neighborColumn < columns) { + /// If this neighbor is a flower, increment total + if (this[neighborRow][neighborColumn].isFlower) { + this[i][j]._nearbyFlowers++; + } + } + } + } + } + } + } + } +} diff --git a/exercises/practice/flower-field/.meta/tests.toml b/exercises/practice/flower-field/.meta/tests.toml new file mode 100644 index 00000000..c2b24fda --- /dev/null +++ b/exercises/practice/flower-field/.meta/tests.toml @@ -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" diff --git a/exercises/practice/flower-field/analysis_options.yaml b/exercises/practice/flower-field/analysis_options.yaml new file mode 100644 index 00000000..c06363d6 --- /dev/null +++ b/exercises/practice/flower-field/analysis_options.yaml @@ -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 diff --git a/exercises/practice/flower-field/lib/flower_field.dart b/exercises/practice/flower-field/lib/flower_field.dart new file mode 100644 index 00000000..2b5a7a7d --- /dev/null +++ b/exercises/practice/flower-field/lib/flower_field.dart @@ -0,0 +1,3 @@ +class FlowerField { + // Put your code here. +} diff --git a/exercises/practice/flower-field/pubspec.yaml b/exercises/practice/flower-field/pubspec.yaml new file mode 100644 index 00000000..8bf188c7 --- /dev/null +++ b/exercises/practice/flower-field/pubspec.yaml @@ -0,0 +1,5 @@ +name: 'flower_field' +environment: + sdk: '>=3.2.0 <4.0.0' +dev_dependencies: + test: '<2.0.0' diff --git a/exercises/practice/flower-field/test/flower_field_test.dart b/exercises/practice/flower-field/test/flower_field_test.dart new file mode 100644 index 00000000..548970f5 --- /dev/null +++ b/exercises/practice/flower-field/test/flower_field_test.dart @@ -0,0 +1,66 @@ +import 'package:flower_field/flower_field.dart'; +import 'package:test/test.dart'; + +void main() { + group('Flower Field', () { + test('no rows', () { + final result = FlowerField([]).annotated; + expect(result, equals([])); + }, skip: false); + + test('no columns', () { + final result = FlowerField(['']).annotated; + expect(result, equals([''])); + }, skip: true); + + test('no flowers', () { + final result = FlowerField([' ', ' ', ' ']).annotated; + expect(result, equals([' ', ' ', ' '])); + }, skip: true); + + test('garden full of flowers', () { + final result = FlowerField(['***', '***', '***']).annotated; + expect(result, equals(['***', '***', '***'])); + }, skip: true); + + test('flower surrounded by spaces', () { + final result = FlowerField([' ', ' * ', ' ']).annotated; + expect(result, equals(['111', '1*1', '111'])); + }, skip: true); + + test('space surrounded by flowers', () { + final result = FlowerField(['***', '* *', '***']).annotated; + expect(result, equals(['***', '*8*', '***'])); + }, skip: true); + + test('horizontal line', () { + final result = FlowerField([' * * ']).annotated; + expect(result, equals(['1*2*1'])); + }, skip: true); + + test('horizontal line, flowers at edges', () { + final result = FlowerField(['* *']).annotated; + expect(result, equals(['*1 1*'])); + }, skip: true); + + test('vertical line', () { + final result = FlowerField([' ', '*', ' ', '*', ' ']).annotated; + expect(result, equals(['1', '*', '2', '*', '1'])); + }, skip: true); + + test('vertical line, flowers at edges', () { + final result = FlowerField(['*', ' ', ' ', ' ', '*']).annotated; + expect(result, equals(['*', '1', ' ', '1', '*'])); + }, skip: true); + + test('cross', () { + final result = FlowerField([' * ', ' * ', '*****', ' * ', ' * ']).annotated; + expect(result, equals([' 2*2 ', '25*52', '*****', '25*52', ' 2*2 '])); + }, skip: true); + + test('large gardem', () { + final result = FlowerField([' * * ', ' * ', ' * ', ' * *', ' * * ', ' ']).annotated; + expect(result, equals(['1*22*1', '12*322', ' 123*2', '112*4*', '1*22*2', '111111'])); + }, skip: true); + }); +} From 7640cd3cc42e63e0d16cfcbd33a2f15cd3e9f03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Thu, 26 Jun 2025 08:25:33 -0700 Subject: [PATCH 2/2] ignore Stack Overflow URL --- .lycheeignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.lycheeignore b/.lycheeignore index 9fadf5b4..e918d2c1 100644 --- a/.lycheeignore +++ b/.lycheeignore @@ -1 +1,2 @@ https://www.reddit.com/r/dartlang +https://stackoverflow.com/questions/tagged/dart