Skip to content
Snippets Groups Projects
Select Git revision
  • edff7d5fb1be55e62687da82a11809ee32bf0ff1
  • 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.08 KiB
    import '../provider/data.dart';
    import '../utils/random_pick_word.dart';
    
    class GameUtils {
      static Future<void> quitGame(Data myProvider) async {
        myProvider.updateGameIsRunning(false);
      }
    
      static Future<void> startGame(Data myProvider) async {
        print('Starting game (' +
            myProvider.lang +
            ' / ' +
            myProvider.length +
            ' / ' +
            myProvider.level +
            ' )');
    
        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 void addLetter(Data myProvider, String letter) {
        myProvider.currentGuessAddLetter(letter);
      }
    
      static void removeLetter(Data myProvider) {
        myProvider.currentGuessRemoveLetter();
      }
    
      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);
        }
    
        // Check correctly placed letters
        for (int i = 0; i < wordLength; i++) {
          if ((tips[i] == '') && (word[i] == candidate[i])) {
            myProvider.addFoundLetter(word[i], i);
            word = replaceCharAt(word, i, ' ');
            candidate = replaceCharAt(candidate, i, ' ');
            tips[i] = 'good';
          }
        }
    
        // Check misplaced letters
        for (int i = 0; i < wordLength; i++) {
          for (int j = 0; j < wordLength; j++) {
            if ((candidate[j] != ' ') && (candidate[j] == word[i])) {
              word = replaceCharAt(word, i, ' ');
              candidate = replaceCharAt(candidate, j, ' ');
              tips[j] = 'misplaced';
            }
          }
        }
    
        return tips;
      }
    
      static void submitWord(Data myProvider) {
        if (GameUtils.checkCurrentlyGuessedWordExists(myProvider)) {
          print('Ok word allowed: "' + myProvider.currentGuess + '".');
          myProvider.currentGuessSubmitWord();
        } else {
          print('Unknown word: "' + myProvider.currentGuess + '".');
          myProvider.currentGuessSubmitWrongWord();
        }
      }
    
      static bool checkCurrentlyGuessedWordExists(Data myProvider) {
        return RandomPickWord.checkWordExists(myProvider.currentGuess);
      }
    }