Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick Loop Question
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hossack




PostPosted: Fri May 16, 2008 8:20 pm   Post subject: Quick Loop Question

is there a way to restart a Loop without using an reposting the whole loop inside the loop inside an if statement. In my program i have an if statement asking to play again and if they hit "y" then it suppossed to play again but i didn't want to post the whole loop inside it again. Is there another way?
Sponsor
Sponsor
Sponsor
sponsor
cavetroll




PostPosted: Fri May 16, 2008 8:33 pm   Post subject: RE:Quick Loop Question

You could put the loop in a procedure and call the procedure if they respond with "y"
hossack




PostPosted: Fri May 16, 2008 8:48 pm   Post subject: RE:Quick Loop Question

how do i do that?
gitoxa




PostPosted: Fri May 16, 2008 8:51 pm   Post subject: RE:Quick Loop Question

or, you could just exit the loop when they enter n
hossack




PostPosted: Fri May 16, 2008 8:54 pm   Post subject: RE:Quick Loop Question

it does do that i want it to restart when they enter y without posting the loop again inside an if statement
gitoxa




PostPosted: Fri May 16, 2008 9:05 pm   Post subject: RE:Quick Loop Question

You only exit if they type in n. If they type in y, then it'll loop back up to the top again. You don't need any if statements.

code:
loop
    %Does stuffs here
    %More stuff, for lulz
    put "Would you like ot play again?"
    get Answer
    exit when Answer = "n"
end loop
hossack




PostPosted: Fri May 16, 2008 9:10 pm   Post subject: RE:Quick Loop Question

i did that and the program doesnt restart to the starting of the loop
cavetroll




PostPosted: Fri May 16, 2008 9:11 pm   Post subject: RE:Quick Loop Question

Yup, you're right. I over thought that one way too much. Been learning about procedures in school so I'm used to using them to solve all problems.
Sponsor
Sponsor
Sponsor
sponsor
hossack




PostPosted: Fri May 16, 2008 9:17 pm   Post subject: RE:Quick Loop Question

so im pretty much stuck doing it the really long way?
gitoxa




PostPosted: Fri May 16, 2008 9:18 pm   Post subject: RE:Quick Loop Question

No, it means you've screwed up your code.


Mod edit: Let's keep away from the swear words.
hossack




PostPosted: Fri May 16, 2008 9:20 pm   Post subject: RE:Quick Loop Question

damn it.. lol everything works just not the play again thing
gitoxa




PostPosted: Fri May 16, 2008 9:23 pm   Post subject: RE:Quick Loop Question

Then re-do the exit part. Post what you have if you can't get it still.
hossack




PostPosted: Fri May 16, 2008 9:26 pm   Post subject: RE:Quick Loop Question

post my whole program or just he exit part?
hossack




PostPosted: Fri May 16, 2008 9:30 pm   Post subject: Re: Quick Loop Question

Turing:
xvar rock, paper, scissors, rules : int
var x, y, buttonnumber, buttonup, WinID : int
var answer : string
var key : string (1)
var userchoice, compchoice : int
var win, lose, tie : int := 0

process domusic
    Music.PlayFileLoop ("daft punk - musique.mp3")
end domusic

randomize

fork domusic
drawfill (1, 1, 102, 102) %Filling in Backround
locatexy (200, 280) %locating Rock, Paper, Scissors
colorback (102)
put "Welcome to Ryan's Rock, Paper, Scissors Game!"

rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
Pic.Draw (rock, 10, 10, picCopy)

paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
Pic.Draw (paper, 190, 10, picCopy)

scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
Pic.Draw (scissors, 380, 16, picCopy)

locatexy (500, 1)
put "By Ryan Hossack"

locatexy (1, 1) %Clicking to go to next page
put "click to continue"
buttonwait ("down", x, y, buttonnumber, buttonup)
if x > 0 and x < 640 and y > 0 and y < 400 then
    cls
end if

rules := Pic.FileNew ("scissorDM_468x389.bmp") %Putting in Picture for Rules
Pic.Draw (rules, 100, 5, picCopy)

locatexy (1, 1) %Clicking to go to next page
put "click to continue"
buttonwait ("down", x, y, buttonnumber, buttonup)
if x > 0 and x < 640 and y > 0 and y < 400 then
    cls
end if


locatexy (280, 300)
put "Pick Rock, Paper, or Scissors"
rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
Pic.Draw (rock, 10, 10, picCopy)

paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
Pic.Draw (paper, 190, 10, picCopy)

scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
Pic.Draw (scissors, 380, 16, picCopy)


loop
    buttonwait ("down", x, y, buttonnumber, buttonup)
    if x > 10 and x < 170 and y > 10 and y < 280 then %If choose Rock
        userchoice := 1
        randint (compchoice, 1, 3) %Computer Choosing

        if userchoice = 1 and compchoice = 3 then     %If computer chooses scissors then win
            delay (10)
            cls
            put "You Chose Rock"
            rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
            Pic.Draw (rock, 10, 10, picCopy)
            delay (10)
            locatexy (450, 400)
            put "Computer Chose Scissors"
            scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
            Pic.Draw (scissors, 380, 16, picCopy)
            locatexy (320, 280) %Saying You Won
            put "You Won" ..
            win := win + 1
            locatexy (300, 200) %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..
            locatexy (1, 350)
            put "Play again? y or n"
            get answer
            exit when answer = "n"
 
           
        elsif userchoice = 1 and compchoice = 2 then     %If computer chooses Paper then lose
            cls
            put "You chose Rock"
            rock := Pic.FileNew ("rock.bmp")     %Inserting Rock Image
            Pic.Draw (rock, 10, 10, picCopy)
            delay (10)
            locatexy (450, 400)
            put "Computer Chose Paper"
            paper := Pic.FileNew ("paper.bmp")     %Inserting Paper Image
            Pic.Draw (paper, 380, 10, picCopy)
            delay (100)
            locatexy (320, 280)     %Saying You Lost
            put "You Lost" ..
            lose := lose + 1
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..

        elsif userchoice = compchoice then     %If computer chooses Rock then tie
            cls
            put "You chose Rock"
            rock := Pic.FileNew ("rock.bmp")     %Inserting Rock Image
            Pic.Draw (rock, 10, 10, picCopy)
            delay (10)
            locatexy (450, 400)
            put "Computer Chose Rock"
            rock := Pic.FileNew ("rock.bmp")     %Inserting Rock Image
            Pic.Draw (rock, 380, 10, picCopy)
            delay (100)
            locatexy (320, 280)     %Saying You Tied
            put "You Tied" ..
            tie := tie + 1
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..
        end if



    elsif x > 180 and x < 400 and y > 10 and y < 280 then     %If choose Paper
        userchoice := 2
        randint (compchoice, 1, 3)     %Computer Choosing
        if userchoice = 2 and compchoice = 1 then     %If computer chooses rock you win
            cls
            put "You Chose Paper"
            paper := Pic.FileNew ("paper.bmp")     %Inserting Paper Image
            Pic.Draw (paper, 10, 10, picCopy)
            locatexy (450, 400)
            delay (10)
            put "Computer Chose Rock"
            rock := Pic.FileNew ("rock.bmp")     %Inserting Rock Image
            Pic.Draw (rock, 380, 10, picCopy)
            locatexy (320, 280)     %Saying You Won
            put "You Won" ..
            win := win + 1
            delay (10)
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..

        elsif userchoice = 2 and compchoice = 2 then     %If computer chooses paper tie
            cls
            put "You Chose Paper"
            paper := Pic.FileNew ("paper.bmp")     %Inserting Paper Image
            Pic.Draw (paper, 10, 10, picCopy)
            locatexy (450, 400)
            put "Computer Chose Paper"
            paper := Pic.FileNew ("paper.bmp")     %Inserting Paper Image
            Pic.Draw (paper, 450, 10, picCopy)
            locatexy (320, 280)     %Saying You Tied
            put "You Tied" ..
            tie := tie + 1
            delay (10)
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..
        elsif userchoice = 2 and compchoice = 3 then     %If computer chooses scissors lose
            cls
            put "You Chose Paper"
            paper := Pic.FileNew ("paper.bmp")     %Inserting Paper Image
            Pic.Draw (paper, 10, 10, picCopy)
            locatexy (450, 400)
            put "Computer Chose Scissors"
            scissors := Pic.FileNew ("scissors.bmp")     %Inserting Scissors Image
            Pic.Draw (scissors, 380, 16, picCopy)
            locatexy (320, 280)     %Saying You lost
            put "You Lost" ..
            lose := lose + 1
            delay (10)
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..
        end if



    elsif x > 420 and x < 620 and y > 16 and y < 280 then     %If choose Scissors
        userchoice := 3
        randint (compchoice, 1, 3)     %Computer Choosing
        if userchoice = 3 and compchoice = 1 then     %If computer chooses rock Lose
            cls
            put "You Chose Scissors"
            scissors := Pic.FileNew ("scissors.bmp")     %Inserting Scissors Image
            Pic.Draw (scissors, 10, 10, picCopy)
            delay (10)
            locatexy (450, 400)
            put "Computer Chose Rock"
            rock := Pic.FileNew ("rock.bmp")     %Inserting Rock Image
            Pic.Draw (rock, 380, 10, picCopy)
            locatexy (320, 280)     %Saying You lost
            put "You Lost" ..
            lose := lose + 1
            delay (10)
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..
        elsif userchoice = 3 and compchoice = 2 then     %If computer chooses paper Win
            cls
            put "You Chose Scissors"
            scissors := Pic.FileNew ("scissors.bmp")     %Inserting Scissors Image
            Pic.Draw (scissors, 10, 10, picCopy)
            delay (10)
            locatexy (450, 400)
            put "Computer Chose Paper"
            paper := Pic.FileNew ("paper.bmp")     %Inserting Paper Image
            Pic.Draw (paper, 380, 10, picCopy)
            delay (100)
            locatexy (320, 280)     %Saying You Lost
            put "You Win" ..
            win := win + 1
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..
        elsif userchoice = compchoice then         %If computer chooses Scissors then tie
            cls
            put "You Chose Scissors"
            scissors := Pic.FileNew ("scissors.bmp")     %Inserting Scissors Image
            Pic.Draw (scissors, 10, 10, picCopy)
            delay (10)
            locatexy (450, 400)
            put "Computer Chose Scissors"
            scissors := Pic.FileNew ("scissors.bmp")     %Inserting Scissors Image
            Pic.Draw (scissors, 380, 16, picCopy)
            delay (100)
            locatexy (320, 280)     %Saying You Tied
            put "You Tied" ..
            tie := tie + 1
            locatexy (300, 200)     %Displaying stats
            put "Wins: ", win ..
            locatexy (300, 180)
            put "Loses: ", lose ..
            locatexy (300, 160)
            put "Ties: ", tie ..
        end if
    end if

end loop

if key = "y" then             %play again
    loop
        buttonwait ("down", x, y, buttonnumber, buttonup)
        if x > 10 and x < 170 and y > 10 and y < 280 then %If choose Rock
            userchoice := 1
            randint (compchoice, 1, 3) %Computer Choosing

            if userchoice = 1 and compchoice = 3 then %If computer chooses scissors then win
                delay (10)
                cls
                put "You Chose Rock"
                rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
                Pic.Draw (rock, 10, 10, picCopy)
                delay (10)
                locatexy (450, 400)
                put "Computer Chose Scissors"
                scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
                Pic.Draw (scissors, 380, 16, picCopy)
                locatexy (320, 280) %Saying You Won
                put "You Won" ..
                win := win + 1
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..
                locatexy (1, 350)
                put "Play again? y or n"
                getch (key)
            elsif userchoice = 1 and compchoice = 2 then %If computer chooses Paper then lose
                cls
                put "You chose Rock"
                rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
                Pic.Draw (rock, 10, 10, picCopy)
                delay (10)
                locatexy (450, 400)
                put "Computer Chose Paper"
                paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
                Pic.Draw (paper, 380, 10, picCopy)
                delay (100)
                locatexy (320, 280) %Saying You Lost
                put "You Lost" ..
                lose := lose + 1
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..

            elsif userchoice = compchoice then %If computer chooses Rock then tie
                cls
                put "You chose Rock"
                rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
                Pic.Draw (rock, 10, 10, picCopy)
                delay (10)
                locatexy (450, 400)
                put "Computer Chose Rock"
                rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
                Pic.Draw (rock, 380, 10, picCopy)
                delay (100)
                locatexy (320, 280) %Saying You Tied
                put "You Tied" ..
                tie := tie + 1
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..
            end if



        elsif x > 180 and x < 400 and y > 10 and y < 280 then %If choose Paper
            userchoice := 2
            randint (compchoice, 1, 3) %Computer Choosing
            if userchoice = 2 and compchoice = 1 then %If computer chooses rock you win
                cls
                put "You Chose Paper"
                paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
                Pic.Draw (paper, 10, 10, picCopy)
                locatexy (450, 400)
                delay (10)
                put "Computer Chose Rock"
                rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
                Pic.Draw (rock, 380, 10, picCopy)
                locatexy (320, 280) %Saying You Won
                put "You Won" ..
                win := win + 1
                delay (10)
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..

            elsif userchoice = 2 and compchoice = 2 then %If computer chooses paper tie
                cls
                put "You Chose Paper"
                paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
                Pic.Draw (paper, 10, 10, picCopy)
                locatexy (450, 400)
                put "Computer Chose Paper"
                paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
                Pic.Draw (paper, 450, 10, picCopy)
                locatexy (320, 280) %Saying You Tied
                put "You Tied" ..
                tie := tie + 1
                delay (10)
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..
            elsif userchoice = 2 and compchoice = 3 then %If computer chooses scissors lose
                cls
                put "You Chose Paper"
                paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
                Pic.Draw (paper, 10, 10, picCopy)
                locatexy (450, 400)
                put "Computer Chose Scissors"
                scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
                Pic.Draw (scissors, 380, 16, picCopy)
                locatexy (320, 280) %Saying You lost
                put "You Lost" ..
                lose := lose + 1
                delay (10)
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..
            end if



        elsif x > 420 and x < 620 and y > 16 and y < 280 then %If choose Scissors
            userchoice := 3
            randint (compchoice, 1, 3) %Computer Choosing
            if userchoice = 3 and compchoice = 1 then %If computer chooses rock Lose
                cls
                put "You Chose Scissors"
                scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
                Pic.Draw (scissors, 10, 10, picCopy)
                delay (10)
                locatexy (450, 400)
                put "Computer Chose Rock"
                rock := Pic.FileNew ("rock.bmp") %Inserting Rock Image
                Pic.Draw (rock, 380, 10, picCopy)
                locatexy (320, 280) %Saying You lost
                put "You Lost" ..
                lose := lose + 1
                delay (10)
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..
            elsif userchoice = 3 and compchoice = 2 then %If computer chooses paper Win
                cls
                put "You Chose Scissors"
                scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
                Pic.Draw (scissors, 10, 10, picCopy)
                delay (10)
                locatexy (450, 400)
                put "Computer Chose Paper"
                paper := Pic.FileNew ("paper.bmp") %Inserting Paper Image
                Pic.Draw (paper, 380, 10, picCopy)
                delay (100)
                locatexy (320, 280) %Saying You Lost
                put "You Win" ..
                win := win + 1
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..
            elsif userchoice = compchoice then     %If computer chooses Scissors then tie
                cls
                put "You Chose Scissors"
                scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
                Pic.Draw (scissors, 10, 10, picCopy)
                delay (10)
                locatexy (450, 400)
                put "Computer Chose Scissors"
                scissors := Pic.FileNew ("scissors.bmp") %Inserting Scissors Image
                Pic.Draw (scissors, 380, 16, picCopy)
                delay (100)
                locatexy (320, 280) %Saying You Tied
                put "You Tied" ..
                tie := tie + 1
                locatexy (300, 200) %Displaying stats
                put "Wins: ", win ..
                locatexy (300, 180)
                put "Loses: ", lose ..
                locatexy (300, 160)
                put "Ties: ", tie ..
            end if
        end if

    end loop
riveryu




PostPosted: Fri May 16, 2008 9:59 pm   Post subject: Re: Quick Loop Question

It doesnt work when i ran your program...your program is rather long... Sry...
also, when you are posting, click the link/button that says "more tags" and click on "syntax" which will generate the [syntax =""] tag, add "turing" into the quotations and press syntax again to close the tag with [/syntax].
your code should be in btw the opening and closing of syntax tag ex. [syntax="turing""] ...code... [/syntax].

This is what it will looks like
Turing:
loop
    %Does stuffs here
    %More stuff, for lulz
    put "Would you like ot play again?"
    get Answer
    exit when Answer = "n"
end loop
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: