FB Shutting Down

New to FreeBASIC? Post your questions here.
Post Reply
RJKouba
Posts: 67
Joined: Mar 14, 2008 20:17

FB Shutting Down

Post by RJKouba »

I made the following code, and whenever i type kill dog and press enter, free basic quits.

Code: Select all

#include once "getinput.bas"
#include once "randomizednumbers.bas"
dim shared h2 as cmd_type
dim s as integer
dim r as integer
dim shared as integer maxhp,currenthp,doghp
maxhp=20
currenthp=20
doghp=10
print "To kill something type the word kill, followed by a space, and then the thing you want to kill"
area:
print "A fierce dog rushes at you"
h2=getcmd(">")
          select case h2.cmd
              case "kill"
                  select case h2.what1
                      case "dog"
                        s=Int(Rnd(1)*2) 'returns either a 0 or 1'
                        if s=0 then
                          print "The dog misses you": goto dog
                        else
                          print "The dog hits you, minus 3 hp.": currenthp=-3: print "HP= ": print currenthp
                        end if    
                        if currenthp<=0 then
                          print "You die": goto room
                        else: goto dog
                        end if
          end select
dog: 
                        r=Int (Rnd(1)*2)
                          if r=0 then
                            print "You miss the dog": goto area
                          else
                            print "You hit the dog, minus 7 hp.": doghp=-7: print "Dog HP= "
                            print doghp
                          end if
                            if doghp<=0 then 
                              goto room
                            else: goto area
                            end if
                        goto area
            end select
room:
print "Wazzup"
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

Code: Select all

#include once "getinput.bas"
#include once "randomizednumbers.bas"
I can't test the code without these, and Goto makes such messy code I cannot mentally process it. xD
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Put a sleep at the end of the code, FreeBASIC doesn't pause at the end like QB did.
RJKouba
Posts: 67
Joined: Mar 14, 2008 20:17

Post by RJKouba »

here's what i got now, its just going to the sub room and not executing the stuff inbetween.

The Game File:

Code: Select all

#include once "getinput.bas"
#include once "randomizednumbers.bas"
dim shared h2 as cmd_type
dim s as integer
dim r as integer
dim shared as integer maxhp,currenthp,doghp
maxhp=20
currenthp=20
doghp=10
declare sub room
print "To kill something type the word kill, followed by a space, and then the thing you want to kill"
area:
print "A fierce dog rushes at you"
h2=getcmd(">")
          select case h2.cmd
              case "kill"
                  select case h2.what1
                      case "dog"
                        s=Int(Rnd(1)*2) 'returns either a 0 or 1'
                        if s=0 then
                          print "The dog misses you": goto dog
                        else
                          print "The dog hits you, minus 3 hp."
                          currenthp-=3
                          print "HP= "
                          print currenthp
                        end if    
                        if currenthp<=0 then
                          print "You die":room
                        else: goto dog
                        end if
          end select
dog: 
                        r=Int (Rnd(1)*2)
                          if r=0 then
                            print "You miss the dog": goto area
                          else
                            print "You hit the dog, minus 1 hp."
                            doghp=10-7
                            print "Dog HP= "
                            print doghp
                          end if
                            if doghp<=0 then:room
                            else: goto area
                            end if
                        goto area
            end select
sub room
print "Wazzup"
h2=getcmd(">")
end sub
sleep
getinput.bas:

Code: Select all

type cmd_type
        cmd as string
        what1 as string
        'how as string
        what2 as string
end type
declare function getcmd(prompt as string) as cmd_type



'''''''''''''''''''''''''''''''''''''''''''''
function getcmd(prompt as string) as cmd_type
        dim c as cmd_type
        dim as integer spac, lastspac, w
        dim as string l
        print prompt;
        if not right$(prompt,1)=" " then print " ";
        line input l
        l=ltrim(rtrim(l))
        spac=0
        lastspac=spac
        spac=instr(spac+1,l," ")
        if not spac then
                c.cmd=l
                return c
        end if
        c.cmd=mid$(l,1,spac-1)
        w=instr(l,"with")
        if not w then
                c.what1=mid$(l,spac+1,len(l)-spac)
                return c
        end if
        c.what1=mid$(l,spac+1,w-(spac+2))
        c.what2=mid$(l,w+5,len(l)-(w+4))
        return c
end function
and randomizednumbers.bas:

Code: Select all

'get a random number between low and high
declare function GetRandom(lowerbound as integer, upperbound as integer) as integer
function GetRandom(lowerbound as integer, upperbound as integer) as integer
   GetRandom = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
end function 'credit goes to rdc from freebasic.net/forums'
i set it so that if you hit the dog, it minuses 1 hp, and the dogs total hp is 10. Every time i type kill dog, it goes to the sub room. thanks!
speedlemon
Posts: 55
Joined: Sep 21, 2005 22:19

Post by speedlemon »

i think that there's a problem w/ your getcmd function.

Code: Select all

 Type cmd_type
        cmd As String
        what1 As String
        'how as string
        what2 As String
End Type
Declare Function getcmd(prompt As String) As cmd_type



'''''''''''''''''''''''''''''''''''''''''''''
Function getcmd(prompt As String) As cmd_type
        Dim c As cmd_type
        Dim As Integer spac, lastspac, w
        Dim As String l
        Print prompt;
        If Not Right$(prompt,1)=" " Then Print " ";
        Line Input l
        l=Ltrim(Rtrim(l))
        spac=0
        lastspac=spac
        spac=Instr(spac+1,l," ")
        If Not spac Then
                c.cmd=l
                Return c
        End If
        c.cmd=Mid$(l,1,spac-1)
        w=Instr(l,"with")
        If Not w Then
                c.what1=Mid$(l,spac+1,Len(l)-spac)
                Return c
        End If
        c.what1=Mid$(l,spac+1,w-(spac+2))
        c.what2=Mid$(l,w+5,Len(l)-(w+4))
        Return c
End Function


Dim Shared h2 As cmd_type

h2=getcmd(">")
print h2.cmd
print h2.what1
print h2.what2
sleep
 

in other words, it is not splitting up the cmd correctly, therefore, the programs ends (does not go to subroom)

btw, you do 'doghp=10-7'.. why not just do 'doghp=3'.. but i think you made a typing error... according to comment, should be 'doghp-=1'
RJKouba
Posts: 67
Joined: Mar 14, 2008 20:17

Post by RJKouba »

still not working
speedlemon
Posts: 55
Joined: Sep 21, 2005 22:19

Post by speedlemon »

I'm aware of that. I didn't even change that function at all. I was just trying to show you that this function is your problem. Get it fixed and your program should work fine.
RJKouba
Posts: 67
Joined: Mar 14, 2008 20:17

Post by RJKouba »

ok got it, thanks
RJKouba
Posts: 67
Joined: Mar 14, 2008 20:17

Post by RJKouba »

figured it out. it was the file getinput.bas. heres what i have now:

Code: Select all

dim shared as integer doghp,currenthp,maxhp,s,r
dim shared as string blah, duh
doghp=10
currenthp=20
maxhp=20
declare sub you
declare sub dog
declare sub done
declare sub death
begining:
print "To kill something, type the word kill followed by the name of it, like dog. Hit enter if you got that"
input ">", blah
  select case blah
    case ""
      you
    case else
      print "You can't do that!": goto begining
  end select
      sub you
      print "A fierce dog rushes at you"
      input ">", duh
        select case duh
          case "kill dog"
            s=Int(Rnd(1)*2) 'returns either a 0 or 1'
            if s=0 then
              print "The dog misses you.":goto hp
            else
              print "The dog hits you, minus 2 hp.": currenthp-=2: print "HP=": print currenthp: goto hp
            end if
          case else
            print "That is not possible at this time.": you
          end select
      hp:
        if currenthp<=0 then
          print "You die!":death
        else
          dog
        end if
      end sub
      
      sub dog
        r=Int(Rnd(1)*2) 'returns either a 0 or 1'
        if r=1 then
          print "You miss the dog": goto doggy
        else
          print "You hit the dog, minus 5 hp.":doghp-=5:print "Dog HP=":print doghp
doggy:
            if doghp=0 then
              print "The dog died!":done
            else
              print "The dog isn't dead yet!":you
            end if
        end if
      end sub
      
      sub done
        print "You win!"
        sleep
      end sub
      
      sub death
        print "You are dead, fellow!"
        sleep
      end sub
Post Reply