guess the number topic
Forum » ASCII board. / ASCII programming. » guess the number topic
started by: EKVirtanenEKVirtanen
on: 1183572167|%e %b %Y, %H:%M %Z|agohover
number of posts: 14
rss icon RSS: new posts
guess the number; FB & python
EKVirtanenEKVirtanen 1183572167|%e %b %Y, %H:%M %Z|agohover

While waiting sauna to heat up, i decided to study more python since it seems to be quiet in every darn forum there is on internet.
First, i created simple "guess the number" game with freebasic and then converted it to python.
For python programmers, my code prolly looks ugly as hell, but it is like my first > 5 lines long self-made code, so shut up :P

Anyway, after studying more python documents, it feels interesting language. Want to learn more eventho i still hate since cant use CAPS at anywhere :D Silly me.

guess.bas

' Guess what number for FreeBASIC .17b stable
' E.K.virtanen. 2007, public domain

Randomize TIMER
CONST MIN = 1
CONST MAX = 100
Dim AS Integer Plr_guess, Randomed, Rounds

' Main module of program.
DO
    Color 15, 0 : CLS
    Randomed = INT(RND * ((MAX + 1) - MIN) + MIN)
    Rounds = 0
    Print "Ok, i think random number between "; MIN; MAX; "."
    Print "Your job is to guess what it is in as minimal tries as possible."
    Print ""
    Print "After your every guess, ill give you hint is my number higher or lower than your guess."
    Print "You can exit by 'guessing' 0."
    Print "Press any key to start game."; : GETKEY

    ' From here starts the gaming loop itself. This is looped until guess it right one.
    CLS
    DO
        Rounds = (Rounds + 1)
        Print "This is round number: "; Rounds; "."
        INPUT "Give your guess: "; Plr_guess

        Color 14, 0
        IF Plr_guess > Randomed THEN
            Print "My number is smaller..."
        END IF

        IF Plr_guess < Randomed THEN
            Print "My number is higher..."
        END IF
        Color 15, 0
        IF Plr_guess = Randomed THEN EXIT DO

    LOOP While Plr_Guess <> 0

    IF Randomed <> Plr_Guess THEN EXIT DO

    Print "You got it!!!"
    Print "It took "; Rounds; " rounds to guess right number."
    Print ""
    Print "Press 'y' to play again. Any other key to quit."
    DO Until INKEY$ <> "" : Loop
    IF INKEY$ <> "y" AND INKEY$ <> "Y" THEN EXIT DO
Loop
END

guess.py. Result of 2 hours python learning.

# Guess what number for python. My one of first python codes so it prolly looks ugly as hell.
# E.K.virtanen. 2007, public domain

# modules we need here
import random
import os

# function wich solves what OS is used. linux and windows regignized.
def os_clear():
    return_value = "\n"
    if os.name == "posix":
        return_value = 'clear'
    elif os.name in ("nt", "dos", "ce"):
        return_value = 'CLS'
    return return_value

# Constant variables
const_min = 1
const_max = 100

clear_screen = os_clear()

# MAIN MODULE OF PROGRAM
guess_game = end_game = 'play'
while end_game == 'play':
    os.system(clear_screen)

    randomed = random.randint(const_min, const_max)
    rounds = 0
    print 
    print 'Ok, i think random number between ', str(const_min), ' to ' + str(const_max)
    print 'Your job is to guess what it is in as minimal tries as possible.'
    print ''
    print 'After your every guess, ill give you hint is my number higher or lower than your guess.'
    print 'You can exit by "guessing" (0).'
    raw_input('Press <ENTER> to start game.')

        # guessing part of game
    os.system(clear_screen)
    while guess_game == 'play':
        rounds += 1
        print 'This is rounds number: ', str(rounds) + '.'
        print
        plr_guess = int(raw_input('Give your guess: '))
        if int(plr_guess) < randomed:
            print 'My number is higher...'
        elif int(plr_guess) > randomed:
            print 'Mu number is lower...'
        elif int(plr_guess) == randomed:
            guess_game = 'stop'
        elif int(plr_guess) == 0:
            break
        else:
            print 'Give value between ', str(const_min), ' to ' + str(const_max), ' please.'

    if guess_game == 'stop':
        end_game = 'stop'
        os.system(clear_screen)
        print 'You got it!'
        print 'It took ', str(rounds), ' rounds to guess right number.'
        print 
        print ' Press <y> + <ENTER> to play again. <ENTER> only to quit.'
        if raw_input() == 'y':
            guess_game = end_game = 'play'
unfold guess the number; FB & python by EKVirtanenEKVirtanen, 1183572167|%e %b %Y, %H:%M %Z|agohover
Re: guess the number; FB & python
Anonymous (66.24.254.x) 1183574490|%e %b %Y, %H:%M %Z|agohover

Well it's not BUTT Ugly code yet. But when you get better at python it might actually look worst than this ;-). hehe…

This actually looks pretty good for 2 hours of learning. and well, it works, really the bottom line here :-)

unfold Re: guess the number; FB & python by Anonymous (66.24.254.x), 1183574490|%e %b %Y, %H:%M %Z|agohover
Re: guess the number; FB & python
EKVirtanenEKVirtanen 1183575873|%e %b %Y, %H:%M %Z|agohover

Well Mystik, its prettier than you :D
I freakin jerk forgot that i once made this darn game before with python (memory over… you know lol)

#! /usr/bin/python
import random, os
To get right keyword to clear screen for linux or windows
usr_os = os.name
if usr_os != "POSIX":
   clear_screen = "clear"
else: 
   clear_screen = "cls"
# lets get random number to guess
rand_number = random.randint(1,100)
# ok, we reset round counter and value of user guess.
round_counter = guessed = 0
# ok, "main" loop of game
while guessed != rand_number:
   os.system(clear_screen)
   round_counter = round_counter + 1

   print "This is round number:", round_counter
   print rand_number #ok, now we can see the number to make testing easier.
   guessed = input ("What is your guess?")

    if guessed < rand_number:
       print "Sorry, try higher number."
    elif guessed > rand_number:
       print "No, try smaller number."
    elif guessed < 1 or guessed > 100:
       print "Value must be between 1 to 100"
    else:
       print "Error in given value."

    raw_input ("Press <ENTER> to continue!")

print "Correct number."
print 
print "Random number was:", rand_number, " and your guess was:", guessed, "."
print
print "It took:", round_counter, " rounds to guess it."
print "Thank you for playing."
unfold Re: guess the number; FB & python by EKVirtanenEKVirtanen, 1183575873|%e %b %Y, %H:%M %Z|agohover
Re: guess the number; FB & python
EKVirtanenEKVirtanen 1187028807|%e %b %Y, %H:%M %Z|agohover

Im nearly finished my Bash-Script version of this game. Would be cool to get more these. Guys, you got any time here? Mystik, ADA ;) Rick?

I created link to menu, you can see it. Would be nice to have 5 to 10 different programming/scripting language version soon so we could open that link :)

ASCII only, language free naturally. :)

unfold Re: guess the number; FB & python by EKVirtanenEKVirtanen, 1187028807|%e %b %Y, %H:%M %Z|agohover
Re: guess the number; FB & python
MystikShadowsMystikShadows 1187055324|%e %b %Y, %H:%M %Z|agohover

I don't have the Ada compiler installed, but I did do a FreePascal Version

{ Guess program.  public domain as of august 13th 2007. Mystikshadows }

Program GuessingGame;

Uses
     Crt;

Const
     RandomMinimum = 1;
     RandomMaximum = 100;

Var
     RandomNumber:   Integer;
     UserGuess:      Integer;
     UserAnswer:     Char;
     RoundNumber:    Integer;
     CanExit:        Integer;
     CancelGuessing: Integer;
Begin

     Randomize;
     CanExit := 0;
     While CanExit = 0 Do
     Begin
           RandomNumber := Random(RandomMaximum) + 1;
           WriteLn('Ok, I just thought of a number between ', RandomMinimum, ' and ', RandomMaximum);
           WriteLn('And you can try to guess it in the least amount of tries.');
           WriteLn;
           WriteLn('After each guess I will give you hints as to if my number is');
           WriteLn('higher or lower than your guess again or enter 0 to exit the');
           WriteLn('current guessing.');
           WriteLn;
           RoundNumber    := 1;
           CancelGuessing := 0;
           UserGuess      := 0;
           UserAnswer     := #32;
           While (RandomNumber <> UserGuess) And (CancelGuessing = 0) Do
           Begin
                 Write('Round # ', RoundNumber, ': ');
                 ReadLn(UserGuess);
                 If (UserGuess = 0) Then
                    CancelGuessing := 1;
                 If (UserGuess > RandomNumber) Then
                    WriteLn('You are too high, guess lower.');
                 If (UserGuess < RandomNumber) Then
                    WriteLn('You are too low, guess higher.');
                 RoundNumber := RoundNumber + 1;
           End;
           If RandomNumber = UserGuess Then
           Begin
              WriteLn;
              WriteLn('You guess the number in ', RoundNumber - 1, ' guesses.');
           End;
           While (UserAnswer <> 'y') And (UserAnswer <> 'Y') And (UserAnswer <> 'n') And (UserAnswer <> 'N') Do
           Begin
                 WriteLn;
                 Write('Do you want to try another round (Y for Yes, N for No)?');
                 ReadLn(UserAnswer);
           End;
           If (UserAnswer = 'n') Or (UserAnswer = 'N') Then
              CanExit := 1;
     End;

End.
last edited on 1187103298|%e %b %Y, %H:%M %Z|agohover by MystikShadows + show more
unfold Re: guess the number; FB & python by MystikShadowsMystikShadows, 1187055324|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
EKVirtanenEKVirtanen 1187106581|%e %b %Y, %H:%M %Z|agohover

http://www.ascii-world.com/guess-it ;)

unfold Re: guess the number topic by EKVirtanenEKVirtanen, 1187106581|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
EKVirtanenEKVirtanen 1191739507|%e %b %Y, %H:%M %Z|agohover

Linux Bash-Script version http://www.ascii-world.com/guess-it-bash-script

unfold Re: guess the number topic by EKVirtanenEKVirtanen, 1191739507|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
EKVirtanenEKVirtanen 1194589501|%e %b %Y, %H:%M %Z|agohover

Just added thinBasic version provided by ErosOlmi from thinBasic forums.
Now we have these versions here, looking good :)

Guess it in Bash-Script
Guess It in Ansi C
Guess It in ChipMunk BASIC
Guess It in FreeBASIC
Guess It in FreePascal
Guess It in python
Guess It in QBASIC
Guess It in Run BASIC
Guess It in SmallBasic
Guess It in thinBasic
Guess It in yaBasic

unfold Re: guess the number topic by EKVirtanenEKVirtanen, 1194589501|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
Anonymous (88.112.81.x) 1195593303|%e %b %Y, %H:%M %Z|agohover

Here is mine, written in CoolBasic:

//the Ultimate Guessing Game (is that name proper English?)
//By: Jarkko 'Jare' Linnanvirta 2007 (e-mail me if you want to ask something: kpelit2003@hotmail.com)
//Can be used freely.

'The very important title
SetWindow "the Ultimate Guessing Game!"

'Start by randomizing a number
theNumber = Rand(1,100)

Repeat

    Text 0,0, "Guess what number I am thinking (1 - 100)"

    'Show console for user's ansrver
    Locate 0, ScreenHeight()-20
    ansver = Input("? ")

    'Check ansver when user hit's enter
    If KeyHit(cbKeyReturn) Then
        times + 1
        If ansver = theNumber Then
            'User got it right
            Text 0,20, "Yes, that's what I was thinking!"
            Text 0,40, "You tried "+times+" times."
            DrawScreen
            WaitKey
            theNumber = Rand(1,100)
            times = 0
        ElseIf ansver < theNumber Then
            'User got it wrong
            Text 0,20, "No, too low!"
            DrawScreen
            WaitKey
        Else
            'User got it wrong again
            Text 0,20, "No, too high!"
            DrawScreen
            WaitKey
        End If
        CloseInput 'Clear ansver in console
    End If
    DrawScreen 'Update screen so we can see something

Forever 'Eternal loop (of course there is an inbuild safe exit feature: Escape key closes the program)

admin edit: added code tags.

last edited on 1195644412|%e %b %Y, %H:%M %Z|agohover by EKVirtanen + show more
unfold Re: guess the number topic by Anonymous (88.112.81.x), 1195593303|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
EKVirtanenEKVirtanen 1195644597|%e %b %Y, %H:%M %Z|agohover

Thanks Jare for your contribution.
Finnish
Pystyisitkö muokkaamaan tuota niin että se olisi hieman enemmän samallalailla toimiva kuin muutkin sorsat?
Eli

PÄÄLOOPPI
tehdään valimistelut, printataan alkuteksti blaablaa

PELILOOPPI
kysellään kunnes oikein
//PELILOOPPI

Pääloopissa pysytään kunnen pelaaja arvaa 0 (eli haluaa lopettaa)
//PÄÄLOOPPI

unfold Re: guess the number topic by EKVirtanenEKVirtanen, 1195644597|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
Anonymous (88.112.81.x) 1195593588|%e %b %Y, %H:%M %Z|agohover

Sorry, the code just didn't end up in the way I ment (no indents etc.).

Here is more better looking one: http://koti.mbnet.fi/jare1/filer/files/guess.CB

unfold Re: guess the number topic by Anonymous (88.112.81.x), 1195593588|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
EKVirtanenEKVirtanen 1195649410|%e %b %Y, %H:%M %Z|agohover

Important about this project; Read more http://ascii-world.wikidot.com/forum/t-27823/guess-it-pages-changed

unfold Re: guess the number topic by EKVirtanenEKVirtanen, 1195649410|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
rCX (guest) 1233461234|%e %b %Y, %H:%M %Z|agohover

Really cool project. Here's one in MATLAB. I was going to write one if FASM for DOS as well.

% Guess what number for MATLAB 7.5.0
% rCX. 2009, public domain

MIN = 1;
MAX = 100;
Plr_Guess = -1;

while true()
    clc %Clear Command Window
    Randomed = uint8(rand() * ((MAX + 1) - MIN) + MIN);
    rounds = 0;
    disp(['Ok, i think random integer between ',num2str(MIN),' and ',num2str(MAX),'.'])
    disp('Your job is to guess what it is in as minimal tries as possible.')
    disp(' ')
    disp('After your every guess, ill give you hint is my number higher or lower than your guess.')
    disp('You can exit by ''guessing'' 0.')
    disp('Press any key to start game.')
    pause

    clc
    while true()
        rounds = rounds + 1;
        disp(['This is round number: ',num2str(rounds),'.'])

        Plr_Guess = str2num(input('Give your guess: ','s'));  %'s' tells MATLAB to return input as string

        if Plr_Guess == 0
            return  %exit program
        elseif Plr_Guess > Randomed
            disp('My number is smaller...')
        elseif Plr_Guess < Randomed
            disp('My number is higher...')
        elseif Plr_Guess == Randomed
            break
        else
            return %exit program
        end

        disp(' ')
    end

    disp('You got it!!!')
    disp(['It took ',num2str(rounds),' rounds to guess right number.'])
    disp(' ')
    key = input('Do you want to play again? Enter ''y'' or ''n''. ','s');

    if  ~strcmp(key,'y') && ~strcmp(key,'Y')
        break
    end
end
unfold Re: guess the number topic by rCX (guest), 1233461234|%e %b %Y, %H:%M %Z|agohover
Re: guess the number topic
E.K.Virtanen (guest) 1233568565|%e %b %Y, %H:%M %Z|agohover

Thanks rCX. Cool to get new versions for this one. Ill add your code in guess-it page when i get back home from work.

unfold Re: guess the number topic by E.K.Virtanen (guest), 1233568565|%e %b %Y, %H:%M %Z|agohover
new post
page_revision: 0, last_edited: 1181740773|%e %b %Y, %H:%M %Z (%O ago)
Unless stated otherwise Content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.