csaboka

Untitled

Jun 24th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.61 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 "Cribbage Fifteens"
  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.         "*Cards* is an XBus input from a playing card reader.",
  25.         "*Score* is an XBus output connected to a display.",
  26.         "When a hand of 5 cards are received from *Cards*, calculate the points according to Cribbage scoring for fifteens and send it to *Score*.",
  27.         "<PAGE>",
  28.         "*Cards* consists of hands of 5 cards where each card is represented by one XBus packet.",
  29.         "When a Cribbage hand is scored for fifteens, face cards have a scoring value of 10, and aces have a scoring value of 1.",
  30.         "Each subset of cards whose scoring values sum to exactly 15 is worth 2 points.",
  31.         "| XBus packet   | 1   | 2 - 10 | 11   | 12    | 13   ",
  32.         "---------------------------------------------------",
  33.         "| Card          | Ace | 2 - 10 | Jack | Queen | King ",
  34.         "| Scoring value | 1   | 2 - 10 | 10   | 10    | 10   "
  35.     }
  36. end
  37.  
  38. -- The function get_board() allows you to specify an 18x7 "ASCII art" grid to customize
  39. -- the layout of the board and the placement of the input and output terminals. Generally
  40. -- speaking, inputs are placed on the left of boards, while outputs are placed on the right.
  41. --
  42. -- For empty space, use the '.' character.
  43. -- For buildable board space, use the '#' character.
  44. -- For an input or output terminal, use characters '0' through '9' no more than once each.
  45. -- For the bottom-left corner of the radio, use the 'R' character.
  46. -- For the bottom-left corners of dials, use characters 'A', 'B', and 'C'.
  47. --
  48. function get_board()
  49.     return [[
  50.         .################1
  51.         ##################
  52.         ##################
  53.         ##################
  54.         ##################
  55.         ##################
  56.         0################.
  57.     ]]
  58. end
  59.  
  60. -- The function get_data() is called both to establish information about the puzzle (such as what
  61. -- the inputs and outputs are) and to generate the random test cases. Signal levels and XBus data
  62. -- should change from call to call, but information like the names and types of terminals should
  63. -- not change at all.
  64. --
  65. -- To create a standard input or output terminal, call create_terminal(). Valid terminal types are
  66. -- TYPE_SIMPLE, TYPE_XBUS, and TYPE_XBUS_NONBLOCKING. Valid terminal directions are DIR_INPUT and
  67. -- DIR_OUTPUT. Valid data for a simple I/O signal is an array of integers, 0 - 100 inclusive. Valid
  68. -- data for an XBus signal is an array of integer arrays, each with values -999 to 999 inclusive.
  69. --
  70. --       create_terminal(name, board_character, type, direction, data)
  71. --
  72. -- To create a radio (C2S-RF901), call create_radio(). You may only create one radio in each puzzle.
  73. -- Since radios are XBus-only, the only valid data for data_rx and data_tx are arrays of integer arrays,
  74. -- each with values -999 to 999 inclusive. You cannot customize the signal names for a radio.
  75. --
  76. -- By default the radio will be placed in the bottom-left corner of the screen. However, if you use an
  77. -- 'R' character in your board layout, the bottom-left corner of the radio will be placed there instead.
  78. --
  79. --       create_radio(data_rx, data_tx)
  80. --
  81. -- To create a dial (N4DL-1000), call create_dial(). You may create up to three dials in each puzzle.
  82. -- The names of dials should be kept short, as there is not much room to display them visually. A valid
  83. -- value is an integer between 0 and 99, inclusive.
  84. --
  85. -- By default dials will be placed in the bottom-left corner of the screen. However, if you use an
  86. -- 'A', 'B', or 'C' character in your board layout, the bottom-left corners of the first, second, and
  87. -- third dials will be placed there, respectively.
  88. --
  89. --       create_dial(name, value)
  90. --
  91. -- NOTE: To generate random values you should use math.random(). However, you SHOULD NOT seed
  92. --       the random number generator with a new seed value, as that is how the game ensures that
  93. --       the first test run is consistent for all users, and thus something that allows for the
  94. --       comparison of cycle scores.
  95. --
  96. -- NOTE: Fun fact! Arrays in Lua are implemented as tables (dictionaries) with integer keys that
  97. --       start at 1 by convention. Contrast this with nearly every other programming language, in
  98. --       which arrays start with an index of 0. Because of this, the 60 "time slices" that make
  99. --       up a test case are indexed from 1 to 60 inclusive.
  100. --
  101.  
  102. function fifteens(hand, start_card_idx, total)
  103.     score = 0
  104.     for card_idx = start_card_idx, 5 do
  105.         card_val = hand[card_idx]
  106.         if card_val > 9 then
  107.             card_val = 10
  108.         end
  109.        
  110.         subtotal = total + card_val
  111.         if subtotal == 15 then
  112.             score = score + 2
  113.         else
  114.             if subtotal < 15 then
  115.                 score = score + fifteens(hand, card_idx + 1, subtotal)
  116.             end
  117.         end
  118.     end
  119.     return score
  120.  end
  121.  
  122.  
  123. function get_data()
  124.     cards = {}
  125.     scores = {}
  126.     time = 1
  127.  
  128.     while time <= 60 do
  129.         time = time + math.random(1, 2)
  130.  
  131.         hand = {}
  132.         for card_idx = 1, 5 do
  133.             hand[card_idx] = math.random(1, 13)
  134.         end
  135.         while ((hand[1] == hand[5]) and
  136.                (hand[1] == hand[4]) and
  137.                (hand[1] == hand[3]) and
  138.                (hand[1] == hand[2])) do
  139.             hand[5] = math.random(1, 13)
  140.         end
  141.         cards[time] =  hand
  142.  
  143.         scores[time] = { fifteens(hand, 1, 0) }
  144.  
  145.         time = time + 5
  146.     end
  147.  
  148.     create_terminal("Cards", "0", TYPE_XBUS, DIR_INPUT, cards)
  149.     create_terminal("Score", "1", TYPE_XBUS, DIR_OUTPUT, scores)
  150. end
Advertisement
Add Comment
Please, Sign In to add comment