Skip to content

Commit

Permalink
renames and various bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnysbug committed Apr 21, 2022
1 parent 62b9f2b commit 36f76e5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 70 deletions.
12 changes: 11 additions & 1 deletion lib/domain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@ class Context {
this.remainingTries, this.message, this.currentIndex, this.lastPlayed);

factory Context.fromJson(Map<String, dynamic> json) {
var board = Board.fromJson(json['board']);
var isPrevious = json['board'] is List;
Board board;
if (isPrevious) {
var tiles = <Letter>[];
json['board'].forEach((letter) {
tiles.add(Letter.fromJson(letter));
});
board = Board(tiles);
} else {
board = Board.fromJson(json['board']);
}
var keys = <List<Letter>>[];
if (json['keys'] != null) {
json['keys'].forEach((row) {
Expand Down
42 changes: 24 additions & 18 deletions lib/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Flutterdle {

static final StatsService _statsService = StatsService();

final baseDate = DateTime(2021, DateTime.june, 19);
final _baseDate = DateTime(2021, DateTime.june, 19);

late Context _context;
late Stats _stats;
Expand All @@ -26,9 +26,11 @@ class Flutterdle {

final _wordService = WordService();

bool isEvaluating = false;

Stats get stats => _stats;
Settings get settings => _settings;
int get _gameNumber => DateTime.now().difference(baseDate).inDays;
int get _gameNumber => DateTime.now().difference(_baseDate).inDays;

void updateBoard(List<Letter> attempt) {
for (var i = 0; i < attempt.length; i++) {
Expand Down Expand Up @@ -89,10 +91,11 @@ class Flutterdle {
var board = Board(List.filled(boardSize, Letter(0, '', GameColor.unset), growable: false));
_context = Context(board, KeyboardService.init().keys, '', '', [], TurnResult.unset, totalTries,
'Good Luck!', 0, DateTime.now());
_context.answer = _wordService.getWordOfTheDay(baseDate);
_context.answer = _wordService.getWordOfTheDay(_baseDate);
}

bool didWin(List<Letter> attempt) => attempt.every((l) => l.color == GameColor.exact);
bool didWin(List<Letter> attempt) =>
attempt.isNotEmpty && attempt.every((l) => l.color == GameColor.exact);

String _winningMessage(int remainingTries) {
switch (remainingTries) {
Expand Down Expand Up @@ -141,6 +144,7 @@ class Flutterdle {
}

void evaluateTurn(String letter) {
isEvaluating = true;
_context.turnResult = TurnResult.partial;
if (KeyboardService.isEnter(letter)) {
if (!_wordService.isLongEnough(_context.guess)) {
Expand Down Expand Up @@ -175,21 +179,23 @@ class Flutterdle {
List<GlobalKey<AnimatorWidgetState>> get bounceKeys => _bounceKeys;

Future updateAfterSuccessfulGuess() async {
_context.keys = _updateKeys(_context.keys, _context.attempt);
var won = didWin(_context.attempt);
if (won || _context.remainingTries == 1) {
_stats = await _updateStats(won, _context.remainingTries);
if (_context.turnResult == TurnResult.successful) {
_context.keys = _updateKeys(_context.keys, _context.attempt);
var won = didWin(_context.attempt);
if (won || _context.remainingTries == 1) {
_stats = await _updateStats(won, _context.remainingTries);
}
var remaining = _context.remainingTries - 1;
_context.guess = '';
_context.attempt = [];
_context.remainingTries = won ? 0 : remaining;
_context.message = won
? _winningMessage(remaining)
: remaining == 0
? 'Sorry, you lost'
: '';
persist();
}
var remaining = _context.remainingTries - 1;
_context.guess = '';
_context.attempt = [];
_context.remainingTries = won ? 0 : remaining;
_context.message = won
? _winningMessage(remaining)
: remaining == 0
? 'Sorry, you lost'
: '';
persist();
}

void persist() {
Expand Down
12 changes: 8 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flurdle',
title: 'Flutterdle',
debugShowCheckedModeBanner: false,
theme: AppTheme.lightTheme,
themeMode: _appTheme,
darkTheme: AppTheme.darkTheme,
home: MyHomePage(_appTheme, _streamController, title: 'Flurdle'),
home: MyHomePage(_appTheme, _streamController, title: 'Flutterdle'),
);
}
}
Expand Down Expand Up @@ -104,14 +104,15 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _onKeyPressed(String val) {
if (_game.context.remainingTries == 0) {
if (_game.context.remainingTries == 0 || _game.isEvaluating) {
return;
}
setState(() {
_game.evaluateTurn(val);
if (_game.context.turnResult == TurnResult.unsuccessful) {
var index = (_game.context.remainingTries - Flutterdle.totalTries).abs();
_game.shakeKeys[index].currentState?.forward();
_game.isEvaluating = false;
} else if (_game.context.turnResult == TurnResult.successful) {
for (var i = 0; i < _game.context.attempt.length; i++) {
var offset =
Expand Down Expand Up @@ -141,8 +142,11 @@ class _MyHomePageState extends State<MyHomePage> {
setState(() {
_game.updateAfterSuccessfulGuess().then((_) => setState(() {}));
_resetMessage();
_game.isEvaluating = false;
});
});
} else {
_game.isEvaluating = false;
}
});
_resetMessage();
Expand Down Expand Up @@ -197,7 +201,7 @@ class _MyHomePageState extends State<MyHomePage> {
centerTitle: true,
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
padding: const EdgeInsets.only(left: 16, right: 16.0),
child: GestureDetector(
onTap: () => _openStats(),
child: const Icon(
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/how_to.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HowTo extends StatelessWidget {
text: 'Guess the ',
children: [
TextSpan(
text: 'FLURDLE',
text: 'FLUTTERDLE',
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' in six tries.')
],
Expand Down Expand Up @@ -217,7 +217,7 @@ class HowTo extends StatelessWidget {
text: 'A new ',
children: [
TextSpan(
text: 'FLURDLE',
text: 'FLUTTERDLE',
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' is available each day.')
],
Expand Down
93 changes: 49 additions & 44 deletions lib/widgets/stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class StatsWidget extends StatefulWidget {
}

class _StatsState extends State<StatsWidget> {
int _getFlex(int number, int total) => (number / (number + (total - number)) * 10).ceil();
int _getFlex(int number, int total) => total == 0 ? 0 : (number / (number + (total - number)) * 10).ceil();

Future<Stats> get _stats => Future.microtask(() => StatsService().loadStats());

Expand All @@ -36,14 +36,14 @@ class _StatsState extends State<StatsWidget> {
fit: BoxFit.contain,
child: SizedBox(
width: 500,
height: 500,
height: 520,
child: Stack(children: [
Positioned(
top: 0,
left: 0,
child: SizedBox(
width: 410,
height: 500,
height: 520,
child: Column(children: [
Row(
children: [
Expand Down Expand Up @@ -181,61 +181,66 @@ class _StatsState extends State<StatsWidget> {
),
Padding(
padding: const EdgeInsets.all(20.0),
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.only(right: 16),
child: IntrinsicHeight(
child: Row(
children: [
Expanded(
child: Column(
children: [
const Center(
child: Text(
"NEXT FLURDLE",
"NEXT FLUTTERDLE",
style: TextStyle(
fontSize: 18,
fontSize: 16,
),
)),
CountdownWidget(widget.newGame),
],
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(right: 16, left: 16),
child: ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.green),
onPressed: () {
var guesses = widget.stats.lastGuess == -1
? 'X'
: widget.stats.lastGuess;
Share.share(
'Flurdle ${widget.stats.gameNumber} $guesses/6\n${widget.stats.lastBoard}',
subject: 'Flurdle $guesses/6');
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Text('SHARE',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 18,
)),
SizedBox(
width: 5,
),
Icon(
Icons.share,
color: Colors.white,
size: 24.0,
),
],
VerticalDivider(
color: Theme.of(context).colorScheme.secondary,
width: 2,
indent: 2,
endIndent: 2,
thickness: 2,),
Expanded(
child: Padding(
padding: const EdgeInsets.only(right: 16, left: 16),
child: ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.green),
onPressed: () {
var guesses = widget.stats.lastGuess == -1
? 'X'
: widget.stats.lastGuess;
Share.share(
'Flutterdle ${widget.stats.gameNumber} $guesses/6\n${widget.stats.lastBoard}',
subject: 'Flutterdle $guesses/6');
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Text('SHARE',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 18,
)),
SizedBox(
width: 5,
),
Icon(
Icons.share,
color: Colors.white,
size: 24.0,
),
],
),
),
),
),
),
],
],
),
),
)
]),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
version: 1.0.3+1

environment:
sdk: ">=2.16.1 <3.0.0"
Expand Down

0 comments on commit 36f76e5

Please sign in to comment.