stop program with entering a key...

Discussion in 'Ruby' started by hazal Ates, Jul 26, 2010.

  1. hazal Ates

    hazal Ates Guest

    i m a new in Ruby . sorry may be its a silly question.
    my small game " finding a randon number" in codes i want to put
    "pls press -1 to stop game"
    or
    end of first game i want to ask
    "do you cont. or stop ( y / n ) enter , pls"

    but i dont know that how (-1) or y/n work to stop or cont.

    i want to like this code :
    if keyenter=(-1) goto last line and stop
    if keyenter = y goto beginning of game
    if keyenter = n goto end of prog. it says puts "thank you , game over"
    like this...
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Jul 26, 2010
    #1
    1. Advertising

  2. The following code doesn't do exactly what you need, but it should be
    enough to get you started in the right direction.

    def prompt
    STDOUT.write ">> "
    gets
    end

    def game_over
    puts "thank you.\ngame over!"
    exit
    end

    trap ("INT") { puts; game_over }

    puts "welcome to my game. please press 'q' to quit."

    while (code =3D prompt)
    case code.strip
    when "y"
    puts "start game"
    when "q"
    game_over
    else
    puts "sorry, i don't understand."
    end
    end

    --
    Michael Jackson
    http://mjijackson.com
    @mjijackson



    On Mon, Jul 26, 2010 at 5:50 AM, hazal Ates <> wrote:
    > i m a new in Ruby . sorry may be its a silly question.
    > my small game " finding a randon number" in codes i want to put
    > "pls press -1 to stop game"
    > or
    > end of first game i want to ask
    > "do you cont. or =A0stop ( y / n ) enter , pls"
    >
    > but i dont know that how (-1) or y/n work to stop or cont.
    >
    > i want to like this code =A0:
    > if keyenter=3D(-1) goto last line and stop
    > if keyenter =3D y goto beginning of game
    > if keyenter =3D n goto end of prog. it says puts "thank you , game over"
    > like this...
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Michael Jackson, Jul 26, 2010
    #2
    1. Advertising

  3. hazal Ates

    hazal Ates Guest

    Michael Jackson wrote:
    > The following code doesn't do exactly what you need, but it should be
    > enough to get you started in the right direction.
    >
    > def prompt
    > STDOUT.write ">> "
    > gets
    > end
    >
    > def game_over
    > puts "thank you.\ngame over!"
    > exit
    > end
    >
    > trap ("INT") { puts; game_over }
    >
    > puts "welcome to my game. please press 'q' to quit."
    >
    > while (code = prompt)
    > case code.strip
    > when "y"
    > puts "start game"
    > when "q"
    > game_over
    > else
    > puts "sorry, i don't understand."
    > end
    > end
    >
    > --
    > Michael Jackson
    > http://mjijackson.com
    > @mjijackson


    thank you MJ

    my desire generaaly not for func. of this but

    after press. "y" for example ( at the last line ) prog execution must
    GOTO beginning of program .

    may be you now in pascal its easy with GOTO

    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Jul 27, 2010
    #3
  4. hazal Ates

    Thiago Massa Guest

    [Note: parts of this message were removed to make it a legal post.]

    Just define a function like "play_game" and do a loop that gets the input in
    order to play the game or not and calls that function.

    I don't even know how to use goto's.

    2010/7/26 hazal Ates <>

    > Michael Jackson wrote:
    > > The following code doesn't do exactly what you need, but it should be
    > > enough to get you started in the right direction.
    > >
    > > def prompt
    > > STDOUT.write ">> "
    > > gets
    > > end
    > >
    > > def game_over
    > > puts "thank you.\ngame over!"
    > > exit
    > > end
    > >
    > > trap ("INT") { puts; game_over }
    > >
    > > puts "welcome to my game. please press 'q' to quit."
    > >
    > > while (code = prompt)
    > > case code.strip
    > > when "y"
    > > puts "start game"
    > > when "q"
    > > game_over
    > > else
    > > puts "sorry, i don't understand."
    > > end
    > > end
    > >
    > > --
    > > Michael Jackson
    > > http://mjijackson.com
    > > @mjijackson

    >
    > thank you MJ
    >
    > my desire generaaly not for func. of this but
    >
    > after press. "y" for example ( at the last line ) prog execution must
    > GOTO beginning of program .
    >
    > may be you now in pascal its easy with GOTO
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Thiago Fernandes Massa
    11 83979414
     
    Thiago Massa, Jul 27, 2010
    #4
  5. Hello,

    > I don't even know how to use goto's.


    Concerning GOTO's, there is a little comic I really like:

    http://xkcd.com/292/

    :eek:)

    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kl=E9ber
     
    Jean-Julien Fleck, Jul 27, 2010
    #5
  6. hazal Ates

    hazal Ates Guest

    Thiago Massa wrote:
    > Just define a function like "play_game" and do a loop that gets the
    > input in
    > order to play the game or not and calls that function.
    >
    > I don't even know how to use goto's.
    >
    > 2010/7/26 hazal Ates <>


    thank you TM and others ;

    let me give an example what i want in Pascal code :

    writeln('continue ? // finish game =====> y / n ');
    writeln;
    readln(k);
    if (k='E') or (k='e')
    then
    goto (begining of prog.);

    can you give an example to run same code in ruby ?
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Jul 27, 2010
    #6
  7. hazal Ates

    hazal Ates Guest

    hazal Ates wrote:
    > >>

    >> 2010/7/26 hazal Ates <>

    >
    > thank you TM and others ;
    >
    > let me give an example what i want in Pascal code :
    >
    > writeln('continue ? // finish game =====> y / n ');
    > writeln;
    > readln(k);
    > if (k='E') or (k='e')
    > then
    > goto (begining of prog.);
    >
    > can you give an example to run same code in ruby ?


    change the line like ; if (k='Y') or (k='y')
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Jul 27, 2010
    #7
  8. def menu
    p "Write a number"
    p "Q to stop"
    the_replay = gets.chomp
    end

    def do_exit
    p "Bye bye"
    exit
    end

    while 1 == 1 do
    the_reply = menu
    case the_reply
    when "q" then do_exit
    when "Q" then do_exit
    end
    p the_reply
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Giampiero Zanchi, Jul 27, 2010
    #8
  9. hazal Ates

    hazal Ates Guest

    Giampiero Zanchi wrote:
    > def menu
    > p "Write a number"
    > p "Q to stop"
    > the_replay = gets.chomp
    > end
    >
    > def do_exit
    > p "Bye bye"
    > exit
    > end
    >
    > while 1 == 1 do
    > the_reply = menu
    > case the_reply
    > when "q" then do_exit
    > when "Q" then do_exit
    > end
    > p the_reply
    > end


    thank you G. Z.
    i use your codes to my problem.
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Jul 28, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Theule

    Stop Debugging doesn't stop in ASP.NET

    Matt Theule, Jul 23, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    753
    Matt Theule
    Jul 24, 2003
  2. Son KwonNam
    Replies:
    11
    Views:
    2,622
    mr_organic
    Apr 9, 2004
  3. Phoe6
    Replies:
    12
    Views:
    614
    Gordon Burditt
    Feb 28, 2005
  4. M P
    Replies:
    1
    Views:
    502
  5. Bennett Haselton
    Replies:
    2
    Views:
    282
    Justin C
    Dec 13, 2010
Loading...

Share This Page