Guest User

Untitled

a guest
Feb 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. module FJC
  2.  
  3. class Board
  4. def initialize
  5. @initialized = false
  6. @board = {}
  7. end
  8.  
  9. def update distance, matrix
  10. validate_distance_parameter(distance)
  11. validate_matrix_parameter(matrix)
  12.  
  13. first_time(distance) unless initialized?
  14. store_board(matrix)
  15.  
  16. @destinations &= possible_destinations(distance)
  17. end
  18.  
  19. def initialized?
  20. @initialized
  21. end
  22.  
  23. def position
  24. [ @dx, @dy ]
  25. end
  26.  
  27. def destinations
  28. @destinations
  29. end
  30.  
  31. def destination
  32. case @destinations.size
  33. when 0
  34. raise
  35. when 1
  36. @destinations[0]
  37. else
  38. nil
  39. end
  40. end
  41.  
  42. def [] coordinates
  43. @board[coordinates]
  44. end
  45.  
  46. def move direction
  47. case direction
  48. when :up
  49. @dy += 1
  50. when :down
  51. @dy -= 1
  52. when :left
  53. @dx -= 1
  54. when :right
  55. @dx += 1
  56. else
  57. raise
  58. end
  59. end
  60.  
  61. def look direction
  62. delta_x, delta_y = deltas(direction)
  63. @board[ [position[0] + delta_x, position[1] + delta_y] ]
  64. end
  65.  
  66. private
  67.  
  68. def first_time distance
  69. raise if initialized?
  70.  
  71. @initialized = true
  72.  
  73. @dx, @dy = 0, 0
  74.  
  75. @destinations = possible_destinations(distance)
  76. end
  77.  
  78. def possible_destinations distance
  79. [
  80. [ position[0] + distance[0], position[1] + distance[1] ],
  81. [ position[0] - distance[0], position[1] + distance[1] ],
  82. [ position[0] - distance[0], position[1] - distance[1] ],
  83. [ position[0] + distance[0], position[1] - distance[1] ]
  84. ].uniq
  85. end
  86.  
  87. def validate_distance_parameter distance
  88. raise unless distance.is_a?(Array)
  89. raise unless distance.size == 2
  90. raise unless distance[0].is_a?(Integer)
  91. raise unless distance[1].is_a?(Integer)
  92.  
  93. raise "Yay, I already won!" if distance == [0, 0]
  94.  
  95. raise if distance.sort[0] < 0
  96. end
  97.  
  98. def validate_matrix_parameter matrix
  99. raise unless matrix.is_a?(Array)
  100. raise unless matrix.size == 3
  101.  
  102. raise unless matrix[0].size == 3
  103. raise unless matrix[1].size == 3
  104. raise unless matrix[2].size == 3
  105.  
  106. matrix.flatten.each{|e|
  107. raise unless e.is_a?(String)
  108. raise unless e.size == 1
  109. raise unless %w{ E F G O P R }.include?(e)
  110. }
  111.  
  112. raise unless matrix.flatten.select{|e| e == 'P'}.size == 1
  113. raise unless matrix.flatten.select{|e| e == 'R'}.size <= 1
  114.  
  115. raise unless matrix[1][1] == 'P'
  116. raise unless matrix[1][1] != 'R'
  117. end
  118.  
  119. def store_board matrix
  120. @board[ [position[0] - 1, position[1] + 1] ] = matrix[0][0]
  121. @board[ [position[0] - 0, position[1] + 1] ] = matrix[0][1]
  122. @board[ [position[0] + 1, position[1] + 1] ] = matrix[0][2]
  123. @board[ [position[0] - 1, position[1] - 0] ] = matrix[1][0]
  124. @board[ [position[0] - 0, position[1] - 0] ] = matrix[1][1]
  125. @board[ [position[0] + 1, position[1] - 0] ] = matrix[1][2]
  126. @board[ [position[0] - 1, position[1] - 1] ] = matrix[2][0]
  127. @board[ [position[0] - 0, position[1] - 1] ] = matrix[2][1]
  128. @board[ [position[0] + 1, position[1] - 1] ] = matrix[2][2]
  129. end
  130.  
  131. def deltas direction
  132. case direction
  133. when :up
  134. [0, 1]
  135. when :upright
  136. [1, 1]
  137. when :right
  138. [1, 0]
  139. when :downright
  140. [1, -1]
  141. when :down
  142. [0, -1]
  143. when :downleft
  144. [-1, -1]
  145. when :left
  146. [-1, 0]
  147. when :upleft
  148. [-1, 1]
  149. else
  150. raise
  151. end
  152. end
  153. end
  154.  
  155. end
Add Comment
Please, Sign In to add comment