Advertisement
JereTheJuggler

mastermind.lua

Nov 26th, 2021
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 KB | None | 0 0
  1. term.clear()
  2. local width,height = term.getSize()
  3. local code = {}
  4. local codeWins = {}
  5. for i=1,4 do
  6.   table.insert(codeWins,i,window.create(term.current(),4,6+3*(i-1),2,2))
  7. end
  8. local selectedColor = nil
  9. local guessNum
  10. local resultsWin = window.create(term.current(),8,3,2,1)
  11. local currentGuess = {}
  12. local guessWins = {}
  13. local endGame = false
  14. for i=1,4 do
  15.   table.insert(guessWins,i,window.create(term.current(),8,5+3*(i-1),2,2))
  16. end
  17. local boardStrings = {
  18. "   ________________________________________",
  19. "  /   |                                    \\ ",
  20. " |    |                                     |",
  21. "  \\___|____________________________________/"}
  22. term.setCursorPos(1,3)
  23. term.write(boardStrings[1])
  24. term.setCursorPos(1,4)
  25. term.write(boardStrings[2])
  26. for i=1,12 do
  27.   term.setCursorPos(1,4+i)
  28.   term.write(boardStrings[3])
  29. end
  30. term.setCursorPos(1,height-2)
  31. term.write(boardStrings[4])
  32. local pegColors = {colors.white,colors.green,colors.red,colors.orange,colors.blue,colors.yellow}
  33. local closeButton = window.create(term.current(),width-5,height,6,1)
  34. closeButton.setBackgroundColor(colors.red)
  35. closeButton.setTextColor(colors.black)
  36. closeButton.setCursorPos(1,1)
  37. closeButton.write(" CLOSE")
  38. local white,green,red,orange,blue,yellow
  39. local swatches = {white,green,red,orange,blue,yellow}
  40. for i=1,6 do
  41.   swatches[i] = window.create(term.current(),width-3,1+3*(i-1),2,2)
  42.   swatches[i].setTextColor(pegColors[i])
  43.   swatches[i].setCursorPos(1,1)
  44.   swatches[i].write("/\\")
  45.   swatches[i].setCursorPos(1,2)
  46.   swatches[i].write("\\/")
  47. end
  48. local submitButton = window.create(term.current(),1,height,6,1)
  49. submitButton.setBackgroundColor(colors.lime)
  50. submitButton.setTextColor(colors.black)
  51. submitButton.setCursorPos(1,1)
  52. submitButton.write("SUBMIT")
  53. term.setTextColor(colors.red)
  54. term.setCursorPos(1,1)
  55. term.write("Red = Correct color and location")
  56. term.setTextColor(colors.white)
  57. term.setCursorPos(1,2)
  58. term.write("White = Correct color but wrong place")
  59. term.setCursorPos(9,height)
  60. term.write("Mastermind - Made by JereTheJuggler")
  61. local buttons = {}
  62. for i=1,6 do
  63.   table.insert(buttons,i,swatches[i])
  64. end
  65. for i=1,4 do
  66.   table.insert(buttons,6+i,guessWins[i])
  67. end
  68. table.insert(buttons,11,closeButton)
  69. table.insert(buttons,12,submitButton)
  70. local function playSound (sound,freq)
  71.   local note = peripheral.find("note_block")
  72.   if note == nil then
  73.     return
  74.   end
  75.   note.playSound(sound,freq,1)
  76. end
  77. local function drawPeg (win,text,background)
  78.   if text ~= nil then
  79.     win.setTextColor(text)
  80.   end
  81.   if background ~= nil then
  82.     win.setBackgroundColor(background)
  83.   end
  84.   win.setCursorPos(1,1)
  85.   win.write("/\\")
  86.   win.setCursorPos(1,2)
  87.   win.write("\\/")
  88. end
  89. local function getClick ()
  90.   while true do
  91.     local event,b,x,y = os.pullEvent("mouse_click")
  92.     for i=1,table.getn(buttons) do
  93.       local xMin,yMin = buttons[i].getPosition()
  94.       local w,h = buttons[i].getSize()
  95.       local xMax = xMin + w - 1
  96.       local yMax = yMin + h - 1
  97.       if x <= xMax and x >= xMin and y <= yMax and y >= yMin then
  98.         return buttons[i]
  99.       end
  100.     end
  101.   end
  102. end
  103. local function repositionGuess (num)
  104.   for i=1,4 do
  105.     guessWins[i].reposition(9+3*(num-1),6+3*(i-1))
  106.     drawPeg(guessWins[i])
  107.   end
  108.   resultsWin.reposition(9+3*(num-1),4)
  109.   resultsWin.clear()
  110. end
  111. local function revealCode ()
  112.   for i=1,4 do
  113.     drawPeg(codeWins[i])
  114.   end
  115. end
  116. local function newGame ()
  117.   code={}
  118.   for i=1,4 do
  119.     codeWins[i].clear()
  120.     table.insert(code,i,1+math.floor(math.random()*6))
  121.     codeWins[i].setTextColor(pegColors[code[i]])
  122.     guessWins[i].setTextColor(colors.gray)
  123.   end
  124.   guessNum = 1
  125.   for i=12,2,-1 do
  126.     repositionGuess(i)
  127.   end
  128.   for i=1,4 do
  129.     guessWins[i].setTextColor(colors.lightGray)
  130.   end
  131.   repositionGuess(1)
  132.   currentGuess = {0,0,0,0}
  133. end
  134. local function analyzeGuess()
  135.   local usedColors = {false,false,false,false}
  136.   local matchingColors = 0
  137.   for g=1,4 do
  138.     for c=1,4 do
  139.       if not usedColors[c] then
  140.         if currentGuess[g] == code[c] then
  141.           usedColors[c] = true
  142.           matchingColors = matchingColors + 1
  143.           break
  144.         end
  145.       end
  146.     end
  147.   end
  148.   local correctPegs = 0
  149.   for i=1,4 do
  150.     if currentGuess[i] == code[i] then
  151.       correctPegs = correctPegs + 1
  152.       matchingColors = matchingColors - 1
  153.     end
  154.   end
  155.   resultsWin.setTextColor(colors.red)
  156.   resultsWin.setCursorPos(1,1)
  157.   resultsWin.write(correctPegs)
  158.   resultsWin.setTextColor(colors.white)
  159.   resultsWin.write(matchingColors)
  160.   if correctPegs == 4 then
  161.     revealCode()
  162.     endGame = true
  163.   else
  164.     guessNum = guessNum + 1
  165.     if guessNum ~= 13 then
  166.       for i=1,4 do
  167.         guessWins[i].setTextColor(colors.lightGray)
  168.         currentGuess[i]=0
  169.       end
  170.       repositionGuess(guessNum)
  171.     else
  172.       revealCode()
  173.       endGame = true
  174.     end
  175.   end
  176. end
  177.  
  178. local function playGame ()
  179.   newGame()
  180.   endGame = false
  181.   while not endGame do
  182.     local clicked = getClick()
  183.     if clicked == closeButton then
  184.       return "quit"
  185.     elseif clicked == submitButton then
  186.       local validGuess = true
  187.       for i=1,4 do
  188.         if currentGuess[i] == 0 then
  189.           validGuess = false
  190.         end
  191.       end
  192.       if validGuess then
  193.         playSound("note.hat",.9)
  194.         analyzeGuess()
  195.       end
  196.     else
  197.       for i=1,6 do
  198.         if clicked == swatches[i] then
  199.           if selectedColor == i then
  200.             break
  201.           end
  202.           if selectedColor ~= nil then
  203.             drawPeg(swatches[selectedColor],nil,colors.black)
  204.           end
  205.           selectedColor = i
  206.           drawPeg(swatches[i],nil,colors.gray)
  207.           playSound("random.click",.9)
  208.           break
  209.         end
  210.       end
  211.       if selectedColor ~= nil then
  212.         for i=1,4 do
  213.           if clicked == guessWins[i] then
  214.             if currentGuess[i] == selectedColor then
  215.               break
  216.             end
  217.             currentGuess[i] = selectedColor
  218.             drawPeg(guessWins[i],pegColors[selectedColor])
  219.             playSound("random.click",.9)
  220.             break
  221.           end
  222.         end
  223.       end
  224.     end
  225.   end
  226.   if guessNum == 13 then
  227.     return "lost"
  228.   else
  229.     return "won"
  230.   end
  231. end
  232.  
  233. while true do
  234.   local status = playGame()
  235.   if status == "quit" then
  236.     break
  237.   elseif status == "lost" then
  238.     playSound("note.bass",.9)
  239.     playSound("note.bass",1.32)
  240.   elseif status == "won" then
  241.     playSound("random.levelup",1)
  242.   end
  243.   submitButton.clear()
  244.   submitButton.setCursorPos(2,1)
  245.   submitButton.write("PLAY")
  246.   while true do
  247.     local clicked = getClick()
  248.     if clicked == submitButton then
  249.       submitButton.setCursorPos(1,1)
  250.       submitButton.write("SUBMIT")
  251.       break
  252.     elseif clicked == closeButton then
  253.       status = "quit"
  254.       break
  255.     end
  256.   end
  257.   if status == "quit" then
  258.     break
  259.   end
  260. end
  261.  
  262. term.setBackgroundColor(colors.black)
  263. term.clear()
  264. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement