Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- local width,height = term.getSize()
- local code = {}
- local codeWins = {}
- for i=1,4 do
- table.insert(codeWins,i,window.create(term.current(),4,6+3*(i-1),2,2))
- end
- local selectedColor = nil
- local guessNum
- local resultsWin = window.create(term.current(),8,3,2,1)
- local currentGuess = {}
- local guessWins = {}
- local endGame = false
- for i=1,4 do
- table.insert(guessWins,i,window.create(term.current(),8,5+3*(i-1),2,2))
- end
- local boardStrings = {
- " ________________________________________",
- " / | \\ ",
- " | | |",
- " \\___|____________________________________/"}
- term.setCursorPos(1,3)
- term.write(boardStrings[1])
- term.setCursorPos(1,4)
- term.write(boardStrings[2])
- for i=1,12 do
- term.setCursorPos(1,4+i)
- term.write(boardStrings[3])
- end
- term.setCursorPos(1,height-2)
- term.write(boardStrings[4])
- local pegColors = {colors.white,colors.green,colors.red,colors.orange,colors.blue,colors.yellow}
- local closeButton = window.create(term.current(),width-5,height,6,1)
- closeButton.setBackgroundColor(colors.red)
- closeButton.setTextColor(colors.black)
- closeButton.setCursorPos(1,1)
- closeButton.write(" CLOSE")
- local white,green,red,orange,blue,yellow
- local swatches = {white,green,red,orange,blue,yellow}
- for i=1,6 do
- swatches[i] = window.create(term.current(),width-3,1+3*(i-1),2,2)
- swatches[i].setTextColor(pegColors[i])
- swatches[i].setCursorPos(1,1)
- swatches[i].write("/\\")
- swatches[i].setCursorPos(1,2)
- swatches[i].write("\\/")
- end
- local submitButton = window.create(term.current(),1,height,6,1)
- submitButton.setBackgroundColor(colors.lime)
- submitButton.setTextColor(colors.black)
- submitButton.setCursorPos(1,1)
- submitButton.write("SUBMIT")
- term.setTextColor(colors.red)
- term.setCursorPos(1,1)
- term.write("Red = Correct color and location")
- term.setTextColor(colors.white)
- term.setCursorPos(1,2)
- term.write("White = Correct color but wrong place")
- term.setCursorPos(9,height)
- term.write("Mastermind - Made by JereTheJuggler")
- local buttons = {}
- for i=1,6 do
- table.insert(buttons,i,swatches[i])
- end
- for i=1,4 do
- table.insert(buttons,6+i,guessWins[i])
- end
- table.insert(buttons,11,closeButton)
- table.insert(buttons,12,submitButton)
- local function playSound (sound,freq)
- local note = peripheral.find("note_block")
- if note == nil then
- return
- end
- note.playSound(sound,freq,1)
- end
- local function drawPeg (win,text,background)
- if text ~= nil then
- win.setTextColor(text)
- end
- if background ~= nil then
- win.setBackgroundColor(background)
- end
- win.setCursorPos(1,1)
- win.write("/\\")
- win.setCursorPos(1,2)
- win.write("\\/")
- end
- local function getClick ()
- while true do
- local event,b,x,y = os.pullEvent("mouse_click")
- for i=1,table.getn(buttons) do
- local xMin,yMin = buttons[i].getPosition()
- local w,h = buttons[i].getSize()
- local xMax = xMin + w - 1
- local yMax = yMin + h - 1
- if x <= xMax and x >= xMin and y <= yMax and y >= yMin then
- return buttons[i]
- end
- end
- end
- end
- local function repositionGuess (num)
- for i=1,4 do
- guessWins[i].reposition(9+3*(num-1),6+3*(i-1))
- drawPeg(guessWins[i])
- end
- resultsWin.reposition(9+3*(num-1),4)
- resultsWin.clear()
- end
- local function revealCode ()
- for i=1,4 do
- drawPeg(codeWins[i])
- end
- end
- local function newGame ()
- code={}
- for i=1,4 do
- codeWins[i].clear()
- table.insert(code,i,1+math.floor(math.random()*6))
- codeWins[i].setTextColor(pegColors[code[i]])
- guessWins[i].setTextColor(colors.gray)
- end
- guessNum = 1
- for i=12,2,-1 do
- repositionGuess(i)
- end
- for i=1,4 do
- guessWins[i].setTextColor(colors.lightGray)
- end
- repositionGuess(1)
- currentGuess = {0,0,0,0}
- end
- local function analyzeGuess()
- local usedColors = {false,false,false,false}
- local matchingColors = 0
- for g=1,4 do
- for c=1,4 do
- if not usedColors[c] then
- if currentGuess[g] == code[c] then
- usedColors[c] = true
- matchingColors = matchingColors + 1
- break
- end
- end
- end
- end
- local correctPegs = 0
- for i=1,4 do
- if currentGuess[i] == code[i] then
- correctPegs = correctPegs + 1
- matchingColors = matchingColors - 1
- end
- end
- resultsWin.setTextColor(colors.red)
- resultsWin.setCursorPos(1,1)
- resultsWin.write(correctPegs)
- resultsWin.setTextColor(colors.white)
- resultsWin.write(matchingColors)
- if correctPegs == 4 then
- revealCode()
- endGame = true
- else
- guessNum = guessNum + 1
- if guessNum ~= 13 then
- for i=1,4 do
- guessWins[i].setTextColor(colors.lightGray)
- currentGuess[i]=0
- end
- repositionGuess(guessNum)
- else
- revealCode()
- endGame = true
- end
- end
- end
- local function playGame ()
- newGame()
- endGame = false
- while not endGame do
- local clicked = getClick()
- if clicked == closeButton then
- return "quit"
- elseif clicked == submitButton then
- local validGuess = true
- for i=1,4 do
- if currentGuess[i] == 0 then
- validGuess = false
- end
- end
- if validGuess then
- playSound("note.hat",.9)
- analyzeGuess()
- end
- else
- for i=1,6 do
- if clicked == swatches[i] then
- if selectedColor == i then
- break
- end
- if selectedColor ~= nil then
- drawPeg(swatches[selectedColor],nil,colors.black)
- end
- selectedColor = i
- drawPeg(swatches[i],nil,colors.gray)
- playSound("random.click",.9)
- break
- end
- end
- if selectedColor ~= nil then
- for i=1,4 do
- if clicked == guessWins[i] then
- if currentGuess[i] == selectedColor then
- break
- end
- currentGuess[i] = selectedColor
- drawPeg(guessWins[i],pegColors[selectedColor])
- playSound("random.click",.9)
- break
- end
- end
- end
- end
- end
- if guessNum == 13 then
- return "lost"
- else
- return "won"
- end
- end
- while true do
- local status = playGame()
- if status == "quit" then
- break
- elseif status == "lost" then
- playSound("note.bass",.9)
- playSound("note.bass",1.32)
- elseif status == "won" then
- playSound("random.levelup",1)
- end
- submitButton.clear()
- submitButton.setCursorPos(2,1)
- submitButton.write("PLAY")
- while true do
- local clicked = getClick()
- if clicked == submitButton then
- submitButton.setCursorPos(1,1)
- submitButton.write("SUBMIT")
- break
- elseif clicked == closeButton then
- status = "quit"
- break
- end
- end
- if status == "quit" then
- break
- end
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement