Computer Science Canada

Need Help with Turing Game

Author:  Kyle Biro [ Mon Mar 04, 2013 12:39 am ]
Post subject:  Need Help with Turing Game

What is it you are trying to achieve?
I am trying to make an inventory system for a text based RPG


What is the problem you are having?
I cannot figure out an easy, efficient way to do it

Turing:


var woodSword : boolean := false
var stoneSword : boolean := false
var bronzeSword : boolean := false
var ironSword : boolean := false
put "There are four swords at your feet."
put "A wooden, a stone, a bronze, and an iron sword."

class Inventory
    import ironSword, woodSword, stoneSword, bronzeSword
    export displayNames, displayDesc

    var itemNames : array 1 .. 10 of string
    itemNames (1) := "wooden sword"
    itemNames (2) := "stone sword"
    itemNames (3) := "bronze sword"
    itemNames (4) := "iron sword"

    var itemDesc : array 1 .. 10 of string
    itemDesc (1) := "a weak sword made of wood."
    itemDesc (2) := "a weak sword made of stone."
    itemDesc (3) := "a strong sword made of bronze."
    itemDesc (4) := "a very strong sword made of iron."

    proc displayNames ()
        if woodSword = true then
            put itemNames (1)
        end if
        if stoneSword = true then
            put itemNames (2)
        end if
        if bronzeSword = true then
            put itemNames (3)
        end if
        if ironSword = true then
            put itemNames (4)
        end if
    end displayNames

    proc displayDesc ()
        if woodSword = true then
            put itemDesc (1)
        end if
        if stoneSword = true then
            put itemDesc (2)
        end if
        if bronzeSword = true then
            put itemDesc (3)
        end if
        if ironSword = true then
            put itemDesc (4)
        end if
    end displayDesc
end Inventory

var commands : array 1 .. 6 of string
commands (1) := "take"
commands (2) := "go"
commands (3) := "attack"
commands (4) := "look around"
commands (5) := "open"
commands (6) := "examine"

var items : array 1 .. 5 of string
items (1) := "wooden sword"
items (2) := "stone sword"
items (3) := "bronze sword"
items (4) := "iron sword"
items (5) := "inventory"

var movements : array 1 .. 4 of string
movements (1) := "north"
movements (2) := "south"
movements (3) := "east"
movements (4) := "west"

var monsters : array 1 .. 3 of string
monsters (1) := "giant rat"
monsters (2) := "skeleton"
monsters (3) := "goblin"

var input, input2 : string
var Awesome : ^Inventory
new Awesome

loop
    get input
    put "" ..
    get input2 : *

    if input = commands (1) and input2 = items (1) then
        woodSword := true
        put "You took the ", items (1)
        end if
    if input = commands (5) then
        put "You have:"
        Awesome -> displayNames
    elsif input = commands (6) and input2 = items (1) then
        Awesome -> displayDesc
    end if

end loop



Please specify what version of Turing you are using
4.1.1

Author:  Tony [ Mon Mar 04, 2013 1:01 am ]
Post subject:  RE:Need Help with Turing Game

What kind of problems are you having with your current design?

Also
Quote:

itemDesc (4) := "a very strong sword made of iron."

don't you think that the ironSword class should be holding that description about itself, and not have this very tight coupling? In how many places would you have to change your Inventory class to add a 5th type of a sword? Does it make sense to change the way Inventory works that much, when a sword is essentially a tuple of (name, description, damage)?

Author:  Kyle Biro [ Mon Mar 04, 2013 5:25 pm ]
Post subject:  Re: Need Help with Turing Game

So what you are saying is have a class for every sword type?
Also, my problem is that I don't want to have 100 if statements to display the names and descriptions. There must be an easier way.

Author:  Raknarg [ Mon Mar 04, 2013 6:38 pm ]
Post subject:  RE:Need Help with Turing Game

So instead, compile them into an array.

This is how I think most games do it: each weapon would have a kind of weapon ID, so a wood sword ID would inherently be one and iron sword would inherently be four. This way you can just call a number, and have a function or procedure do all this for you.

Description (1), damage (1), name (1) would all refer to item 1. Much simpler than creating a boolean for every item type. Otherwise, there's not really a point to classes.

Turing:

proc displayNames (id : int)
        if woodSword = true then
            put itemNames (id)
    end displayNames

    proc displayDesc (id : int)
            put itemDesc (id)
    end displayDesc


Also, take in to account what Tony said. You want to create objects that are flexible, not tie them to certain things. For instance, you are coupling all the item information and objects together when they should be seperate.

Author:  Kyle Biro [ Mon Mar 04, 2013 11:03 pm ]
Post subject:  Re: Need Help with Turing Game

Turing:


put "There is a wooden sword in this room."

var woodSword : boolean := false
var input, input2 : string

var itemsDesc : array 1 .. 10 of string
itemsDesc (1) := "a weak sword made of wood."
itemsDesc (2) := "your inventory, this is where your items are stored."

var items : array 1 .. 10 of string
items (1) := "wooden sword"
items (2) := "inventory"

var commands : array 1 .. 5 of string
commands (1) := "take"
commands (2) := "open"
commands (3) := "examine"

class Inventory
    import items, itemsDesc, woodSword
    export displayItems, displayDesc

    proc displayItems (id : int)
        if woodSword = true then
            put items (id)
        end if
    end displayItems

    proc displayDesc (id : int)
        if woodSword = true then
            put itemsDesc (id)
        else
            put "You do not have a ", items (id)
        end if
    end displayDesc
end Inventory

var equipment : ^Inventory
new equipment

loop
    get input
    put "" ..
    get input2 : *

    if input = commands (1) and input2 = items (1) then %take wooden sword
        woodSword := true
        put "You took the ", items (1)
    elsif input = commands (3) and input2 = items (1) then %examine wooden sword
        equipment -> displayDesc (1)
    elsif input = commands (2) and input2 = items (2) then %open inventory
        put "You have:"
        equipment -> displayItems (1) %display the inventory
    end if

end loop



Something like this? I feel like something very obvious is missing and/or wrong.

Author:  DemonWasp [ Tue Mar 05, 2013 2:22 am ]
Post subject:  RE:Need Help with Turing Game

The first thing that's wrong is that you no longer need the woodSword boolean. Your code doesn't need to know whether the player has woodSword through a boolean: you can look at all the items the player has, and if any of them is a "Wooden Sword" then they have the wooden sword. Your Inventory->displayItems() and Inventory->displayDesc() procedures should have exactly one line in them (for the moment).

The big idea that you're missing is that you can use a class to describe items:

Turing:

class Item
    var name, description : string
    % other variables to describe that object here...
end Item


This lets you add new objects whenever: just make another instance of Item and set its properties:

Turing:

var woodenSword : ^Item
new woodenSword

woodenSword->name := "Wooden Sword"
woodenSword->description := "A weak sword made of wood. Who makes a sword out of wood, seriously?"

var steelSword : ^Item
new steelSword

steelSword->name := "Sword"
steelSword->description := "Now that's more like it!"


Once you have items, you can even put them into arrays:

Turing:

var inventorySlots : array 1..MAX_INVENTORY of ^Item

inventorySlots(0) := woodenSword % puts the woodenSword in the player inventory (at slot 0, potentially replacing something)
inventorySlots(1) := steelSword % now the player inventory has a woodenSword and a steelSword and nothing else (every other slot is nil)


From there, you can probably imagine an Inventory class, with a "slots" variable with type "array 1..max of ^Item". You could have methods like addItem(item:^Item) that puts a new item in the first open inventory space, dropItem(^Item) that drops an item if present, or even a function like findItem(name:string):^Item, which looks for an item matching that name in the player's inventory and returns a pointer to it.

Author:  Raknarg [ Tue Mar 05, 2013 4:50 pm ]
Post subject:  RE:Need Help with Turing Game

This still seems like a kind of inefficient solution... You shouldn't have to hard code every single item

Author:  DemonWasp [ Tue Mar 05, 2013 6:00 pm ]
Post subject:  RE:Need Help with Turing Game

Well, obviously the solution is to put all your data in a file and then just read it in. But, as a starting point, create a couple of objects in Turing code then figure out how to read them from a file.

Author:  Kyle Biro [ Tue Mar 05, 2013 6:08 pm ]
Post subject:  Re: Need Help with Turing Game

Turing:


class Item
    export name, description, damage
    var name, description : string
    var damage : int
    %other variables
end Item

var woodSword : ^Item
new woodSword

woodSword -> name := "wooden sword"
woodSword -> description := "A weak sword made of wood. Seriously, who makes a sword of wood?"
woodSword -> damage := 5

var steelSword : ^Item
new steelSword

steelSword -> name := "steel sword"
steelSword -> description := "Now this is a real sword!"
steelSword -> damage := 10

put steelSword -> name
put steelSword -> description



This gives me a warning saying: "'name' is read-only and cannot be assigned to" and it says this for "description" and "damage".
It still puts the name and description.

Just to add: classes and arrays are new to me.

Author:  Dreadnought [ Tue Mar 05, 2013 7:45 pm ]
Post subject:  Re: Need Help with Turing Game

I suggest you read the documentation for export. Variable members of a class are exported as "read-only" by default.

Author:  Kyle Biro [ Tue Mar 05, 2013 7:59 pm ]
Post subject:  RE:Need Help with Turing Game

Okay, I'll make sure I read that.

Author:  Kyle Biro [ Wed Mar 06, 2013 1:24 am ]
Post subject:  Re: Need Help with Turing Game

I'm still a little confused how to add the items on to the array, then display them if the user types something like "inventory".
From other languages I've programming in I know that they have "push" and "pop", but it seems that Turing doesn't have this function.

Some help would be appreciated.
Thanks!

Author:  Tony [ Wed Mar 06, 2013 2:02 am ]
Post subject:  Re: Need Help with Turing Game

Kyle Biro @ Wed Mar 06, 2013 1:24 am wrote:

From other languages I've programming in I know that they have "push" and "pop", but it seems that Turing doesn't have this function.

push and pop are conceptual operations on a stack, not an array. Though since a stack can be backed by an array, some libraries implement those methods as well. E.g. Ruby
Quote:

ruby-1.9.3-p0 :001 > stack = []
=> []
ruby-1.9.3-p0 :002 > stack.push(1)
=> [1]
ruby-1.9.3-p0 :003 > stack.push(2)
=> [1, 2]
ruby-1.9.3-p0 :004 > stack.pop
=> 2

but that doesn't give you random access to any element directly (how many times do you have to pop to get the 3rd element?)

array's index gives you that functionality
Quote:

Python 2.6.1 (r261:67515, Aug 2 2010, 20:10:18)
>>> array = ["one","two","three","four"]
>>> array[2]
'three'
>>> array[0]
'one'


Turing's arrays at the latter -- you can access any specific element directly. There's an additional constraint that you must declare the size of the array. If you run out of space, you'd need to create a new array and copy data over, much like one would do in C.

Someone will probably also mention flexible arrays

Author:  Kyle Biro [ Wed Mar 06, 2013 3:00 pm ]
Post subject:  RE:Need Help with Turing Game

When I compile the items into an array called "inventorySlots" the program runs with no errors, which is what I expected. When I try to do "put inventorySlots(1)" it throws an error at me saying "illegal put item".

Is there something I'm doing wrong, or is there another way to display items stored in an array?

Author:  Tony [ Wed Mar 06, 2013 4:27 pm ]
Post subject:  RE:Need Help with Turing Game

put can display strings, numbers, some other basic types. It doesn't know how to display an instance of type ^Item

you might have seen methods like to_string in other languages -- you'd need to implement and use such to get to a string representation of the object.

Author:  Raknarg [ Wed Mar 06, 2013 8:05 pm ]
Post subject:  RE:Need Help with Turing Game

In the same way that you call a a procedure, you can also refer to a variable.

For instance if you have a class with a procedure called show_description, you can do this:

object -> show_description

Lets say you have a variable called description. You can call it the same way:

put object -> description

Author:  Kyle Biro [ Wed Mar 06, 2013 10:30 pm ]
Post subject:  RE:Need Help with Turing Game

Well, I have an idea of how to add things to the inventory. My problem is displaying the array of type ^Item. As Tony said, I'd have to use something like "to_string" which I've never seen before. As far as I can think of, your way would require checking to see if the item is present, and the whole thing I'm stuck on is not wanting 100 if statements to check all of this.

Author:  Tony [ Wed Mar 06, 2013 10:48 pm ]
Post subject:  Re: RE:Need Help with Turing Game

I don't understand your question. You already have an instance of of ^Item, that's what you are trying to print.

It would be up to the ^Item to contain the code to describe itself, which should be straight-forward.

Kyle Biro @ Wed Mar 06, 2013 10:30 pm wrote:
the whole thing I'm stuck on is not wanting 100 if statements to check all of this.

This is a very good design objective to have Smile

Author:  Kyle Biro [ Sun Mar 10, 2013 5:26 pm ]
Post subject:  RE:Need Help with Turing Game

YES! I finally got it to work! It displays the inventory and I got dropping items to work.
Thank you everyone for your help!


: