Advertisement
Guest User

Uno card Game in lua

a guest
Oct 20th, 2016
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.51 KB | None | 0 0
  1. -- Todo:
  2. --  1) Make all text input use arrow selector instead
  3. --  2) Allow user to have infinte players. (insert deck into istelf when the amount left in the deck is less than X)
  4. --  3) Add "Colorblind" Option
  5. --  4) Add normal computer use (dont use term.setBackgroundColor() and make everything in "colorblind" mode)
  6. term.clear()
  7. term.setCursorPos(1,1)
  8. print("Card Games\n\nChoose a game:\n\n1) Uno\n2) Go fish")
  9. repeat
  10.   game = string.lower(read())
  11.   if type(tonumber(game)) == "number" then
  12.     if game == "1" then
  13.       game = "uno"
  14.     elseif game == "2" then
  15.       game = "go fish"
  16.     end
  17.   end
  18. until game == "uno" or game == "go fish"
  19. --Uno
  20. if game == "uno" then
  21.   deck = {"R0", "R1", "R1", "R2", "R2", "R3", "R3", "R4", "R4", "R5", "R5", "R6", "R6", "R7", "R7", "R8", "R8", "R9", "R9", "RSK", "RSK", "RR", "RR", "RD2", "RD2",
  22.     "Y0", "Y1", "Y1", "Y2", "Y2", "Y3", "Y3", "Y4", "Y4", "Y5", "Y5", "Y6", "Y6", "Y7", "Y7", "Y8", "Y8", "Y9", "Y9", "YSK", "YSK", "YR", "YR", "YD2", "YD2",
  23.     "B0", "B1", "B1", "B2", "B2", "B3", "B3", "B4", "B4", "B5", "B5", "B6", "B6", "B7", "B7", "B8", "B8", "B9", "B9", "BSK", "BSK", "BR", "BR", "BD2", "BD2",
  24.     "G0", "G1", "G1", "G2", "G2", "G3", "G3", "G4", "G4", "G5", "G5", "G6", "G6", "G7", "G7", "G8", "G8", "G9", "G9", "GSK", "GSK", "GR", "GR", "GD2", "GD2",
  25.     "AW", "AW", "AW", "AW", "AD4", "AD4", "AD4", "AD4"}
  26.   --Players VS Robots!
  27.   players = 0
  28.   robots = 0
  29.   print("How many human players? (1-4)")
  30.   repeat
  31.     players = tonumber(read())
  32.   until type(players) == "number" and (players < 5 and players > 0)
  33.   if players < 4  and players > 1 then
  34.     print("How many robots? (0-"..tostring(4 - players)..")")
  35.     repeat
  36.       robots = tonumber(read())
  37.     until type(robots) == "number" and ((robots < 5 - players) and robots > -1)
  38.   end
  39.   if players == 1 then
  40.     print("How many robots? (1-"..tostring(4 - players)..")")
  41.     repeat
  42.       robots = tonumber(read())
  43.     until type(robots) == "number" and ((robots < 5 - players) and robots > 0)
  44.   end
  45.   --Shuffling!
  46.   function shuffle()
  47.     for i = 1, 50 do
  48.       math.randomseed(1000*os.time())
  49.       for j = #deck, 2, -1 do
  50.         k = math.random(i)
  51.         deck[j], deck[k] = deck[k], deck[j]
  52.       end
  53.     end
  54.   end
  55.   shuffle()
  56.   --Dealing!
  57.   play1 = {""}
  58.   play2 = {""}
  59.   play3 = {""}
  60.   play4 = {""}
  61.   robot1 = {""}
  62.   robot2 = {""}
  63.   robot3 = {""}
  64.   function deal(playdeck)
  65.     for i = 1, 7 do
  66.       table.insert(playdeck, table.remove(deck))
  67.     end
  68.     table.remove(playdeck, 1)
  69.   end
  70.   deal(play1)
  71.   if players >= 2 then
  72.     deal(play2)
  73.   end
  74.   if players >= 3 then
  75.     deal(play3)
  76.   end
  77.   if players == 4 then
  78.     deal(play4)
  79.   end
  80.   if robots >= 1 then
  81.     deal(robot1)
  82.   end
  83.   if robots >= 2 then
  84.     deal(robot2)
  85.   end
  86.   if robots == 3 then
  87.     deal(robot3)
  88.   end
  89.   --Gameplay and Visuals!
  90.   discard = {""}
  91.   table.insert(discard, table.remove(deck))
  92.   table.remove(discard, 1)
  93.   player = play1
  94.   playnum = 1
  95.   goneout = 0
  96.   direction = 1
  97.   realPlayNum = 1
  98.   playAction = "Game Started."
  99.   while goneout == 0 do
  100.     shuffle()
  101.     term.clear()
  102.     if playnum > players then
  103.       playtype = true
  104.     else
  105.       playtype = false
  106.     end
  107.     function cardtype(cardspecimen, setback, wilds)
  108.       if cardspecimen ~= "RR" then
  109.         local subcolor = string.sub(cardspecimen, 2)
  110.         color = string.gsub(cardspecimen, subcolor, "")
  111.         card = string.sub(cardspecimen, 2)
  112.       else
  113.         color = "R"
  114.         card = "R"
  115.       end
  116.       cardstype = {color, card}
  117.       if setback then
  118.         if color == "R" then
  119.           term.setBackgroundColor(colors.red)
  120.         elseif color == "B" then
  121.           term.setBackgroundColor(colors.blue)
  122.         elseif color == "Y" then
  123.           term.setBackgroundColor(colors.yellow)
  124.         elseif color == "G" then
  125.           term.setBackgroundColor(colors.green)
  126.         else
  127.           term.setBackgroundColor(colors.white)
  128.           if wilds then
  129.             if color == "A" then
  130.               term.setBackgroundColor(colors.white)
  131.             elseif color == "r" then
  132.               term.setBackgroundColor(colors.red)
  133.             elseif color == "g" then
  134.               term.setBackgroundColor(colors.green)
  135.             elseif color == "b" then
  136.               term.setBackgroundColor(colors.blue)
  137.             else
  138.               term.setBackgroundColor(colors.yellow)
  139.             end
  140.           end
  141.         end
  142.       end
  143.     end
  144.     term.setTextColor(colors.white)
  145.     term.setCursorPos(1,1)
  146.     print("Player "..playnum)
  147.     cardtype(discard[#discard],false,false)
  148.     color = string.lower(color)
  149.     if color == "a" then
  150.       color = "Any"
  151.       term.setTextColor(colors.white)
  152.     elseif color == "r" then
  153.       color = "Red"
  154.       term.setTextColor(colors.red)
  155.     elseif color == "g" then
  156.       color = "Green"
  157.       term.setTextColor(colors.green)
  158.     elseif color == "b" then
  159.       color = "Blue"
  160.       term.setTextColor(colors.blue)
  161.     elseif color == "y" then
  162.       color = "Yellow"
  163.       term.setTextColor(colors.yellow)
  164.     end
  165.     term.setCursorPos(1,2)
  166.     print("Color: "..color)
  167.     term.setTextColor(colors.white)
  168.     term.setCursorPos(1,4)
  169.     print(playAction)
  170.     cardtype(discard[#discard],true,true)
  171.     term.setTextColor(colors.gray)
  172.     term.setCursorPos(21,2)
  173.     print("+--------+")
  174.     term.setCursorPos(21,3)
  175.     print("|        |")
  176.     term.setCursorPos(21,4)
  177.     print("|        |")
  178.     term.setCursorPos(21,5)
  179.     print("|        |")
  180.     term.setCursorPos(21,6)
  181.     print("|        |")
  182.     term.setCursorPos(21,7)
  183.     print("|        |")
  184.     term.setCursorPos(21,8)
  185.     print("|        |")
  186.     term.setCursorPos(21,9)
  187.     print("|        |")
  188.     term.setCursorPos(21,10)
  189.     print("|        |")
  190.     term.setCursorPos(21,11)
  191.     print("+--------+")
  192.     term.setCursorPos(33,11)
  193.     term.setBackgroundColor(colors.black)
  194.     term.setTextColor(colors.white)
  195.     print("Arrows: Select Card")
  196.     term.setCursorPos(33,12)
  197.     print("Enter: Discard Card")
  198.     term.setCursorPos(33,13)
  199.     print("Space: Draw Card")
  200.     term.setTextColor(colors.gray)
  201.     for i = 1, #player do
  202.       cardtype(player[i],true,false)
  203.       term.setCursorPos(2*i-1,14)
  204.       print("-+")
  205.       term.setCursorPos(2*i-1,15)
  206.       print(" |")
  207.       term.setCursorPos(2*i-1,16)
  208.       print(" |")
  209.       term.setCursorPos(2*i-1,17)
  210.       print(" |")
  211.       term.setCursorPos(2*i-1,18)
  212.       print(" |")
  213.       for j = 1, string.len(card) do
  214.         term.setCursorPos(2*i-1,14+j)
  215.         print(string.sub(card, j, j))
  216.       end
  217.     end
  218.     function showDiscard(printcards)
  219.       cardtype(discard[#discard],true,true)
  220.       if card == "R" then
  221.         card = "Reverse"
  222.       elseif card == "SK" then
  223.         card = "Skip"
  224.       elseif card == "D2" then
  225.         card = "Draw 2"
  226.       elseif card == "W" then
  227.         card = "Wild"
  228.       elseif card == "D4" then
  229.        card = "Draw 4 W"
  230.       end
  231.       if printcards then
  232.         term.setCursorPos(22,3)
  233.         print(card)
  234.         term.setCursorPos(30 - string.len(card),10)
  235.         print(card)
  236.       end
  237.     end
  238.     showDiscard(true)
  239.     term.setCursorPos(1,19)
  240.     term.setBackgroundColor(colors.black)
  241.     term.setTextColor(colors.white)
  242.     write("                                                   ")
  243.     arrow = 1
  244.     local function printarrow()
  245.       term.setCursorPos(2*arrow - 3,19)
  246.       write(" ")
  247.       term.setCursorPos(2*arrow + 1,19)
  248.       write(" ")
  249.       term.setCursorPos(2*arrow - 1,19)
  250.       write("^")
  251.     end
  252.     printarrow()
  253.     local endturn = false
  254.     if not(playtype) then
  255.       while endturn == false do
  256.         event, key = os.pullEvent("key")
  257.         if event == "key" then
  258.           if key == 203 then
  259.             if arrow > 1 then
  260.               arrow = arrow - 1
  261.               printarrow()
  262.             end
  263.           elseif key == 205 then
  264.             if arrow < #player then
  265.               arrow = arrow + 1
  266.               printarrow()
  267.             end
  268.           elseif key == 28 then
  269.             cardtype(player[arrow],false,false)
  270.             playcard = cardstype
  271.             cardtype(discard[#discard],false,false)
  272.             matchcard = cardstype
  273.             if (string.lower(playcard[1]) == string.lower(matchcard[1]) or playcard[2] == matchcard[2]) and (playcard[2] ~= "W" and playcard[2] ~= "D4") then
  274.               table.insert(discard, table.remove(player, arrow))
  275.               endturn = true
  276.               playAction = "Player "..realPlayNum.." discarded \ncard "..playcard[1]..playcard[2]
  277.             elseif playcard[2] == "W" or playcard[2] == "D4" then
  278.               table.remove(player, arrow)
  279.               endturn = true
  280.               term.setCursorPos(1,19)
  281.               write("What color? (Red, Blue, Green, Yellow) ")
  282.               repeat
  283.                 inputcolor = string.lower(read())
  284.               until inputcolor == "red" or inputcolor == "blue" or inputcolor == "green" or inputcolor == "yellow"
  285.               if inputcolor == "red" then
  286.                 table.insert(discard, "r"..playcard[2])
  287.               elseif inputcolor == "blue" then
  288.                 table.insert(discard, "b"..playcard[2])
  289.               elseif inputcolor == "green" then
  290.                 table.insert(discard, "g"..playcard[2])
  291.               elseif inputcolor == "yellow" then
  292.                 table.insert(discard, "y"..playcard[2])
  293.               end
  294.               playAction = "Player "..realPlayNum.." discarded \na wild."
  295.             elseif matchcard[1] == "A" then
  296.               table.insert(discard, table.remove(player, arrow))
  297.               endturn = true
  298.               playcard = "None"
  299.               matchcard = "None"
  300.               showDiscard(false)
  301.               playAction = "Player "..realPlayNum.." discarded \na wild."
  302.             end
  303.           elseif key == 57 then
  304.             if #player < 25 then
  305.               table.insert(player, table.remove(deck))
  306.               endturn = true
  307.               playcard = "None"
  308.               matchcard = "None"
  309.             end
  310.             playAction = "Player "..realPlayNum.." drew a \ncard."
  311.           end
  312.         end
  313.       end
  314.     end
  315.     --Robots!
  316.     if playtype then
  317.       robot1 = {"AW", "rD4", "GR", "BSK", "YD2", "R9"}
  318.       for i = 1, #player do
  319.         cardtype(player[i],false,false)
  320.         playcard = cardstype
  321.         cardtype(discard[#discard],false,false)
  322.         matchcard = cardstype
  323.         if (string.lower(playcard[1]) == string.lower(matchcard[1]) or playcard[2] == matchcard[2]) and (playcard[2] ~= "W" and playcard[2] ~= "D4") then
  324.           table.insert(discard, table.remove(player, i))
  325.           endturn = true
  326.           playAction = "Robot "..realPlayNum.." discarded \ncard "..playcard[1]..playcard[2]
  327.         elseif playcard[2] == "W" or playcard[2] == "D4" then
  328.           table.remove(player, i)
  329.           endturn = true
  330.           inputcolor = math.random(4)
  331.           if inputcolor == 1 then
  332.             table.insert(discard, "r"..playcard[2])
  333.           elseif inputcolor == 2 then
  334.             table.insert(discard, "b"..playcard[2])
  335.           elseif inputcolor == 3 then
  336.             table.insert(discard, "g"..playcard[2])
  337.           elseif inputcolor == 4 then
  338.             table.insert(discard, "y"..playcard[2])
  339.           end
  340.           playAction = "Robot "..realPlayNum.." discarded \na wild."
  341.         elseif matchcard[1] == "A" then
  342.           table.insert(discard, table.remove(player, i))
  343.           endturn = true
  344.           playcard = "None"
  345.           matchcard = "None"
  346.           playAction = "Robot "..realPlayNum.." discarded \na wild."
  347.         end
  348.       end
  349.       if endturn == false then
  350.         table.insert(player, table.remove(deck))
  351.         playAction = "Robot "..realPlayNum.." drew a \ncard."
  352.       end
  353.       read()
  354.     end
  355.     --Special Cards and Next Player!
  356.     if #player == 0 then
  357.       goneout = 1
  358.       if playtype then
  359.         print("\nRobot "..realPlayNum.." Won!")
  360.       else
  361.         print("\nPlayer "..playnum.." Won!")
  362.       end
  363.     end
  364.     function checkdeck()
  365.       if #deck == 0 then
  366.         for i = 1, #discard do
  367.           table.insert(deck,table.remove(discard))
  368.           shuffle()
  369.         end
  370.         table.insert(discard,table.remove(deck))
  371.       end
  372.     end
  373.     function nextplayer()
  374.       playnum = playnum + direction
  375.       if playnum == 0 then
  376.         playnum = players + robots
  377.       elseif playnum == -1 then
  378.         playnum = players + robots - 1
  379.       end
  380.       if playnum == players + 1 and robots > 0 then
  381.         player = robot1
  382.       elseif playnum == players + 2 and robots > 1 then
  383.         player = robot2
  384.       elseif playnum == players + 3 and robots > 2 then
  385.         player = robot3
  386.       elseif playnum == players + robots + 1 then
  387.         playnum = 1
  388.       elseif playnum == players + robots + 2 then
  389.         playnum = 2
  390.       end
  391.       if playnum == 1 then
  392.         player = play1
  393.       elseif playnum == 2 then
  394.         player = play2
  395.       elseif playnum == 3 then
  396.         player = play3
  397.       elseif playnum == 4 then
  398.         player = play4
  399.       end
  400.       if playnum > players then
  401.         realPlayNum = playnum - players
  402.       else
  403.         realPlayNum = playnum
  404.       end
  405.     end
  406.     if playcard[2] == "R" then
  407.       if players + robots == 2 then
  408.         nextplayer()
  409.         if playtype then
  410.           playAction = "Robot "..realPlayNum.." got \nskipped."
  411.         else
  412.           playAction = "Player "..playnum.." got \nskipped."
  413.         end
  414.       else
  415.         direction = direction * -1
  416.         if playtype then
  417.           playAction = "Robot "..realPlayNum.." reversed \nthe direction."
  418.         else
  419.           playAction = "Player "..playnum.." reversed \nthe direction."
  420.         end
  421.       end
  422.     elseif playcard[2] == "SK" then
  423.       nextplayer()
  424.       if playtype then
  425.           playAction = "Robot "..realPlayNum.." got \nskipped."
  426.         else
  427.           playAction = "Player "..playnum.." got \nskipped."
  428.         end
  429.     end
  430.     if playcard[2] == "D2" then
  431.       nextplayer()
  432.       for i = 1, 2 do
  433.         checkdeck()
  434.         table.insert(player, table.remove(deck))
  435.       end
  436.       if playtype then
  437.         playAction = "Robot "..realPlayNum.." had to \ndraw two cards."
  438.       else
  439.         playAction = "Player "..playnum.." had to \ndraw two cards."
  440.       end
  441.     elseif playcard[2] == "D4" then
  442.       nextplayer()
  443.       for i = 1, 4 do
  444.         checkdeck()
  445.         table.insert(player, table.remove(deck))
  446.       end
  447.       if playtype then
  448.         playAction = "Robot "..realPlayNum.." had to \ndraw four cards."
  449.       else
  450.         playAction = "Player "..playnum.." had to \ndraw four cards."
  451.       end
  452.     end
  453.     checkdeck()
  454.     nextplayer()
  455.     if playnum > players then
  456.       currentPlayer = " Robot"
  457.     else
  458.       currentPlayer = "Player"
  459.     end
  460.     if goneout == 0 then
  461.       term.clear()
  462.       term.setCursorPos(13,7)
  463.       print("+------------------------+")
  464.       term.setCursorPos(13,8)
  465.       print("|                        |")
  466.       term.setCursorPos(13,9)
  467.       print("|    "..currentPlayer.." "..realPlayNum.."'s turn!    |")
  468.       term.setCursorPos(13,10)
  469.       print("|                        |")
  470.       term.setCursorPos(13,11)
  471.       print("+------------------------+")
  472.       term.setCursorPos(13,13)
  473.       os.sleep(2)
  474.     end
  475.   end
  476. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement