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

Username:   Password: 
 RegisterRegister   
 My First Three Programs - details inside
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kaykao




PostPosted: Fri Feb 22, 2013 4:40 pm   Post subject: My First Three Programs - details inside

Hello there!

I'm not very experienced with turing yet, but I figured I would share the first three little programs I've made. Please, tell me what you think! If the code could be shortened or I've made an error, or something could in any way be improved on any of these programs, I'd love to hear it.

I'm now attempting to make a simple calculator, but I'm searching for other simple little projects to do to help expand my knowledge of the program. So feel free to suggest little projects I could tackle if you feel like it.

And I apologize for this thread being so long... I didn't know how else to put these up. >>;

Math Practice - A program that asks the user a simple math question, and will tell the user if the answer they give is right or wrong.
I did my best to work out the bugs with division... But there might still be something. Eheh.
Code:
var windowID := Window.Open ("graphics:300;200")

%-----PROCEDURES-----%
procedure clear
    for x : 0 .. maxx %Draws white lines across the screen from left to right to clear it
        delay (1)
        Draw.Line (x, 0, x, 160, white)
    end for
end clear

procedure add
    delay (500)
    var y : int
    var a : int := Rand.Int (1, 20)
    var b : int := Rand.Int (1, 20)
    var input : string
    Text.Locate (4, 1)
    put a, " + ", b, " = " ..
    loop
        get input
        if strintok (input) then
            y := strint (input)
            exit
        else
            put "That's not a number!"
            delay (2000)
            clear
            Text.Locate (4, 1)
            put a, " + ", b, " = " ..
        end if
    end loop
    if y = a + b then
        put "Correct!"
    else
        put "Incorrect... The answer was ", a + b
    end if
    delay (2000)
    clear
end add

procedure subtract
    delay (500)
    var y : int
    var a : int := Rand.Int (1, 20)
    var b : int := Rand.Int (1, 20)
    var input : string
    loop
        if a < b then
            a := Rand.Int (1, 20)
            b := Rand.Int (1, 20)
        elsif a >= b then
            exit
        end if
    end loop
    Text.Locate (4, 1)
    put a, " - ", b, " = " ..
    loop
        get input
        if strintok (input) then
            y := strint (input)
            exit
        else
            put "That's not a number!"
            delay (2000)
            clear
            Text.Locate (4, 1)
            put a, " - ", b, " = " ..
        end if
    end loop
    if y = a - b then
        put "Correct!"
    else
        put "Incorrect... The answer was ", a - b
    end if
    delay (2000)
    clear
end subtract

procedure multiply
    delay (500)
    var y : int
    var a : int := Rand.Int (0, 12)
    var b : int := Rand.Int (0, 12)
    var input : string
    Text.Locate (4, 1)
    put a, " x ", b, " = " ..
    loop
        get input
        if strintok (input) then
            y := strint (input)
            exit
        else
            put "That's not a number!"
            delay (2000)
            clear
            Text.Locate (4, 1)
            put a, " x ", b, " = " ..
        end if
    end loop
    if y = a * b then
        put "Correct!"
    else
        put "Incorrect... The answer was ", a * b
    end if
    delay (2000)
    clear
end multiply

procedure divide
    delay (500)
    var y : real
    var input : string
    var a : int := Rand.Int (1, 12)
    var b : int := Rand.Int (1, 12)
    var c : real := a / b + 0.01
    loop
        if a < b then
            a := Rand.Int (1, 12)
            b := Rand.Int (1, 12)
        elsif a >= b then
            exit
        end if
    end loop
    loop
        Text.Locate (4, 1)
        put a, " ? ", b, " = " ..
        get input
        if strrealok (input) then
            y := strreal (input)
            exit
        else
            put "That's not a number!"
            delay (2000)
            clear
            delay (500)
        end if
    end loop
    if y >= a / b and y <= c then
        put "Correct!"
    elsif y = a / b then
        put "Correct"
    elsif y < a / b or y > c then
        put "Incorrect... The answer was ", a / b
    end if
    delay (2000)
    clear
end divide

%----------------------%
%-----MAIN PROGRAM-----%
%----------------------%

var random : int
var font1 : int

%-----HEADER-----%
font1 := Font.New ("comicsans:17")
Font.Draw ("Math Practice", 10, 175, font1, black)
Draw.Box (5, 170, 153, 195, black)

loop
    randint (random, 1, 4)
    if random = 1 then
        add
    elsif random = 2 then
        subtract
    elsif random = 3 then
        multiply
    elsif random = 4 then
        divide
    end if
end loop




Guess The Number - A very simple game that asks the user to guess a number between 1 and 10. If the user doesn't guess right on the first try, the program will tell them whether they need to guess higher or lower. It generates a new number every play through, and you can either quit or choose to play another round after each guessed number.
I was thinking of limiting how many guesses you have before you fail somehow... perhaps using a for loop? What do you think?

Code:
var windowID := Window.Open ("graphics:500;500")

%-----VARIABLES-----%
var input : string
var x : int
var yesno : string

%-----PROCEDURES-----%

procedure clear
    for a : 0 .. maxx     %Draws white lines across the screen from left to right to clear it.
        delay (1)
        Draw.Line (a, 0, a, maxy, white)
    end for

end clear

%-----MAIN PROGRAM-----%

loop
    var font1 : int
    font1 := Font.New ("comicsans:17")
    Font.Draw ("Guess the Number!", 10, 475, font1, black)
    Draw.Box (5, 468, 215, 497, black)
   
    var n : int := Rand.Int (1, 10) % Generates the number that the user is trying to guess
    Text.Locate (5, 1)
    put "Guess the number! It's between 1 and 10..."
    loop
        loop
            get input
            if strintok (input) then
                x := strint (input)
                exit
            else
                put "That's not a number!"
                delay (1000)
                clear
                Text.Locate (5, 1)
                put "Guess the number! It's between 1 and 10..."
            end if
        end loop
        if x = n then
            put "Congratulations! You guessed correctly!"
            exit
        elsif x > n then
            put "Hmmm... You should try guessing lower..."
        elsif x < n then
            put "Not quite... You should guess higher."
        end if
    end loop
    delay (1000)
    put ""
    put "Play again? y/n"
    get yesno
    %-----Determines whether to run the program again or not-----------------------------------------
    if yesno = "n" then         % result if use chooses not to play again
        put ""
        put "You have chosen to quit... Bye!"
        delay (1000)
        exit
    elsif yesno = "y" then         % result if user chooses to play again
        put ""
        put "You have chosen to play again! Yay!"
        delay (1000)
        clear
    else         % Forces user play again if they do not enter either a 'y' or a 'n'
        put ""
        put "I'm gonna take that as a play again..."
        delay (1000)
        clear
    end if
end loop

Window.Close (windowID)




Paint Rip-Off - This program lets you draw lines within the bordered area. You can pick from a few different pencil colours and sizes for your drawing, and clear the canvas if you for some reason wish to murder your obviously beautiful creation.
Man did this one ever take some figuring out... And I know it doesn't have all the tools MS Paint has, I just couldn't think up a better name for it.
Code:
%-----VARIABLES-----%

var windowID := Window.Open ("graphics:500;400")
var x, y, x2, y2, x3, y3, button, count, LineColour, LineThickness : int
LineColour := 7
LineThickness := 2
var mode : string := "pencil"
View.Set ("graphics")
buttonchoose ("multibutton")

%-----PROCEDURES-----%

procedure ColourPicker
    Mouse.Where (x3, y3, button)
    if x3 < 30 and x3 > 20 and y3 < 270 and y3 > 260 and buttonmoved ("downup") then
        LineColour := 7
    elsif x3 < 30 and x3 > 20 and y3 < 290 and y3 > 280 and buttonmoved ("downup") then
        LineColour := 12
    elsif x3 < 30 and x3 > 20 and y3 < 310 and y3 > 300 and buttonmoved ("downup") then
        LineColour := 32
    elsif x3 < 30 and x3 > 20 and y3 < 330 and y3 > 320 and buttonmoved ("downup") then
        LineColour := 10
    elsif x3 < 30 and x3 > 20 and y3 < 250 and y3 > 240 and buttonmoved ("downup") then
        LineColour := 31
    end if
end ColourPicker

procedure clear
    for a : 91 .. 579     %Draws white lines across the screen from left to right to clear it
        delay (1)
        Draw.Line (a, 41, a, 349, white)
    end for
end clear

procedure QuitButton
    Mouse.Where (x3, y3, button)
    if x3 < 70 and x3 > 10 and y3 < 40 and y3 > 10 and buttonmoved ("downup") then
        Window.Close (windowID)
    end if
end QuitButton

procedure PencilSizePicker
    Mouse.Where (x3, y3, button)
    if x3 < 12 and x3 > 8 and y3 < 377 and y3 > 373 and buttonmoved ("downup") then
        LineThickness := 2
    elsif x3 < 33 and x3 > 27 and y3 < 378 and y3 > 372 and buttonmoved ("downup") then
        LineThickness := 4
    elsif x3 < 54 and x3 > 46 and y3 < 379 and y3 > 371 and buttonmoved ("downup") then
        LineThickness := 6
    elsif x3 < 75 and x3 > 65 and y3 < 380 and y3 > 370 and buttonmoved ("downup") then
        LineThickness := 8
    elsif x3 < 96 and x3 > 84 and y3 < 381 and y3 > 369 and buttonmoved ("downup") then
        LineThickness := 10
    end if
end PencilSizePicker

%----COLOUR PICKER-----%
Text.Locate (4, 1)
put "Colour:"
Draw.Box (20, 240, 30, 250, 7) % white (eraser)
Draw.FillBox (20, 260, 30, 270, 7) % black
Draw.FillBox (20, 280, 30, 290, 12) % red
Draw.FillBox (20, 300, 30, 310, 32) % blue
Draw.FillBox (20, 320, 30, 330, 10) % green

%-----PENCIL SIZE PICKER-----%
Text.Locate (1, 1)
put "Size:":30, "Left click to draw, right click to clear canvas."
Draw.FillOval (10, 375, 2, 2, 7) % size 1
Draw.FillOval (30, 375, 3, 3, 7) % size 2
Draw.FillOval (50, 375, 4, 4, 7) % size 3
Draw.FillOval (70, 375, 5, 5, 7) % size 4
Draw.FillOval (90, 375, 6, 6, 7) % size 5

%-----QUIT BUTTON-----%
Text.Locate (24, 4)
put "Quit"
Draw.Box (10, 10, 70, 40, 7)

%----------------------%
%-----MAIN PROGRAM-----%
%----------------------%

Draw.Box (90, 40, 580, 350, 7) % Canvas Area

loop
    count := 0
    loop
        count := count + 1
        Mouse.Where (x2, y2, button)
        if count = 1 then
            x := x2
            y := y2
        end if
        ColourPicker
        PencilSizePicker
        if button = 1 and x2 < 580 and x2 > 90 and y2 < 350 and y2 > 40 then
            Draw.ThickLine (x, y, x2, y2, LineThickness, LineColour)
            x := x2
            y := y2
        elsif button = 100 and x2 < 580 and x2 > 90 and y2 < 350 and y2 > 40 then
            Draw.FillBox (85, 35, 585, 355, 31)
            Draw.Box (90, 40, 580, 350, 7)
        end if
        exit when button = 0
        QuitButton
    end loop
end loop
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Fri Feb 22, 2013 9:50 pm   Post subject: RE:My First Three Programs - details inside

Im on my iPod ATM so it's hard to look at everything. However one thing I did notice is that you split up all your operations Into four separate procedures. In fact, this can all be shortened into one, because you basically do the same thing over and over except change the operation
BigBear




PostPosted: Sat Feb 23, 2013 9:43 am   Post subject: RE:My First Three Programs - details inside

Raknarg is right! For your first program you have a loop to get the input in each of your operation (+, -, *, /)

Turing:
loop
        get input
        if strintok (input) then
            y := strint (input)
            exit
        else
            put "That's not a number!"
            delay (2000)
            clear
            Text.Locate (4, 1)
            put a, " + ", b, " = " ..
        end if
    end loop
Cancer Sol




PostPosted: Thu Apr 04, 2013 7:47 pm   Post subject: Re: My First Three Programs - details inside

Nice program! Way better than my brother's math program xD
His (my brother's) code... it's so hard to understand.
Not even one comment, and everything is all over the place.

Edit: You taught me how to organize my code in Turing better now Razz
I don't code in Turing anymore, but I'll have to next year in CompSci Confused
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: