The Challenge:

Level: Beginner

Points: 50

Create a program that will generate a random number 1-15.

The user must have a limited amount of guesses, (you can decide this number)

The user then has to take a guess at what the number is.

If the user is wrong, the program should tell them if they are hot, cold, or warm, depending on how close they are.

If the user guessed the correct answer, let them know.

If the user runs out of guesses, tell them it's game over with the correct answer.

The program should tell the user how many guesses are remaining after each guess.

Hot should be a number two digits away in any direction from the answer or closer:

If the number is "7", 5, 6, 8, and 9 will produce hot

Warm should be any number over 2 spaces away in any direction, but less than 5 digits away in that direction

If the number is "7", 3, 4, 10, and 11 produce warm.

Cold should be any number that isn't the answer, warm, or hot.

--------------

Not challenging enough? Get 25 more points!

Add a feature in the game that allows the user to choose a difficulty value of 1-100, where each difficulty level increases the amount of numbers to guess from by 5. The program should also increase the hot, warm, and cold ranges.

Credit goes to centip3de for helping with this linear algorithm!

Warm = 4 + ((difficulty - 1) * 2), level 1 range = 4, level 2 range = 6, level 3 range = 8, level 4 range = 10, etc.

Hot = 2 + ((difficulty -1) * 1), level 1 range = 2, level 2 range = 3, level 3 range = 4, level 4 range = 5, etc. (in short this is warm divided by 2)

Cold = All numbers that are not the answer or hot/warm.

Chuck Norris wrote:Guesses Remaining: 3

Guess: 3

Your guess is cold.

Guesses Remaining: 2

Guess: 7

Your guess is hot.

Guesses Remaining: 1

Guess: 6

Game over, the correct answer is: 8

My submission:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Language: Ruby

- Code: Select all
`#!/usr/bin/ruby`

guess_amount = 3;

random = rand(1...15);

while guess_amount > 0 do

puts "Guesses left: #{guess_amount}"

print "Guess: "

guess = gets.chomp.to_i

if guess == random-1 or guess == random-2 or guess == random+1 or guess == random+2 then puts "Your guess is hot!"

elsif guess == random-3 or guess == random-4 or guess == random+3 or guess == random+4 then puts "Your guess is warm."

elsif guess == random then abort("Correct answer, thank you for playing!")

else puts "Your guess is cold."

end

guess_amount -= 1;

end

puts "Game over, the answer is: #{random}"