Advertisement
Xelostar

MasterMind

Jul 12th, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1. --[[
  2.     This Program is made by Xelostar.
  3.     When showing it, please give credit.
  4.  
  5.     Link to my channel:
  6.     https://www.youtube.com/channel/UCDE2STpSWJrIUyKtiYGeWxw
  7. ]]--
  8.  
  9. code = {}
  10.  
  11. tries = {}
  12. points = {}
  13.  
  14. function randomColor()
  15.     local color = math.random(1, 8)
  16.    
  17.     if (color == 1) then
  18.         color = colors.red
  19.     elseif (color == 2) then
  20.         color = colors.orange
  21.     elseif (color == 3) then
  22.         color = colors.yellow
  23.     elseif (color == 4) then
  24.         color = colors.lime
  25.     elseif (color == 5) then
  26.         color = colors.green
  27.     elseif (color == 6) then
  28.         color = colors.lightBlue
  29.     elseif (color == 7) then
  30.         color = colors.blue
  31.     elseif (color == 8) then
  32.         color = colors.purple
  33.     end
  34.  
  35.     return color
  36. end
  37.  
  38. function generateRandomCode()
  39.     while table.getn(code) < 4 do
  40.         local color = randomColor()
  41.  
  42.         local newColor = true
  43.         for k, v in pairs(code) do
  44.             if (v == color) then
  45.                 newColor = false
  46.             end
  47.         end
  48.  
  49.         if (newColor == true) then
  50.             table.insert(code, color)
  51.         end
  52.     end
  53. end
  54.  
  55. function drawScreen()
  56.     for tryNr, try in pairs(tries) do
  57.         for k, v in pairs(try) do
  58.             paintutils.drawFilledBox(k * 4, tryNr * 2, k * 4 + 3, tryNr * 2 + 1, v)
  59.         end
  60.     end
  61.  
  62.     for k, v in pairs(points) do
  63.         for k2, v2 in pairs(v) do
  64.             paintutils.drawFilledBox(k2 * 4 + 20, k * 2, k2 * 4 + 3 + 20, k * 2 + 1, v2)
  65.         end
  66.     end
  67.  
  68.     paintutils.drawFilledBox(44, 2, 48, 3, colors.red)
  69.     paintutils.drawFilledBox(44, 4, 48, 5, colors.orange)
  70.     paintutils.drawFilledBox(44, 6, 48, 7, colors.yellow)
  71.     paintutils.drawFilledBox(44, 8, 48, 9, colors.lime)
  72.     paintutils.drawFilledBox(44, 10, 48, 11, colors.green)
  73.     paintutils.drawFilledBox(44, 12, 48, 13, colors.lightBlue)
  74.     paintutils.drawFilledBox(44, 14, 48, 15, colors.blue)
  75.     paintutils.drawFilledBox(44, 16, 48, 17, colors.purple)
  76. end
  77.  
  78. function getInput()
  79.     try = {}
  80.  
  81.     while true do
  82.         local Event, button, X, Y = os.pullEventRaw()
  83.  
  84.         if (Event == "mouse_click") and (button == 1) then
  85.             if (X >= 44 and X <= 48) then
  86.                 if (Y == 2 or Y == 3) then
  87.                     table.insert(try, colors.red)
  88.                 elseif (Y == 4 or Y == 5) then
  89.                     table.insert(try, colors.orange)
  90.                 elseif (Y == 6 or Y == 7) then
  91.                     table.insert(try, colors.yellow)
  92.                 elseif (Y == 8 or Y == 9) then
  93.                     table.insert(try, colors.lime)
  94.                 elseif (Y == 10 or Y == 11) then
  95.                     table.insert(try, colors.green)
  96.                 elseif (Y == 12 or Y == 13) then
  97.                     table.insert(try, colors.lightBlue)
  98.                 elseif (Y == 14 or Y == 15) then
  99.                     table.insert(try, colors.blue)
  100.                 elseif (Y == 16 or Y == 17) then
  101.                     table.insert(try, colors.purple)
  102.                 end
  103.             end
  104.         end
  105.  
  106.         if (table.getn(try) >= 4) then
  107.             for k, v in pairs(try) do
  108.                 for k2, v2 in pairs(try) do
  109.                     if (v == v2 and k ~= k2) then
  110.                         try = {}
  111.                     end
  112.                 end
  113.             end
  114.             if (table.getn(try) >= 4) then
  115.                 break
  116.             end
  117.         end
  118.     end
  119.     table.insert(tries, try)
  120. end
  121.  
  122. function check()
  123.     try = tries[table.getn(tries)]
  124.     score = {}
  125.  
  126.     for k, v in pairs(try) do
  127.         for k2, v2 in pairs(code) do
  128.             if (v == v2 and k == k2) then
  129.                 table.insert(score, 1)
  130.             elseif (v == v2) then
  131.                 table.insert(score, 0)
  132.             end
  133.         end
  134.     end
  135.  
  136.     scoreS = {}
  137.  
  138.     for k, v in pairs(score) do
  139.         if (v == 1) then
  140.             table.insert(scoreS, colors.black)
  141.         end
  142.     end
  143.  
  144.     for k, v in pairs(score) do
  145.         if (v == 0) then
  146.             table.insert(scoreS, colors.white)
  147.         end
  148.     end
  149.  
  150.     table.insert(points, scoreS)
  151. end
  152.  
  153. function testForGuess()
  154.     if (points[table.getn(points)][1] == colors.black) and (points[table.getn(points)][2] == colors.black) and (points[table.getn(points)][3] == colors.black) and (points[table.getn(points)][4] == colors.black) then
  155.         os.reboot()
  156.     end
  157. end
  158.  
  159. generateRandomCode()
  160.  
  161. term.setBackgroundColor(colors.gray)
  162. term.clear()
  163.  
  164. while true do
  165.     drawScreen()
  166.     getInput()
  167.     check()
  168.     testForGuess()
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement