Create a Guess Game

This is the place for ALL of the user submitted challenges. If you create a little challenge/mission/riddle/whatever, post it here.
Forum rules
Do not post missions that you did NOT create without proper citing.

Create a Guess Game

Post by -Ninjex- on Sat Aug 10, 2013 8:26 pm
([msg=76804]see Create a Guess Game[/msg])

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}"
Last edited by -Ninjex- on Sun Aug 11, 2013 6:34 pm, edited 1 time in total.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by centip3de on Sun Aug 11, 2013 6:21 pm
([msg=76813]see Re: Create a Guess Game[/msg])

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);
   }
}
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Addict
Addict
 
Posts: 1479
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by apples on Sun Aug 11, 2013 6:28 pm
([msg=76814]see Re: Create a Guess Game[/msg])

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
User avatar
apples
New User
New User
 
Posts: 37
Joined: Sat Apr 12, 2008 8:30 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by -Ninjex- on Sun Aug 11, 2013 6:31 pm
([msg=76815]see Re: Create a Guess Game[/msg])

@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.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by pretentious on Mon Aug 12, 2013 4:21 am
([msg=76820]see Re: Create a Guess Game[/msg])

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

Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Create a Guess Game

Post by -Ninjex- on Tue Aug 13, 2013 3:19 pm
([msg=76834]see Re: Create a Guess Game[/msg])

@pretentious +50, the code checks out again. Good work!
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by PinkFloyd22 on Thu Aug 15, 2013 12:17 pm
([msg=76854]see Re: Create a Guess Game[/msg])

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
     
^_^
User avatar
PinkFloyd22
Experienced User
Experienced User
 
Posts: 70
Joined: Sat Sep 26, 2009 4:35 am
Blog: View Blog (0)


Re: Create a Guess Game

Post by -Ninjex- on Thu Aug 15, 2013 3:08 pm
([msg=76856]see Re: Create a Guess Game[/msg])

@pinkfloyd22, great work, it's nice to see some new people participating as well. +50
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by Assassian360 on Thu Aug 15, 2013 10:30 pm
([msg=76862]see Re: Create a Guess Game[/msg])

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;
}
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Create a Guess Game

Post by -Ninjex- on Thu Aug 15, 2013 10:42 pm
([msg=76864]see Re: Create a Guess Game[/msg])

@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
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Next

Return to User Submitted

Who is online

Users browsing this forum: No registered users and 0 guests