Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. -- [Lua] The classic card game, War  (v.2.1) --
  2. f = assert(io.open("DataCollection.txt"))
  3. carddeck = {}
  4. TheDeck = {}
  5. player = { hand={} }
  6. opponent = { hand={} }
  7. arena = { playerPile={}, opponentPile={} }
  8. game ={ condition = "go" }
  9. battleWinner = ""
  10. roundCounter = 0
  11. file = io.open("DataCollection.txt", "a+")
  12.  
  13.  
  14. -- Standard seed based on time for use with safeRandom()
  15. math.randomseed(os.time())
  16.  
  17. --[[ This is simply because Lua's math.random() function is unreliable.  I've found
  18. that the numbers generated are hardly random until the third generated number, so
  19. I threw this together to give me the third randomly generated number every time I
  20. rand a random number.]]--
  21. function safeRandom(num)
  22.     a,b,c = math.random(num), math.random(num), math.random(num)
  23.     return c end
  24.  
  25. --[[ The deck constructor.  It produces a set of 52 cards, each containing a name(string),
  26. a value(number), a suit(string) and a color(string, derived from suit). ]]--
  27. function TheDeck:make()
  28.     for i=0,51 do
  29.         local freshCard={}
  30.         freshCard.value = (i % 13) + 2
  31.         local suit
  32.         local color
  33.         local name
  34.  
  35.         if i < 13 then freshCard.suit = "spades"
  36.         elseif i < 26 then freshCard.suit = "clubs"
  37.         elseif i < 39 then freshCard.suit = "hearts"
  38.         elseif i < 52 then freshCard.suit = "diamonds" end
  39.  
  40.         if freshCard.suit == "spades" or freshCard.suit == "clubs" then freshCard.color = "black"
  41.         else freshCard.color = "red" end
  42.  
  43.         if freshCard.value < 11 then freshCard.name = freshCard.value .. " of " .. freshCard.suit
  44.         elseif freshCard.value == 11 then freshCard.name = "Jack of " .. freshCard.suit
  45.         elseif freshCard.value == 12 then freshCard.name = "Queen of " .. freshCard.suit
  46.         elseif freshCard.value == 13 then freshCard.name = "King of " .. freshCard.suit
  47.         elseif freshCard.value == 14 then freshCard.name = "Ace of " .. freshCard.suit end
  48.  
  49.         carddeck[i+1] = freshCard end
  50. end
  51.  
  52.  
  53. --[[ These two functions are how the opponent adds cards to his hand and
  54. plays a card into the arena, thus losing it from his hand. ]]--
  55. function player:getCard(inboundCard)
  56.     table.insert(player.hand, (#player.hand+1), inboundCard)
  57. end
  58. -- Play a card
  59. function player:playCard()
  60.     if #player.hand == 0 then game.condition = "stop"
  61.     else
  62.     table.insert(arena.playerPile, player.hand[1])
  63.     table.remove(player.hand, 1)
  64.     end
  65. end
  66.  
  67.  
  68. --[[ These two functions are how the opponent adds cards to his hand and
  69. plays a card into the arena, thus losing it from his hand. ]]--
  70. function opponent:getCard(inboundCard)
  71.     table.insert(opponent.hand, (#opponent.hand+1), inboundCard)
  72. end
  73. -- Play a card
  74. function opponent:playCard()
  75.     if #opponent.hand == 0 then game.condition = "stop"
  76.     else
  77.         table.insert(arena.opponentPile, opponent.hand[1])
  78.         table.remove(opponent.hand, 1)
  79.     end
  80. end
  81.  
  82.  
  83. --[[ This deals the cards to the player and the opponent one at a time.
  84. Used at the start of a match. ]]--
  85. function TheDeck:deal()
  86.     for i=1,52,2 do
  87.         player:getCard(carddeck[i])
  88.         opponent:getCard(carddeck[i+1]) end
  89. end
  90.  
  91. --[[ This is the shuffle function.  It receives a table as the passed
  92. parameter and procedes to permute the values of the table's indexes. ]]--
  93. function shuffle(pile)
  94.     for i=#pile,1,-1 do
  95.         toMove = safeRandom(i)
  96.         pile[toMove], pile[i] = pile[i], pile[toMove] end
  97. end
  98.  
  99. function game:setup()
  100.     TheDeck:make()
  101.     shuffle(carddeck)
  102.     TheDeck:deal()
  103. end
  104.  
  105. function game:reset()
  106.     carddeck = {}
  107.     player.hand={}
  108.     opponent.hand={}
  109.     arena.playerPile={} ; arena.opponentPile={}
  110.     game.condition = "go"
  111.     battleWinner = ""
  112.     roundCounter = 0
  113.     TheDeck:make()
  114.     shuffle(carddeck)
  115.     TheDeck:deal()
  116. end
  117.  
  118.  
  119. --[[ This is where the game starts.  Nothing really happens before this
  120. function is called. ]]--
  121. function game:start()
  122.     os.execute("cls")
  123.     io.write("-- War, the classic card game --\n")
  124.     io.write("The deck has been shuffled and dealt.\n")
  125.     print("You have " .. #player.hand .. " cards.")
  126.     print("Opponent has " .. #opponent.hand .. " cards.")
  127. end
  128.  
  129. function game:over()
  130.     if #player.hand == 0 or #opponent.hand == 0 then return 1
  131.     else return nil end
  132. end
  133.  
  134. function game:match()
  135.     player.playCard() ; opponent.playCard()
  136.     if game.condition == "stop" then
  137.     else
  138.         arena.battle()
  139.         arena.awardCards()
  140.         end
  141. end
  142.  
  143. function arena:awardCards()
  144.     local cardsToBeAwarded = {}
  145.     for i=1,#arena.playerPile do
  146.         table.insert(cardsToBeAwarded, arena.playerPile[i]) end
  147.     for i=1,#arena.opponentPile do
  148.         table.insert(cardsToBeAwarded, (i+#arena.playerPile), arena.opponentPile[i]) end
  149.  
  150.     shuffle(cardsToBeAwarded)
  151.  
  152.     if battleWinner == "player" then
  153.         for i=1,#cardsToBeAwarded do
  154.             table.insert(player.hand, cardsToBeAwarded[i]) end
  155.     elseif battleWinner == "opponent" then
  156.         for i=1,#cardsToBeAwarded do
  157.             table.insert(opponent.hand, cardsToBeAwarded[i]) end
  158.     end
  159.  
  160.     arena.playerPile = {}
  161.     arena.opponentPile = {}
  162. end
  163.  
  164. function countHands()
  165.  
  166. end
  167.  
  168. function arena:battle()
  169.     if #arena.playerPile == 0 or #arena.opponentPile == 0 then
  170.         elseif arena.playerPile[#arena.playerPile].value ==
  171.             arena.opponentPile[#arena.opponentPile].value then
  172.             arena.war()
  173.         elseif arena.playerPile[#arena.playerPile].value >
  174.             arena.opponentPile[#arena.opponentPile].value then battleWinner =  "player"
  175.         elseif arena.playerPile[#arena.playerPile].value <
  176.             arena.opponentPile[#arena.opponentPile].value then battleWinner = "opponent"
  177.     end
  178. end
  179.  
  180. function arena:war()
  181.     player.playCard() ; player.playCard()
  182.     opponent.playCard() ; opponent.playCard()
  183.     arena.battle()
  184. end
  185.  
  186.  
  187. function game:over()
  188.     if #player.hand == 0 then file:write("Loss - ")
  189.     elseif #opponent.hand == 0 then file:write("Win - ")
  190.     else file:write("Error:  Unable to determine winner.\n")
  191.     print(#player.hand) ; print(#opponent.hand)
  192.     end
  193.     file:write(roundCounter .. "\n")
  194. end
  195.  
  196. --[[ Main body of the program begins here. ]] --
  197. if arg[1] == nil then arg[1] = 1 end
  198. TheDeck:make()
  199. shuffle(carddeck)
  200. TheDeck:deal()
  201. for i=arg[1],0,-1 do
  202.     game.reset()
  203.     while game.condition == "go" do
  204.         game.match()
  205.         roundCounter = roundCounter+1
  206.         if #player.hand > 0 and #opponent.hand > 0 then game.condition = "go" end
  207.         end
  208.     game.over()
  209. end
  210. game.condition="go"
  211. file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement