This commit is contained in:
Linloir 2022-03-07 16:26:03 +08:00
parent dda182c22c
commit 4f2db49d65
7 changed files with 149 additions and 115 deletions

View File

@ -49,7 +49,7 @@ android {
defaultConfig { defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.wordle" applicationId "com.linloir.wordle"
minSdkVersion flutter.minSdkVersion minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger() versionCode flutterVersionCode.toInteger()

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-03-05 20:56:05 * @Date : 2022-03-05 20:56:05
* @LastEditTime : 2022-03-07 12:03:43 * @LastEditTime : 2022-03-07 14:37:33
* @Description : The display widget of the wordle game * @Description : The display widget of the wordle game
*/ */
@ -28,7 +28,7 @@ class _WordleDisplayWidgetState extends State<WordleDisplayWidget> with TickerPr
void _validationAnimation(List<int> validation) async { void _validationAnimation(List<int> validation) async {
onAnimation = true; onAnimation = true;
bool result = true; bool result = true;
for(int i = 0; i < 5; i++) { for(int i = 0; i < 5 && onAnimation; i++) {
setState((){ setState((){
inputs[r][i]["State"] = validation[i]; inputs[r][i]["State"] = validation[i];
}); });
@ -37,6 +37,9 @@ class _WordleDisplayWidgetState extends State<WordleDisplayWidget> with TickerPr
} }
await Future.delayed(const Duration(seconds: 1)); await Future.delayed(const Duration(seconds: 1));
} }
if(!onAnimation) {
return;
}
mainBus.emit(event: "AnimationStops", args: []); mainBus.emit(event: "AnimationStops", args: []);
onAnimation = false; onAnimation = false;
r++; r++;
@ -212,7 +215,7 @@ class _WordleDisplayWidgetState extends State<WordleDisplayWidget> with TickerPr
c++; c++;
}); });
} }
else { else if(onAnimation){
return true; return true;
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-03-06 15:03:57 * @Date : 2022-03-06 15:03:57
* @LastEditTime : 2022-03-07 11:16:16 * @LastEditTime : 2022-03-07 15:55:48
* @Description : Word generator * @Description : Word generator
*/ */
@ -11,55 +11,69 @@ import 'dart:math';
abstract class Words { abstract class Words {
static Set<String> dataBase = <String>{}; static Set<String> dataBase = <String>{};
static int _length = 0; static Set<String> dictionary = <String>{};
static int _length = 5;
static String _cache = ""; static String _cache = "";
//static Map<String, String> explainations = {}; //static Map<String, String> explainations = {};
static Future<bool> importWordsDatabase({int length = 5}) async { static Future<bool> importWordsDatabase({int length = 5}) async {
//explainations.clear(); //explainations.clear();
if(length != _length || dataBase.isEmpty){ if(length != _length || dataBase.isEmpty || dictionary.isEmpty){
_length = length; _length = length;
dataBase.clear(); dataBase.clear();
_cache = ""; _cache = "";
var data = await rootBundle.loadString('assets/popular.txt'); try {
// LineSplitter.split(data).forEach((line) { var data = await rootBundle.loadString('assets/popular.txt');
// int seperatePos = line.indexOf(','); LineSplitter.split(data).forEach((line) {
// if(seperatePos != length + 2) { if(line.length == length && dataBase.lookup(line.substring(0,length - 1)) == null && dataBase.lookup(line.substring(0, length - 2)) == null) {
// return; dataBase.add(line.toLowerCase());
// } }
// var word = line.substring(1, seperatePos - 1); _cache = line;
// var expl = line.substring(seperatePos + 2, line.length - 1); });
// dataBase.add(word); } catch (e) {
// explainations[word] = expl; throw "Failed loading question database";
// }); }
LineSplitter.split(data).forEach((line) { try {
if(line.length == length && line != _cache) { var data = await rootBundle.loadString('assets/unixWords.txt');
dataBase.add(line.toLowerCase()); LineSplitter.split(data).forEach((line) {
} if(line.length == length && dictionary.lookup(line.substring(0,length - 1)) == null && dictionary.lookup(line.substring(0, length - 2)) == null) {
_cache = line + "s"; dictionary.add(line.toLowerCase());
}); }
_cache = line;
});
} catch (e) {
throw "Failed loading validation database";
}
} }
if(dataBase.isEmpty) { if(dataBase.isEmpty) {
return false; throw "Empty question database";
}
else if(dictionary.isEmpty) {
throw "Empty validation database";
} }
return true; return true;
} }
static Future<String?> generateWord() async{ static Future<String> generateWord() async {
int bound = dataBase.length; int bound = dataBase.length;
if(bound == 0) { if(bound == 0) {
var import = await Words.importWordsDatabase(); try{
if(!import) { await Words.importWordsDatabase(length: _length);
return null; } catch (e) {
} return 'crash';
bound = dataBase.length; }
} bound = dataBase.length;
}
var rng = Random(); var rng = Random();
return dataBase.elementAt(rng.nextInt(bound)); String res = dataBase.elementAt(rng.nextInt(bound));
if(dictionary.lookup(res) == null) {
dictionary.add(res);
}
return res;
} }
static bool isWordValidate(String word) { static bool isWordValidate(String word) {
return dataBase.lookup(word.toLowerCase()) != null; return dictionary.lookup(word.toLowerCase()) != null;
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-03-05 20:21:34 * @Date : 2022-03-05 20:21:34
* @LastEditTime : 2022-03-07 09:20:28 * @LastEditTime : 2022-03-07 14:40:28
* @Description : * @Description :
*/ */
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -59,73 +59,105 @@ class _MyAppState extends State<MyApp> {
} }
} }
class HomePage extends StatelessWidget { class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key); const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool _ignorance = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder( return FutureBuilder(
future: Words.importWordsDatabase(), future: Words.importWordsDatabase(),
builder: (context, snapshot) { builder: (context, snapshot) {
if(snapshot.connectionState == ConnectionState.done) { if(snapshot.connectionState == ConnectionState.done || _ignorance) {
return Scaffold( if(snapshot.hasData || _ignorance){
body: Center( return Scaffold(
child: Padding( body: Center(
padding: const EdgeInsets.all(50.0), child: Padding(
child: AspectRatio( padding: const EdgeInsets.all(50.0),
aspectRatio: 16 / 9, child: AspectRatio(
child: OutlinedButton( aspectRatio: 16 / 9,
child: Padding( child: OutlinedButton(
padding: const EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0), child: Padding(
child: Align( padding: const EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
alignment: Alignment.centerLeft, child: Align(
child: Column( alignment: Alignment.centerLeft,
mainAxisAlignment: MainAxisAlignment.center, child: Column(
crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center,
children: [ crossAxisAlignment: CrossAxisAlignment.start,
Text( children: [
'OFFLINE PLAYGROUND', Text(
style: TextStyle( 'OFFLINE PLAYGROUND',
color: Theme.of(context).brightness == Brightness.dark ? Colors.white : Colors.grey[850]!, style: TextStyle(
fontSize: 20.0, color: Theme.of(context).brightness == Brightness.dark ? Colors.white : Colors.grey[850]!,
fontWeight: FontWeight.bold fontSize: 20.0,
fontWeight: FontWeight.bold
),
), ),
), // Padding(
// Padding( // padding: const EdgeInsets.symmetric(vertical: 8.0),
// padding: const EdgeInsets.symmetric(vertical: 8.0), // child: Container(
// child: Container( // width: 80.0,
// width: 80.0, // height: 8.0,
// height: 8.0, // decoration: BoxDecoration(
// decoration: BoxDecoration( // color: Colors.grey[800],
// color: Colors.grey[800], // borderRadius: BorderRadius.circular(3.5),
// borderRadius: BorderRadius.circular(3.5), // ),
// ), // ),
// ), // ),
// ), Text(
Text( 'Play Wordle game offline using local word database',
'Play Wordle game offline using local word database', style: TextStyle(
style: TextStyle( color: Colors.grey[500],
color: Colors.grey[500], fontSize: 12.0,
fontSize: 12.0, ),
), ),
), ],
], ),
), ),
), ),
), style: ButtonStyle(
style: ButtonStyle( shape: MaterialStateProperty.all<OutlinedBorder?>(RoundedRectangleBorder(borderRadius: BorderRadius.circular(10))),
shape: MaterialStateProperty.all<OutlinedBorder?>(RoundedRectangleBorder(borderRadius: BorderRadius.circular(10))), padding: MaterialStateProperty.all<EdgeInsets?>(const EdgeInsets.all(0)),
padding: MaterialStateProperty.all<EdgeInsets?>(const EdgeInsets.all(0)), ),
), onPressed: () async {
onPressed: () { Navigator.of(context).pushNamed("/Offline");
Navigator.of(context).pushNamed("/Offline"); //Future.delayed(const Duration(milliseconds: 10)).then((e) => mainBus.emit(event: "NewGame", args: []));
mainBus.emit(event: "NewGame", args: []); }
} )
) ),
)
),
);
}
else {
return Container(
color: Colors.black38,
child: AlertDialog(
title: const Text('Failure'),
content: Text(snapshot.error as String),
actions: [
TextButton(
child: const Text('Retry'),
onPressed: () => setState(() {}),
), ),
) TextButton(
), child: const Text('Ignore'),
); onPressed: () {
setState((){
_ignorance = true;
});
},
),
],
),
);
}
} }
else { else {
return Scaffold( return Scaffold(

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-03-05 20:41:41 * @Date : 2022-03-05 20:41:41
* @LastEditTime : 2022-03-07 11:05:14 * @LastEditTime : 2022-03-07 14:40:37
* @Description : Offline page * @Description : Offline page
*/ */

View File

@ -1,12 +1,11 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-03-05 21:40:51 * @Date : 2022-03-05 21:40:51
* @LastEditTime : 2022-03-07 11:15:55 * @LastEditTime : 2022-03-07 14:40:46
* @Description : Validation Provider class * @Description : Validation Provider class
*/ */
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'show_dialogs.dart';
import './event_bus.dart'; import './event_bus.dart';
import './generator.dart'; import './generator.dart';
@ -39,23 +38,11 @@ class _ValidationProviderState extends State<ValidationProvider> {
_newGame(); _newGame();
} }
void _newGame() async{ void _newGame() async {
showLoadingDialog(context: context); curAttempt = "";
var generated = await Words.generateWord(); curAttemptCount = 0;
Navigator.of(context).pop(); acceptInput = true;
while(generated == null) { answer = await Words.generateWord();
var retry = await showFailedDialog(context: context);
if(retry == null) {
Navigator.of(context).popUntil(ModalRoute.withName('/'));
break;
}
else {
showLoadingDialog(context: context);
generated = await Words.generateWord();
Navigator.of(context).pop();
}
}
answer = generated ?? "";
answer = answer.toUpperCase(); answer = answer.toUpperCase();
letterMap = {}; letterMap = {};
answer.split('').forEach((c) { answer.split('').forEach((c) {
@ -63,9 +50,6 @@ class _ValidationProviderState extends State<ValidationProvider> {
letterMap[c] = letterMap[c]! + 1; letterMap[c] = letterMap[c]! + 1;
}); });
letterMap = Map.unmodifiable(letterMap); letterMap = Map.unmodifiable(letterMap);
curAttempt = "";
curAttemptCount = 0;
acceptInput = true;
} }
void _onGameEnd(dynamic args) { void _onGameEnd(dynamic args) {
@ -110,6 +94,7 @@ class _ValidationProviderState extends State<ValidationProvider> {
@override @override
void initState(){ void initState(){
super.initState(); super.initState();
_newGame();
mainBus.onBus(event: "NewGame", onEvent: _onNewGame); mainBus.onBus(event: "NewGame", onEvent: _onNewGame);
mainBus.onBus(event: "Result", onEvent: _onGameEnd); mainBus.onBus(event: "Result", onEvent: _onGameEnd);
} }

View File

@ -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. # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at # Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 0.3.9+0939 version: 0.3.7+1451
environment: environment:
sdk: ">=2.16.1 <3.0.0" sdk: ">=2.16.1 <3.0.0"