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
1 change: 1 addition & 0 deletions .lycheeignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
https://www.reddit.com/r/dartlang
https://stackoverflow.com/questions/tagged/dart
20 changes: 19 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -665,7 +682,8 @@
"loops",
"math",
"object_oriented_programming"
]
],
"status": "deprecated"
},
{
"slug": "diamond",
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": [
"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."
}
125 changes: 125 additions & 0 deletions exercises/practice/flower-field/.meta/lib/example.dart
Original file line number Diff line number Diff line change
@@ -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<Cell> _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<Row> _garden = [];

/// Access [index]th row
Row operator [](int index) => _garden[index];

/// Annoted version of garden
List<String> 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<String> 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++;
}
}
}
}
}
}
}
}
}
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"
18 changes: 18 additions & 0 deletions exercises/practice/flower-field/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions exercises/practice/flower-field/lib/flower_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class FlowerField {
// Put your code here.
}
5 changes: 5 additions & 0 deletions exercises/practice/flower-field/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'flower_field'
environment:
sdk: '>=3.2.0 <4.0.0'
dev_dependencies:
test: '<2.0.0'
66 changes: 66 additions & 0 deletions exercises/practice/flower-field/test/flower_field_test.dart
Original file line number Diff line number Diff line change
@@ -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(<String>[]).annotated;
expect(result, equals(<String>[]));
}, skip: false);

test('no columns', () {
final result = FlowerField(<String>['']).annotated;
expect(result, equals(<String>['']));
}, skip: true);

test('no flowers', () {
final result = FlowerField(<String>[' ', ' ', ' ']).annotated;
expect(result, equals(<String>[' ', ' ', ' ']));
}, skip: true);

test('garden full of flowers', () {
final result = FlowerField(<String>['***', '***', '***']).annotated;
expect(result, equals(<String>['***', '***', '***']));
}, skip: true);

test('flower surrounded by spaces', () {
final result = FlowerField(<String>[' ', ' * ', ' ']).annotated;
expect(result, equals(<String>['111', '1*1', '111']));
}, skip: true);

test('space surrounded by flowers', () {
final result = FlowerField(<String>['***', '* *', '***']).annotated;
expect(result, equals(<String>['***', '*8*', '***']));
}, skip: true);

test('horizontal line', () {
final result = FlowerField(<String>[' * * ']).annotated;
expect(result, equals(<String>['1*2*1']));
}, skip: true);

test('horizontal line, flowers at edges', () {
final result = FlowerField(<String>['* *']).annotated;
expect(result, equals(<String>['*1 1*']));
}, skip: true);

test('vertical line', () {
final result = FlowerField(<String>[' ', '*', ' ', '*', ' ']).annotated;
expect(result, equals(<String>['1', '*', '2', '*', '1']));
}, skip: true);

test('vertical line, flowers at edges', () {
final result = FlowerField(<String>['*', ' ', ' ', ' ', '*']).annotated;
expect(result, equals(<String>['*', '1', ' ', '1', '*']));
}, skip: true);

test('cross', () {
final result = FlowerField(<String>[' * ', ' * ', '*****', ' * ', ' * ']).annotated;
expect(result, equals(<String>[' 2*2 ', '25*52', '*****', '25*52', ' 2*2 ']));
}, skip: true);

test('large gardem', () {
final result = FlowerField(<String>[' * * ', ' * ', ' * ', ' * *', ' * * ', ' ']).annotated;
expect(result, equals(<String>['1*22*1', '12*322', ' 123*2', '112*4*', '1*22*2', '111111']));
}, skip: true);
});
}