hazal
July 26, 2010, 1:46pm
1
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…
hazal
July 26, 2010, 7:09pm
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 = prompt)
case code.strip
when “y”
puts “start game”
when “q”
game_over
else
puts “sorry, i don’t understand.”
end
end
–
Michael J.
The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...
@mjijackson
hazal
July 27, 2010, 8:47am
3
Michael J. 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 J.
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
hazal
July 27, 2010, 9:53am
4
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 [email protected]
hazal
July 27, 2010, 10:29am
5
Hello,
I don’t even know how to use goto’s.
Concerning GOTO’s, there is a little comic I really like:
:o)
hazal
July 27, 2010, 1:43pm
6
Thiago M. 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 [email protected]
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 ?
hazal
July 27, 2010, 1:50pm
7
hazal Ates wrote:
2010/7/26 hazal Ates [email protected]
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’)
hazal
July 28, 2010, 1:43pm
8
Giampiero Z. 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.
hazal
July 27, 2010, 4:41pm
9
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