lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello, I became interrested in Lua and wanted to learn it. I came from Python and Ruby.
To learn the new language I have decided to make a small program, a roulette game, but I've encountered a small problem. My randomize function always returns the number 31, no matter what.
Here's the whole code. Thanks for any comments, I'm still learning and not too familiar with this language.

function menu()
    --a welcome menu
    print("Welcome to the Lua Roulette.")
    repeat
        print("Are you ready to start the game? (y/n)")
        answer = io.read()
    until answer == 'y' or answer == 'n'
    return answer
end

function sleep(t)
    --sleep function, to make the game more dramatic
    os.execute("sleep " .. tonumber(t))
end

function randomize(r)
    --returns a random number, always returns 31 no matter what.
    print("The wheel is spinning...")
    sleep(2)
    r = math.random(36)
    print("The ball has landed on the number " .. r .. ".\n")
    return r
end

function calculate(r, money, bet, loc)
    --This function is not finished yet
    if type(tonumber(bet)) == "number" then
        if loc == r then
            money = money + (35 + bet)
        return money
        end
    elseif loc == "manque" then
        if r <= 1 and r >= 18 then
            money = money + ((36 / 18) + bet)
        return money
        end
    elseif loc == "passe" then
        if r <= 19 and r >= 36 then
            money = money + ((36 / 18) + bet)
        return money
        end       
    end
end

function game()
    wheel = 0
    chips = 150
    bet = ""
    betLoc = "" -- on which number did the player bet
    repeat
        print("Place your bets! You can bet on numbers((1-36)type 'number'), Manque(1-18),\n")
        print(" Passe(19-36), red, black, even, odd or quit(q).\n")
        print("Your current balance is " .. "$" .. tostring(chips))
        betLoc = io.read()
        if betLoc == "q" then
            os.exit()
        else
            print("How many do you want to bet?\n")
            bet = io.read()
            bet = tonumber(bet)
            chips = chips - bet
            randomize(wheel)
            calculate(wheel, chips, bet, betLoc)
        end
    until chips == 0
        print("Sorry, you lost all your money.")
        print("Do you want to play again? (y/n)\n")
        --ask if the player wants to play again
        local playagain = io.read()
        if playagain == 'y' then
            menu()
        else
            os.exit()
        end
end
       
menu()
if answer == 'y' then
    game()
else
    os.exit()
end