Skip to content
Snippets Groups Projects
Select Git revision
  • 698d376a87adedfc6e44ad590c228ab4bf15a04b
  • master default protected
  • 31-upgrade-framework-and-dependencies
  • 12-improve-ai
  • 14-improve-app-metadata
  • Release_0.8.0_25 protected
  • Release_0.7.2_24 protected
  • Release_0.7.1_23 protected
  • Release_0.7.0_22 protected
  • Release_0.6.0_21 protected
  • Release_0.5.0_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.2_18 protected
  • Release_0.3.1_17 protected
  • Release_0.3.0_16 protected
  • Release_0.2.1_15 protected
  • Release_0.2.0_14 protected
  • Release_0.1.1_13 protected
  • Release_0.1.0_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.9_9 protected
  • Release_0.0.8_8 protected
  • Release_0.0.7_7 protected
  • Release_0.0.6_6 protected
25 results

game_cubit.dart

Blame
  • random_pick_word.dart 2.35 KiB
    import 'dart:async';
    import 'dart:convert';
    import "dart:math";
    
    import 'package:flutter/services.dart';
    
    class RandomPickWord {
      static Set<String> wordList = <String>{};
      static Set<String> dictionary = <String>{};
    
      static String _lang = '';
      static int _length = 5;
      static String _level = '';
    
      RandomPickWord();
    
      String _word = '';
      String get word => _word;
    
      init(String lang, int length, String level) async {
        if (lang != _lang || length != _length || level != _level || wordList.isEmpty || dictionary.isEmpty) {
          _lang = lang;
          _length = length;
          _level = level;
    
          dictionary.clear();
          wordList.clear();
    
          String wordBaseFilename = 'words-' + length.toString() + '-' + lang;
    
          // Get full dictionary (eligible words)
          print('Reload dictionary / ' + lang + ' / ' + length.toString());
          try {
            String wordsFile = wordBaseFilename + '-' + 'dictionary';
            var data = await rootBundle.loadString('assets/files/' + wordsFile + '.txt');
            LineSplitter.split(data).forEach((line) {
              if (line.length == length) {
                dictionary.add(line.toUpperCase());
              }
            });
          } catch (e) {
            throw "Failed loading dictionary database";
          }
    
          // Get guessable words list (will pick random word from)
          print('Reload words list  / ' + lang + ' / ' + length.toString() + ' / ' + level);
          try {
            String wordsFile = wordBaseFilename + '-' + level;
            var data = await rootBundle.loadString('assets/files/' + wordsFile + '.txt');
            LineSplitter.split(data).forEach((line) {
              if (line.length == length) {
                wordList.add(line.toUpperCase());
              }
            });
          } catch (e) {
            throw "Failed loading words database";
          }
        }
    
        print('Words in dictionary: ' + dictionary.length.toString());
        print('Words in words list: ' + wordList.length.toString());
    
        _word = '';
        await wordFromLocalFile();
      }
    
      Future<void> wordFromLocalFile() async {
        // Check we have enough words
        if (wordList.length < 1) {
          print('Not enough words in list.');