csaboka

Untitled

Jun 24th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.37 KB | None | 0 0
  1. -- The function get_name() should return a single string that is the name of the puzzle.
  2. --
  3. function get_name()
  4.     return "MOTORISED COORDINATE TRACKER"
  5. end
  6.  
  7. -- The function get_description() should return an array of strings, where each string is
  8. -- a line of description for the puzzle. Surrounding text with asterisks will cause it to
  9. -- be rendered in bold, something we use when mentioning a signal by name.
  10. --
  11. -- By using the syntax that we use in our puzzle descriptions, you can also create tables:
  12. --
  13. --       "| This is a table. | This is a second column. | This is a column with "
  14. --       "|                  |                          | multiple line of text."
  15. --       "----------------------------------------------------------------------"
  16. --       "| This is a second |                          | More text here!       "
  17. --       "| table row.       |                          |                       "
  18. --
  19. -- Puzzle descriptions are not automatically paginated, so you can use the string "<PAGE>"
  20. -- to start a new page of text. If you don't it will overflow and make Zach sad.
  21. --
  22. function get_description()
  23.     return {
  24.         "*MotorA* and *MotorB* are simple inputs connected to the left and right tracks of the bot.",
  25.         "*coord-X* and *coord-Y* are XBus outputs connected to a map grid system.",
  26.         "The motors have 0, 50 and 100 outputs to control reverse, halt and forward functions respectively.",
  27.         "For each time unit, track the position of the bot.",
  28.         "The Bot starts facing up (North).",
  29.         "Turning does not change the coordinates.",
  30.         "<PAGE>",
  31.         "HALTING means the bot does not move or turn.",
  32.         "Moving FORWARD means moving the bot one unit in the direction it is facing.",
  33.         "Moving BACKWARD means moving the bot one unit in the opposing direction it is facing.",
  34.         "Turning LEFT means changing the direction the bot is facing 90 degrees anti-clockwise from Bird's Eye.",
  35.         "Turning RIGHT means changing the direction the bot is facing 90 degrees clockwise from Bird's Eye.",
  36.         "Table on next page >>>",
  37.         "<PAGE>",
  38.         "| Function | MotorA | Motor B",
  39.         "-----------------------------",
  40.         "| HALT | 50 | 50",
  41.         "-----------------------------",
  42.         "| MOVE FORWARD | 100 | 100",
  43.         "-----------------------------",
  44.         "| MOVE BACKWARD | 0 | 0",
  45.         "-----------------------------",
  46.         "| TURN LEFT | 0 | 100",
  47.         "-----------------------------",
  48.         "| TURN RIGHT | 100 | 0",
  49.     }
  50. end
  51.  
  52. -- The function get_board() allows you to specify an 18x7 "ASCII art" grid to customize
  53. -- the layout of the board and the placement of the input and output terminals. Generally
  54. -- speaking, inputs are placed on the left of boards, while outputs are placed on the right.
  55. --
  56. -- For empty space, use the '.' character.
  57. -- For buildable board space, use the '#' character.
  58. -- For an input or output terminal, use characters '0' through '9' no more than once each.
  59. -- For the bottom-left corner of the radio, use the 'R' character.
  60. -- For the bottom-left corners of dials, use characters 'A', 'B', and 'C'.
  61. --
  62. function get_board()
  63.     return [[
  64.         #################.
  65.         0################2
  66.         ##################
  67.         ##################
  68.         ##################
  69.         1################3
  70.         ################..
  71.     ]]
  72. end
  73.  
  74. -- The function get_data() is called both to establish information about the puzzle (such as what
  75. -- the inputs and outputs are) and to generate the random test cases. Signal levels and XBus data
  76. -- should change from call to call, but information like the names and types of terminals should
  77. -- not change at all.
  78. --
  79. -- To create a standard input or output terminal, call create_terminal(). Valid terminal types are
  80. -- TYPE_SIMPLE, TYPE_XBUS, and TYPE_XBUS_NONBLOCKING. Valid terminal directions are DIR_INPUT and
  81. -- DIR_OUTPUT. Valid data for a simple I/O signal is an array of integers, 0 - 100 inclusive. Valid
  82. -- data for an XBus signal is an array of integer arrays, each with values -999 to 999 inclusive.
  83. --
  84. --       create_terminal(name, board_character, type, direction, data)
  85. --
  86. -- To create a radio (C2S-RF901), call create_radio(). You may only create one radio in each puzzle.
  87. -- Since radios are XBus-only, the only valid data for data_rx and data_tx are arrays of integer arrays,
  88. -- each with values -999 to 999 inclusive. You cannot customize the signal names for a radio.
  89. --
  90. -- By default the radio will be placed in the bottom-left corner of the screen. However, if you use an
  91. -- 'R' character in your board layout, the bottom-left corner of the radio will be placed there instead.
  92. --
  93. --       create_radio(data_rx, data_tx)
  94. --
  95. -- To create a dial (N4DL-1000), call create_dial(). You may create up to three dials in each puzzle.
  96. -- The names of dials should be kept short, as there is not much room to display them visually. A valid
  97. -- value is an integer between 0 and 99, inclusive.
  98. --
  99. -- By default dials will be placed in the bottom-left corner of the screen. However, if you use an
  100. -- 'A', 'B', or 'C' character in your board layout, the bottom-left corners of the first, second, and
  101. -- third dials will be placed there, respectively.
  102. --
  103. --       create_dial(name, value)
  104. --
  105. -- NOTE: To generate random values you should use math.random(). However, you SHOULD NOT seed
  106. --       the random number generator with a new seed value, as that is how the game ensures that
  107. --       the first test run is consistent for all users, and thus something that allows for the
  108. --       comparison of cycle scores.
  109. --
  110. -- NOTE: Fun fact! Arrays in Lua are implemented as tables (dictionaries) with integer keys that
  111. --       start at 1 by convention. Contrast this with nearly every other programming language, in
  112. --       which arrays start with an index of 0. Because of this, the 60 "time slices" that make
  113. --       up a test case are indexed from 1 to 60 inclusive.
  114. --
  115.  
  116. function Turn(n, left)
  117.     if left then
  118.         return (n-1)%4
  119.     end
  120.     return (n+1)%4
  121. end
  122.  
  123.  
  124.  
  125. function get_data()
  126.     inputA = {}
  127.     inputB = {}
  128.     X = {}
  129.     Y = {}
  130.     time = 1
  131.    
  132.     BiasMove = math.random(100, 200)
  133.     BiasTurn = math.random(50, 100)
  134.    
  135.     BiasLeft = math.random(0, 99)
  136.     BiasForward = math.random(0, 99)
  137.    
  138.     Direction = 0
  139.     x = 0
  140.     y = 0
  141.    
  142.    
  143.     while time <= 60 do
  144.         instr = math.random(0, 300)
  145.         move = false
  146.         if BiasMove >= instr then
  147.             move = true
  148.             if BiasForward > math.random(0, 99) then
  149.                 inputA[time] = 100
  150.                 inputB[time] = 100
  151.             else
  152.                 inputA[time] = 0
  153.                 inputB[time] = 0
  154.             end
  155.         elseif BiasMove + BiasTurn >= instr then
  156.             if BiasLeft > math.random(0, 99) then
  157.                 inputA[time] = 0
  158.                 inputB[time] = 100
  159.                 Direction = Turn(Direction, true)
  160.             else
  161.                 inputA[time] = 100
  162.                 inputB[time] = 0
  163.                 Direction = Turn(Direction, false)
  164.             end
  165.         else
  166.             inputA[time] = 50
  167.             inputB[time] = 50
  168.         end
  169.        
  170.         if move then
  171.             Ahead = (inputA[time]==100)
  172.             if Direction > 1 then
  173.                 Ahead = not Ahead
  174.             end
  175.             if Direction%2 == 0 then
  176.                 if Ahead then
  177.                     y = y + 1
  178.                 else
  179.                     y = y - 1
  180.                 end
  181.             else
  182.                 if Ahead then
  183.                     x = x + 1
  184.                 else
  185.                     x = x - 1
  186.                 end
  187.             end
  188.         end
  189.        
  190.         X[time] = {x}
  191.         Y[time] = {y}
  192.        
  193.         time = time + 1
  194.     end
  195.    
  196.     create_terminal("MotorA", "0", TYPE_SIMPLE, DIR_INPUT, inputA)
  197.     create_terminal("MotorB", "1", TYPE_SIMPLE, DIR_INPUT, inputB)
  198.     create_terminal("coord-X", "2", TYPE_XBUS, DIR_OUTPUT, X)
  199.     create_terminal("coord-Y", "3", TYPE_XBUS, DIR_OUTPUT, Y)
  200. end
Advertisement
Add Comment
Please, Sign In to add comment