Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. require "ostruct"
  2.  
  3. STDOUT.sync = true # DO NOT REMOVE
  4. # Auto-generated code below aims at helping you parse
  5. # the standard input according to the problem statement.
  6.  
  7. LEFT_BORDER = 0
  8. TOP_BORDER = 0
  9. BOTTOM_BORDER = 19
  10. RIGHT_BORDER = 29
  11.  
  12. @obstacles = {}
  13. directions = ["LEFT", "UP", "RIGHT", "DOWN"]
  14. DIRECTIONS_OFFSETS = {
  15. "LEFT" => [-1, 0],
  16. "UP" => [0, -1],
  17. "RIGHT" => [1, 0],
  18. "DOWN" => [0, 1]
  19. }
  20.  
  21. (LEFT_BORDER..RIGHT_BORDER).each do |x|
  22. @obstacles[[x, TOP_BORDER - 1]] = true
  23. @obstacles[[x, BOTTOM_BORDER + 1]] = true
  24. end
  25.  
  26. (TOP_BORDER..BOTTOM_BORDER).each do |y|
  27. @obstacles[[LEFT_BORDER - 1, y]] = true
  28. @obstacles[[RIGHT_BORDER + 1, y]] = true
  29. end
  30.  
  31. def blocked?(hero, direction)
  32. offset = DIRECTIONS_OFFSETS[direction]
  33. next_coordinate = [hero.x + offset[0], hero.y + offset[1]]
  34. @obstacles[next_coordinate]
  35. end
  36.  
  37. current_direction = "LEFT"
  38.  
  39. def debug(message)
  40. STDERR.puts(message.inspect)
  41. end
  42.  
  43. # game loop
  44. loop do
  45. # n: total number of players (2 to 4).
  46. # p: your player number (0 to 3).
  47. n, p = gets.split(" ").collect {|x| x.to_i}
  48. players = []
  49. n.times do |index|
  50. # x0: starting X coordinate of lightcycle (or -1)
  51. # y0: starting Y coordinate of lightcycle (or -1)
  52. # x1: starting X coordinate of lightcycle (can be the same as X0 if you play before this player)
  53. # y1: starting Y coordinate of lightcycle (can be the same as Y0 if you play before this player)
  54. x0, y0, x1, y1 = gets.split(" ").collect {|x| x.to_i}
  55. players << OpenStruct.new(:x => x1, :y => y1)
  56. @obstacles[[x0, y0]] = true
  57.  
  58. @obstacles[[x1, y1]] = true
  59. end
  60.  
  61. hero = players[p]
  62. debug(hero)
  63.  
  64. debug(@obstacles.keys.last(3))
  65.  
  66. if blocked?(hero, current_direction)
  67. current_direction = directions.find{|direction|
  68. !blocked?(hero, direction)
  69. }
  70. end
  71. # Write an action using puts
  72. # To debug: STDERR.puts "Debug messages..."
  73. puts(current_direction)
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement