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];
}