O'Reilly logo

Wicked Cool Ruby Scripts by Steve Pugh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Word Scramble

Word Scramble

wordScramble.rb

As I mentioned in "#29 Sudoku Solver" on Sudoku Solver, word scramble is a classic game. There is something fascinating about a game that can hold your attention while exercising your vocabulary. This classic game can be found in newspapers, magazines, and even books dedicated solely to word scrambles. But who needs those when you can customize your own game?

The Code

 unless ARGV[0] and File.exists?(ARGV[0])
     puts "\n\nUsage is wordScramble.rb <word.file>\n\n"
     exit
 end

 tries = 10

 words = File.readlines(ARGV[0])
 mystery_word = words[rand(words.size)].chomp

 scramble_word = mystery_word.split(//).sort_by{rand}.join ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required