Guessing Number
Hellloo! Z. is here, and I'm really interested in numbers because they are so pretty. This is the main reason that I want to make this game. Basically, you just need to guess the number and provide the range of the numbers that you want to guess. I hope you will enjoy it 😊💙💐
Note: There is some previous existing code related to this, and here's the reference for my code that I wrote, but of course, I made my code become more precious and more cool than the other one:) So try to play it! HAHAHAHA! 😁
(defn read-int
"Tries to read a number from a string. Returns nil if fails"
[num]
(try
(js/parseInt num)
(catch js/Error e nil)))
(defn want-a-number[]
(js/prompt "What is the maximum number that you want to guess it?\nenter the maximum integer"))
(defn rand-num
"Randomly generates a number by the user."
[]
(rand-int (read-int (want-a-number))))
(defn run
"A guessing game where the user tries to guess a number between 1 and the maximum number that the user types."
[]
(println "Hello and welcome to guessing number game!\nYou must enter the largest positive integer that you can think of to use as a guess for the given number.")
(loop [ans (rand-num)
guess (js/prompt "Let's start it!\nGood luck!\nWhat is your guess?")]
(if-let [n (read-int guess)]
(if (= n ans)
(let [again? (js/prompt "You got it! Would you like to play again (y/n)?")]
(if (= again? "y")
(recur (rand-num) (js/prompt "what is your guess?"))
(println "Bye-byeee! hope you will come back later :)!")))
(recur ans (js/prompt (if (> n ans)
"Your hunch is too far off lol.\nMake another guess:"
"Your estimate is too low!\nAdd one more hunch:")))))))