Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 1st, 2009, 03:23 PM
Allu_Akbar Allu_Akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 5 Allu_Akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 26 sec
Reputation Power: 0
Hangman Game

How do you select random words from a list of words using a global variable? Or a different method?

Reply With Quote
  #2  
Old December 1st, 2009, 04:17 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
Use Array#shuffle
Code:
irb(main):001:0> letters = ('a'..'z').to_a
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
irb(main):002:0> letters.shuffle.shift
=> "l"
irb(main):003:0> letters.shuffle.shift
=> "i"
irb(main):004:0> letters.shuffle.shift
=> "p"
irb(main):005:0> letters.shuffle.shift
=> "h"
irb(main):006:0> letters.shuffle.shift
=> "q"
If Array#shuffle is not available in your distribution you can 'fake' it with sort_by
Code:
irb(main):007:0> letters.sort_by { rand }
=> ["q", "g", "l", "f", "p", "d", "e", "y", "v", "h", "z", "r", "n", "s", "k", "u", "a", "x", "b", "t", "o", "c", "w", "i", "j", "m"]
irb(main):008:0> letters.sort_by { rand }
=> ["n", "y", "z", "o", "h", "p", "x", "v", "b", "m", "t", "r", "q", "k", "a", "g", "j", "c", "u", "d", "l", "w", "s", "f", "i", "e"]

The examples above use only letters for brevity, but you can use words as well.
Code:
irb(main):010:0> ["A", "List", "Of", "Words"].shuffle
=> ["Words", "List", "Of", "A"]
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #3  
Old December 1st, 2009, 06:26 PM
Allu_Akbar Allu_Akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 5 Allu_Akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 26 sec
Reputation Power: 0
Thanks but

im looking for something different. Here is a example

def initialize(word=nil)
# if there is a word, use it
if word != nil
@word = word
else
# otherwise use a random word from the dictionary
@word = $words
end

$words is my global varibale that links my words but i dont know how to randomize it and select one word from my list.

Reply With Quote
  #4  
Old December 1st, 2009, 10:16 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
Quote:
Originally Posted by Allu_Akbar
$words is my global varibale that links my words
That statement doesnt make much sense. What do you mean by links?
If $words is an array, just shuffle it.
Code:
else
   # otherwise use a random word from the dictionary
   @word = $words.shuffle.shift
end

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Hangman Game

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap