Unanswered I Need Help...

  • Thursday, February 23, 2012 8:41 AM
     
     

    Ive been trying for about a week to convert this maths quiz I made to graphicswindow and i cant get it working, any help you can give is appreciated, this is my original code: 

    begin:

    If (Clock.Hour < 12) Then

      TextWindow.WriteLine("Good Morning")
      TextWindow.WriteLine("the time is: " + Clock.Time)
    EndIf
    If (Clock.Hour >= 12) Then
      TextWindow.WriteLine("Good Afternoon")
      TextWindow.WriteLine("the time is: " + Clock.Time)
    EndIf
    TextWindow.Write("What is your Name: ")
    Name = TextWindow.Read()
    TextWindow.Write("Welcome " + Name + ". ")
    TextWindow.WriteLine("Which Set would you like: ")
    TextWindow.WriteLine("a.   10 Questions")
    TextWindow.WriteLine("b.   20 Questions")
    TextWindow.WriteLine("c.   50 Questions")
    TextWindow.WriteLine("d.   100 Questions")
    TextWindow.Write("")
    x = TextWindow.Read() 
    TextWindow.WriteLine("Select Difficulty: ")
    TextWindow.WriteLine("a.   Beginner")
    TextWindow.WriteLine("b.   Easy")
    TextWindow.WriteLine("c.   Medium")
    TextWindow.WriteLine("d.   Hard")
    TextWindow.Write("")
    u = TextWindow.Read() 
    TextWindow.WriteLine("Please enter the opperation you want to perform (+, -, *, /) ")
    TextWindow.WriteLine("+   Addition")
    TextWindow.WriteLine("-   Subtraction")
    TextWindow.WriteLine("*   Multiplication")
    TextWindow.WriteLine("/   Division")
    TextWindow.Write("")
    z = TextWindow.Read()
    Sub Calculate
    If z = "+" Then
    result = op1 + op2
    EndIf
    If z = "-" Then
    result = op1 - op2
    EndIf
    If z = "*" Then
    result = op1 * op2
    EndIf
    If z = "/" Then
    result = op1 / op2
    EndIf
      EndSub
    If x = "a" then    
    for i = 1 To 10
    If u = "a" then  
    op1 =  Math.GetRandomNumber(10)
    op2 =  Math.GetRandomNumber(10)
    EndIf
    If u = "b" then  
    op1 =  Math.GetRandomNumber(20)
    op2 =  Math.GetRandomNumber(20)
    EndIf
    If u = "c" then  
    op1 =  Math.GetRandomNumber(50)
    op2 =  Math.GetRandomNumber(50)
    EndIf
    If u = "d" then  
    op1 =  Math.GetRandomNumber(100)
    op2 =  Math.GetRandomNumber(100)
    EndIf
    TextWindow.WriteLine(op1 + z + op2)
      answer = TextWindow.Read() 
       Calculate()
    if (answer = result) then
      TextWindow.WriteLine("Correct")
        score = score + 1
    else
      TextWindow.WriteLine("incorrect")
    EndIf
    EndFor
    EndIf
    If x = "b" then    
    for i = 1 To 20
    If u = "a" then  
    op1 =  Math.GetRandomNumber(10)
    op2 =  Math.GetRandomNumber(10)
    EndIf
    If u = "b" then  
    op1 =  Math.GetRandomNumber(20)
    op2 =  Math.GetRandomNumber(20)
    EndIf
    If u = "c" then  
    op1 =  Math.GetRandomNumber(50)
    op2 =  Math.GetRandomNumber(50)
    EndIf
    If u = "d" then  
    op1 =  Math.GetRandomNumber(100)
    op2 =  Math.GetRandomNumber(100)
    EndIf
    TextWindow.WriteLine(op1 + z + op2)
      answer = TextWindow.Read() 
       Calculate()
    if (answer = result) then
      TextWindow.WriteLine("Correct")
        score = score + 1
    else
      TextWindow.WriteLine("incorrect")
    EndIf
    EndFor
    EndIf
    If x = "c" then    
    for i = 1 To 50
    If u = "a" then  
    op1 =  Math.GetRandomNumber(10)
    op2 =  Math.GetRandomNumber(10)
    EndIf
    If u = "b" then  
    op1 =  Math.GetRandomNumber(20)
    op2 =  Math.GetRandomNumber(20)
    EndIf
    If u = "c" then  
    op1 =  Math.GetRandomNumber(50)
    op2 =  Math.GetRandomNumber(50)
    EndIf
    If u = "d" then  
    op1 =  Math.GetRandomNumber(100)
    op2 =  Math.GetRandomNumber(100)
    EndIf
    TextWindow.WriteLine(op1 + z + op2)
      answer = TextWindow.Read() 
       Calculate()
    if (answer = result) then
      TextWindow.WriteLine("Correct")
        score = score + 1
    else
      TextWindow.WriteLine("incorrect")
    EndIf
    EndFor
    EndIf
    If x = "d" then    
    for i = 1 To 100
    If u = "a" then  
    op1 =  Math.GetRandomNumber(10)
    op2 =  Math.GetRandomNumber(10)
    EndIf
    If u = "b" then  
    op1 =  Math.GetRandomNumber(20)
    op2 =  Math.GetRandomNumber(20)
    EndIf
    If u = "c" then  
    op1 =  Math.GetRandomNumber(50)
    op2 =  Math.GetRandomNumber(50)
    EndIf
    If u = "d" then  
    op1 =  Math.GetRandomNumber(100)
    op2 =  Math.GetRandomNumber(100)
    EndIf
    TextWindow.WriteLine(op1 + z + op2)
      answer = TextWindow.Read() 
       Calculate()
    if (answer = result) then
      TextWindow.WriteLine("Correct")
        score = score + 1
    else
      TextWindow.WriteLine("incorrect")
    EndIf
    EndFor
    EndIf
    if x = "a" then
      TextWindow.WriteLine("your Score is: " + Score + "/10")
    EndIf
    if x = "b" then
      TextWindow.WriteLine("your Score is: " + Score + "/20")
    EndIf
    if x = "c" then
      TextWindow.WriteLine("your Score is: " + Score + "/50")
    EndIf
    if x = "d" then
      TextWindow.WriteLine("your Score is: " + Score + "/100")
    EndIf


    TextWindow.WriteLine("Would you like to perform another Quiz? (Y or N)")
    TextWindow.Write("")


    doagain = TextWindow.Readkey()


    If(doagain = "Y" Or doagain = "y") Then 
      TextWindow.Clear()
      Goto begin
    EndIf

    http://irealm.webs.com/cooltext583251732.jpg

All Replies

  • Thursday, February 23, 2012 10:17 AM
     
     

    Hi,

    so lets work on your code a little bit.

    First you should always put Subs on top or at the end. A sub directly in the middle of other code is not that easy to read. (So I placed the Sub Calculate at the end).

    Next you should make sure that you do not mix up blocks of code. So as a golden rule you can have a block inside a block but you cannot have something like:
    StartBlock1
    StartBlock2
    EndBlock1
    StartBlock3
    EndBlock2
    EndBlock3
    Where Block 2 starts inside Block1 and ends inside Block3.

    Maybe you could think of blocks of code as chests. These child play thing which you could put into each other or put on top of each other?
    You can put a box into a (bigger) box, but you cannot put 2 boxes next to each other and then put another box so, that half of it is in one box and the other half is inside the 2nd box.

    So the solution could be to first set the numberOfQuestions inside if statements and then do a for i = 1 to numberOfQuestions.

    Same with the difficulty - just set maxNumber to the value you like.

    I also didn't see that you reset the score. Or should it be added up with each try? So I added that, too.

    And when the user entered something invalid, you should handle that, too. I did that check, too.

    My code is now this:

    begin:
     
    If (Clock.Hour < 12) Then
       TextWindow.WriteLine("Good Morning")
       TextWindow.WriteLine("the time is: " + Clock.Time)
     EndIf
     If (Clock.Hour >= 12) Then
       TextWindow.WriteLine("Good Afternoon")
       TextWindow.WriteLine("the time is: " + Clock.Time)
     EndIf
     TextWindow.Write("What is your Name: ")
     Name = TextWindow.Read()
     TextWindow.Write("Welcome " + Name + ". ")
     
     TextWindow.WriteLine("Which Set would you like: ")
     TextWindow.WriteLine("a.   10 Questions")
     TextWindow.WriteLine("b.   20 Questions")
     TextWindow.WriteLine("c.   50 Questions")
     TextWindow.WriteLine("d.   100 Questions")
     TextWindow.Write("")
     x = TextWindow.Read()
     
     numberOfQuestions=0
     If x = "a" then   
       numberOfQuestions =  10
     EndIf
     If x = "b" then   
       numberOfQuestions =  20
     EndIf
     If x = "c" then   
       numberOfQuestions =  50
     EndIf
     If x = "d" then   
       numberOfQuestions =  100
     EndIf

    TextWindow.WriteLine("Select Difficulty: ")
     TextWindow.WriteLine("a.   Beginner")
     TextWindow.WriteLine("b.   Easy")
     TextWindow.WriteLine("c.   Medium")
     TextWindow.WriteLine("d.   Hard")
     TextWindow.Write("")
     u = TextWindow.Read()

     maxNumber=0
     If u = "a" then 
       maxNumber = 10
     EndIf
     If u = "a" then 
       maxNumber = 20
     EndIf
     If u = "a" then 
       maxNumber = 50
     EndIf
     If u = "a" then 
       maxNumber = 100
     EndIf

     TextWindow.WriteLine("Please enter the opperation you want to perform (+, -, *, /) ")
     TextWindow.WriteLine("+   Addition")
     TextWindow.WriteLine("-   Subtraction")
     TextWindow.WriteLine("*   Multiplication")
     TextWindow.WriteLine("/   Division")
     TextWindow.Write("")
     z = TextWindow.Read()

    If numberOfQuestions > 0 And maxNumber > 0 And (z = "+" Or z="-" or z="*" Or z="/") Then
      score = 0
      For i = 1 To numberOfQuestions
        op1 =  Math.GetRandomNumber(maxNumber)
        op2 =  Math.GetRandomNumber(maxNumber)
        TextWindow.WriteLine(op1 + z + op2)
        answer = TextWindow.Read()
        Calculate()
        if (answer = result) then
          TextWindow.WriteLine("Correct")
          score = score + 1
        else
          TextWindow.WriteLine("incorrect")
        EndIf
      EndFor
     
      if x = "a" then
        TextWindow.WriteLine("your Score is: " + Score + "/10")
      EndIf
      if x = "b" then
        TextWindow.WriteLine("your Score is: " + Score + "/20")
      EndIf
      if x = "c" then
        TextWindow.WriteLine("your Score is: " + Score + "/50")
      EndIf
      if x = "d" then
        TextWindow.WriteLine("your Score is: " + Score + "/100")
      EndIf
     
    Else
      TextWindow.WriteLine("You entered some invalid information!")
    EndIf
     

    TextWindow.WriteLine("Would you like to perform another Quiz? (Y or N)")
    TextWindow.Write("")
    doagain = TextWindow.Readkey()

    If(doagain = "Y" Or doagain = "y") Then
       TextWindow.Clear()
       Goto begin
    EndIf
     
    Sub Calculate
      If z = "+" Then
        result = op1 + op2
      EndIf
      If z = "-" Then
        result = op1 - op2
      EndIf
      If z = "*" Then
        result = op1 * op2
       EndIf
       If z = "/" Then
         result = op1 / op2
       EndIf
     EndSub

    I hope I was able to help you. If you have problems to understand a change or why it was solved that way or how I got it: Please ask! I provided a full solution so you get a quick success but it is very important for me that you understand everything. So please keep in mind that there are no dumb questions - only dumb answers. Everybody started some time in the past so please feel free to ask as much as you want!

    With kind regards,

    Konrad

    Edit: And I completly forgot to say: It was a good approach already! So the greeting worked great and you see: I did no change at all! (And I am a person who really likes to refactor code :) )
  • Thursday, February 23, 2012 2:21 PM
     
     

    Hello,

    I've got some additional tips!

    Why not alwayz use TextWindow.Readkey() instead of TextWindow.Read() when you want 1 character input only? It saves ya a lotta 'Enter' key presses!

    It's a pity the "Intellisense" doesn't show TextWindow.Readkey() as an option when you type though =(

  • Tuesday, February 28, 2012 1:15 AM
     
     
    sorry, although you did point out a few of my errors, I think you may have misunderstood me i meant, i want to try displaying this maths quiz in "graphicswindow" instead of "textwindow" ive been trying for a while re-writing the code and it dosent seem to work, but thanks for all the help... 
    • Edited by Demonicdan Tuesday, February 28, 2012 11:21 PM
    •  
  • Tuesday, February 28, 2012 1:44 AM
     
     

    Oops! I must confess I haven't read what you were asking for. I was just passing by and put my eyes on towards the source code.

    I haven't realized at all you were asking for a "text" into "graphics" conversion!

    But like I said, it was just a tip. It had no errors at all! Just an enhancement to save some onerous Enter key presses.

    But in GraphicsWindows we'd be using keypress events instead ^_^

    I too, have a program I want to convert to GraphicsWindow :D

  • Tuesday, February 28, 2012 5:22 AM
    Answerer
     
      Has Code
    How about using the Controls object...

    Setup()
    '''''''''''''''''''''''''
    Sub Setup
      Signs = "1=+;2=-;3=×;4=÷;" 'setup operations characters for display
      
      GraphicsWindow.FontSize = 24 'increase textsize of controls
      
      'setup 2 boxes for integers and one for operation sign
      Num1Button = Controls.AddButton(Num1, 200, 100)
      MathOpButton = Controls.AddButton(Signs[MathOp], 150, 150)
      Num2Button = Controls.AddButton(Num2, 200, 150)
      
      'create a textbox where user types answer into
      AnswerBox = Controls.AddTextBox(200,200)
      'assign a sub to the event handler to check when text is changed
      Controls.TextTyped = GetAnswer
      
      'resize all controls
      Controls.SetSize(Num1Button, 50, 50)
      Controls.SetSize(MathOpButton, 50, 50)
      Controls.SetSize(Num2Button, 50, 50)
      Controls.SetSize(AnswerBox, 50, 50)
      
      'start the questions
      NewQuestion()
    EndSub
    '''''''''''''''''''''''''
    Sub GetAnswer
      Answer = Controls.GetTextBoxText(AnswerBox)
      'compare what is in the textbox with the correct answer
      If (Answer = CorrectAnswer) Then    
        'tell user they got it right
        GraphicsWindow.ShowMessage("Correct","")
        'setup a new question
        NewQuestion()
      EndIf
    EndSub
    '''''''''''''''''''''''''
    Sub NewQuestion
      'pick a new math operation
      MathOp = Math.GetRandomNumber(4)
      Num1 = Math.GetRandomNumber(9)
      Num2 = Math.GetRandomNumber(9)
      
      'depending on the operation -- do the math :) also set correctanswer
      If (MathOp = 1) Then 'add
        CorrectAnswer = Num1 + Num2
      ElseIf (MathOp = 2) Then 'sub
        'do not allow a negative answer
        If (Num2 > Num1) Then 
          Swap()
        EndIf      
        CorrectAnswer = Num1 - Num2
      ElseIf (MathOp = 3) Then 'mul
        CorrectAnswer = Num1 * Num2
      ElseIf (MathOp = 4) Then 'div
        'make the first number larger
        If (Num2 > Num1) Then 
          Swap()
        EndIf      
        'alter the numbers to give an easy math problem
        Num1 = Num1 * Num2
        CorrectAnswer = Num1 / Num2
      EndIf
      'show the problem to the user
      UpdateScreen()
    EndSub
    '''''''''''''''''''''''''
    Sub Swap
      TempNum = Num1
      Num1 = Num2
      Num2 = TempNum  
    EndSub
    '''''''''''''''''''''''''
    Sub UpdateScreen
      'fill in the numbers from newquestion sub
      Controls.SetButtonCaption(Num1Button, Num1)
      Controls.SetButtonCaption(MathOpButton, Signs[MathOp])  
      Controls.SetButtonCaption(Num2Button, Num2)  
      'reset the answer box
      Controls.SetTextBoxText(AnswerBox, "")
    EndSub
      

  • Tuesday, February 28, 2012 10:19 PM
    Moderator
     
     

    That's an Intellisense bug. I'll create a tracking work item for that.

    Thanks.

  • Tuesday, February 28, 2012 10:51 PM
     
     

    Thankyou all for your help... :)


    http://irealm.webs.com/cooltext583251732.jpg