Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cards = {}
- backColor = colors.blue
- tableColor = colors.lime
- darkTableColor = colors.green
- selectColor = colors.lightBlue
- tableText = colors.white
- buttonColor = colors.red
- buttonText = colors.black
- suits = {"D","C","S","H"}
- displayValues = {"A","2","3","4","5","6","7","8","9","T","J","Q","K"}
- sounds = {
- drawCard="dig.cloth",
- pickCard="random.click",
- deselect="note.hat",
- moveCard="step.cloth"
- }
- function makeDeck ()
- playsound(sounds.moveCard)
- cards = {}
- for s=1,4 do
- for v=1,13 do
- c=nil
- if s == 1 or s == 4 then
- c = "R"
- else
- c = "B"
- end
- if s==1 and v==1 then
- table.insert(cards,{suit=s,value=v,color=c,selected=false,faceUp=false})
- else
- table.insert(cards,1+math.floor(math.random()*(table.getn(cards)+1)),{suit=s,value=v,color=c,selected=false,faceUp=false})
- end
- end
- end
- end
- tableau = {}
- drawPile = {}
- discardPile = {}
- width,height = term.getSize()
- function dealCards ()
- discardPile["cards"] = {}
- drawPile["cards"] = {}
- currentCard = 1
- tableau = {}
- for c=1,7 do
- currentColumn = {}
- for r=1,c do
- table.insert(currentColumn,r,cards[currentCard])
- currentCard = currentCard + 1
- end
- table.insert(tableau,c,currentColumn)
- tableau[c][table.getn(tableau[c])]["faceUp"] = true
- end
- for c=currentCard,52 do
- table.insert(drawPile.cards,cards[c])
- end
- end
- tabWindows = {}
- drawPile = {cards=nil,win=nil}
- discardPile = {cards=nil,win=window.create(term.current(),5,2,2,height-9)}
- foundations = {}
- function drawTable ()
- term.setBackgroundColor(tableColor)
- term.setTextColor(tableText)
- term.clear()
- term.setCursorPos(1,1)
- term.write("|Stock| |------Tableau-------| |Foundations|")
- renderCloseButton()
- renderNewGame()
- text = "Made by JereTheJuggler"
- term.setTextColor(colors.black)
- term.setCursorPos(width-string.len(text)+1,height)
- term.write(text)
- text = "Solitaire"
- term.setCursorPos(width-string.len(text)+1,height-1)
- term.write(text)
- end
- function buildWindows ()
- tabWindows = {}
- for i=1,7 do
- table.insert(tabWindows,window.create(term.current(),12+3*(i-1),2,2,height-2))
- end
- foundations = {}
- for f=1,4 do
- table.insert(foundations,f,{suit=nil,value=nil,win=window.create(term.current(),37+3*(f-1),2,2,height-3),selected=false})
- end
- drawPile["cards"] = {}
- drawPile["win"] = window.create(term.current(),2,2,2,height-9)
- end
- buildWindows()
- closeButton = window.create(term.current(),2,height-1,9,1)
- function renderCloseButton ()
- closeButton.setBackgroundColor(buttonColor)
- closeButton.setTextColor(buttonText)
- closeButton.clear()
- closeButton.setCursorPos(2,1)
- closeButton.write(" Close")
- end
- newGame = window.create(term.current(),2,height-3,9,1)
- function renderNewGame ()
- newGame.setBackgroundColor(buttonColor)
- newGame.setTextColor(buttonText)
- newGame.clear()
- newGame.setCursorPos(2,1)
- newGame.write("Restart")
- end
- function renderAllCards ()
- renderTableau()
- renderFoundations()
- renderDrawPile()
- renderDiscardPile()
- end
- function renderTableau (column)
- startIndex,endIndex = -1,-1
- if column == nil then
- startIndex,endIndex = 1,7
- else
- startIndex,endIndex = column,column
- end
- for s=startIndex,endIndex do
- tabWindows[s].setBackgroundColor(tableColor)
- tabWindows[s].clear()
- if table.getn(tableau[s]) == 0 then
- tabWindows[s].setCursorPos(1,1)
- renderCard(tabWindows[s],nil)
- else
- for c=1,table.getn(tableau[s]) do
- tabWindows[s].setCursorPos(1,c)
- renderCard(tabWindows[s],tableau[s][c])
- end
- end
- end
- end
- function renderFoundations (foundation)
- startIndex = -1
- endIndex = -1
- if foundation == nil then
- startIndex=1
- endIndex=4
- else
- startIndex=foundation
- endIndex=foundation
- end
- for f=startIndex,endIndex do
- foundations[f].win.setBackgroundColor(tableColor)
- foundations[f].win.clear()
- if foundations[f].suit == nil then
- foundations[f].win.setCursorPos(1,1)
- renderCard(foundations[f].win,nil)
- else
- foundations[f].win.setCursorPos(1,1)
- if foundations[f].suit == 1 or foundations[f].suit == 4 then
- foundations[f].win.setTextColor(colors.red)
- else
- foundations[f].win.setTextColor(colors.black)
- end
- foundations[f].win.setBackgroundColor(colors.white)
- for c=1,foundations[f].value do
- foundations[f].win.setCursorPos(1,c)
- if foundations[f].selected and c == foundations[f].value then
- foundations[f].win.setBackgroundColor(selectColor)
- end
- foundations[f].win.write(displayValues[c]..suits[foundations[f].suit])
- end
- end
- end
- end
- function renderDrawPile ()
- drawPile.win.setCursorPos(1,1)
- if table.getn(drawPile.cards) == 0 then
- drawPile.win.setBackgroundColor(darkTableColor)
- drawPile.win.setTextColor(selectColor)
- drawPile.win.write("()")
- else
- renderCard(drawPile.win,drawPile.cards[table.getn(drawPile.cards)])
- end
- end
- function renderDiscardPile ()
- discardPile.win.setCursorPos(1,1)
- if table.getn(discardPile.cards) == 0 then
- renderCard(discardPile.win,nil)
- else
- renderCard(discardPile.win,discardPile.cards[1])
- end
- end
- function printerror (text,subtext)
- p=peripheral.find("printer")
- p.newPage()
- p.setCursorPos(1,1)
- p.write("ERROR: "..text)
- if type(subtext) == "table" then
- for l=1,table.getn(subtext) do
- p.setCursorPos(1,1+l)
- p.write(tostring(subtext[l]))
- end
- else
- p.setCursorPos(1,2)
- p.write(subtext)
- end
- p.endPage()
- kill()
- end
- function drawThree ()
- for c=1,3 do
- if table.getn(drawPile.cards) > 0 then
- table.insert(discardPile.cards,1,drawPile.cards[table.getn(drawPile.cards)])
- table.remove(drawPile.cards,table.getn(drawPile.cards))
- discardPile.cards[1]["faceUp"] = true
- renderDiscardPile()
- if table.getn(drawPile.cards) == 0 then
- renderDrawPile()
- end
- playsound(sounds.drawCard)
- sleep(.25)
- end
- end
- end
- function restockDrawPile ()
- for c=1,table.getn(discardPile.cards) do
- table.insert(drawPile.cards,c,discardPile.cards[c])
- drawPile.cards[c]["faceUp"] = false
- end
- discardPile["cards"] = {}
- renderDrawPile()
- renderDiscardPile()
- playsound(sounds.drawCard)
- end
- function playsound (sound,freq,vol)
- note = peripheral.find("note_block")
- if note == nil then
- return
- end
- if freq == nil then
- freq = .9
- end
- if vol == nil then
- vol = 1.5
- end
- note.playSound(sound,freq,vol)
- end
- function renderCard (win,card)
- --printerror("test",{"value: "..card.value,"suit: "..card.suit,"color: "..card.color})
- if card == nil then
- win.setBackgroundColor(darkTableColor)
- win.write(" ")
- else
- if card.faceUp then
- if card.selected then
- win.setBackgroundColor(selectColor)
- else
- win.setBackgroundColor(colors.white)
- end
- if card.color == "R" then
- win.setTextColor(colors.red)
- else
- win.setTextColor(colors.black)
- end
- win.write(displayValues[card.value]..suits[card.suit])
- else
- win.setBackgroundColor(backColor)
- win.setTextColor(colors.white)
- win.write("XX")
- end
- end
- end
- function clearFoundations ()
- for f=1,4 do
- foundations[f]["value"] = nil
- foundations[f]["suit"] = nil
- foundations[f]["selected"] = false
- end
- renderFoundations()
- end
- selectedColumn = -1
- selectedCards = {}
- function playGame ()
- makeDeck()
- dealCards()
- drawTable()
- selectedCards = {}
- renderAllCards()
- selectedColumn = 0
- clearFoundations()
- continue = true
- while continue do
- for c=1,7 do
- if table.getn(tableau[c]) > 0 then
- if not tableau[c][table.getn(tableau[c])].faceUp then
- tableau[c][table.getn(tableau[c])]["faceUp"] = true
- renderTableau(c)
- end
- end
- end
- --renderTableau()
- event,arg1,arg2,arg3,arg4 = os.pullEvent()
- if event == "mouse_click" then
- xPos = arg2
- yPos = arg3
- if isWithinBounds(getBounds(drawPile.win)) then
- if table.getn(selectedCards) ~= 0 then
- if selectedColumn == "discardPile" then
- deselect(discardPile.cards)
- elseif selectedColumn < 0 then
- deselect(selectedColumn)
- else
- deselect(tableau[selectedColumn])
- end
- end
- if table.getn(drawPile.cards) > 0 then
- drawThree()
- elseif table.getn(discardPile.cards) > 0 then
- restockDrawPile()
- end
- elseif isWithinBounds(getBounds(discardPile.win)) then
- if arg1 == 2 and table.getn(discardPile.cards) > 0 then
- tryToMoveToFoundation("discardPile")
- elseif table.getn(selectedCards) == 0 and table.getn(discardPile.cards) > 0 then
- selectedColumn = "discardPile"
- table.insert(selectedCards,discardPile.cards[1])
- discardPile.cards[1]["selected"] = true
- playsound(sounds.pickCard)
- renderDiscardPile()
- elseif selectedColumn == "discardPile" and table.getn(discardPile.cards) > 0 then
- selectedColumn = 0
- selectedCards = {}
- discardPile.cards[1]["selected"] = false
- playsound(sounds.deselect)
- renderDiscardPile()
- end
- elseif isWithinBounds(getBounds(closeButton)) then
- continue = false
- restart = false
- elseif isWithinBounds(getBounds(newGame)) then
- continue = false
- restart = true
- else
- for w=1,7 do
- if isWithinBounds(getBounds(tabWindows[w])) then
- if selectedColumn ~= "discardPile" and selectedColumn < 0 then
- if foundations[selectedColumn*-1].suit == 1 or foundations[selectedColumn*-1].suit == 4 then
- c = "R"
- else
- c = "B"
- end
- if foundations[selectedColumn*-1].value == tableau[w][table.getn(tableau[w])].value-1 and tableau[w][table.getn(tableau[w])].color ~= c then
- table.insert(tableau[w],table.getn(tableau[w])+1,{faceUp=true,color=c,value=foundations[selectedColumn*-1].value,suit=foundations[selectedColumn*-1].suit,selected=false})
- foundations[selectedColumn*-1]["value"] = foundations[selectedColumn*-1].value - 1
- if foundations[selectedColumn*-1].value == 0 then
- foundations[selectedColumn*-1]["suit"] = nil
- foundations[selectedColumn*-1]["value"] = nil
- end
- renderTableau(w)
- end
- foundations[selectedColumn*-1]["selected"] = false
- renderFoundations(selectedColumn*-1)
- selectedColumn = 0
- selectedCards = {}
- --renderFoundations(selectedColumn*-1)
- elseif table.getn(selectedCards) == 0 then
- row=yPos-1
- column = w
- if table.getn(tableau[column]) ~= 0 then
- if arg1==2 then
- selectedColumn = w
- tryToMoveToFoundation(w)
- renderTableau(w)
- elseif table.getn(tableau[column]) >= row then
- if tableau[column][row].faceUp then
- playsound(sounds.pickCard)
- selectedCards = {}
- for k=row,table.getn(tableau[column]) do
- table.insert(selectedCards,k-row+1,tableau[column][k])
- tableau[column][k]["selected"] = true
- end
- selectedColumn = w
- renderTableau(w)
- end
- elseif arg1 == 1 then
- m=1
- while not tableau[column][m].faceUp do
- m = m + 1
- end
- row = m
- playsound(sounds.pickCard)
- selectedCards = {}
- for k=row,table.getn(tableau[column]) do
- table.insert(selectedCards,k-row+1,tableau[column][k])
- tableau[column][k]["selected"] = true
- end
- selectedColumn = w
- renderTableau(w)
- end
- end
- else
- if w == selectedColumn then
- playsound(sounds.deselect)
- deselect(tableau[selectedColumn])
- renderTableau()
- else
- previousLocation = nil
- if selectedColumn == "discardPile" then
- previousLocation = discardPile.cards
- else
- previousLocation = tableau[selectedColumn]
- end
- if table.getn(tableau[w]) == 0 then
- if selectedCards[1].value == 13 then
- removeSelectedCardsFrom(previousLocation)
- if selectedColumn == "discardPile" then
- renderDiscardPile()
- end
- moveCardsTo(tableau[w])
- renderTableau(w)
- else
- playsound(sounds.deselect)
- deselect(previousLocation)
- end
- else
- topCard = tableau[w][table.getn(tableau[w])]
- if selectedCards[1].color ~= topCard.color and selectedCards[1].value == topCard.value-1 then
- removeSelectedCardsFrom(previousLocation)
- moveCardsTo(tableau[w])
- if selectedColumn == "discardPile" then
- renderDiscardPile()
- end
- renderTableau(w)
- else
- deselect(previousLocation)
- end
- end
- end
- end
- break
- end
- end
- for f=1,4 do
- if isWithinBounds(getBounds(foundations[f].win)) then
- if selectedColumn == -1 * f then
- playsound(sounds.deselect)
- foundations[f]["selected"] = false
- renderFoundations(f)
- selectedColumn = 0
- selectedCards = {}
- elseif table.getn(selectedCards) == 1 then
- previousLocation = nil
- if selectedColumn == "discardPile" then
- previousLocation = discardPile.cards
- else
- previousLocation = tableau[selectedColumn]
- end
- if selectedCards[1].value == 1 and foundations[f].suit == nil then
- foundations[f]["suit"] = selectedCards[1].suit
- foundations[f]["value"] = 1
- removeSelectedCardsFrom(previousLocation)
- renderFoundations(f)
- if selectedColumn ~= "discardPile" then
- renderTableau(seletedColumn)
- end
- selectedCards = {}
- selectedColumn = 0
- elseif foundations[f].suit ~= nil then
- if foundations[f].suit == selectedCards[1].suit and foundations[f].value == selectedCards[1].value-1 then
- foundations[f]["value"] = foundations[f].value+1
- removeSelectedCardsFrom(previousLocation)
- renderFoundations(f)
- if selectedColumn ~= "discardPile" then
- renderTableau(selectedColumn)
- end
- selectedCards = {}
- selectedColumn = 0
- end
- end
- elseif foundations[f].value ~= nil and table.getn(selectedCards) == 0 then
- playsound(sounds.pickCard)
- if foundations[f].suit == 1 or foundations[f].suit == 4 then
- c = "R"
- else
- c = "B"
- end
- table.insert(selectedCards,1,{value=foundations[f].value,suit=foundations[f].suit,faceUp=true,selected=false,color=c})
- selectedColumn = -1 * f
- foundations[f]["selected"] = true
- renderFoundations(f)
- end
- break
- end
- end
- end
- elseif event == "key" then
- if arg1 == keys.backspace then
- continue = false
- restart = false
- end
- end
- end
- end
- function tryToMoveToFoundation (pile)
- card = nil
- if pile == "discardPile" then
- selectedColumn = "discardPile"
- pile = discardPile.cards
- card = discardPile.cards[1]
- else
- pile = tableau[selectedColumn]
- card = tableau[selectedColumn][table.getn(tableau[selectedColumn])]
- end
- moveTo = -1
- for f=1,4 do
- if foundations[f].suit == nil and card.value == 1 then
- moveTo = f
- foundations[f]["value"] = 1
- foundations[f]["suit"] = card.suit
- table.insert(selectedCards,1,card)
- renderFoundations(f)
- break
- elseif foundations[f].suit ~= nil then
- if foundations[f].suit == card.suit and foundations[f].value == card.value-1 then
- moveTo = f
- foundations[f]["value"] = foundations[f].value + 1
- table.insert(selectedCards,1,card)
- renderFoundations(f)
- end
- end
- end
- if moveTo ~= -1 then
- playsound(sounds.moveCard)
- if selectedColumn == "discardPile" then
- removeSelectedCardsFrom()
- else
- table.remove(tableau[selectedColumn],table.getn(tableau[selectedColumn]))
- renderTableau(selectedColumn)
- end
- end
- selectedColumn = 0
- selectedCards = {}
- end
- function removeSelectedCardsFrom (location)
- if selectedColumn == "discardPile" then
- table.remove(discardPile.cards,1)
- renderDiscardPile()
- else
- for i=1,table.getn(selectedCards) do
- table.remove(location,table.getn(location))
- end
- renderTableau(selectedColumn)
- end
- end
- function deselect (location)
- --playsound("note.harp")
- --printerror("test",selectedColumn)
- if selectedColumn == "discardPile" then
- discardPile.cards[1]["selected"] = false
- --renderDiscardPile()
- elseif selectedColumn > 0 then
- --playsound("note.harp",1.32)
- for i=1,table.getn(location) do
- location[i]["selected"] = false
- end
- renderTableau()
- else
- foundations[-1*selectedColumn]["selected"] = false
- renderFoundations(-1*selectedColumn)
- end
- selectedCards = {}
- selectedColumn = 0
- end
- function moveCardsTo (location)
- for i=1,table.getn(selectedCards) do
- selectedCards[i]["selected"] = false
- table.insert(location,table.getn(location)+1,selectedCards[i])
- end
- selectedCards = {}
- selectedColumn = 0
- end
- xPos,yPos = -1,-1
- --takes table in form {xMin,yMin,xMax,yMax}
- function isWithinBounds (bounds)
- xMin = bounds[1]
- yMin = bounds[2]
- xMax = bounds[3]
- yMax = bounds[4]
- return (xPos >= xMin and xPos <= xMax and yPos <= yMax and yPos >= yMin)
- end
- function getBounds (win)
- xMin,yMin = win.getPosition()
- winWidth,winHeight = win.getSize()
- xMax = xMin+winWidth-1
- yMax = yMin+winHeight-1
- return {xMin,yMin,xMax,yMax}
- end
- restart = true
- repeat
- playGame()
- until restart == false
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement