2

The program I created compares the letters of the target word and the guessed word. An example: the target word is "compulsory" and the guessed word is "submission". The output should be "-O-O--X-O-".

"X" meaning the letter in the guessed word is also in the target word and is at the same spot. "O" meaning the letter in the guessed word is also in the target word but isn't at the same spot. "-" means that it's the wrong letter.

But I can't seem to find a way to cope with duplicate letters. For example, if the target word is "teethe" and the guessed word is "health" it should give me the output: "OX--O-". Though my program returns the output: "OX--OO".

Test Solution:

  Word testSolution = Word(letters: <Letter>[
    Letter(val: '0'),
    Letter(val: '0'),
    Letter(val: '0'),
    Letter(val: '0'),
    Letter(val: '0'),
  ]);

Future _onEnterTapped code:

if (_gameStatus == GameStatus.playing &&
            _currentWord != null &&
            !_currentWord!.letters.contains(Letter.empty())) {
          _gameStatus = GameStatus.submitting;

          for (var i = 0; i < _currentWord!.letters.length; i++) {
            testSolution.letters[i] = _solution.letters[i];
          }

          //--------------------------------------------------------------------

          for (var i = 0; i < _currentWord!.letters.length; i++) {
            final currentWordLetter = _currentWord!.letters[i];
            final currentSolutionLetter = testSolution.letters[i];

            setState(() {
              if (currentWordLetter == currentSolutionLetter) {
                _currentWord!.letters[i] =
                    currentWordLetter.copyWith(status: LetterStatus.correct);
                testSolution.letters[i] = Letter(val: '1');
                //_currentWord!.letters[i] = Letter(val: '0');
                final letter = _keyboardLetters.firstWhere(
                      (e) => e.val == currentWordLetter.val,
                  orElse: () => Letter.empty(),
                );
                if (letter.status != LetterStatus.correct) {
                  _keyboardLetters
                      .removeWhere((e) => e.val == currentWordLetter.val);
                  _keyboardLetters.add(_currentWord!.letters[i]);
                }
              }
            });
          }

          //--------------------------------------------------------
          for (var i = 0; i < _currentWord!.letters.length; i++) {
            final currentWordLetter = _currentWord!.letters[i];
            final currentSolutionLetter = testSolution.letters[i];

            setState(() {
              if (testSolution.letters.contains(currentWordLetter) && testSolution.letters[i] != Letter(val: '1')) {
                _currentWord!.letters[i] =
                    currentWordLetter.copyWith(status: LetterStatus.inWord);
                testSolution.letters[i] = Letter(val: '1');
                final letter = _keyboardLetters.firstWhere(
                      (e) => e.val == currentWordLetter.val,
                  orElse: () => Letter.empty(),
                );
                if (letter.status != LetterStatus.correct) {
                  _keyboardLetters
                      .removeWhere((e) => e.val == currentWordLetter.val);
                  _keyboardLetters.add(_currentWord!.letters[i]);
                }
              }
            });
          }

          //--------------------------------------------------------
          for (var i = 0; i < _currentWord!.letters.length; i++) {
            final currentWordLetter = _currentWord!.letters[i];
            final currentSolutionLetter = _solution.letters[i];

            setState(() {
              if (_solution.letters.contains(currentWordLetter)) {
                _currentWord!.letters[i] =
                    currentWordLetter.copyWith(status: LetterStatus.inWord);
                testSolution.letters[i] = Letter(val: '1');
                final letter = _keyboardLetters.firstWhere(
                      (e) => e.val == currentWordLetter.val,
                  orElse: () => Letter.empty(),
                );
                if (letter.status != LetterStatus.correct) {
                  _keyboardLetters
                      .removeWhere((e) => e.val == currentWordLetter.val);
                  _keyboardLetters.add(_currentWord!.letters[i]);
                }
              }
            });
          }

          //--------------------------------------------------------

          for(var i = 0; i < _currentWord!.letters.length; i++) {
            await Future.delayed(
            const Duration(milliseconds: 150),
                () => _flipCardKeys[_currentWordIndex][i]
                .currentState
                ?.toggleCard(),
          );
          }
          _checkIfWinOrLoss();
        }

Letter Model:

import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:wordle/core/constants/constants.dart';

enum LetterStatus { initial, notInWord, inWord, correct }

class Letter extends Equatable {
  Letter({
    required this.val,
    this.status = LetterStatus.initial,
  });

  String val;
  LetterStatus status;

  factory Letter.empty() => Letter(val: '');

  Color get backgroundColor {
    switch (status) {
      case LetterStatus.initial:
        return Colors.transparent;
      case LetterStatus.notInWord:
        return notInWordColor;
      // case LetterStatus.inWord:
      //   return inWordColor;
      case LetterStatus.inWord:
        return inWordColor;
      case LetterStatus.correct:
        return correctColor;
    }
  }

  Color get borderColor {
    switch (status) {
      case LetterStatus.initial:
        return Colors.grey;
      default:
        return Colors.transparent;
    }
  }

  Letter copyWith({
    String? val,
    LetterStatus? status,
  }) {
    return Letter(
      val: val ?? this.val,
      status: status ?? this.status,
    );
  }

  @override
  // TODO: implement props
  List<Object?> get props => [val, status];
}

Word Model:

import 'package:equatable/equatable.dart';
import 'letter_model.dart';

class Word extends Equatable {
  const Word({required this.letters});

  factory Word.fromString(String word) =>
      Word(letters: word.split('').map((e) => Letter(val: e)).toList());

  final List<Letter> letters;
  String get wordString => letters.map((e) => e.val).join();

  void addLetter(String val) {
    final currentIndex = letters.indexWhere((e) => e.val.isEmpty);
    if (currentIndex != -1) {
      letters[currentIndex] = Letter(val: val);
    }
  }

  void removeLetter() {
    final recentLetterIndex = letters.lastIndexWhere((e) => e.val.isNotEmpty);
    if (recentLetterIndex != -1) {
      letters[recentLetterIndex] = Letter.empty();
    }
  }

  @override
  List<Object?> get props => [letters];
}

umutcan bağdu
  • 158
  • 1
  • 1
  • 9
  • 1
    I haven't looked through your code, but one way is: 1. Iterate over the letters in the guess and find exact matches (same letter, same position). 2. Iterate over the letters in the guess and find inexact matches (same letter, wrong position). In both cases, when you find a match, mark/remove/replace the corresponding letter in the solution to prevent it from being matched again. – jamesdlin Apr 11 '22 at 17:31
  • I can think of the solution but I can't code the algorithm :/ – umutcan bağdu Apr 11 '22 at 18:58
  • I can upload the whole project if you want to help. – umutcan bağdu Apr 11 '22 at 18:59
  • 1
    Well, it sounds like you already have code that identify exact and inexact matches, and you've already split the words into `List`s, so an easy fix should be to replace the matched element with something else (`null`, a non-alphabetic character, etc.). – jamesdlin Apr 11 '22 at 20:15
  • I did what you wrote but the problem is not solved. I updated the code. – umutcan bağdu Apr 11 '22 at 21:33
  • I created a worlde cli app that you are more than welcome to take a look at if it helps in any way https://github.com/Luckey-Elijah/clordle – Apealed Apr 12 '22 at 00:58

0 Answers0