Skip to content

Instantly share code, notes, and snippets.

Created July 23, 2011 04:28
Show Gist options
  • Save anonymous/1101017 to your computer and use it in GitHub Desktop.
Save anonymous/1101017 to your computer and use it in GitHub Desktop.
-- [Lua] The classic card game, War (v.2.1) --
TheDeck = {}
carddeck = {}
player = { hand={} }
opponent = { hand={} }
arena = { hand={} }
-- Standard seed based on time, called once
math.randomseed(os.time())
--[[ This is simply because Lua's math.random function is unreliable. I've found
that the numbers generated are hardly random until the third generated number, so
I threw this together to give me the third randomly generated number every time I
rand a random number.]]--
function safeRandom(num)
a,b,c = math.random(num), math.random(num), math.random(num)
return c end
--[[ The deck constructor. It produces a set of 52 cards, each containing a name(string),
a value(number), a suit(string) and a color(string, derived from suit). ]]--
function TheDeck:make()
for i=0,51 do
local freshCard={}
freshCard.value = (i % 13) + 2
local suit
local color
local name
if i < 13 then freshCard.suit = "spades"
elseif i < 26 then freshCard.suit = "clubs"
elseif i < 39 then freshCard.suit = "hearts"
elseif i < 52 then freshCard.suit = "diamonds" end
if freshCard.suit == "spades" or freshCard.suit == "clubs" then freshCard.color = "black"
else freshCard.color = "red" end
if freshCard.value < 11 then freshCard.name = freshCard.value .. " of " .. freshCard.suit
elseif freshCard.value == 11 then freshCard.name = "Jack of " .. freshCard.suit
elseif freshCard.value == 12 then freshCard.name = "Queen of " .. freshCard.suit
elseif freshCard.value == 13 then freshCard.name = "King of " .. freshCard.suit
elseif freshCard.value == 14 then freshCard.name = "Ace of " .. freshCard.suit end
carddeck[i+1] = freshCard end
end
--[[ This is how the player adds cards to his hand. ]]--
function player:getCard(inboundCard)
table.insert(player.hand, (#player.hand+1), inboundCard)
end
--[[ This is how the player plays a card into the arena, thus losing
it from his hand. ]]--
function player:playCard(cardsToPlay)
for i=cardsToPlay,0,-1 do
table.insert(arena.hand, (#arena.hand+1), player.hand[1])
table.remove(player.hand, 1)
if #player.hand == 0 then game.over() break
else end
end
end
--[[ This is how the opponent adds cards to his hand. ]]--
function opponent:getCard(inboundCard)
table.insert(opponent.hand, (#opponent.hand+1), inboundCard)
end
--[[ This is how the opponent plays a card into the arena, thus losing
it from his hand. ]]--
function opponent:playCard(cardsToPlay)
for i=cardsToPlay,0,-1 do
table.insert(arena.hand, (#arena.hand+1), opponent.hand[1])
table.remove(opponent.hand, 1)
if #opponent.hand == 0 then game.over() break
else end
end
end
--[[ Deals the cards to the player and the opponent one at a time.]]--
function TheDeck:deal()
for i=1,52,2 do
player:getCard(carddeck[i])
opponent:getCard(carddeck[i+1]) end
end
--[[ This is the shuffle function. The must receive a table as the passed
parameter and procedes to shuffles whatever size "deck" it's given. ]]--
function shuffle(pile)
for i=#pile,1,-1 do
toMove = safeRandom(i)
pile[toMove], pile[i] = pile[i], pile[toMove] end
end
--[[ Main body of the program begins here. ]] --
TheDeck:make()
shuffle(carddeck)
--for i=1,52 do print(carddeck[i].name) end
TheDeck:deal()
print("Player has " .. #player.hand .. " cards.")
-- for i=1,#player.hand do print(player.hand[i].name) end
print("Opponent has " .. #opponent.hand .. " cards.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment