Page 1 of 5

Create a Guess Game

PostPosted: Sat Aug 10, 2013 8:26 pm
by -Ninjex-
This is part of the continuous programming challenges!

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}"

Re: Create a Guess Game

PostPosted: Sun Aug 11, 2013 6:21 pm
by centip3de
Solution uno:

Scala:
Code: Select all
package org;

import scala.util.Random;

object Main
{
   def main(args:Array[String])
   {
     print("Difficulty level: ");
     val difficulty = Console.readInt;
     val range = 15 + ((difficulty - 1) * 5);
     val num = Random.nextInt(range);
     val hotLow = 2 + ((difficulty -1) * 1);
     val hotHigh = 1;
     val warmLow = 4 + ((difficulty - 1) * 2);
     val warmHigh = hotLow - 1;
     var guessNum = 10;
     print("Guess a number between 0 and " + range + ": ");
     var guess = Console.readInt;
    
     while(num != guess)
     {
        if(guessNum == 0)
        {
          println("Ooh, you were too slow. The answer is: " + num);
          System.exit(0);
        }
        else if((num - guess <= hotLow && num - guess >= hotHigh) ||
          (guess - num <= hotLow && guess - num >= hotHigh))
         {
         println("Hot! Hot! Hot!");
         }
         else if((num - guess <= warmLow && num - guess > warmHigh) ||
             (guess - num <= warmLow && guess - num > warmHigh))
         {
            println("Warmer...")
         }
         else
         {
           println("Cold.")
         }
       print("Guesses left (" + guessNum + "): ");
       guess = Console.readInt;
       guessNum -= 1;
     }
     println("Ding, ding, ding! We have our winner!");
     System.exit(0);
   }
}

Re: Create a Guess Game

PostPosted: Sun Aug 11, 2013 6:28 pm
by apples
Code: Select all
import System.IO
import System.Random

data Temp = Exact | Hot | Warm | Cold deriving (Show, Eq)

temp :: Int -> Temp
temp dist | dist == 0 = Exact
          | dist <= 2 = Hot
          | dist <= 4 = Warm
          | otherwise = Cold

guess :: Int -> Int -> IO ()
guess 0 ans = putStrLn $ "Game over, the answer is: " ++ show ans
guess i ans = do
  putStrLn $ "Guesses left: " ++ show i
  putStr "Guess: "

  line <- getLine
  let dist = temp . abs . (ans -) . read $ line
  if dist == Exact
    then putStrLn "Correct answer, thank you for playing!"
    else do
      putStrLn $ "Your guess is " ++ show dist
      guess (i - 1) ans

main = do
  hSetBuffering stdout NoBuffering
  randomRIO (1, 15) >>= guess 3

Re: Create a Guess Game

PostPosted: Sun Aug 11, 2013 6:31 pm
by -Ninjex-
@centip3de, 75 points for you. Great work here again.
@apples, you should add the language and modules to your post. The code checked out though: +50 for you.

Re: Create a Guess Game

PostPosted: Mon Aug 12, 2013 4:21 am
by pretentious
programming finesse is overrated :P
Used C and stole the random initialising code from http://www.cplusplus.com/reference/cstdlib/rand/
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
srand (time(NULL));
int guesses = 7;
int target = rand()%15;
int guess;
int guessed = 0;
for(;guesses>0;guesses--){
printf("%d guesses remaining\nguess: ", guesses);
scanf("%d", &guess);
if(guess == target){
   printf("you got it!\n");
   guessed = 1;
   break;
}
else if(guess == target+1)
   printf("hot\n");
else if(guess == target+2)
   printf("hot\n");
else if(guess == target-1)
   printf("hot\n");
else if(guess == target-2)
   printf("hot\n");
else if(guess == target+3)
   printf("warm\n");
else if(guess == target+4)
   printf("warm\n");
else if(guess == target-3)
   printf("warm\n");
else if(guess == target-4)
   printf("warm\n");
else
   printf("cold\n");
}
if (guessed == 0)
   printf("the number was %d\n", target);
return 0;
}


Re: Create a Guess Game

PostPosted: Tue Aug 13, 2013 3:19 pm
by -Ninjex-
@pretentious +50, the code checks out again. Good work!

Re: Create a Guess Game

PostPosted: Thu Aug 15, 2013 12:17 pm
by PinkFloyd22
Nice ideas you got.This is just what I needed to program a bit after a 2 week break :D.
Here is my noobish code in python
Code: Select all
from random import randint
from sys import exit
random_num = randint(1,15)
guesses = 4
hotp1 = random_num + 1
hotp2 = random_num + 2
hotm1 = random_num - 1
hotm2 = random_num - 2
warmp3 = random_num + 3
warmp4 = random_num + 4
warmm3 = random_num - 3
warmm4 = random_num - 4
while guesses > 0:
     print "Guesses remaining: %d" % guesses
     guess = raw_input("> ")
     if int(guess) == hotp1 or int(guess) == hotp2 or int(guess) == hotm1 or int(guess) == hotm2:
         print "Your guess is hot"
         guesses -= 1
     elif int(guess) == warmp3 or int(guess) == warmm3 or int(guess) == warmp4 or int(guess) == warmm4:
         print "Your guess is  warm"
         guesses -= 1
     elif int(guess) == random_num:
         print "Congratulations you have guessed right.You win!"
         exit()
     else:
         print "Your guess is cold"
         guesses -= 1
if guesses == 0:
     print "Game over.The number was: %d" % random_num
     

Re: Create a Guess Game

PostPosted: Thu Aug 15, 2013 3:08 pm
by -Ninjex-
@pinkfloyd22, great work, it's nice to see some new people participating as well. +50

Re: Create a Guess Game

PostPosted: Thu Aug 15, 2013 10:30 pm
by Assassian360
Here's a C++ version:

Code: Select all
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;

int main()
{
   srand(time(NULL));
   int difficulty;
   cout << "Enter difficulty: ";
   cin >> difficulty;
   int solution = rand() % (15 + difficulty * 5) + 1;
   int guessRemaining = 5 + (int)(difficulty * 0.75);
   cout << "Guess numbers between 1 and " << (15 + difficulty * 5) << endl;
   while(guessRemaining) {
      cout << "You have " << guessRemaining
         << " guesses remaining.\n";
      cout << "Enter your guess: ";
      int guess;
      cin >> guess;
      if(guess == solution) {
         cout << "Congratulations, that was correct!\n";
         break;
      } else if(abs(guess - solution) < 2 + (difficulty-1)) {
         cout << "HOT! You are really close!\n";
      } else if(abs(guess - solution) < 4 + 2 * (difficulty-1)) {
         cout << "Kind of warm, but a bit far off.\n";
      } else {
         cout << "Kind of cold, try to find something warmer.\n";
      }
      guessRemaining--;
   }

   if(!guessRemaining)
      cout << "Sorry, you used up all your attempts.\n";
   cin >> difficulty;
   return 0;
}

Re: Create a Guess Game

PostPosted: Thu Aug 15, 2013 10:42 pm
by -Ninjex-
@assassian360, I am getting compilation errors here:

Code: Select all
>> g++ guess.cpp
guess.cpp: In function ‘int main()’:
guess.cpp:8:24: error: ‘srand’ was not declared in this scope
guess.cpp:12:28: error: ‘rand’ was not declared in this scope
guess.cpp:24:41: error: ‘abs’ was not declared in this scope