import random, sequtils, strformat, strutils proc encode(correct, guess: string): string = result.setlen(correct.len) for i in 0..correct.high: result[i] = if correct[i] == guess[i]: 'X' else: (if guess[i] in correct: 'O' else: '-') result.join(" ") proc safeIntInput(prompt: string; minVal, maxVal: Positive): int = while true: stdout.write prompt let userInput = stdin.readLine() result = try: parseInt(userInput) except ValueError: continue if result in minVal..maxVal: return proc playGame() = echo "You will need to guess a random code." echo "For each guess, you will receive a hint." echo "In this hint, X denotes a correct letter, " & "and O a letter in the original string but in a different position." echo "" let numLetters = safeIntInput("Select a number of possible letters for the code (2-20): ", 2, 20) let codeLength = safeIntInput("Select a length for the code (4-10): ", 4, 10) let letters = "ABCDEFGHIJKLMNOPQRST"[0.. {encode(code, guess)}" for guess in guesses: echo "------------------------------------" echo guess echo "------------------------------------" randomize() playGame()