From: Daniel Moore Date: 2009-03-24T03:51:57+09:00 Subject: [SUMMARY] Sliding Puzzle (#196) The sole submission to this week's quiz comes from Martin Boese. Martin provided a simple and versatile solution. It can generate puzzles of any size, and is able to shuffle to a random starting configuration. It also throws a nice party when the player succeeds. The program provides a driver that reads keyboard commands and translates them into the correct actions on the puzzle class. The program accepts four movement commands as well as commands to quit and shuffle. Here's a look at the puzzle's shuffle method to randomize the configuration: def shuffle 100.times { move 'udlr'[(rand*4).to_i].chr } end The shuffling is done by making 100 random moves, this ensures that the puzzle remains solvable; a random shuffling of the tiles without maintaining the movement constraints is not necessarily solvable. Additionally, this leverages the existing functionality of the `move` method to keep the code simple. One potential drawback is that this doesn't select all possible starting positions with equal probability. However, having the random configurations generated with equal probability wasn't a quiz requirement, and it is often best to go with the least complex solution that meets the requirements. The method to detect whether or not the puzzle is solved also leverages existing functionality to reduce complexity, it checks if the current board is equal to a newly created board. Because the newly created board starts in the solved position and the board is an array of integers, this leads to a short and sweet definition: def solved? @board == mkboard(@x,@y) && @moved end The check for `@moved` to be true is to prevent the puzzle from partying when the game is first created and before it is shuffled. Speaking of the party, here's the section responsible for that magical functionality: puts 10.times { $stdout.print '*'; sleep 0.1; $stdout.flush } puts " You won!\n" It creates a rapid display of asterisks and congratulates the player! Thank you, Martin, for a wonderful solution! http://rubyquiz.strd6.com/quizzes/196 - Daniel