Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [Lua] The classic card game, War (v.2.1) --
- f = assert(io.open("DataCollection.txt"))
- carddeck = {}
- TheDeck = {}
- player = { hand={} }
- opponent = { hand={} }
- arena = { playerPile={}, opponentPile={} }
- game ={ condition = "go" }
- battleWinner = ""
- roundCounter = 0
- file = io.open("DataCollection.txt", "a+")
- -- Standard seed based on time for use with safeRandom()
- 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
- --[[ These two functions are how the opponent adds cards to his hand and
- plays a card into the arena, thus losing it from his hand. ]]--
- function player:getCard(inboundCard)
- table.insert(player.hand, (#player.hand+1), inboundCard)
- end
- -- Play a card
- function player:playCard()
- if #player.hand == 0 then game.condition = "stop"
- else
- table.insert(arena.playerPile, player.hand[1])
- table.remove(player.hand, 1)
- end
- end
- --[[ These two functions are how the opponent adds cards to his hand and
- plays a card into the arena, thus losing it from his hand. ]]--
- function opponent:getCard(inboundCard)
- table.insert(opponent.hand, (#opponent.hand+1), inboundCard)
- end
- -- Play a card
- function opponent:playCard()
- if #opponent.hand == 0 then game.condition = "stop"
- else
- table.insert(arena.opponentPile, opponent.hand[1])
- table.remove(opponent.hand, 1)
- end
- end
- --[[ This deals the cards to the player and the opponent one at a time.
- Used at the start of a match. ]]--
- 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. It receives a table as the passed
- parameter and procedes to permute the values of the table's indexes. ]]--
- function shuffle(pile)
- for i=#pile,1,-1 do
- toMove = safeRandom(i)
- pile[toMove], pile[i] = pile[i], pile[toMove] end
- end
- function game:setup()
- TheDeck:make()
- shuffle(carddeck)
- TheDeck:deal()
- end
- function game:reset()
- carddeck = {}
- player.hand={}
- opponent.hand={}
- arena.playerPile={} ; arena.opponentPile={}
- game.condition = "go"
- battleWinner = ""
- roundCounter = 0
- TheDeck:make()
- shuffle(carddeck)
- TheDeck:deal()
- end
- --[[ This is where the game starts. Nothing really happens before this
- function is called. ]]--
- function game:start()
- os.execute("cls")
- io.write("-- War, the classic card game --\n")
- io.write("The deck has been shuffled and dealt.\n")
- print("You have " .. #player.hand .. " cards.")
- print("Opponent has " .. #opponent.hand .. " cards.")
- end
- function game:over()
- if #player.hand == 0 or #opponent.hand == 0 then return 1
- else return nil end
- end
- function game:match()
- player.playCard() ; opponent.playCard()
- if game.condition == "stop" then
- else
- arena.battle()
- arena.awardCards()
- end
- end
- function arena:awardCards()
- local cardsToBeAwarded = {}
- for i=1,#arena.playerPile do
- table.insert(cardsToBeAwarded, arena.playerPile[i]) end
- for i=1,#arena.opponentPile do
- table.insert(cardsToBeAwarded, (i+#arena.playerPile), arena.opponentPile[i]) end
- shuffle(cardsToBeAwarded)
- if battleWinner == "player" then
- for i=1,#cardsToBeAwarded do
- table.insert(player.hand, cardsToBeAwarded[i]) end
- elseif battleWinner == "opponent" then
- for i=1,#cardsToBeAwarded do
- table.insert(opponent.hand, cardsToBeAwarded[i]) end
- end
- arena.playerPile = {}
- arena.opponentPile = {}
- end
- function countHands()
- end
- function arena:battle()
- if #arena.playerPile == 0 or #arena.opponentPile == 0 then
- elseif arena.playerPile[#arena.playerPile].value ==
- arena.opponentPile[#arena.opponentPile].value then
- arena.war()
- elseif arena.playerPile[#arena.playerPile].value >
- arena.opponentPile[#arena.opponentPile].value then battleWinner = "player"
- elseif arena.playerPile[#arena.playerPile].value <
- arena.opponentPile[#arena.opponentPile].value then battleWinner = "opponent"
- end
- end
- function arena:war()
- player.playCard() ; player.playCard()
- opponent.playCard() ; opponent.playCard()
- arena.battle()
- end
- function game:over()
- if #player.hand == 0 then file:write("Loss - ")
- elseif #opponent.hand == 0 then file:write("Win - ")
- else file:write("Error: Unable to determine winner.\n")
- print(#player.hand) ; print(#opponent.hand)
- end
- file:write(roundCounter .. "\n")
- end
- --[[ Main body of the program begins here. ]] --
- if arg[1] == nil then arg[1] = 1 end
- TheDeck:make()
- shuffle(carddeck)
- TheDeck:deal()
- for i=arg[1],0,-1 do
- game.reset()
- while game.condition == "go" do
- game.match()
- roundCounter = roundCounter+1
- if #player.hand > 0 and #opponent.hand > 0 then game.condition = "go" end
- end
- game.over()
- end
- game.condition="go"
- file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement