Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

BlackJack (the card game)

I have an idea for card games, their are very little tutorials on gard games for LUA.
If I can complete this card game, I could build on it, change the game by changing the rules, changing the amount of cards dealt and so fourth.

The Card game here is written in python, and I would like to change it to LUA programming Language.
The code isnt to long so as not to give it to much trouble. First here is the Logic of the Game.

- - BlackJack Planning Phase
- - Dealer Cards
- - Player Cards
- - Deal the Cards 
- - Display the Cards
- - Sum of the Dealer
- - Sum of the Player                        
- - Compare the sum of the Cards, D vs. P
- - The Rules
- - if P cards sum is > 21 = Bust 
- - if P cards sum is < 21 = Option : Hit or Stay
- - if Option P Stay compare of, D vs. P 
- - if P sum is < 21 && and > D sum then P Wins !
- - if P sum is < D sum then P loses !

This is from a Youtube Video (22min long)
- - Code a Game of BlackJack withPython
- -

Now here is the Code (Written in Python) :...
~~~~~~~

- - Dealer Cards (This should be a Table )
        dealer_cards [ ]
- - Player Cards (This should be a Table )

player_cards [ ]

- - Deal the Cards (where len = length of string)
- - (Display the Cards)
   while len(dealer_cards) ~= 2 
     dealer_cards.append(math.random(1,11))
        if len(dealer_cards) ==2 

print(“Dealer Has x &”, dealer_cards[1])

- - Player Cards (Display the Cards)
   while len(playler_cards) ~= 2 
     player_cards.append(math.random(1,11))
        if len(player_cards) ==2 

print(“You Have x &”, player_cards)

- - Sum of the Dealer Cards
  If sum(dealer_cards) ==21
   print(“Dealer has 21 and Wins”)
    elif sum(dealer_cards) > 21

print(Dealer has Busted)


    • Sum of the Player Cards
      while sum(player_cards) < 21
      action taken = str(input(“Do you want to stay or Hit ? ))
      If action taken == “hit” :
      player_cards.append(math.random(1,11))
      print(“You now have a total of” + str(sum(player_cards))
      ”From these Cards, player_cards)
      else:
      print(“The Dealer has a total of ” + str(sum(dealer_cards)) +
      “with”, dealer_cards)
      print(“You now have a total of” + str(sum(player_cards)) +
      “with”, player_cards)
      If sum(dealer_cards) > sum(player_cards):
      print(“Dealer Wins”)
      else:
      print(“You Win !”)
      break
      If sum(player_cards) > 21
      print(“You Busted, Dealer Wins”)
      elif sum(player_cards) == 21:
      print(“You Win”)
      ~~~~~~~~~~

Comments

  • dave1707dave1707 Mod
    edited August 11 Posts: 6,230

    @kendog400 There are plenty of card game examples on the forum. Do a forum search to see what you can find.

  • dave1707dave1707 Mod
    Posts: 6,230

    @kendog400 Here's a start for your card game. Some code to create a deck of cards. You can alter this to create what you want.

    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    
    function setup()
        rectMode(CENTER)
        creatDeck()
    end
    
    function draw()
        background(207, 228, 211, 255)
        for value=1,13 do
            for suit=1,4 do
                sprite(cards[suit][value],value*70,HEIGHT-suit*150)
            end
        end
    end
    
    function creatDeck()
        cards={}
        local pic={"♦️","♣️","♥️","♠️"}
        local val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
        for s=1,4 do
            cards[s]={}
            for v=1,13 do
                img=image(50,70)
                setContext(img)
                fill(255)
                stroke(0, 235, 255, 255)
                strokeWidth(3)
                rect(25,35,50,70)  
                sprite("SpaceCute:Star",25,35,75)
                fontSize(13)
                text(pic[s],12,57)
                text(pic[s],38,13)
                fill(255,0,0)
                fontSize(25)
                text(val[v],25,35)
                setContext()
                cards[s][v]=img
            end
        end
    end
    
  • Posts: 33

    Thank You....

  • dave1707dave1707 Mod
    edited August 14 Posts: 6,230

    @kendog400 Here's a version based on what you show above. It's a total waste of Codea's capabilities, but it can be done. Press "h" for hit, "s" for stay, and "a" to play again. It's in portrait mode and you should slide the output window all the way up to show all of the print statements. I think it runs without errors, but I didn't test it that well.

    EDIT: Made minor change for dealer.

    supportedOrientations(PORTRAIT_ANY)
    
    function setup()
        showKeyboard()
        output.clear()
        player,dealer={},{}
        for z=1,2 do
            table.insert(player,math.random(11))
            table.insert(dealer,math.random(11))
        end   
        showPlayer(1)
    end
    
    function draw()
        background(0)
        fill(255)
        if ans=="s" then
            output.clear()
            showDealer()
            showPlayer(2)
            showWinner()        
        elseif ans=="h" then
            output.clear()
            table.insert(player,math.random(11))
            showPlayer(1)
        elseif ans=="a" then
            setup()
        end
        ans=""        
    end
    
    function showDealer()
        dealerSum=0
        for a=1,#dealer do
            dealerSum=dealerSum+dealer[a]
        end
        while dealerSum<17 do
            dealerSum=0
            table.insert(dealer,math.random(11))
            for a=1,#dealer do
                dealerSum=dealerSum+dealer[a]
            end
        end
        print("The dealer has a total of "..dealerSum)
        print("from these cards "..table.concat(dealer," "))
    end
    
    function showPlayer(a)
        playerSum=0
        for a=1,#player do
            playerSum=playerSum+player[a]
        end
        print("You have a total of "..playerSum)
        print("from these cards "..table.concat(player," "))
        if playerSum>21 then
            showWinner()
        elseif a==1 then
            print("\n\nDo you want to stay(s) or hit(h)") 
        end   
    end
    
    function showWinner()
        if playerSum>21 then
            print("Dealer wins!")
        elseif dealerSum>21 then
            print("You win!")
        elseif playerSum>dealerSum then
            print("You win!")
        else
            print("Dealer wins!")
        end
        print("Play again?(a)")
    end
    
    function keyboard(k)
        ans=k
    end
    
Sign In or Register to comment.