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

Username:   Password: 
 RegisterRegister   
 saving a bunch of variables not working
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
goroyoshi




PostPosted: Fri Mar 25, 2011 9:48 pm   Post subject: saving a bunch of variables not working

I'm getting this error when I try to save my variables for a textbased rpg

I/O attempted on unopened or closed stream 4

look at the bottom of code for the save code
Turing:


var xlocation, ylocation : int := 1
var level, save, enemylevel, gold, spell, enemyspell, encounter, ending, health, mana, manacost, enemydamage, escape, bossmove : int
var direction, battle, buys, buy : string
var Exp : real := 0
var weapon, damage, enemyhealth : real

level := 3
gold := 0
weapon := 1
buys := " "

loop
    if buys = "adminspell"
            then
        spell := level * 50
    end if
    escape := 0
    health := level * 10
    mana := level * 5
    damage := level * 2 * weapon
    put "Which direciton would you like to travel"
    put "You are level ", level
    put "You have ", health, " health"
    put "You have ", mana, " mana"
    put "You have ", gold, " gold"
    put "Exp : ", Exp, " / ", 150 * (level - 2)
    put "You are at ", xlocation, " ", ylocation, " (x y)"
    put "Type help to get info on how to play"
    get direction
    if direction = "up"
            then
        if xlocation = 1
                then
            if ylocation = 5
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 4
                    then
                ylocation := ylocation + 1
            end if
            if ylocation = 3
                    then
                ylocation := ylocation + 1
            end if
            if ylocation = 2
                    then
                ylocation := ylocation + 1
            end if
            if ylocation = 1
                    then
                ylocation := ylocation + 1
            end if
        end if
        if xlocation = 2
                then
            if ylocation = 3
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 2
                    then
                ylocation := ylocation + 1
            end if
            if ylocation = 1
                    then
                ylocation := ylocation + 1
            end if
        end if
        if xlocation = 3
                then
            if ylocation = 3
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 2
                    then
                ylocation := ylocation + 1
            end if
            if ylocation = 1
                    then
                ylocation := ylocation + 1
            end if
        end if
        if xlocation = 4
                then
            if ylocation = 3
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 2
                    then
                ylocation := ylocation + 1
            end if
            if ylocation = 1
                    then
                ylocation := ylocation + 1
            end if
        end if
    end if
    if direction = "down"
            then
        if xlocation = 1
                then
            if ylocation = 1
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 2
                    then
                ylocation := ylocation - 1
            end if
            if ylocation = 3
                    then
                ylocation := ylocation - 1
            end if
        end if
        if xlocation = 2
                then
            if ylocation = 1
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 2
                    then
                ylocation := ylocation - 1
            end if
            if ylocation = 3
                    then
                ylocation := ylocation - 1
            end if
        end if
        if xlocation = 3
                then
            if ylocation = 1
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 2
                    then
                ylocation := ylocation - 1
            end if
            if ylocation = 3
                    then
                ylocation := ylocation - 1
            end if
        end if
        if xlocation = 4
                then
            if ylocation = 1
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if ylocation = 2
                    then
                ylocation := ylocation - 1
            end if
            if ylocation = 3
                    then
                ylocation := ylocation - 1
            end if
        end if
    end if
    if direction = "left"
            then
        if ylocation = 1
                then
            if xlocation = 1
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if xlocation = 2
                    then
                xlocation := xlocation - 1
            end if
            if xlocation = 3
                    then
                xlocation := xlocation - 1
            end if
            if xlocation = 4
                    then
                xlocation := xlocation - 1
            end if
        end if
        if ylocation = 2
                then
            if xlocation = 1
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if xlocation = 2
                    then
                xlocation := xlocation - 1
            end if
            if xlocation = 3
                    then
                xlocation := xlocation - 1
            end if
            if xlocation = 4
                    then
                xlocation := xlocation - 1
            end if
        end if
        if ylocation = 3
                then
            if xlocation = 1
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if xlocation = 2
                    then
                xlocation := xlocation - 1
            end if
            if xlocation = 3
                    then
                xlocation := xlocation - 1
            end if
            if xlocation = 4
                    then
                xlocation := xlocation - 1
            end if
        end if
    end if
    if direction = "right"
            then
        if ylocation = 1
                then
            if xlocation = 4
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if xlocation = 3
                    then
                xlocation := xlocation + 1
            end if
            if xlocation = 2
                    then
                xlocation := xlocation + 1
            end if
            if xlocation = 1
                    then
                xlocation := xlocation + 1
            end if
        end if
        if ylocation = 2
                then
            if xlocation = 4
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if xlocation = 3
                    then
                xlocation := xlocation + 1
            end if
            if xlocation = 2
                    then
                xlocation := xlocation + 1
            end if
            if xlocation = 1
                    then
                xlocation := xlocation + 1
            end if
        end if
        if ylocation = 3
                then
            if xlocation = 4
                    then
                put "You can't travel there"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
                cls
            end if
            if xlocation = 3
                    then
                xlocation := xlocation + 1
            end if
            if xlocation = 2
                    then
                xlocation := xlocation + 1
            end if
            if xlocation = 1
                    then
                xlocation := xlocation + 1
            end if
        end if
    end if
    if xlocation = 1 and ylocation = 4
            then
        put "Warning, you are aproaching a very dangerous zone please turn back now"
    end if
    if xlocation = 1 and ylocation = 5
            then
        enemyhealth := 500
        enemydamage := 30
        enemyspell := 100
        loop
            cls
            if health <= 0
                    then
                put "You have died, please restart"
                loop
                end loop
            end if
            put "You have encountered the abyss spectre"
            put "The enemy's level ", enemylevel
            put "The enemy has ", enemyhealth, " hp"
            put "Your hp is ", health, " hp"
            put "You have ", mana, " mana"
            put "There is no escape"
            put "What will you do?"
            get battle
            if battle = "attack"
                    then
                put "you deal ", damage, " damage"
                enemyhealth := enemyhealth - damage
            end if
            if battle = "cast"
                    then
                if mana >= manacost
                        then
                    mana := mana - manacost
                    enemyhealth := enemyhealth - spell
                    put "You deal ", spell, " damage"
                else
                    put "You do not have enough mana"
                end if
            end if
            if enemyhealth < 0
                    then
                enemyhealth := 0
            end if
            exit when enemyhealth = 0
            randint (bossmove, 1, 5)
            if bossmove = 1
                    then
                put "The enemy doesn't attack"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
            end if
            if bossmove = 2 or bossmove = 3 or bossmove = 4
                    then
                health := health - enemydamage
                put "The enemy deals ", enemydamage, " damage"
                put "Press any key to continue"
            end if
            if bossmove = 5
                    then
                health := health - enemyspell
                put "The enemy casts a spell"
                delay (3000)
                put "The enemy deals 100 damage"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
            end if
            loop
                exit when hasch
            end loop
        end loop
        if enemyhealth = 0
                then
            gold := gold + 5000
            level := level + 10
            put "You have gained 10 levels"
            put "Press any key to continue"
            loop
                exit when hasch
            end loop
        end if
    end if
    if xlocation = 4 and ylocation = 3 or xlocation = 4 and ylocation = 2 or xlocation = 3 and ylocation = 3 or xlocation = 3 and ylocation = 2
            then
        randint (encounter, 1, 3)
        if encounter = 1
                then
        end if
        if encounter = 2
                then
        end if
        if encounter = 3
                then
            randint (enemylevel, level - 2, level + 2)
            enemyhealth := enemylevel * 4
            enemydamage := enemylevel * 2
            loop
                cls
                if health <= 0
                        then
                    put "You have died, please restart"
                    loop
                    end loop
                end if
                put "You have encountered a zombie"
                put "This zombie is level ", enemylevel
                put "The zombie has ", enemyhealth, " hp"
                put "Your hp is ", health, " hp"
                put "You have ", mana, " mana"
                put "What will you do?"
                get battle
                if battle = "attack"
                        then
                    put "you deal ", damage, " damage"
                    enemyhealth := enemyhealth - damage
                end if
                if battle = "run"
                        then
                    randint (escape, 1, 5)
                end if
                if battle = "cast"
                        then
                    if mana >= manacost
                            then
                        mana := mana - manacost
                        enemyhealth := enemyhealth - spell
                        put "You deal ", spell, " damage"
                    else
                        put "You do not have enough mana"
                    end if
                end if
                if enemyhealth < 0
                        then
                    enemyhealth := 0
                end if
                exit when escape > 1 or enemyhealth = 0
                health := health - enemydamage
                put "The enemy deals ", enemydamage, " damage"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
            end loop
            if enemyhealth = 0
                    then
                gold := gold + (enemylevel * 4)
                Exp := Exp + ((level + enemylevel) * 10)
                put "You have gained ", (level + enemylevel) * 10, " experience points"
                put "Press any key to continue"
                loop
                    exit when hasch
                end loop
            end if
        end if
    end if
    if Exp >= 150 * (level - 2)
            then
        level := level + 1
        Exp := 0
        put "Congratulations, you have leveled up"
        put "Please press 0 and enter to continue"
        loop
            get escape
            exit when escape = 0
        end loop
    end if
    if xlocation = 4 and ylocation = 1
            then
        put "you have entered the village please type shop to go to the weapon shop"
        get direction
        if direction = "shop"
                then
            cls
            put "You have ", gold, " gold"
            put "A dagger costs 50 gold and multiplies damage by 1.2"
            put "A shortsword costs 100 gold and multiplies damage by 1.5"
            put "A longsword costs 200 gold and multiplies damage by 2"
            put "Type magic to access the magic shop"
            get buy
            if buy = "dagger"
                    then
                if gold >= 50
                        then
                    gold := gold - 50
                    weapon := 1.2
                    put "You have bought the dagger"
                    put "Press any key to continue"
                    loop
                        exit when hasch
                    end loop
                else
                    put "You do not have enough money"
                end if
            end if
            if buy = "shortsword"
                    then
                if gold >= 100
                        then
                    gold := gold - 100
                    weapon := 1.5
                    put "You have bought the shortsword"
                    put "Press any key to continue"
                    loop
                        exit when hasch
                    end loop
                else
                    put "You do not have enough money"
                end if
            end if
            if buy = "longsword"
                    then
                if gold >= 200
                        then
                    gold := gold - 200
                    weapon := 2
                    put "You have bought the longsword"
                    put "Press any key to continue"
                    loop
                        exit when hasch
                    end loop
                else
                    put "You do not have enough money"
                end if
            end if
            if buy = "adminsword"
                    then
                put "You have bought the adminsword"
                put "Press any key to continue"
                weapon := 3
                loop
                    exit when hasch
                end loop
            end if
            if buy = "magic"
                    then
                cls
                put "You have entered the magic shop"
                put "The scroll fireball costs 50 gold and does 35 damage and costs 8 mana"
                put "The scroll thunderbolt costs 50 gold and does 15 damage and costs 2 mana"
                put "The scroll iceshard costs 50 gold and does 25 damage and costs 4 mana"
                get buys
                if buys = "fireball"
                        then
                    if gold >= 50
                            then
                        gold := gold - 50
                        spell := 35
                        manacost := 8
                        put "You have bought the fireball scroll"
                        put "Press any key to continue"
                        loop
                            exit when hasch
                        end loop
                    else
                        put "You do not have enough money"
                        put "Press any key to continue"
                        loop
                            exit when hasch
                        end loop
                    end if
                end if
                if buys = "thunderbolt"
                        then
                    if gold >= 50
                            then
                        gold := gold - 50
                        spell := 15
                        manacost := 2
                        put "You have bought the thunderbolt scroll"
                        put "Press any key to continue"
                        loop
                            exit when hasch
                        end loop
                    else
                        put "You do not have enough money"
                        put "Press any key to continue"
                        loop
                            exit when hasch
                        end loop
                    end if
                end if
                if buys = "iceshard"
                        then
                    if gold >= 50
                            then
                        gold := gold - 50
                        spell := 25
                        manacost := 4
                        put "You have bought the iceshard scroll"
                        put "Press any key to continue"
                        loop
                            exit when hasch
                        end loop
                    else
                        put "You do not have enough money"
                        put "Press any key to continue"
                        loop
                            exit when hasch
                        end loop
                    end if
                end if
                if buys = "adminspell"
                        then
                    spell := 50 * level
                    manacost := 0
                    put "You have bought the adminspell"
                    put "Press any key to continue"
                    loop
                        exit when hasch
                    end loop
                end if
            end if
        end if
    end if
    if direction = "help"
            then
        cls
        put "When the game asks you which direction you would like to go in"
        put "There are 4 directions, up, down, left and right"
        put " "
        put "When in battle, there are three options"
        put "Run, which attempts to run away from the enemy"
        put "Attack, which physically attacks the enemy"
        put "And cast, which casts which ever spell you have bought last"
        put " "
        put "Type save or load to save or load your level, weapon, spell, location, gold and experience points"
        put "Press any key to continue"
        loop
            exit when hasch
        end loop
    end if
    if direction = "save"
            then
        open : save, "savefile.txt", write
        write : xlocation, ylocation, level, buys, spell, Exp, gold, weapon %this doesn't work I/O attempted on unopened or closed stream 4
        close : save
    end if
    if direction = "load"
            then
        open : save, "savefile.txt", read
        read : xlocation, ylocation, level, buys, spell, Exp, gold, weapon
        close : save
    end if
    cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Mar 25, 2011 9:58 pm   Post subject: RE:saving a bunch of variables not working

That's not how writing to file works. write
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
goroyoshi




PostPosted: Fri Mar 25, 2011 10:13 pm   Post subject: Re: saving a bunch of variables not working

thank you I realized my mistake
Zren




PostPosted: Sat Mar 26, 2011 12:49 am   Post subject: RE:saving a bunch of variables not working

I'd recommend functions / sub routines if your making anything as complex as an RPG . . .

Seeing as you've already started however, might as well continue on. As you go, try to notice where you'd make significant changes were you to start from scratch.
goroyoshi




PostPosted: Sat Mar 26, 2011 12:12 pm   Post subject: Re: saving a bunch of variables not working

ya i know places to change but i'm not changing the travel system because i want to add a fence on one side of the town
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  [ 5 Posts ]
Jump to:   


Style:  
Search: