Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Pig Test

turn = "computer"
playerscore = 0
computerscore = 0
tempplayerscore = 0
tempcomputerscore = 0
rollstate = "roll"
roll = 0
choice = {"roll","hold","roll","roll","hold","roll","hold","roll","roll"}
while (computerscore < 100) and (playerscore < 100) 
do
    if (turn == "computer") 
    then
        rollstate = "roll"
        while (rollstate == "roll")
        do
            roll = math.random(6)
            if (roll == 1) 
            then
                tempcomputerscore = 0
                rollstate = "fail"
            else
                tempcomputerscore = tempcomputerscore + roll
                print ("The computer rolled a " .. roll ..  " with an accumulated roll score of " .. tempcomputerscore)
                rollstate = choice[ math.random( #choice ) ]
                roll = math.random(6)
            end
        end
        if (rollstate == "hold") 
        then
            computerscore = computerscore + tempcomputerscore
            tempcomputerscore = 0
            roll = 0
            print ("The computer's score thus far is " ..computerscore)
            turn = "player"        
        end
        if (rollstate == "fail")
        then
            tempcomputerscore = 0
            roll = 0
            print ("The computer rolled a 1, losing all accumulated temporary points. The computer has a total score of ".. computerscore)
            turn = "player"
        end
    end
    if (turn == "player") 
    then
        print ("roll or hold?")
        rollstate = io.read()
        while (rollstate == "roll")
        do
            roll = math.random(6)
            if (roll == 1) then
                tempplayerscore = 0
                rollstate = "fail"
            else
                tempplayerscore = tempplayerscore + roll
                print ("You rolled a " ..roll.. " with a total accumulated temporary score of " ..tempplayerscore)
                print ("roll or hold?")
                rollstate = io.read()
                roll = math.random(6)
            end
        end
        if (rollstate == "hold")
        then
            playerscore = playerscore + tempplayerscore
            print ("Your score thus far is " ..playerscore)
            tempplayerscore = 0
            roll = 0
            turn = "computer"
        end
        if (rollstate == "fail") 
        then
            tempplayerscore = 0
            roll = 0
        end
        turn = "computer"
    end
end
if (playerscore < 100 and computerscore < 100) 
then
    turn = "computer"
elseif (playerscore >= 100) 
    then
    print ("player wins with " ..playerscore.. " points")
    computerscore = 0
    playerscore = 0
    tempplayerscore = 0
    tempcomputerscore = 0
    roll = 0
    turn = "computer"
elseif (computerscore >= 100) then
    print ("computer wins with ".. computerscore.. " points")
    computerscore = 0
    playerscore = 0
    tempplayerscore = 0
    tempcomputerscore = 0
    roll = 0
end

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.