Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Simultaneous text input/output in a command line game

Reply
Thread Tools

Simultaneous text input/output in a command line game

 
 
rubyvic
Guest
Posts: n/a
 
      02-12-2006
Hi everyone!

I=92m currently learning Ruby, and have made this simple number guessing =
game:
____________________________
puts "I'm thinking of a number from 0 to 10. What's your guess?"

my_number =3D rand(11)
user_guess =3D gets.chomp.to_i

until user_guess =3D=3D my_number
if user_guess > my_number
puts "Too high."
else my_number > user_guess
puts "Too low."
end
puts "Guess again:"
user_guess =3D gets.chomp.to_i
end

puts "Yep, it was " + my_number.to_s + ". You win!"
____________________________

A friend of mine wrote a Java version which you can see here:=20
http://violasong.com/tonyguessinggame.java. In addition to what my=20
script does, his is able to taunt you with a random message if you don=92=
t=20
input any data for 5 seconds .

I want to try adding that functionality to my script, and am wondering=20
if it's possible in Ruby =96 to print a message every x seconds *while*=20
it's waiting for you to input something. If someone could point me in=20
the right direction, that would be much appreciated!

Victoria Wang


 
Reply With Quote
 
 
 
 
Timothy Goddard
Guest
Posts: n/a
 
      02-12-2006
As a method without having to worry about threads you could also use:

require 'timeout'

messages = ["What are you waiting for?", "I don't have all day.", "When
I said 'take your time', I didn't mean this long!", "Hurry up and enter
a value!"]

s = nil
until s
begin
Timeout::timeout(5) do
puts "Enter a value: "
s = gets
end
rescue Timeout::Error
puts messages[rand(messages.length).to_i]
end
end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouse Location to Text line or Text line to location kuna.subramaniyam@gmail.com Javascript 2 04-14-2008 01:46 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Error in asp text: Command text not set for the command Jack ASP General 2 03-07-2005 11:27 PM
Need simple command for validating xml files at the command line Bernd Oninger XML 1 07-07-2004 06:20 PM
RUN/execute a Command-Line command from an ASP page. Lucas Cowald ASP .Net 4 10-23-2003 11:09 AM



Advertisments