Skip to content
Snippets Groups Projects
Select Git revision
  • 077ebbc6a04b07134320e8524e7cb30ddf22433e
  • master default protected
  • 58-upgrade-framework-and-dependencies
  • 49-fix-end-game
  • 40-improve-app-metadata
  • 12-improve-layout
  • 4-add-animations
  • Release_0.9.0_46 protected
  • Release_0.8.2_45 protected
  • Release_0.8.1_44 protected
  • Release_0.8.0_43 protected
  • Release_0.7.0_42 protected
  • Release_0.6.0_41 protected
  • Release_0.5.0_40 protected
  • Release_0.4.2_39 protected
  • Release_0.4.1_38 protected
  • Release_0.4.0_37 protected
  • Release_0.3.1_36 protected
  • Release_0.3.0_35 protected
  • Release_0.2.1_34 protected
  • Release_0.2.0_33 protected
  • Release_0.1.2_32 protected
  • Release_0.1.1_31 protected
  • Release_0.1.0_30 protected
  • Release_0.0.29_29 protected
  • Release_0.0.28_28 protected
  • Release_0.0.27_27 protected
27 results

game_utils.dart

Blame
  • game_utils.dart 3.64 KiB
    import '../provider/data.dart';
    import '../utils/random_pick_word.dart';
    
    class GameUtils {
      static Future<void> resetGame(Data myProvider) async {
        myProvider.updateGameIsRunning(false);
      }
    
      static Future<void> startGame(Data myProvider) async {
        print('Starting game');
        print('- lang: ' + myProvider.lang);
        print('- length: ' + myProvider.length);
    
        myProvider.resetGame();
    
        await pickWord(myProvider);
    
        myProvider.updateGameIsRunning(true);
      }
    
      static Future<void> pickWord(Data myProvider) async {
        RandomPickWord randomPickWord;
        String word = '';
    
        int attempts = 0;
        do {
          randomPickWord = RandomPickWord();
          int wordLength = int.parse(myProvider.length);
          await randomPickWord.init(myProvider.lang, wordLength, myProvider.level);
    
          if (randomPickWord.word != '') {
            word = randomPickWord.word;
          }
          attempts++;
    
          if ((word != '') && !myProvider.isRecentlyPicked(word)) {
            myProvider.updateWord(word);
          }
        } while (myProvider.word != word && attempts < 10);
    
        print('Picked word: ' + word);
      }
    
      static bool addLetter(Data myProvider, String letter) {
        print('addLetter: ' + letter);
        myProvider.currentGuessAddLetter(letter);
        return true;
      }
    
      static bool removeLetter(Data myProvider) {
        print('removeLetter');
        myProvider.currentGuessRemoveLetter();
        return true;
      }
    
      static List<String> getTips(Data myProvider, String candidate) {
        String word = myProvider.word;
        int wordLength = int.parse(myProvider.length);
    
        List<String> tips = List<String>.filled(wordLength, '', growable: false);
    
        if ((word.length != wordLength) ||
            (candidate.length != wordLength) ||
            (!RandomPickWord.checkWordExists(candidate))) {
          return List<String>.filled(wordLength, 'wrong', growable: false);
        }
    
        String replaceCharAt(String oldString, int index, String newChar) {
          return oldString.substring(0, index) + newChar + oldString.substring(index + 1);
        }
    
        print('getTips: candidate "' + candidate + '" / word "' + word + '"');
    
        // Check correctly placed letters
        print('Check correctly placed letters');
        for (int i = 0; i < wordLength; i++) {
          if ((tips[i] == '') && (word[i] == candidate[i])) {
            print('Found "' + word[i] + '" on the right place: ' + (i + 1).toString());
            word = replaceCharAt(word, i, ' ');
            candidate = replaceCharAt(candidate, i, ' ');
            tips[i] = 'good';
          }
        }
    
        // Check misplaced letters
        print('Check misplaced letters');
        for (int i = 0; i < wordLength; i++) {
          for (int j = 0; j < wordLength; j++) {
            if ((candidate[j] != ' ') && (candidate[j] == word[i])) {
              print('Found "' +
                  candidate[j] +
                  '" on the wrong place: ' +
                  (j + 1).toString() +
                  ' instead of ' +
                  (i + 1).toString());
              word = replaceCharAt(word, i, ' ');
              candidate = replaceCharAt(candidate, j, ' ');
              tips[j] = 'misplaced';
            }
          }
        }
    
        print('Finished check letters: ' + tips.toString());
    
        return tips;
      }
    
      static bool submitWord(Data myProvider) {
        print('submitWord');
    
        if (GameUtils.checkCurrentlyGuessedWordExists(myProvider)) {
          print('Ok word allowed');
          myProvider.currentGuessSubmitWord();
        } else {
          print('Unknown word');
          myProvider.currentGuessSubmitWrongWord();
        }
    
        return true;
      }
    
      static bool checkCurrentlyGuessedWordExists(Data myProvider) {
        String guessedWord = myProvider.currentGuess;
        print('Checking word "' + guessedWord + '"...');
    
        return RandomPickWord.checkWordExists(guessedWord);
      }
    }