Skip to content

Commit

Permalink
[wordle] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed May 22, 2024
1 parent c01bc4c commit c60f14d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 139 deletions.
8 changes: 6 additions & 2 deletions lib/game/wordle/entity/record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ part "record.g.dart";
class RecordWordle extends GameRecord {
final GameResult result;
final Duration playtime;
final String blueprint;
final List<String> attempts;
final WordleVocabulary vocabulary;
final String blueprint;

const RecordWordle({
required super.ts,
required this.result,
required this.playtime,
required this.blueprint,
required this.vocabulary,
required this.attempts,
required this.blueprint,
});

factory RecordWordle.createFrom({
required Duration playtime,
required GameResult result,
required List<String> attempts,
required WordleVocabulary vocabulary,
}) {
final blueprint = BlueprintWordle(
Expand All @@ -36,6 +39,7 @@ class RecordWordle extends GameRecord {
ts: DateTime.now(),
result: result,
playtime: playtime,
attempts: attempts,
vocabulary: vocabulary,
blueprint: blueprint.build(),
);
Expand Down
6 changes: 4 additions & 2 deletions lib/game/wordle/entity/record.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/game/wordle/manager/logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GameLogic extends StateNotifier<GameStateWordle> {
playtime: playtime,
);

bool get acceptInput => state.input.length < maxLetters;
bool get acceptInput => state.attempts.length < maxAttempts && state.input.length < maxLetters;

bool get canBackspace => state.input.isNotEmpty;

Expand Down Expand Up @@ -73,6 +73,7 @@ class GameLogic extends StateNotifier<GameStateWordle> {
StorageWordle.record.add(RecordWordle.createFrom(
playtime: state.playtime,
vocabulary: state.vocabulary,
attempts: state.attempts,
result: GameResult.victory,
));
}
Expand All @@ -84,6 +85,7 @@ class GameLogic extends StateNotifier<GameStateWordle> {
StorageWordle.record.add(RecordWordle.createFrom(
playtime: state.playtime,
vocabulary: state.vocabulary,
attempts: state.attempts,
result: GameResult.gameOver,
));
applyGameHapticFeedback(HapticFeedbackIntensity.heavy);
Expand Down
110 changes: 45 additions & 65 deletions lib/game/wordle/widget/display.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:rettulf/rettulf.dart';
import '../entity/keyboard.dart';
import '../entity/letter.dart';
import '../entity/status.dart';
Expand Down Expand Up @@ -105,7 +106,7 @@ class _WordleDisplayWidgetState extends State<WordleDisplayWidget> with TickerPr
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
buildDisplayBoard(),
buildDisplayBoard().expanded(),
const Padding(
padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 30.0),
child: WordleKeyboardWidget(),
Expand Down Expand Up @@ -140,74 +141,53 @@ class _WordleDisplayWidgetState extends State<WordleDisplayWidget> with TickerPr
}

Widget buildDisplayBoard() {
return Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 0.0),
child: Align(
alignment: Alignment.center,
child: AspectRatio(
aspectRatio: maxLetters / maxAttempts,
child: Column(
//Column(
children: [
for (int i = 0; i < maxAttempts; i++)
Expanded(
flex: 1,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (int j = 0; j < maxLetters; j++)
AnimatedBuilder(
animation: inputs[i][j].animation,
return AspectRatio(
aspectRatio: maxLetters / maxAttempts,
child: [
for (int i = 0; i < maxAttempts; i++)
[
for (int j = 0; j < maxLetters; j++)
AnimatedBuilder(
animation: inputs[i][j].animation,
builder: (context, child) {
return Transform.scale(
scale: Tween<double>(begin: 1, end: 1.1).evaluate(inputs[i][j].animation),
child: child,
);
},
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, constraints) {
return AnimatedSwitcher(
duration: const Duration(milliseconds: 700),
switchInCurve: Curves.easeOut,
reverseDuration: const Duration(milliseconds: 0),
transitionBuilder: (child, animation) {
return AnimatedBuilder(
animation: animation,
child: child,
builder: (context, child) {
return Transform.scale(
scale: Tween<double>(begin: 1, end: 1.1).evaluate(inputs[i][j].animation),
var _animation = Tween<double>(begin: math.pi / 2, end: 0).animate(animation);
return Transform(
transform: Matrix4.rotationX(_animation.value),
alignment: Alignment.center,
child: child,
);
},
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, constraints) {
return AnimatedSwitcher(
duration: const Duration(milliseconds: 700),
switchInCurve: Curves.easeOut,
reverseDuration: const Duration(milliseconds: 0),
transitionBuilder: (child, animation) {
return AnimatedBuilder(
animation: animation,
child: child,
builder: (context, child) {
var _animation = Tween<double>(begin: math.pi / 2, end: 0).animate(animation);
return Transform(
transform: Matrix4.rotationX(_animation.value),
alignment: Alignment.center,
child: child,
);
},
);
},
child: Padding(
key: ValueKey(inputs[i][j].status),
padding: const EdgeInsets.all(5.0),
child: LetterBox(
status: inputs[i][j].status,
letter: inputs[i][j].letter,
),
),
);
},
),
),
),
],
),
);
},
child: LetterBox(
status: inputs[i][j].status,
letter: inputs[i][j].letter,
).padAll(5.0, key: ValueKey(inputs[i][j].status)),
);
},
),
],
),
),
),
),
);
),
),
].row(maa: MainAxisAlignment.center).expanded(flex: 1),
].column(),
).padSymmetric(h: 20, v: 10).center();
}
}
98 changes: 29 additions & 69 deletions lib/game/wordle/widget/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,75 +84,35 @@ class _WordleKeyboardWidgetState extends State<WordleKeyboardWidget> {

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Spacer(
flex: 1,
),
for (int i = 0; i < 10; i++)
Expanded(
flex: 2,
child: Padding(
padding: const EdgeInsets.fromLTRB(3.0, 5.0, 3.0, 5.0),
child: WordleLetterKeyWidget(
letter: _keyPos[0][i],
status: _keyState[_keyPos[0][i]] ?? LetterStatus.neutral,
),
),
),
const Spacer(
flex: 1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
for (int i = 0; i < 9; i++)
Expanded(
flex: 1,
child: WordleLetterKeyWidget(
letter: _keyPos[1][i],
status: _keyState[_keyPos[1][i]] ?? LetterStatus.neutral,
).padSymmetric(h: 3, v: 5),
),
Expanded(
flex: 2,
child: const WordleBackspaceKeyWidget().padSymmetric(h: 5, v: 5),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
const Spacer(
flex: 1,
),
for (int i = 0; i < 7; i++)
Expanded(
flex: 2,
child: WordleLetterKeyWidget(
letter: _keyPos[2][i],
status: _keyState[_keyPos[2][i]] ?? LetterStatus.neutral,
).padSymmetric(h: 3, v: 5),
),
Expanded(
flex: 6,
child: const WordleEnterKeyWidget().padSymmetric(h: 5, v: 5),
),
const Spacer(
flex: 1,
),
],
),
],
);
return [
[
const Spacer(flex: 1),
for (int i = 0; i < _keyPos[0].length; i++)
WordleLetterKeyWidget(
letter: _keyPos[0][i],
status: _keyState[_keyPos[0][i]] ?? LetterStatus.neutral,
).padSymmetric(h: 3, v: 5).expanded(flex: 2),
const Spacer(flex: 1),
].row(maa: MainAxisAlignment.center),
[
for (int i = 0; i < _keyPos[1].length; i++)
WordleLetterKeyWidget(
letter: _keyPos[1][i],
status: _keyState[_keyPos[1][i]] ?? LetterStatus.neutral,
).padSymmetric(h: 3, v: 5).expanded(flex: 1),
const WordleBackspaceKeyWidget().padSymmetric(h: 5, v: 5).expanded(flex: 2),
].row(maa: MainAxisAlignment.center),
[
const Spacer(flex: 1),
for (int i = 0; i < _keyPos[2].length; i++)
WordleLetterKeyWidget(
letter: _keyPos[2][i],
status: _keyState[_keyPos[2][i]] ?? LetterStatus.neutral,
).padSymmetric(h: 3, v: 5).expanded(flex: 2),
const WordleEnterKeyWidget().padSymmetric(h: 5, v: 5).expanded(flex: 6),
const Spacer(flex: 1),
].row(maa: MainAxisAlignment.center),
].column(maa: MainAxisAlignment.center);
}
}

Expand Down

0 comments on commit c60f14d

Please sign in to comment.