Skip to content

Instantly share code, notes, and snippets.

@wemgl
Last active April 14, 2022 00:42
Show Gist options
  • Save wemgl/ae317cf86afb5ae634f5834123aea12c to your computer and use it in GitHub Desktop.
Save wemgl/ae317cf86afb5ae634f5834123aea12c to your computer and use it in GitHub Desktop.
Hangman Game
String updateGuessState(String correctWord, String guessState, String guess) {
// Update the guess state to account for the latest guessed letter.
var newGuessState = '';
for (var i = 0; i < correctWord.length; i++) {
if (correctWord[i] == guess) {
newGuessState += guess;
} else {
newGuessState += guessState[i];
}
}
return newGuessState;
}
void main() {
const correctWord = 'Hello';
var guessState = correctWord.replaceAll(RegExp(r'\w'), '_');
guessState = updateGuessState(correctWord, guessState, 'H');
print(guessState);
guessState = updateGuessState(correctWord, guessState, 'l');
print(guessState);
guessState = updateGuessState(correctWord, guessState, 'e');
print(guessState);
guessState = updateGuessState(correctWord, guessState, 'o');
print(guessState);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment