Guest User

Untitled

a guest
Feb 24th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.49 KB | None | 0 0
  1. require 'pp'
  2. a = %w(2 3 5 1 0 8 4 7 6)
  3. b = %w(2 3 5 0 1 8 4 7 6)
  4.  
  5. def findMove(beforeArray, afterArray)
  6.         initialindex = beforeArray.index('0')
  7.         afterindex = afterArray.index('0')
  8.         puts initialindex
  9.         puts afterindex
  10.         if afterindex < initialindex
  11.                 if afterindex == (initialindex - 1) #left move
  12.                         return "r#{beforeArray[afterindex]}"
  13.                 else #more than 1 less, up move
  14.                         return "d#{beforeArray[afterindex]}"
  15.                 end
  16.         else
  17.                 if afterindex == (initialindex + 1) #right move
  18.                         return "l#{beforeArray[afterindex]}"
  19.                 else #more than one more, down move
  20.                         return "u#{beforeArray[afterindex]}"
  21.                 end
  22.         end
  23. end
  24.  
  25. #puts findMove(a, b)
  26.  
  27. #pass a file; method reads through file and creates a hash of arrays
  28. def createHash(file)
  29.         i = 0
  30.         hash = {}
  31.         fileinput = IO.readlines(file)
  32.         array = []
  33.         fileinput.each do |line|
  34.                 if line.length > 1
  35.                         line.split("\n")[0].split.each do |number|
  36.                                 array.push(number.to_i)
  37.                         end
  38.                 else #newline, end of puzzle
  39.                         hash[i] = array
  40.                         array = []
  41.                         i++
  42.                 end
  43.         end
  44.         return hash
  45. end
  46.  
  47. pp createHash("solvedpuzzle")
Advertisement
Add Comment
Please, Sign In to add comment