Howdy, Stranger!

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

Help needed to de-bug card game

edited July 21 in Code Sharing Posts: 15

Card geme : Acey-Ducey, stuck on line 153, repeat is wrong word,
Can anyone help ? Heres the code below :smile:

--aceydeucy.lua

--Displays the intro--

function showIntro()

  print [[
ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER
THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP
YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING
ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE
A VALUE BETWEEN THE FIRST TWO.
IF YOU DO NOT WANT TO BET, ENTER A $0 BET
]]

end

--[[
  Initialises the Global BankBalance and Random Number Generator
]]
function initGame()

 bankBalance = 100

 math.randomseed( os.time() )
end

--[[
  shows the card type
]]
function showCard(val)
  local cards={ nil, 2, 3, 4, 5, 6, 7, 8, 9, 10, "JACK", "QUEEN", "KING", "ACE"} 
  return cards[val]
  end

--[[
  Displays the current bank balance (global)
]]
function showBalance()

print("YOU NOW HAVE "..bankBalance.." DOLLARS.")
print()
end

--[[
  Adjusts the global balance
]]
function adjustBalance( amount )
  bankBalance = bankBalance + amount
  showBalance()
end  

--[[
  Generates a random card
]]
function dealCard()
  local nextCardValue = math.random(2,14)
  return nextCardValue
  end

--[[
  Deals cards returns a collection of two
]]
function dealCards()
 print("HERE ARE THE NEXT TWO CARDS:")
 local cardValues = {first=dealCard(),second=dealCard()}
 print( "First Card:" .. showCard(cardValues.first) )
 print( "Second Card:" .. showCard(cardValues.second) )  

return cardValues

end

--[[
  Inputs a bet value
]]
function getBet()

  betOK = false
  local betAmount = 0

  -- Loop to get a valid bet or a zero bet
  repeat
  io.write("WHAT IS YOUR BET:");
  local amount = io.read();
  betAmount = tonumber(amount)
  if betAmount==0 then
    print("CHICKEN!!")
    print()
    betOK = true
  end
  if betAmount <= bankBalance then
      betOK = true
    else
      print("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.")
      print("YOU HAVE ONLY " .. bankBalance .. " DOLLARS TO BET.")
    end
  until betOK

    return betAmount
end

--[[
  Deals another card and compares values
]]
function playRound( cardValues )
  local playerCard = dealCard()
  local firstCard = cardValues.first 
  local secondCard = cardValues.second 

  print( "Dealer Card" .. showCard(playerCard) )  

  local minValue = math.min( firstCard, secondCard )
  local maxValue = math.max( firstCard, secondCard )

  if playerCard>=minValue and playerCard <=maxValue then
    print("YOU WIN!!!")
    return true
  end

  print("You Lose!!")
  return false
end


function endGameReplay()

  print[[
  SORRY, FRIEND, BUT YOU BLEW YOUR WAD.

  ]]

  io.write("TRY AGAIN (Y/N)")
  response = io.read()
  if repsonse == "Y" then
    return true
  else
    print("O.K., HOPE YOU HAD FUN!")  
    return false
  end

end

-- Main Loop - Here's when the action happens

showIntro()

done = false -- when true the game loop ends

initGame()

-- Game Loop
repeat
    local cardValues = dealCards()
    local betAmount = getBet()

    if betAmount == 0 then
      done=true -- user said they don't want to play anymore
    else
      if playRound(cardValues) then
        adjustBalance( betAmount )
      else
        adjustBalance( -betAmount )

        -- if no more money then ask to start again
        if bankBalance <= 0 then
          if endGameReplay()==true then
            -- start again
            initGame() -- reset variables
          end
          done=true
          end
      end
    end
Tagged:

Comments

  • edited July 21 Posts: 140

    "repeat" isn't a Lua keyword. You can use while(condition) .... end for a loop loop. If condition is false, it will skip running the loop and move on. If condition is always true, it will run until it his a break.

  • dave1707dave1707 Mod
    Posts: 5,996

    @kendog400 Anytime you post code, put 3 ~'s on a line before and after the code so it shows correctly.

  • Posts: 15

    Thanks, I'm still lost, but its good to get a response....

  • Posts: 15

    I"ve replaced the repeat with while ( ), but i dont know what to put in the brackets ( )....? Seems like I'm missing something...

  • dave1707dave1707 Mod
    Posts: 5,996

    @kendog400 Where did you get the code from. It looks like you copied it from somewhere and you think it's going to work in Codea. There's a lot of things in the above code that isn't going to work.

  • dave1707dave1707 Mod
    Posts: 5,996

    @syntonica repeat is valid. Try this code.

    function setup()
        a=0
    
        repeat
            a=a+1
            print(a)
        until a>15
    
    end
    
  • edited July 22 Posts: 140

    Ack! Sorry about that to the OP. Repeat/until will run at least once while a while may never run.

    I never use repeat/until since I never need it. :blush:

  • Posts: 15

    Yes, I copied it from a site on-line, I would like to program Card Games, so it is part of a study until I get my roll on...

  • Posts: 15

    The card Game was written LUA, but I see now that, it doesent mean it would work in Codea....In any event Thanks for the Help !

  • dave1707dave1707 Mod
    Posts: 5,996

    @kendog400 Do a forum search for cards games. You might come up with some interesting things.

  • dave1707dave1707 Mod
    Posts: 5,996

    @kendog400 If you want to play with card games, maybe this would help some.

    function setup()   
        rectMode(CENTER)
        suit={"♠️","♣️","♥️","♦️"}
        val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}    
    end
    
    function draw()
        background(40, 40, 50)
        cnt=0
        for x=1,4 do
            for y=1,13 do
                cnt=cnt+1
                card(x*100,y*70,cnt,1)
            end
        end
    end
    
    function card(x,y,v,sc)      -- x,y position    Card value 1 to 52   sc=size of card
        scale(sc)
        local c=(v-1)%13
        local s=(v-1)//13+1
        fill(255)
        stroke(0, 255, 182, 255)
        strokeWidth(3)
        rect(x,y,45,60)
        fill(255,0,0)
        if s<3 then
            fill(0)
        end
        fontSize(25)
        text(val[c+1],x,y)
        fontSize(14)
        text(suit[s],x-10,y+18) 
        text(suit[s],x+10,y-18) 
        scale(1/sc)
    end
    
Sign In or Register to comment.