Prev: ruby calc.rb or just calc.rb
Next: writing binary file
From: hazal Ates on 26 Jul 2010 07:50 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/.
From: Michael Jackson on 26 Jul 2010 13:08 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 On Mon, Jul 26, 2010 at 5:50 AM, hazal Ates <niyaziates77(a)gmail.com> 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 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/. > >
From: cppcodeguy on 26 Jul 2010 19:29 Use puts to print the screen and gets to get the user's input. I'm guessing you want to add something like puts "pls press -1 to stop game" answer = gets if answer.to_i == -1 or answer == randomnumber puts "Continue or stop? (y/n)" answer = gets if answer.chomps == "y" puts "Thank you for playing, game over." Process.exit end end Was that any help? -----Original Message----- From: hazal Ates <niyaziates77(a)gmail.com> To: ruby-talk ML <ruby-talk(a)ruby-lang.org> Sent: Mon, Jul 26, 2010 5:06 am Subject: stop program with entering a key... 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/.
From: hazal Ates on 27 Jul 2010 02:47 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/.
From: Thiago Massa on 27 Jul 2010 03:52
[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 <niyaziates77(a)gmail.com> > 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 |