Microsoft Developer Network > Forums Home > Learning Forums > Small Basic > Having trouble with Subroutines and endif's

Answered Having trouble with Subroutines and endif's

  • Thursday, February 23, 2012 7:41 AM
     
     

    Having trouble with this advanced paper scissors rock game.

    it has 4 types of errors:

    Unexpexted token ( found

    Expected EndIf but could not find one in the right place

    cannot find label 'tie' used in goto statement

    Expected endsub but could not find one in the right place

    'INTRO


    TextWindow.CursorLeft = 26
    TextWindow.WriteLine("Future Warzone")
    TextWindow.WriteLine("")
    TextWindow.WriteLine("What is the name of your Empire?")
    TextWindow.WriteLine("")


    yournm = TextWindow.Read()


    TextWindow.WriteLine("")
    TextWindow.WriteLine("What is the name of the enemy Empire?")
    TextWindow.WriteLine("")


    enemynm = TextWindow.Read()


    TextWindow.WriteLine("")
    TextWindow.WriteLine("The year is 3021 and your Empire, " + yournm + " is on the brink of")
    TextWindow.WriteLine("war with the Alien Race, " + enemynm)
    TextWindow.WriteLine("")


    'INSTRUCTIONS


    TextWindow.WriteLine("This is a war simulation all battles are completely random and your choice will make the differance between winning and losing!")
    TextWindow.WriteLine("")
    TextWindow.WriteLine("Your Choices of Units are:")
    TextWindow.WriteLine("1. Marine, Frontal Attack")
    TextWindow.WriteLine("2. Black Ops, Flank Attack")
    TextWindow.WriteLine("3. Tank, Slow Strong Attack")
    TextWindow.WriteLine("4. Artillery, Slow Ranged Attack")
    TextWindow.WriteLine("")


    'Enemy selects attack


    comp = Math.GetRandomNumber (4)


    'Player selects attack


    TextWindow.WriteLine("What type of attack do you wish to do?")
    user = TextWindow.Read()


    'Sub routines


      'Tie win
    Sub tie  
      Math.GetRandomNumber (2)
      If 1 Then
        TextWindow.WriteLine( enemynm + "wins!")
      If 2 Then 
      TextWindow.WriteLine( yournm + "wins!")
        endif
        EndSub


      'user instant wins
     Sub userinstant
      Math.GetRandomNumber (2)
      If 1 Then
        TextWindow.WriteLine(yournm + " anihilated the invading" + enemynm + "forces, the planet is ours to keep!")
        If 2 Then
      TextWindow.WriteLine(yournm + " anihilated the defending" + enemynm + "forces, the planet is now under your control")
      EndIf
      EndSub




    'comp instant wins
    Sub compinstant
      Math.GetRandomNumber (2)
      If 1 Then
        TextWindow.WriteLine(enemynm + " anihilated the invading" + yournm + "forces, the planet is thiers to keep!")
        If 2 Then
      TextWindow.WriteLine(enemynm + " anihilated the defending" + yournm + "forces, the planet is now under their control")
      EndIf
      EndSub




    '25% chance user wins
    Sub user25
    Math.GetRandomNumber (4)
    If 1 Then
          TextWindow.WriteLine(yournm + " anihilated the invading" + enemynm + "forces, the planet is ours to keep!")
        If 2 Then
          TextWindow.WriteLine(yournm + " anihilated the defending" + enemynm + "forces, the planet is now under your control")
        If (3) Or (4)
          TextWindow.WriteLine("Neither the " + enemynm + " or the " + yournm + "won the battle, its a stalemate!")
           endif
            EndSub




    'Result collection


    If user = comp Then
    Goto tie()
    elseif (user = 1 And comp = 2) Or (user = 2 And comp = 4) Or (user = 3 And comp = 1) Or (user = 4 And comp = 3) Then
      goto userinstant()
    elseif (user = 2 And comp = 1) Or (user = 4 And comp = 2) Or (user = 1 And comp = 3) Or (user = 3 And comp = 4) Then
    goto compinstant()
    elseif (user = 1 And comp = 4) Or (user = 2 And comp = 3) Or (user = 4 And comp = 1) Or (user = 3 And comp = 2) Then
      goto user25()

     EndIf

           Plz help i have looked on several sites and i cant seem to find an answer :(

Answers

  • Thursday, February 23, 2012 8:26 AM
     
     Answered Has Code

    Use elseif instead of double ifs.

    To call on a sub you don't need the goto command. Just type the sub you wish to call userinstant() so on.

    Also in the:

    TextWindow.WriteLine(enemynm + " anihilated the invading" + yournm + "forces, the planet is thiers to keep!")

    Type it like this

    TextWindow.WriteLine(enemynm + " anihilated the invading " + yournm + " forces, the planet is theirs to keep!")

    With the space after the quotes if you don't it is outputed like "Computeranihilated the invadingplayerforces, the planet is theirs to keep!"

    Here's what I changed it too, Im not a professional but I hope it helps.

    'INTRO
     
    TextWindow.CursorLeft = 26
     TextWindow.WriteLine("Future Warzone")
     TextWindow.WriteLine("")
     TextWindow.WriteLine("What is the name of your Empire?")
     TextWindow.WriteLine("")
     
     yournm = TextWindow.Read()
     
    TextWindow.WriteLine("")
     TextWindow.WriteLine("What is the name of the enemy Empire?")
     TextWindow.WriteLine("")
     
    
    enemynm = TextWindow.Read()
     
    
    TextWindow.WriteLine("")
     TextWindow.WriteLine("The year is 3021 and your Empire, " + yournm + " is on the brink of")
     TextWindow.WriteLine("war with the Alien Race, " + enemynm)
     TextWindow.WriteLine("")
     
    
    'INSTRUCTIONS
     
    
    TextWindow.WriteLine("This is a war simulation all battles are completely random and your choice will make the differance between winning and losing!")
     TextWindow.WriteLine("")
     TextWindow.WriteLine("Your Choices of Units are:")
     TextWindow.WriteLine("1. Marine, Frontal Attack")
     TextWindow.WriteLine("2. Black Ops, Flank Attack")
     TextWindow.WriteLine("3. Tank, Slow Strong Attack")
     TextWindow.WriteLine("4. Artillery, Slow Ranged Attack")
     TextWindow.WriteLine("")
     
    
    'Enemy selects attack
     
    
    comp = Math.GetRandomNumber (4)
     
    
    'Player selects attack
     
    
    TextWindow.WriteLine("What type of attack do you wish to do?")
     user = TextWindow.Read()
     
    
    'Sub routines
     
    
      'Tie win
     Sub tie  
       Math.GetRandomNumber (2)
       If 1 Then
         TextWindow.WriteLine( enemynm + "wins!")
       Else 
       TextWindow.WriteLine( yournm + "wins!")
         endif
         EndSub
     
    
      'user instant wins
      Sub userinstant
       Math.GetRandomNumber (2)
       If 1 Then
         TextWindow.WriteLine(yournm + " anihilated the invading " + enemynm + " forces, the planet is ours to keep!")
        Else
       TextWindow.WriteLine(yournm + " anihilated the defending " + enemynm + " forces, the planet is now under your control")
       EndIf
       EndSub
     
    
    
    
    'comp instant wins
     Sub compinstant
       Math.GetRandomNumber (2)
       If 1 Then
         TextWindow.WriteLine(enemynm + " anihilated the invading" + yournm + "forces, the planet is thiers to keep!")
       Else
       TextWindow.WriteLine(enemynm + " anihilated the defending" + yournm + "forces, the planet is now under their control")
       EndIf
       EndSub
     
    
    
    
    '25% chance user wins
     Sub user25
        Math.GetRandomNumber(4)
         If 1 Then
           TextWindow.WriteLine(yournm + " anihilated the invading " + enemynm + " forces, the planet is ours to keep!")
         ElseIf 2 Then
           TextWindow.WriteLine(yournm + " anihilated the defending " + enemynm + " forces, the planet is now under your control")
         Else
           TextWindow.WriteLine(" Neither the " + enemynm + " or the " + yournm + " won the battle, its a stalemate!")
         EndIf
       
       EndSub
            
     
    
    
    
    If user = comp Then
      tie()
     elseif (user = 1 And comp = 2) Or (user = 2 And comp = 4) Or (user = 3 And comp = 1) Or (user = 4 And comp = 3) Then
       userinstant()
     elseif (user = 2 And comp = 1) Or (user = 4 And comp = 2) Or (user = 1 And comp = 3) Or (user = 3 And comp = 4) Then
      compinstant()
     elseif (user = 1 And comp = 4) Or (user = 2 And comp = 3) Or (user = 4 And comp = 1) Or (user = 3 And comp = 2) Then
        user25()
     
     EndIf



    • Edited by Rebyen2000 Thursday, February 23, 2012 8:33 AM
    • Marked As Answer by Superant1996 Thursday, February 23, 2012 9:23 AM
    •  

All Replies

  • Thursday, February 23, 2012 8:26 AM
     
     Answered Has Code

    Use elseif instead of double ifs.

    To call on a sub you don't need the goto command. Just type the sub you wish to call userinstant() so on.

    Also in the:

    TextWindow.WriteLine(enemynm + " anihilated the invading" + yournm + "forces, the planet is thiers to keep!")

    Type it like this

    TextWindow.WriteLine(enemynm + " anihilated the invading " + yournm + " forces, the planet is theirs to keep!")

    With the space after the quotes if you don't it is outputed like "Computeranihilated the invadingplayerforces, the planet is theirs to keep!"

    Here's what I changed it too, Im not a professional but I hope it helps.

    'INTRO
     
    TextWindow.CursorLeft = 26
     TextWindow.WriteLine("Future Warzone")
     TextWindow.WriteLine("")
     TextWindow.WriteLine("What is the name of your Empire?")
     TextWindow.WriteLine("")
     
     yournm = TextWindow.Read()
     
    TextWindow.WriteLine("")
     TextWindow.WriteLine("What is the name of the enemy Empire?")
     TextWindow.WriteLine("")
     
    
    enemynm = TextWindow.Read()
     
    
    TextWindow.WriteLine("")
     TextWindow.WriteLine("The year is 3021 and your Empire, " + yournm + " is on the brink of")
     TextWindow.WriteLine("war with the Alien Race, " + enemynm)
     TextWindow.WriteLine("")
     
    
    'INSTRUCTIONS
     
    
    TextWindow.WriteLine("This is a war simulation all battles are completely random and your choice will make the differance between winning and losing!")
     TextWindow.WriteLine("")
     TextWindow.WriteLine("Your Choices of Units are:")
     TextWindow.WriteLine("1. Marine, Frontal Attack")
     TextWindow.WriteLine("2. Black Ops, Flank Attack")
     TextWindow.WriteLine("3. Tank, Slow Strong Attack")
     TextWindow.WriteLine("4. Artillery, Slow Ranged Attack")
     TextWindow.WriteLine("")
     
    
    'Enemy selects attack
     
    
    comp = Math.GetRandomNumber (4)
     
    
    'Player selects attack
     
    
    TextWindow.WriteLine("What type of attack do you wish to do?")
     user = TextWindow.Read()
     
    
    'Sub routines
     
    
      'Tie win
     Sub tie  
       Math.GetRandomNumber (2)
       If 1 Then
         TextWindow.WriteLine( enemynm + "wins!")
       Else 
       TextWindow.WriteLine( yournm + "wins!")
         endif
         EndSub
     
    
      'user instant wins
      Sub userinstant
       Math.GetRandomNumber (2)
       If 1 Then
         TextWindow.WriteLine(yournm + " anihilated the invading " + enemynm + " forces, the planet is ours to keep!")
        Else
       TextWindow.WriteLine(yournm + " anihilated the defending " + enemynm + " forces, the planet is now under your control")
       EndIf
       EndSub
     
    
    
    
    'comp instant wins
     Sub compinstant
       Math.GetRandomNumber (2)
       If 1 Then
         TextWindow.WriteLine(enemynm + " anihilated the invading" + yournm + "forces, the planet is thiers to keep!")
       Else
       TextWindow.WriteLine(enemynm + " anihilated the defending" + yournm + "forces, the planet is now under their control")
       EndIf
       EndSub
     
    
    
    
    '25% chance user wins
     Sub user25
        Math.GetRandomNumber(4)
         If 1 Then
           TextWindow.WriteLine(yournm + " anihilated the invading " + enemynm + " forces, the planet is ours to keep!")
         ElseIf 2 Then
           TextWindow.WriteLine(yournm + " anihilated the defending " + enemynm + " forces, the planet is now under your control")
         Else
           TextWindow.WriteLine(" Neither the " + enemynm + " or the " + yournm + " won the battle, its a stalemate!")
         EndIf
       
       EndSub
            
     
    
    
    
    If user = comp Then
      tie()
     elseif (user = 1 And comp = 2) Or (user = 2 And comp = 4) Or (user = 3 And comp = 1) Or (user = 4 And comp = 3) Then
       userinstant()
     elseif (user = 2 And comp = 1) Or (user = 4 And comp = 2) Or (user = 1 And comp = 3) Or (user = 3 And comp = 4) Then
      compinstant()
     elseif (user = 1 And comp = 4) Or (user = 2 And comp = 3) Or (user = 4 And comp = 1) Or (user = 3 And comp = 2) Then
        user25()
     
     EndIf



    • Edited by Rebyen2000 Thursday, February 23, 2012 8:33 AM
    • Marked As Answer by Superant1996 Thursday, February 23, 2012 9:23 AM
    •  
  • Thursday, February 23, 2012 9:24 AM
     
     

    Thank you very much!

    Now i can continue coding, when im done i shall send you a copy :)

  • Saturday, February 25, 2012 11:37 PM
     
     

    You could consider using arrays to determine the outcome and print victory conditions instead of many IF statements.  For an example, please see my Rock, Paper, Scissors game that I put together.

    DJN321

    If you like the program, please feel free to modify it to suit your version of the game.

    EDIT: Please see VJX378 instead.  I had to make a change to eliminate the IF statements in the victory conditions.


    • Edited by kam528 Saturday, February 25, 2012 11:49 PM
    •