Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- disk = peripheral.wrap("left")
- local cardName = {"Ace of Spades", "Ace Of Clubs", "Ace Of Hearts", "Ace Of Diamonds", "2 of Spades", "2 of Clubs", "2 of Hearts", "2 of Diamonds", "3 of Spades", "3 of Clubs", "3 of Hearts", "3 of Diamonds", "4 of Spades", "4 of Clubs", "4 of Hearts", "4 of Diamonds", "5 of Spades", "5 of Clubs", "5 of Hearts", "5 of Diamonds", "6 of Spades", "6 of Clubs", "6 of Hearts", "6 of Diamonds", "7 of Spades", "7 of Clubs", "7 of Hearts", "7 of Diamonds", "8 of Spades", "8 of Clubs", "8 of Hearts", "8 of Diamonds", "9 of Spades", "9 of Clubs", "9 of Hearts", "9 of Diamonds", "10 of Clubs", "10 of Spades", "10 of Hearts", "10 of Diamonds", "Jack of Clubs", "Jack of Spades", "Jack of Hearts", "Jack of Diamonds", "Queen of Clubs", "Queen of Spades", "Queen of Hearts", "Queen of Diamonds", "King of Clubs", "King of Spades", "King of Hearts", "King of Diamonds"}
- local cardValue = {11, 11, 11, 11, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
- local playerCards = {}
- local dealerVisibleCards = {}
- local dealerHiddenCards = {}
- local restricted = {}
- local minCards = 1
- local maxCards = 52
- stayPlayer = false
- stayDealer = false
- stay = false
- function activate()
- os.pullEvent("disk")
- end
- function waitClick()
- event, side, x, y = os.pullEvent("monitor_touch")
- end
- function mWrite(message, x, y)
- mon.setCursorPos(x,y)
- mon.write(message)
- end
- function startScreen()
- drawImage("startScreen") -- skal ændres til koden for startScreen
- mWrite("Your hand", 4,2)
- mWrite("Dealer hand", 22,2)
- mWrite("Credits: "..credits, 22,1)
- mWrite("Pot", 13,2)
- mWrite(pot, 13,4)
- end
- function getCredits()
- f = fs.open("/disk/credits", "r")
- credits = f.readLine()
- end
- function generateNumber()
- randomNum = math.random(1, 52)
- end
- function restrictCard()
- restricted = {}
- for i = 1, #playerCards do
- table.insert(restricted, playerCards[i])
- end
- for i = 1, #dealerHiddenCards do
- table.insert(restricted, dealerHiddenCards[i])
- end
- for i = 1, #dealerVisibleCards do
- table.insert(restricted,dealerVisibleCards[i])
- end
- end
- function checkRestrict()
- isRestricted = true
- for key, value in ipairs(restricted) do
- if randomNum ~= tonumber(value) then
- isRestricted = false
- break
- end
- end
- end
- function drawCard()
- generateNumber()
- checkRestrict()
- return randomNum
- end
- function playerCard()
- drawCard()
- playerCard1 = cardName[randomNum]
- drawCard()
- playerCard2 = cardName[randomNum]
- table.insert(playerCards, playerCard1)
- table.insert(playerCards, playerCard2)
- restrictCard()
- mWrite(playerCards[1], 4,8)
- mWrite(playerCards[2], 4,9)
- mWrite(playerHand, 5,4)
- end
- function hitCard(receiver)
- yPlayer = 10
- yDealer = 10
- drawCard()
- card = cardName[randomNum]
- table.insert(receiver, card)
- if receiver == playerCards then
- mWrite(card, 4,yPlayer)
- mWrite(playerHand, 5,4)
- yPlayer = yPlayer +1
- elseif receiver == dealerCards then
- mWrite(card, 17, yDealer)
- mWrite(dealerHand, 33,4)
- yDealer = yDealer +1
- end
- end
- function dealerCard()
- drawCard()
- dealerVisibleCard1 = cardName[randomNum]
- drawCard()
- dealerHiddenCard = cardName[randomNum]
- table.insert(dealerVisibleCards, dealerVisibleCard1)
- table.insert(dealerHiddenCards, dealerHiddenCard)
- restrictCard()
- mWrite(dealerHand, 33,4)
- mWrite("Hidden", 17,8)
- mWrite(dealerVisibleCards[1], 17,9)
- mWrite(dealerHand, 33,4)
- end
- function doubleDown()
- pot = stake * 2
- hitPlayer()
- stayPlayer = true
- if stayDealer == true then
- stay = true
- end
- end
- function hitPlayer()
- hitCard(playerCards)
- end
- function hitDealer()
- hitCard(dealerVisibleCards)
- end
- function dealerMove()
- if dealerHand < 17 then
- hitDealer()
- elseif dealerHand > 17 then
- stayDealer = true
- if stayPlayer == true then
- stay = true
- end
- end
- end
- function checkBust()
- if playerHand > 21 then
- playerBust()
- elseif dealerHand > 21 then
- dealerBust()
- end
- end
- function playerBust()
- drawImage("messageScreen")
- mWrite("You busted!", 6,15)
- dealerWin()
- end
- function dealerBust()
- drawImage("messageScreen")
- mWrite("Dealer busted!", 6,15)
- playerWin()
- end
- function resolve()
- if playerHand > 21 then
- playerBust()
- elseif dealerHand > 21 then
- dealerBust()
- elseif dealerHand == 21 then
- dealerBlackjack()
- elseif dealerHand > playerHand then
- dealerWin()
- elseif dealerHand == playerHand then
- draw()
- elseif playerHand == 21 and dealerHand ~= 21 then
- playerBlackjack()
- elseif dealerHand < playerHand then
- playerWin()
- end
- checkBroke()
- end
- function draw()
- drawImage("messageScreen")
- mWrite("Draw", 6,15)
- end
- function dealerWin()
- drawImage("messageScreen")
- mWrite("Dealer wins", 6,15)
- credits = credits - stake
- mWrite("Credits: "..credits, 22,1)
- end
- function playerWin()
- drawImage("messageScreen") -- message image
- mWrite("You win!", 6,15) -- player vinder
- credits = credits + stake
- mWrite("Credits: "..credits, 22,1)
- end
- function dealerBlackjack()
- drawImage("messageScreen")
- mWrite("Dealer got Blackjack, you loose", 6,15)
- credits = credits - stake
- mWrite("Credits: "..credits, 22,1)
- end
- function playerBlackjack()
- drawImage("messageScreen")
- mWrite("You got Blackjack, you win!", 6,15)
- credits = credits + (math.floor(stake * 2))
- mWrite("Credits: "..credits, 22,1)
- end
- function checkBroke()
- if credits < 1 then
- broke()
- end
- end
- function broke()
- drawImage("messageScreen")
- mWrite("No more credits", 6,15)
- sleep(10)
- os.restart()
- end
- function checkCredit()
- if credits < 1 then
- mWrite("No credits", 11,12)
- end
- end
- function placeBet()
- stake = 0
- pot = stake
- drawImage("betScreen")
- mWrite("Place your bet", 11,11)
- while true do
- waitClick()
- if x >9 and x < 12 and y == 13 then
- stake = stake + 1
- credits = credits - 1
- mWrite("Credits: "..credits, 22,1)
- mWrite(stake, 12,12)
- checkCredit()
- elseif x >9 and x < 12 and y == 14 then
- stake = stake + 10
- credits = credits - 10
- mWrite("Credits: "..credits, 22,1)
- mWrite(stake, 12,12)
- checkCredit()
- elseif x >9 and x < 12 and y == 15 then
- stake = stake + 100
- credits = credits - 100
- mWrite("Credits: "..credits, 22,1)
- mWrite(stake, 12,12)
- checkCredit()
- elseif x >17 and x < 21 and y == 13 then
- stake = stake - 1
- credits = credits + 1
- mWrite("Credits: "..credits, 22,1)
- mWrite(stake, 12,12)
- checkCredit()
- elseif x >17 and x < 21 and y == 14 then
- stake = stake - 10
- credits = credits + 10
- mWrite("Credits: "..credits, 22,1)
- mWrite(stake, 12,12)
- checkCredit()
- elseif x >17 and x < 21 and y == 15 then
- stake = stake - 100
- credits = credits + 100
- mWrite("Credits: "..credits, 22,1)
- mWrite(stake, 12,12)
- checkCredit()
- elseif x >12 and x < 18 and y == 14 then
- break
- end
- end
- end
- function hitOrStay()
- waitClick() -- venter på stay, hit eller double down
- if x >2 and x < 10 and y > 13 and y < 17 then -- hit
- hitPlayer()
- if stayDealer == false then
- dealerMove()
- end
- elseif x >11 and x < 19 and y > 13 and y < 17 then -- double down
- doubleDown()
- while stayDealer == false do
- dealerMove()
- end
- elseif x >20 and x < 28 and y > 13 and y < 17 then -- stay
- stayPlayer = true
- while stayDealer == false do
- dealerMove()
- end
- end
- end
- function writeCredits()
- file = fs.open("/disk/credits", "w")
- file.write(credits)
- end
- function playAgain()
- drawImage("newGameScreen") -- tegn boks med spørgsmål
- mWrite("New game", 6, 10) -- nyt spil knap
- mWrite("Cash out", 18, 10) -- cash out
- waitClick()
- if x >5 and x < 13 and y > 8 and y < 12 then -- new game
- newGame()
- elseif x >17 and x < 25 and y > 8 and y < 12 then -- Cash out
- cashOut()
- end
- end
- function newGame()
- stay = false
- stayDealer = false
- satyPlayer = false
- local playerCards = {}
- local dealerVisibleCards = {}
- local dealerHiddenCards = {}
- end
- function cashOut()
- drawImage("messageScreen") -- draw message image
- writeCredits()
- mWrite("Thanks for playing", 6,15) -- message screen
- sleep(10)
- os.restart()
- end
- function drawImage(image)
- paintutils.loadImage(image)
- paintutils.drawImage(image, 1, 1)
- end
- --################Loop##############
- while true do
- activate()
- term.redirect(peripheral.wrap("right"))
- getCredits()
- while true do
- drawImage("betScreen")
- placeBet()
- playerCard()
- dealerCard()
- mWrite("Hit", 5,15)
- mWrite("Stay", 22,15)
- mWrite("Double Down", 13,15)
- while true do
- hitOrStay()
- checkBust()
- if stay == true then
- break
- end
- resolve()
- end
- sleep(5)
- playAgain()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement