文档库 最新最全的文档下载
当前位置:文档库 › lua实现的BlackJack游戏

lua实现的BlackJack游戏

--
-- StartGUI
--
-- Define constant values for all scripts

-- Standard LuaGUI event codes
GUI_EVENT_BUTTON_UP = 0
GUI_EVENT_BUTTON_DOWN = 1
GUI_EVENT_SELECTION_CHANGED = 2
GUI_EVENT_TEXTFIELD_CLICKED = 3
GUI_KEY_PRESS = 4
GUI_REENTER_INTERFACE = 5
GUI_TIMER_EXPIRED = 6;
GUI_ENTER_INTERFACE = 7
GUI_TEXT_SCROLL_END = 8
GUI_MOUSE_BUTTON_DOWN = 12
GUI_MOUSE_BUTTON_UP = 13

--Use constants
NO = 0
YES = 1
LEFT = 1
RIGHT = 2

-- Sub-interface constants
GUI_INGAME = 0

-- This initializes the game at startup.
math.randomseed(os.date("%d%H%M%S"))

-- Load in the support functions
dofile("Scripts\\LuaSupport.Lua")

--Start an interface
RunGUI("GUI_InGame.lua")


--
-- LuaSupport
--

--blackjack constants
SPADE = 1
DIAMOND = 2
CLUB = 3
HEART = 4
ACE = 1
JACK = 11
QUEEN = 12
KING = 13
HUMAN = 1
DEALER = 2

HOUSE_STAND = 17

--gamestate values
HUMAN_TURN = 1
DEALER_TURN = 2
HUMAN_BUST = 3
DEALER_DONE = 4

THINKING = 1
DONE_THINKING = 2
BUST = 3


function CreateDeck()
curDeckLocation = 1
myDeck = {}
cardNum = 1
for suit = SPADE, HEART do
for card = ACE, KING do
myDeck[cardNum] = {}
myDeck[cardNum].suit = suit
myDeck[cardNum].card = card
cardNum = cardNum + 1
end
end
print("deck created")
end

function ShuffleDeck()
shuffleValue = 1000
for indx = 1, shuffleValue do
card1 = math.random(1,52)
card2 = math.random(1,52)
hold = {}
hold = myDeck[card1]
myDeck[card1] = myDeck[card2]
myDeck[card2] = hold
end
print("deck shuffled")
end

function DrawCard(suit, card, player, position)
if player == DEALER then
yValue = 100
xValue = 50 + (115 * position)
cardID = 100 + position
else
yValue = 300
xValue = 50 + (115 * position)
cardID = 120 + position
end
if suit == SPADE then
theSuit = "s"
end
if suit == DIAMOND then
theSuit = "d"
end
if suit == CLUB then
theSuit = "c"
end
if suit == HEART then
theSuit = "h"
end
cardName = string.format("%s%s%s%s%s", "crd_", theSuit, "_", tostring(card), ".bmp")
--check if it's the dealer's first card (hidden)
if (gameState == HUMAN_TURN) and (player == DEALER) and (position == 1) then
cardName = "crdback.bmp"
end
CreateItem(cardID, "Sprite", cardName)
SetItemPosition(cardID, xValue, yValue, 107, 150)
print(string.format("%s%s", "card drawn: ", cardName))
end

function DrawHand(player)
if table.getn(theHand[player]) > 0 then
--there are cards in the hand
for indx = 1, table.getn(theHand[player]) do
DrawCard(myDeck[theHand[player][indx]].suit, myDeck[theHand[player][indx]].card, player, indx)
end
end
end

function InitHands()
theHand = {}
theHand[

DEALER] = {}
theHand[HUMAN] = {}
print("player hands initialized")
end

function GetCard(player)
if curDeckLocation > 52 then
print("Out of cards! Start new game!")
else
table.insert(theHand[player], curDeckLocation)
DrawHand(player)
curDeckLocation = curDeckLocation + 1
end
end

function InitialDeal()
gameState = HUMAN_TURN
for indx = 1,2 do
GetCard(DEALER)
GetCard(HUMAN)
end
ItemCommand(MESSAGE, "SetString", "tap H for hit, S for stand")
print("initial deal")
end

function ClearOldCards()
for indx = 1,7 do
cardID = 100 + indx
DeleteItem(cardID)
cardID = 120 + indx
DeleteItem(cardID)
end
end


function CheckHandValue(player)
myValue = 0
numAces = 0
for indx = 1, table.getn(theHand[player]) do
curCardValue = myDeck[theHand[player][indx]].card
if curCardValue > 10 then
curCardValue = 10
end
if curCardValue == ACE then
--we have an ace
numAces = numAces + 1
else
myValue = myValue + curCardValue
end
end
if numAces > 0 then
--deal with one ace
if numAces == 1 then
if myValue + 11 > 21 then
myValue = myValue + 1
else
myValue = myValue + 11
end
end
--deal with two aces
if numAces == 2 then
if myValue + 12 > 21 then
myValue = myValue + 2
else
myValue = myValue + 12
end
end
--deal with three aces
if numAces == 3 then
if myValue + 13 > 21 then
myValue = myValue + 3
else
myValue = myValue + 13
end
end
--deal with four aces
if numAces == 4 then
if myValue + 14 > 21 then
myValue = myValue + 4
else
myValue = myValue + 14
end
end
end
return myValue
end

function Think(player)
result = THINKING
if CheckHandValue(player) < HOUSE_STAND then
GetCard(player)
else
result = DONE_THINKING
end
if CheckHandValue(player) > 21 then
result = BUST
end
return result
end

--
-- GUI_InGame
--
-- first steps


--dofile calls for sub-GUIs


--100s Sprites







--200s Text items

CreateItem(GUI_INGAME + 200,"TextField")
SetItemPosition(GUI_INGAME + 200, 10, 10, 200, 20)
SetFont(GUI_INGAME + 200, "Arial", 24)
ItemCommand(GUI_INGAME + 200, "SetColor", 255,255,255,255)
ItemCommand(GUI_INGAME + 200, "SetString", "{blackjack demo}")

CreateItem(GUI_INGAME + 202,"TextField")
SetItemPosition(GUI_INGAME + 202, 10, 45, 200, 20)
SetFont(GUI_INGAME + 202, "Arial", 24)
ItemCommand(GUI_INGAME + 202, "SetCo

lor", 255,255,255,255)
ItemCommand(GUI_INGAME + 202, "SetString", "Tap Enter to play a hand.")


CreateItem(GUI_INGAME + 205,"TextField")
SetItemPosition(GUI_INGAME + 205, 50, 150, 200, 20)
SetFont(GUI_INGAME + 205, "Arial", 22)
ItemCommand(GUI_INGAME + 205, "SetColor", 255,255,255,255)
ItemCommand(GUI_INGAME + 205, "SetString", "...thinking...")
EnableObject(GUI_INGAME + 205, 0, 0)

MESSAGE = GUI_INGAME + 210
CreateItem(MESSAGE,"TextField")
SetItemPosition(MESSAGE, 165, 500, 200, 20)
SetFont(MESSAGE, "Arial", 24)
ItemCommand(MESSAGE, "SetColor", 255,255,255,255)




--300s Buttons


--400s Check Boxes and Radio buttons


--500s List objects


--600s Special objects (bar, chart, sprite ring, etc.)



--Initial run sets the event handler
SetEventHandler("InGameEvent")


--Event handler
function InGameEvent(id, eventCode)

if eventCode == GUI_ENTER_INTERFACE then
end

if eventCode == GUI_KEY_PRESS then
if id == 13 then --enter key
EnableObject(GUI_INGAME + 202, 0, 0)
ClearOldCards()
CreateDeck()
ShuffleDeck()
InitHands()
InitialDeal()
end

if id == 104 then -- h key
if gameState == HUMAN_TURN then
GetCard(HUMAN)
if CheckHandValue(HUMAN) > 21 then
ItemCommand(MESSAGE, "SetString", "BUST!!")
gameState = HUMAN_BUST
end
end
end

if id == 115 then -- s key
if gameState == HUMAN_TURN then
gameState = DEALER_TURN
EnableObject(GUI_INGAME + 205, 1, 1)
StartTimer(1.5)
end
end

end

if eventCode == GUI_MOUSE_BUTTON_DOWN then
end

if eventCode == GUI_MOUSE_BUTTON_UP then
end

if eventCode == GUI_TIMER_EXPIRED then
progress = Think(DEALER)
if progress == BUST then
EnableObject(GUI_INGAME + 205, 0, 0)
gameState = DEALER_DONE
DrawHand(DEALER)
ItemCommand(MESSAGE, "SetString", "Dealer busts... YOU WIN!!")
elseif progress == THINKING then
StartTimer(1.5)
else
--dealer is done...process results
EnableObject(GUI_INGAME + 205, 0, 0)
gameState = DEALER_DONE
DrawHand(DEALER)
if CheckHandValue(HUMAN) > CheckHandValue(DEALER) then
theScore = tostring(CheckHandValue(HUMAN))
ItemCommand(MESSAGE, "SetString", string.format("%s%s%s","You win the hand with a ", theScore, "!"))
else
theScore = tostring(CheckHandValue(DEALER))
ItemCommand(MESSAGE, "SetString", string.format("%s%s%s","Dealer takes the hand with a score of ", theScore, "."))
end
EnableObject(GUI_INGAME + 202, 1, 1)
end
en

d


end

相关文档