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

Username:   Password: 
 RegisterRegister   
 Editing a code, Trying to add a replay y/n option - Need help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aange10




PostPosted: Sat Sep 03, 2011 2:12 pm   Post subject: Editing a code, Trying to add a replay y/n option - Need help

What is it you are trying to achieve?
I have just started learning turing, and I have read a lot of the time the best way to learn is to look at other people's code and edit/add to the code. I went to the 20 lines or less weekly contest (I figured very good programers would be there, with short code that I could understand, and little "extra features". Allowing me to add these "extra features") and I found a nice random triangle drawing code, and wanted to add a replay button to it.


What is the problem you are having?
I have successfully (and probably messily) added a way to replay. However I can not find out how to "bug proof" the y/n system.

Describe what you have tried to solve this problem
I have tried adding another else statement that tells them that what they have entered is an invalid letter, and makes them choose another. My coding (probably messily) works! But the only problem is if they enter an invalid character again, it will just act as if they pressed "y" (except it doesn't clear the screen).

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
The origional code is:

Turing:

View.Set ("graphics:max;max")

var d : array 1 .. 8 of real

for i : 0 .. 3
    d (i * 2 + 1) := Rand.Int (1, maxx)
    d (i * 2 + 2) := Rand.Int (1, maxy)
end for

for : 0 .. 100000
    var r := Rand.Int (0, 2)
    d (7) := (d (7) + d (r * 2 + 1)) / 2
    d (8) := (d (8) + d (r * 2 + 2)) / 2
    Draw.Dot (floor (d (7)), floor (d (8)), black)
end for


The code now (after my adding) looks like this:

Turing:

View.Set ("graphics:max;max")
loop
var d : array 1 .. 8 of real
var text : string

for i : 0 .. 3
    d (i * 2 + 1) := Rand.Int (1, maxx)
    d (i * 2 + 2) := Rand.Int (1, maxy)
end for

for : 0 .. 100000
    var r := Rand.Int (0, 2)
    d (7) := (d (7) + d (r * 2 + 1)) / 2
    d (8) := (d (8) + d (r * 2 + 2)) / 2
    Draw.Dot (floor (d (7)), floor (d (8)), black)
end for
put "Would you like to coninue? y/n " ..
%Asks them if they'd like to continue
get text
%Gets answer
if text = "y" then
cls
% If y, it clears the screen, and the loop draws another triangle (sketchy on whats happening, but it works - I think it ends the if statement and the loop keeps going)
elsif text = "n" then
% If n, then it says good bye, waits 5 seconds, clears the screen and says Program closing.. then stops the program 2 seconds later.
put "Good bye!"
delay (5000)
cls
put "Program closing..."
delay (2000)
exit
else put "That is not a valid action."
% If anything else if put it tell them that it is not a valid action
delay (2000)
put "Would you like to continue? y/n " ..
% After letting them know the action isn't valid, it repeats the original question
get text
    if text = "n" then
    put "As you wish..."
    delay (2000)
    cls
    put "Program closing."
    delay (2000)
% If n was put, then it says As you wish... waits 2 seconds, says Program closing, waits 2 seconds and then closes
    exit
    elsif text = "y" then
% If y was put, then it clears the screen, and another triangle is drawn
    cls
end if
end if
end loop
% I know you guys probably know the coding, and my comments are unnecessary, but I'm also using these comments
% to show you my thought process. I coded the replay system by myself and have been reading up on turing for only about 2 days now.



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Sep 03, 2011 2:20 pm   Post subject: RE:Editing a code, Trying to add a replay y/n option - Need help

You're going in the right direction.

In order to 'bug proof' your system, you can put the prompt into another loop.

code:

loop
    get text
    exit when text = "y" || text = "n"
    put "That is not a valid action." %if y or n is entered, this will not be displayed because the loop will have exited
end loop
if text = "n" then break %No need to check for y, 'cause if y is entered you do nothing. Just continue the loop.
Aange10




PostPosted: Sat Sep 03, 2011 2:29 pm   Post subject: RE:Editing a code, Trying to add a replay y/n option - Need help

Thankyou, insectoid! However, y does clear the screen and then repeat the loop. (So the triangles won't be drawn over each other)

So would

code:

loop
    get text
    if text = "y" or text = "n"
    cls
    exit
end if
end loop
if text = "n" then break
end if

work?

EDIT: This code works, thank you a ton for assisting me!
code:

View.Set ("graphics:max;max")
loop
var d : array 1 .. 8 of real
var text : string

for i : 0 .. 3
    d (i * 2 + 1) := Rand.Int (1, maxx)
    d (i * 2 + 2) := Rand.Int (1, maxy)
end for

for : 0 .. 100000
    var r := Rand.Int (0, 2)
    d (7) := (d (7) + d (r * 2 + 1)) / 2
    d (8) := (d (8) + d (r * 2 + 2)) / 2
    Draw.Dot (floor (d (7)), floor (d (8)), black)
end for
loop
    put "Would you like to continue? y/n: " ..
    get text
    if text = "y" or text = "n" then
    cls
    exit
    else put "That is invalid. Please try again"
    delay (1000)
    cls
end if
end loop
if text = "n" then
put "Good Bye!"
delay (5000)
cls
put "Program Closing!"
delay (2000)
break
end if
end loop
Insectoid




PostPosted: Sat Sep 03, 2011 2:58 pm   Post subject: RE:Editing a code, Trying to add a replay y/n option - Need help

Better to cls at the top of the main loop. It will perform the same, but it's good coding practice.

The program flow should look like this:

code:

%declare all variables
loop %main loop
    %initialize all necessary variables
    cls %cls before drawing anything, to automatically clear when you 'replay'.

    %main program goes here

    loop %and ofc the exit loop
        put "Would you like to continue? y/n: " ..
        get text
        exit when text = "y" || text = "n"
        put "That is not a valid action."
    end loop
    if text = "n" then break
end loop


It's important that your code is organized.
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 1  [ 4 Posts ]
Jump to:   


Style:  
Search: