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.

Re: Create a Guess Game

Post by DrRoach on Sun Oct 20, 2013 2:07 pm
([msg=77774]see Re: Create a Guess Game[/msg])

Here's my attempt in java :

Code: Select all
import java.util.*;

class guessGame {
   public static void main(String[] args){
      int guesses=3;
      int winner=0;
      int warm=4;
      int hot=2;
      int number=(int)(Math.random()*15)+1;
      System.out.println("You have 3 guesses, good luck !");
      Scanner sc=new Scanner(System.in);
      while(guesses>0&&winner==0){
      int guess=sc.nextInt();
         if(guess==number){
            System.out.println("You Got It !!!");
            winner++;
         } else if((number-2)<guess&&(number+2)>guess){
            System.out.println("Your guess is hot");
         } else if((number-4)<guess&&(number+4)>guess) {
            System.out.println("Your guess is warm");
         } else {
            System.out.println("Your guess is cold");
         }
         guesses--;
         if(winner==0){
            System.out.println("Guesses left : "+guesses);
         }
      }
      if(winner==0){
         System.out.println("The correct answer was "+number);
      }
   }
}
DrRoach
Poster
Poster
 
Posts: 155
Joined: Fri Feb 22, 2013 6:53 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by theincrediblehacker on Sun Sep 28, 2014 1:12 pm
([msg=83854]see Re: Create a Guess Game[/msg])

Here's my attempt in Rexx:
Code: Select all
say "How Difficult?"
parse pull input
   a=input
   h=2+(((a-1)*1)/2)
   w=2*h
   intrange=(a*5)+10
   ctr=0
   c=0
   b=random(1,intrange)
/*checked up to here*/
   do forever
      ctr=ctr+1
      if ctr<4 then do; say "Guess "ctr"/3"
         parse pull input
            a=input
            if a=b then do; say "Correct!"; exit; end
            if a<=b+h & a>=b-h then do; say "Hot"; c=1; end
            if a<=b+h & a>=b-h then iterate
            if a>=b-w & a<=b+w & c=0 then say "Warm"
            if a>=b-w & a<=b+w & c=0 then iterate
            say "cold"
            end
         end
say "The Number was" b". You Lose"
exit
theincrediblehacker
New User
New User
 
Posts: 1
Joined: Sun Sep 28, 2014 1:03 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by neolithicLoser on Fri Oct 31, 2014 4:07 pm
([msg=84705]see Re: Create a Guess Game[/msg])

Here's my try in C

Code: Select all
# include <stdio.h>
# include <time.h>
# include <math.h>

int main (void)
{
   int secret = 0, guess = 3 ;
   int x = 0 ;

   srand (time(NULL)) ;

   secret = rand () % 15 + 1 ;

   printf ("\n%d", secret) ;

   while (guess > 0)
   {
      printf ("\nGuesses remaining : %d", guess) ;
      printf ("\nEnter guess : ") ;
      scanf ("%d", &x) ;
      
      if (x == secret)
      {
         printf ("\nYour guess is correct\n") ;
         break ;
      }
      else if (abs (secret - x) <= 2 )
         printf ("\nHot") ;

      else if (abs (secret - x) < 5 && abs (secret - x) > 2 )
         printf ("\nWarm") ;
      else
         printf ("\nCold") ;

      guess-- ;   
   }
   if (guess == 0 )
      printf ("\nSorry man, you are too slow\n") ;

   return 0 ;
}


I coded the normal version, not the extended version. :)
neolithicLoser
New User
New User
 
Posts: 11
Joined: Sun Jul 14, 2013 1:02 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by WallShadow on Fri Oct 31, 2014 5:23 pm
([msg=84707]see Re: Create a Guess Game[/msg])

I'm bored.

Code: Select all
use feature 'say';
print "please enter a difficulty: " and my $difficulty = int <STDIN>;
die "invalid difficulty" if $difficulty < 1 or $difficulty > 100;
my $n = 1 + int rand 10 + 5 * $difficulty;
my $guesses_left = 5 + $difficulty;
while (print "please enter a guess: " and $_ = <STDIN>) {
   if ( $n == $_ ) { say "you got it!"; exit }
   elsif ( abs ($n - $_) < 1 + $difficulty ) { say "you're hot!" }
   elsif ( abs ($n - $_) < 2 + $difficulty * 2 ) { say "you're warm" }
   else { say "you're cold :(" }
   say "sorry, you ran out of guesses! correct answer was $n" and last if --$guesses_left == 0;
   say "you have $guesses_left guesses left"
}

perl code obviously. invoke with perl because there's no shebang.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 686
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by -Ninjex- on Sun Nov 02, 2014 6:55 am
([msg=84731]see Re: Create a Guess Game[/msg])

WallShadow wrote:I'm bored.

Code: Select all
use feature 'say';
print "please enter a difficulty: " and my $difficulty = int <STDIN>;
die "invalid difficulty" if $difficulty < 1 or $difficulty > 100;
my $n = 1 + int rand 10 + 5 * $difficulty;
my $guesses_left = 5 + $difficulty;
while (print "please enter a guess: " and $_ = <STDIN>) {
   if ( $n == $_ ) { say "you got it!"; exit }
   elsif ( abs ($n - $_) < 1 + $difficulty ) { say "you're hot!" }
   elsif ( abs ($n - $_) < 2 + $difficulty * 2 ) { say "you're warm" }
   else { say "you're cold :(" }
   say "sorry, you ran out of guesses! correct answer was $n" and last if --$guesses_left == 0;
   say "you have $guesses_left guesses left"
}

perl code obviously. invoke with perl because there's no shebang.


+75 points.
I'm thinking about using my server as the new scoreboard, and making some more of these challenges.
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 TheTechBrony on Thu Nov 20, 2014 1:52 am
([msg=85204]see Re: Create a Guess Game[/msg])

I wrote mine in Java:

Code: Select all
import java.util.Random; // Gets the random number utility.
import java.util.Scanner; // Gets the user input utility.

public class GuessingGame {
   public static void main(String[] args) { // PROGRAM STARTUP!
      Scanner input = new Scanner(System.in); // This Scanner is what allows the user to make their guesses.
      Random randomGenerator = new Random(); // This Random creates generator for the correct solution.
      for (int x = 1; x < 2;) { // This just initiates the game. I don't know why it's necessary.
         int correctNumber = randomGenerator.nextInt(15); // This int creates the solution.
         int guessesLeft = 4; // This is the number of guesses you have left. If I set it to 5, it will give you 6. That won't do, will it, now?
         System.out.println("Try to guess the right number in " + (guessesLeft + 1) + " guesses or less.\nEnter a number:"); // Starting prompt. Enter your number!
         int userInput = input.nextInt(); // This just waits for the user's input. if this weren't here, the game wouldn't register user input, and it'd be bloody useless.
         while (userInput != correctNumber && guessesLeft >= 1) { // This begins the loop that tells you you have a bad answer.
            String howClose = null; // String that tells you how close you are to the correct number.
            if(Math.abs(correctNumber - userInput) < 15 && Math.abs(correctNumber - userInput) > 12) { // These if statements initialize the howClose String based on how close to the number you are.
               howClose = "cold.";
            } else if (Math.abs(correctNumber - userInput) <= 12 && Math.abs(correctNumber - userInput) > 8) {
               howClose = "cool.";
            } else if (Math.abs(correctNumber - userInput) <= 8 && Math.abs(correctNumber - userInput) > 4) {
               howClose = "warm.";
            } else if (Math.abs(correctNumber - userInput) <= 4 && Math.abs(correctNumber - userInput) >= 1) {
               howClose = "hot.";
            }
            if (userInput > 14) { // If the user's input is too large, tell them to enter something valid.
               System.out.println("Please enter a valid integer.");
               userInput = input.nextInt(); // This HAS to be here. If it weren't there'd be an infinite loop of "Please enter a valid integer." nextInt() just means "Wait until the next integer input.
            } else if (guessesLeft > 1) { // if your answer isn't correct:
               guessesLeft -= 1; // Decrement the guesses left...
               System.out.println("Sorry, that is incorrect. Try again. Your guess was " + howClose + " You have " + (guessesLeft + 1) + " guesses left."); // Display an "incorrect answer" message...
               userInput = input.nextInt(); // And wait for the next input.
            } else if (guessesLeft == 1) { // This is just here to make the computer seem more human.
               guessesLeft -= 1;
               System.out.println("Sorry, that is incorrect. Try again. Your guess was " + howClose + " You have 1 guess left.");
               userInput = input.nextInt();
            }
         }
         if (userInput == correctNumber) { // If you got the right number...
            System.out.println("Correct! You got it in " + (5 - guessesLeft) + " guesses!"); // Display the "you win" message with the number of guesses you got it in.
         } else if (userInput != correctNumber) { // If you didn't get the right number...
            System.out.println("Sorry, you ran out of guesses. The correct number was " + correctNumber + "."); // Display the "game over" message with the correct number.
         }
      }
   }
}

All code was written by me with the assistance of CaveOfProgramming's fabulous tutorials. Please give me credit if you use this code elsewhere under the name of Pleistenes.

You should do, like, +250 points for anyone who can do this in Malbolge



When do I get my points? It's been, like, a month.
Portal is love.
Portal is life.
The cake is a lie.
Goomy has nothing to do with lines 1 & 2.
User avatar
TheTechBrony
Poster
Poster
 
Posts: 222
Joined: Tue Sep 02, 2014 12:07 pm
Location: Tyrone, GA, USA
Blog: View Blog (0)


Re: Create a Guess Game

Post by limdingwen on Sun Dec 14, 2014 9:06 am
([msg=85837]see Re: Create a Guess Game[/msg])

Made one in Linux x86 assembly. But right now it's still in toolow/toohigh mode, shouldn't be too hard to implement the hot/warm/cold and the custom messages.

Yeah, got a bit carried away and made a whole new game out of it :D

https://dl.dropboxusercontent.com/u/144 ... guess.html

Code: Select all
; Guessing game source code
; Beta 1.0
;
; Lim Ding Wen
; Singapore, 659442

section .text

   global _start

_start:

   ; Get random number

   call __open
   mov ebx, _dev_random
   mov ecx, 0 ; RDONLY
   call __syscall

   mov ebx, eax
   push eax
   call __read
   mov ecx, randint
   mov edx, 4 ; 4 bytes of random; 32-bit
   call __syscall
   
   call __close
   pop ebx
   call __syscall

   mov eax, [randint]
   jmp _modend

_modup:

   add eax, maxrand
   jmp _modend

_moddown:

   sub eax, maxrand

_modend:

   cmp eax, maxrand
   jg _moddown
   cmp eax, 1 ; Is it lower than 1?
   jl _modup

   mov [randint], eax

   ;call __write
   ;mov ebx, 1
   ;mov ecx, randint
   ;mov edx, 4
   ;call __syscall

   ; Write hello message
   
   call __write
   mov ebx, 1 ; Stdout
   mov ecx, hello
   mov edx, hello_len
   call __syscall

_loop:

   ; Write prompt

   mov eax, [tries]
   mov ebx, 1 ; Optimization warning: May change. Do not use if tries > 9. Use standard __itoa instead.
   mov ecx, 10 ; Optimized
   call __itoa_knowndigits
   
   mov ecx, eax
   mov edx, ebx
   
   call __write
   mov ebx, 1 ; Stdout
   call __syscall
   
   call __write
   mov ebx, 1 ; Stdout
   mov ecx, prompt
   mov edx, prompt_len
   call __syscall
   
   ; Read input

   call __read
   mov ebx, 0 ; Stdin
   mov ecx, inputbuf
   mov edx, inputbuf_len
   call __syscall

   ; Convert into integer

   mov ecx, eax
   sub ecx, 1 ; Get rid of extra newline
   
   cmp ecx, 1 ; Is the length of the number less than 1? (invalid)
   jl _reenter

   mov ebx, ecx

   mov eax, 0 ; Initalize eax
   jmp _loopconvert_nomul

_loopconvert:

   imul eax, 10 ; Multiply by 10
   
_loopconvert_nomul:

   mov edx, ebx
   sub edx, ecx
   
   push eax
   
   mov ah, [inputbuf+edx]
   
   sub ah, 48 ; ASCII digits offset
   
   cmp ah, 0 ; Less than 0?
   jl _reenter
   cmp ah, 9 ; More than 9?
   jg _reenter

   movzx edx, ah
   
   pop eax
   add eax, edx

   loop _loopconvert
   
   jmp _convertok

_reenter:

   ; Write message

   call __write
   mov ebx, 1 ; Stdout
   mov ecx, reenter
   mov edx, reenter_len
   call __syscall

   ; Repeat enter

   jmp _loop
   
_toohigh:

   call __write
   mov ebx, 1 ; Stdout
   mov ecx, toohigh
   mov edx, toohigh_len
   call __syscall

   jmp _again

_toolow:
   
   call __write
   mov ebx, 1 ; Stdout
   mov ecx, toolow
   mov edx, toolow_len
   call __syscall

_again:

   cmp dword [tries], 1 ; Is this the last try?
   jle _lose

   sub dword [tries], 1 ; Minus one try.
   
   jmp _loop

_lose:

   ; You lose

   call __write
   mov ebx, 1 ; Stdout
   mov ecx, youlose
   mov edx, youlose_len
   call __syscall

   mov eax, [randint]
   call __itoa

   mov ecx, eax
   mov edx, ebx
   call __write
   mov ebx, 1 ; Stdout
   call __syscall

   call __write
   mov ebx, 1 ; Stdout
   mov ecx, youlose2
   mov edx, youlose2_len
   call __syscall

   mov ebx, 2 ; Exit code for OK, lose.

   jmp _exit

_convertok:

   ; Compare input

   cmp eax, [randint]
   jg _toohigh
   jl _toolow

   ; You win

   call __write
   mov ebx, 1 ; Stdout
   mov ecx, youwin
   mov edx, youwin_len
   call __syscall

   mov ebx, 1 ; Exit code for OK, win.

_exit:

   push ebx

   ; Print normal goodbye

   call __write
   mov ebx, 1 ; Stdout
   mov ecx, goodbye
   mov edx, goodbye_len
   call __syscall
   mov ebx, 2 ; Stderr
   call __syscall

   ; Report OK.

   call __write
   mov ebx, 1 ; Stdout
   mov ecx, _ok
   mov edx, _ok_len
   call __syscall
   mov ebx, 2 ; Stderr
   call __syscall

   ; Exit

   call __exit
   pop ebx
   call __syscall
   
; Procedures

__itoa_init:

   ; push eax
   ; push ebx
   ; We do not have to preserve as it will contain
   ; A return value

   pop dword [_itoabuf]

   push ecx
   push edx

   push dword [_itoabuf]
   
   ret

__itoa: ; Accept eax (i), return eax (a), ebx (l)

   call __itoa_init

   mov ecx, 10 ; Start with 10 (first 2-digit)
   mov ebx, 1 ; If less than 10, it has 1 digit.

__itoa_loop:

   cmp eax, ecx
   jl __itoa_loopend

   imul ecx, 10 ; Then go to 100, 1000...
   add ebx, 1 ; Then go to 2, 3...
   jmp __itoa_loop

__itoa_knowndigits: ; Accept eax (i), ebx (d), ecx (m), return eax (a), ebx (l)

   call __itoa_init

__itoa_loopend:

   ; Prepare for loop
   ; edx now contains m
   ; ecx is now ready to count.
   ; eax already has i
   ; ebx already has d.

   mov edx, ecx
   mov ecx, ebx
   
   push ebx

__itoa_loop2:

   push eax

   ; Divide m by 10 into m

   mov eax, edx
   mov edx, 0 ; Exponent is 0
   mov ebx, 10 ; Divide by 10

   idiv ebx
   mov ebx, eax ; New m
   
   ; Divide number by new m into (1)

   mov eax, [esp] ; Number
   mov edx, 0 ; Exponent is 0
   idiv ebx ; (1)

   ; Store into buffer

   mov edx, [esp+4] ; Each dword has 4 bytes
   sub edx, ecx
   
   add eax, 48 ; Offset (1) as ASCII number
   
   mov [_itoabuf+edx], eax

   sub eax, 48 ; Un-offset (1) to prepare for next step

   ; Multiply (1) by m into (1)

   imul eax, ebx

   ; Subtract number by (1) into number
   
   mov edx, ebx ; Restore new-m back to edx as m
   
   pop ebx ; Number
   sub ebx, eax ; New number
   mov eax, ebx   

   loop __itoa_loop2

   ; Return buffer array address and
   ; Pop preserved ebx as length

   mov eax, _itoabuf
   pop ebx

   ; Pop preserved registers and restore

   pop edx
   pop ecx   

   ret

__exit:
   
   mov eax, 1 ; Exit syscall
   ret

__read:

   mov eax, 3 ; Read syscall
   ret

__write:
   
   mov eax, 4 ; Write syscall
   ret

__open:

   mov eax, 5 ; Open syscall
   ret

__close:

   mov eax, 6 ; Close syscall
   ret

__syscall:

   int 0x80 ; Interupt kernel
   ret

; Data declaration

section .data

   _dev_random db "/dev/random", 0x0
   
   maxrand equ 100
   tries dd 6

   prompt db " tries left. Input number (1-100): "
   prompt_len equ $-prompt

   hello db 0xa, "Welcome to the guessing game!", 0xa, "I am now thinking of a number. What is it?", 0xa, "Take a guess, from one to one hundred.", 0xa, 0xa
   hello_len equ $-hello

   reenter db "? REENTER", 0xa, "Invalid unsigned integer. Please re-enter your input.", 0xa
   reenter_len equ $-reenter

   toohigh db "That was too high!", 0xa, 0xa
   toohigh_len equ $-toohigh

   toolow db "That was too low!", 0xa, 0xa
   toolow_len equ $-toolow

   youwin db 0x7, 0xa, "#^$&^@%#^@#! That was correct! You win!", 0xa, 0xa
   youwin_len equ $-youwin

   youlose db "You have no more tries left! You lose!", 0xa, "The answer was "
   youlose_len equ $-youlose

   youlose2 db "! Mwahahah!", 0xa, 0xa
   youlose2_len equ $-youlose2

   goodbye db "Goodbye.", 0xa
   goodbye_len equ $-goodbye

   _ok db "Exit OK. There were no errors.", 0xa, 0xa
   _ok_len equ $-_ok

section .bss

   randint resw 2
   downsize resw 2
   
   _itoabuf resb 1024

   inputbuf resb 1024
   inputbuf_len equ 1024
limdingwen
New User
New User
 
Posts: 3
Joined: Sun Dec 14, 2014 9:01 am
Blog: View Blog (0)


Re: Create a Guess Game

Post by cyberdrain on Sun Dec 14, 2014 6:54 pm
([msg=85842]see Re: Create a Guess Game[/msg])

Wow, nice one :D
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Create a Guess Game

Post by VvGHOSTvV on Wed Sep 30, 2015 1:23 pm
([msg=89911]see Re: Create a Guess Game[/msg])

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


main()

{
    int x;
    int i;
    int guess;
    int a;

    i = 5;
    a = 0;

    srand (time(NULL));

    x = (rand()%15) +1;

    while(a!=i){
    a++;
    printf("take a guess \n");
    scanf("%d", &guess);
    printf("attempt = %d \n you entered %d \n", a, guess);

    if(guess==x){
        printf("YOU WIN \n");
        break;
    }
    else if(guess==x+2 || guess==x-2){
        printf("\nhot\n");
    }
    else if(guess==x+3 || guess==x-3 || guess==x+4 || guess==x-4 ||guess==x+5 || guess==x-5){
        printf("\nwarm\n");
    }
}
    printf("Game Over");
}



V1.0 It works I guess.. I'll be improving in the future. Written in C. Thanks to BhaaL for pointing out that I can't just scanf("%d", guess) and needed the &guess <.< was stuck on that for a while. AND Ninjex for the rand() based on time for an unseeded random number. Note as of now I only allow 5 guesses.
"In our country we have a problem, the government"
User avatar
VvGHOSTvV
New User
New User
 
Posts: 47
Joined: Thu Jul 19, 2012 7:09 pm
Location: Oklahoma
Blog: View Blog (0)


Re: Create a Guess Game

Post by -Ninjex- on Thu Oct 01, 2015 5:55 am
([msg=89928]see Re: Create a Guess Game[/msg])

VvGHOSTvV wrote:V1.0 It works I guess.. I'll be improving in the future. Written in C. Thanks to BhaaL for pointing out that I can't just scanf("%d", guess) and needed the &guess <.< was stuck on that for a while. AND Ninjex for the rand() based on time for an unseeded random number. Note as of now I only allow 5 guesses.


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

Also, maybe you gave me an old code source? Because you left something out, I had to add to get it to run. Take a look at the code again...
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)


PreviousNext

Return to User Submitted

Who is online

Users browsing this forum: No registered users and 0 guests