Last active
April 14, 2022 00:42
-
-
Save wemgl/ae317cf86afb5ae634f5834123aea12c to your computer and use it in GitHub Desktop.
Hangman Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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