-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
137 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"cSpell.words": ["powerable", "Powerable"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:christmas_lights_kata/grid/grid.dart'; | ||
import 'package:christmas_lights_kata/led/dimmable_led.dart'; | ||
|
||
/// A grid of dimmable LEDs. | ||
class DimmableGrid extends Grid<DimmableLed> { | ||
/// Creates a new [Grid] of [DimmableLed]s. | ||
DimmableGrid(int size) : super(size, DimmableLed.new); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:christmas_lights_kata/grid/grid.dart'; | ||
import 'package:christmas_lights_kata/led/powerable_led.dart'; | ||
|
||
/// A grid of powerable LEDs. | ||
class PowerableGrid extends Grid<PowerableLed> { | ||
/// Creates a new [Grid] of [PowerableLed]s. | ||
PowerableGrid(int size) : super(size, PowerableLed.new); | ||
|
||
/// Returns the number of powered [PowerableLed]s in the [Grid]. | ||
int get poweredLedsCount => leds.fold<int>( | ||
0, | ||
(count, row) => count + row.where((led) => led.isPowered).length, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'package:christmas_lights_kata/led/led.dart'; | ||
import 'package:christmas_lights_kata/utils/map_int_v_extension.dart'; | ||
|
||
const _offLight = '⚫️'; | ||
const _brightnessLights = { | ||
0: _offLight, | ||
1: '🔅', | ||
10: '🔆', | ||
}; | ||
|
||
/// A dimmable LED. | ||
class DimmableLed extends Led<int> { | ||
/// Constructs a new [DimmableLed]. | ||
DimmableLed() : _brightness = 0; | ||
|
||
int _brightness; | ||
|
||
/// Whether this [DimmableLed] is turned on or off. | ||
int get brightness => _brightness; | ||
|
||
/// Increases the brightness of this [DimmableLed] by 1. | ||
@override | ||
int turnOn() => _brightness++; | ||
|
||
/// Reduces the brightness of this [DimmableLed] by 1. | ||
@override | ||
int turnOff() { | ||
if (_brightness > 0) _brightness--; | ||
|
||
return _brightness; | ||
} | ||
|
||
/// Increases the brightness of this [DimmableLed] by 2. | ||
@override | ||
int toggle() => _brightness = _brightness + 2; | ||
|
||
@override | ||
String toString() => | ||
_brightnessLights.valueFromThreshold(_brightness) ?? _offLight; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// An LED. | ||
abstract class Led<T> { | ||
/// Turns on this [Led]. | ||
T turnOn(); | ||
|
||
/// Turns off this [Led]. | ||
T turnOff(); | ||
|
||
/// Toggles this [Led]. | ||
T toggle(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/// Map int to V extension. | ||
extension MapIntVExtension<V> on Map<int, V> { | ||
/// Returns the corresponding value [V], if any, when the [threshold] is | ||
/// lower than or equal to one of this [Map] keys. | ||
V? valueFromThreshold(int threshold) { | ||
final value = this[threshold]; | ||
if (value != null) return value; | ||
|
||
for (final entry in entries) { | ||
if (threshold <= entry.key) return entry.value; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters