Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Blackjack Game V2.0 ]]--
- --[[ Created By ChiknNuggets ]]--
- 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 StartingCash = 500 -- How Much cash the person starts with
- local playersCards = {}
- local dealervisibleCards = {}
- local dealerhiddenCards = {}
- local AcesD = 0
- local AcesDh = 0
- local AcesP = 0
- local playercount = 0
- local dealervisiblecount = 0
- local dealerhiddencount = 0
- local dealertotalcount = 0
- local stay = false
- local broke = false
- local Cash = StartingCash
- local function quit()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.setCursorPos(1,1)
- term.clear()
- error()
- end
- local function getColourOf(hex)
- local value = tonumber(hex, 16)
- if not value then return nil end
- value = math.pow(2,value)
- return value
- end
- function math.randomEx(minv,maxv)
- local bExcluded
- local restricted = {}
- for i=1,#playersCards do table.insert(restricted,playersCards[i]) end
- for i=1,#dealervisibleCards do table.insert(restricted,dealervisibleCards[i]) end
- for i=1,#dealerhiddenCards do table.insert(restricted,dealerhiddenCards[i]) end
- local iNum
- repeat
- bExcluded = false;
- iNum = math.random(minv,maxv)
- for _, iVal in ipairs( restricted ) do
- if iNum == tonumber(iVal) then
- bExcluded = true
- break
- end
- end
- until not bExcluded
- return iNum
- end
- local function drawPictureTable(image, xinit, yinit)
- for y=1,#image do
- for x=1,#image[y] do
- term.setCursorPos(xinit + x-1, yinit + y-1)
- local col = getColourOf(string.sub(image[y], x, x))
- if col ~= nil then
- term.setBackgroundColour(col)
- term.write(" ")
- end
- end
- end
- end
- local function GUIdrawAll()
- drawPictureTable(
- {"fffffffffffffffffffffffffffffffffffffffffffffffffff",
- "fffffffffffffffffffffffffffffffffffffffffffffffffff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f8888888888888888888fffffffffff888888888888888888ff",
- "fffffffffffffffffffff444444444fffffffffffffffffffff",
- "fffffffffffffffffffffffffffffffffffffffffffffffffff",
- "f888888888888888888888888888888888888ff4444444444ff",
- "f888888888888888888888888888888888888ffffffffffffff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "f888888888888888888888888888888888888888888888888ff",
- "fffffffffffffffffffffffffffffffffffffffffffffffffff"},1,1)
- term.setCursorPos(3,2)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- write("Dealers Hand:")
- term.setCursorPos(51,1)
- write("X")
- term.setCursorPos(3,11)
- write("Your Hand:")
- term.setCursorPos(40,11)
- write("Your Cash:")
- term.setCursorPos(23,9)
- write("The Pot")
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.yellow)
- term.setCursorPos(40,12)
- write("$"..Cash)
- if pot then term.setCursorPos(22+(string.len("$"..pot)/2),10);write("$"..pot) end
- end
- local function hitstand()
- drawPictureTable({"fffffffffffff","ffeeeeeeeeeef","fffffffffffff","ff5555555555f","fffffffffffff"},38,15)
- term.setCursorPos(43,16)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- write("Hit")
- term.setCursorPos(43,18)
- term.setBackgroundColor(colors.lime)
- term.setTextColor(colors.black)
- write("Stay")
- while true do
- local event,p1,p2,p3 = os.pullEvent()
- if event == "mouse_click" and p1 == 1 then
- if p2 >= 40 and p2 <= 49 then
- if p3 == 16 then -- Hit Function
- drawPictureTable({"888888888888f","888888888888f","888888888888f","888888888888f","fffffffffffff"},38,15)
- return 1
- elseif p3 == 18 then -- Stand Function
- drawPictureTable({"888888888888f","888888888888f","888888888888f","888888888888f","fffffffffffff"},38,15)
- return 2
- end
- elseif p2 == 51 and p3 == 1 then quit() end
- end
- end
- drawPictureTable({"8888888888888","8888888888888","8888888888888","8888888888888","8888888888888"},38,15)
- end
- function Notify(msg,btnmsg)
- drawPictureTable({"ffffffffffffffffffffffffffff","f88888888888888888888888888f","f88888888888888888888888888f","f88888888888888888888888888f","f88888888777777777788888888f","f88888888888888888888888888f","ffffffffffffffffffffffffffff"},(51-28)/2,(19-7)/2)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- if string.len(msg) > 23 then msg = string.sub(msg,1,23-string.len(msg)) end
- term.setCursorPos((51-#msg)/2,8)
- write(msg)
- if string.len(btnmsg) > 7 then btnmsg = string.sub(btnmsg,1,7-string.len(btnmsg)) end
- term.setCursorPos((51-#btnmsg)/2,10)
- term.setBackgroundColor(colors.gray)
- write(btnmsg)
- while true do
- local event,p1,p2,p3 = os.pullEvent()
- if event == "mouse_click" and p1 == 1 and p2 >=20 and p2 <= 29 and p3 == 10 then break
- elseif event == "mouse_click" and p1 == 1 and p2 == 51 and p3 == 1 then quit() end
- end
- end
- function PlaceBet()
- local chipsize = 1
- if Cash > 1 and Cash <= 5 then chipsize = 1
- elseif Cash > 5 and Cash <= 25 then chipsize = 5
- elseif Cash > 25 and Cash <= 100 then chipsize = 25
- elseif Cash > 100 then chipsize = 100 end
- local pot = chipsize
- drawPictureTable({"fffffffffffffffffffffffff","f88888888888888888888888f","f88888888888888888888888f","f88777777777777887777788f","f88444444444444884444488f","f88777777777777887777788f","f88888888888888888888888f","f88777777777777888888888f","f88888888888888888888888f","fffffffffffffffffffffffff"},12,6)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(15,9)
- write(" Raise ")
- term.setCursorPos(15,11)
- write(" Lower ")
- term.setCursorPos(17,13)
- write("Place bet")
- term.setCursorPos(29,9)
- write("/\\ /\\")
- term.setCursorPos(29,11)
- write("\\/ \\/")
- term.setCursorPos(13,6)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- write("Place Your Bet")
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- term.setCursorPos(16+((10-string.len("$"..pot))/2),10)
- print("$"..pot)
- while true do
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- term.setCursorPos(29,10)
- print(" ")
- term.setCursorPos(27+((10-string.len("$"..chipsize))/2),10)
- print("$"..chipsize)
- local event,p1,p2,p3 = os.pullEvent()
- if (event == "key" and p1 == 200 and Cash >= pot+chipsize) or (event == "mouse_click" and p1 == 1 and p2 >= 15 and p2 <= 26 and p3 == 9 and Cash >= pot+chipsize) and pot <= 40000 then pot = pot+chipsize
- elseif (event == "key" and p1 == 208 and Cash >=pot-chipsize and pot-chipsize > 0) or (event == "mouse_click" and p1 == 1 and p2 >= 15 and p2 <= 26 and p3 == 11 and Cash >=pot-chipsize and pot > chipsize) and pot > chipsize then pot = pot-chipsize
- elseif event == "mouse_click" and p1 == 1 and p2 >= 29 and p2 <= 33 and p3 == 9 then
- if chipsize == 1 then chipsize = 5
- elseif chipsize == 5 then chipsize = 25
- elseif chipsize == 25 then chipsize = 100
- elseif chipsize == 100 then chipsize = 500
- elseif chipsize == 500 then chipsize = 1000 end
- elseif event == "mouse_click" and p1 == 1 and p2 >= 29 and p2 <= 33 and p3 == 11 then
- if chipsize == 1000 then chipsize = 500
- elseif chipsize == 500 then chipsize = 100
- elseif chipsize == 100 then chipsize = 25
- elseif chipsize == 25 then chipsize = 5
- elseif chipsize == 5 then chipsize = 1 end
- end
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- term.setCursorPos(15,10)
- print(" ")
- term.setCursorPos(16+((10-string.len("$"..pot))/2),10)
- print("$"..pot)
- term.setCursorPos(29,10)
- print(" ")
- term.setCursorPos(27+((10-string.len("$"..chipsize))/2),10)
- print("$"..chipsize)
- if (event == "mouse_click" and p1 == 1 and p2 >= 15 and p2 <= 26 and p3 == 13) or (event == "key" and p1 == 28) then Cash = Cash-pot;return pot*2
- elseif event == "mouse_click" and p1 == 1 and p2 == 51 and p3 == 1 then quit() end
- end
- end
- function newcard(playername)
- local player
- local drawncard = math.randomEx(1,52)
- if playername == "Player" then
- player = playersCards
- playercount = playercount + CardValue[drawncard]
- elseif playername == "DealerV" then
- player = dealervisibleCards
- dealervisiblecount = dealervisiblecount + CardValue[drawncard]
- elseif playername == "DealerH" then
- player = dealerhiddenCards
- dealerhiddencount = dealerhiddencount + CardValue[drawncard]
- else return false end
- if drawncard <= 4 then
- if playername == "Player" then
- AcesP = AcesP+1
- elseif playername == "DealerV" then
- AcesD = AcesD+1
- elseif playername == "DealerH" then
- AcesDh = AcesDh + 1
- end
- end
- table.insert(player,drawncard)
- CheckCounts()
- refreshCards()
- end
- function refreshCards()
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- if #playersCards > 0 and #playersCards <= 6 then
- for i=1,#playersCards do
- term.setCursorPos(2,11+i)
- print(CardName[playersCards[i]])
- end
- end
- if #dealerhiddenCards == 1 then
- for i=1,#dealerhiddenCards do
- term.setCursorPos(2,2+i)
- if stay == false then
- print("*Un-Turned")
- elseif stay == true then
- print(CardName[dealerhiddenCards[i]])
- end
- end
- end
- if #dealervisibleCards > 0 and #dealervisibleCards <= 5 then
- for i=1,#dealervisibleCards do
- term.setCursorPos(2,2+#dealerhiddenCards+i)
- print(CardName[dealervisibleCards[i]])
- end
- end
- end
- function reset()
- if broke == true then Cash = StartingCash end
- AcesD = 0
- AcesDh = 0
- AcesP = 0
- playercount = 0
- dealervisiblecount = 0
- dealerhiddencount = 0
- playersCards = {}
- dealervisibleCards = {}
- dealerhiddenCards = {}
- pot = 0
- stay = false
- end
- function CheckCounts()
- if playercount > 21 and AcesP > 0 then
- playercount = playercount - 10
- AcesP = AcesP - 1
- GUIdrawAll()
- end
- if dealervisiblecount+dealerhiddencount > 21 and AcesD > 0 and stay == true then
- dealervisiblecount = dealervisiblecount - 10
- AcesD = AcesD - 1
- GUIdrawAll()
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if AcesP >= 1 then
- term.setCursorPos(3,11)
- write("Your Hand: ("..playercount.."/"..(playercount-10)..")")
- elseif AcesP == 0 then
- term.setCursorPos(3,11)
- write("Your Hand: ("..playercount..")")
- end
- if AcesD >= 1 and stay == false then
- term.setCursorPos(3,2)
- write("Dealers Hand: ("..dealervisiblecount.."/"..(dealervisiblecount-10)..")")
- elseif (AcesD + AcesDh) > 1 and stay == true then
- term.setCursorPos(3,2)
- write("Dealers Hand: ("..(dealervisiblecount+dealerhiddencount).."/"..(dealervisiblecount+dealerhiddencount-10)..")")
- elseif AcesD == 0 and stay == false then
- term.setCursorPos(3,2)
- write("Dealers Hand: ("..dealervisiblecount..")")
- elseif AcesD == 0 and stay == true then
- term.setCursorPos(3,2)
- write("Dealers Hand: ("..(dealervisiblecount+dealerhiddencount)..")")
- end
- end
- function dealerDraw()
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- term.setCursorPos(2,3)
- print(CardName[dealerhiddenCards[1]])
- while dealervisiblecount + dealerhiddencount < 17 do
- newcard("DealerV")
- end
- CheckCounts()
- end
- while true do
- drawPictureTable(
- {"fffffffffffffffffffffffffffffffffffffffffffffffffff",
- "f7777777777777777fffffffffffffffff7777777777777777f",
- "f7700000000000077fffffffffffffffff7700000000000077f",
- "f7700000000000077fffffffffffffffff7700000000000077f",
- "f7700f80000f80077fffffffffffffffff7700000000000077f",
- "f77008f00008f0077fffffffffffffffff7700000000000077f",
- "f7700000000000077fffffffffffffffff7700000ff0000077f",
- "f7700f80f80f80077fffffffffffffffff770000ffff000077f",
- "f77008f08f08f0077fffffffffffffffff77000ffffff00077f",
- "f7700000000000077fffffffffffffffff7700ffffffff0077f",
- "f7700f80f80f80077fffffffffffffffff7700ffffffff0077f",
- "f77008f08f08f0077fffffffffffffffff77000ffffff00077f",
- "f7700000000000077fffff5555555fffff7700000ff0000077f",
- "f7700f80000f80077fffffffffffffffff770000ffff000077f",
- "f77008f00008f0077fffffffffffffffff7700000000000077f",
- "f7700000000000077fffffffffffffffff7700000000000077f",
- "f7700000000000077fffffffffffffffff7700000000000077f",
- "f7777777777777777fffffffffffffffff7777777777777777f",
- "fffffffffffffffffffffffffffffffffffffffffffffffffff"},1,1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(37,3)
- write("A")
- term.setCursorPos(48,17)
- write("A")
- term.setCursorPos(4,3)
- write("10")
- term.setCursorPos(14,17)
- write("10")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(51,1)
- write("X")
- term.setCursorPos(22,6)
- write("Blackjack")
- term.setBackgroundColor(colors.lime)
- term.setTextColor(colors.black)
- term.setCursorPos(24,13)
- write("Start")
- while true do
- event,p1,p2,p3 = os.pullEvent()
- if event == "mouse_click" and p1 == 1 then
- if (p2 == 51 and p3 == 1) then quit()
- elseif (p2 >= 23 and p2 <= 29 and p3 == 13) then break end
- end
- end
- while true do
- GUIdrawAll()
- pot = PlaceBet()
- GUIdrawAll()
- sleep(.5)
- newcard("Player")
- sleep(.5)
- newcard("DealerH")
- sleep(.5)
- newcard("Player")
- sleep(.5)
- newcard("DealerV")
- sleep(.5)
- while true do
- if playercount == 21 and (dealerhiddencount + dealervisiblecount) ~=21 and #playersCards == 2 and (#dealervisibleCards + #dealerhiddenCards) == 2 then
- sleep(1.5)
- Notify("Blackjack, You won $"..pot,"Continue")
- Cash = Cash + pot
- break
- elseif playercount ~= 21 and (dealerhiddencount + dealervisiblecount) == 21 and #playersCards == 2 and (#dealervisibleCards + #dealerhiddenCards) == 2 then
- sleep(1.5)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- term.setCursorPos(2,3)
- print(CardName[dealerhiddenCards[1]])
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(3,2)
- write("Dealers Hand: ("..(dealervisiblecount+dealerhiddencount)..")")
- if Cash > 0 then
- Notify("Dealer Has BlackJack","Continue")
- else
- Notify("Dealer Has BlackJack","Continue")
- Notify("You're Broke, Game Over","Menu")
- broke = true
- end
- break
- elseif playercount == (dealervisiblecount+dealerhiddencount) and playercount == 21 then
- sleep(1.5)
- Notify("Drawed, you get $"..pot/2,"Continue")
- Cash = Cash + (pot/2)
- break
- end
- local choice = hitstand()
- sleep(.5)
- if choice == 1 then
- newcard("Player")
- if playercount > 21 then
- if Cash > 0 then
- sleep(1.5)
- Notify("Bad Luck, You Busted!","Continue")
- break
- else
- sleep(1.5)
- Notify("You're Broke, Game Over","Menu")
- broke = true
- break
- end
- elseif playercount == 21 then
- stay = true
- dealerDraw()
- if (dealervisiblecount+dealerhiddencount) ~= 21 then
- sleep(1.5)
- Notify("Congrats, You won $"..pot,"Continue")
- Cash = Cash + pot
- break
- elseif (dealervisiblecount+dealerhiddencount) == 21 then
- sleep(1.5)
- Notify("Drawed, you get $"..pot/2,"Continue")
- Cash = Cash + (pot/2)
- break
- end
- end
- elseif choice == 2 then
- stay = true
- dealerDraw()
- if playercount <= 21 and (dealervisiblecount+dealerhiddencount) <=21 and (dealervisiblecount+dealerhiddencount) < playercount then
- sleep(1.5)
- Notify("Congrats, You won $"..pot,"Continue")
- Cash = Cash + pot
- elseif playercount <= 21 and (dealervisiblecount+dealerhiddencount) <=21 and (dealervisiblecount+dealerhiddencount) > playercount then
- sleep(1.5)
- if Cash > 0 then Notify("Dealer Wins, Bad Luck","Continue")
- else Notify("Dealer Wins, Bad Luck","Continue");Notify("You're Broke, Game Over","Menu");broke = true end
- elseif playercount == (dealervisiblecount+dealerhiddencount) and playercount <=21 then
- sleep(1.5)
- Notify("Drawed, you get $"..pot/2,"Continue")
- Cash = Cash + (pot/2)
- elseif playercount <=21 and (dealervisiblecount+dealerhiddencount) > 21 then
- sleep(1.5)
- Notify("Congrats, You won $"..pot,"Continue")
- Cash = Cash + pot
- end
- break
- end
- end
- GUIdrawAll()
- refreshCards()
- sleep(2)
- reset()
- if broke == true then
- broke = false
- break
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement