Posted: 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 itemDesc :array1.. 10ofstring
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 =truethen put itemNames (1) endif if stoneSword =truethen put itemNames (2) endif if bronzeSword =truethen put itemNames (3) endif if ironSword =truethen put itemNames (4) endif end displayNames
proc displayDesc () if woodSword =truethen put itemDesc (1) endif if stoneSword =truethen put itemDesc (2) endif if bronzeSword =truethen put itemDesc (3) endif if ironSword =truethen put itemDesc (4) endif end displayDesc
end Inventory
Please specify what version of Turing you are using
4.1.1
Sponsor Sponsor
Tony
Posted: 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)?
Posted: 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.
Raknarg
Posted: 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 =truethen 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.
Kyle Biro
Posted: 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 :array1.. 10ofstring
itemsDesc (1):="a weak sword made of wood."
itemsDesc (2):="your inventory, this is where your items are stored."
var items :array1.. 10ofstring
items (1):="wooden sword"
items (2):="inventory"
var commands :array1.. 5ofstring
commands (1):="take"
commands (2):="open"
commands (3):="examine"
class Inventory
import items, itemsDesc, woodSword
export displayItems, displayDesc
proc displayItems (id :int) if woodSword =truethen put items (id) endif end displayItems
proc displayDesc (id :int) if woodSword =truethen put itemsDesc (id) else put"You do not have a ", items (id) endif end displayDesc
end Inventory
Something like this? I feel like something very obvious is missing and/or wrong.
DemonWasp
Posted: 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 :array1..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.
Raknarg
Posted: 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
DemonWasp
Posted: 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.
Sponsor Sponsor
Kyle Biro
Posted: 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.
Dreadnought
Posted: 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.
Kyle Biro
Posted: Tue Mar 05, 2013 7:59 pm Post subject: RE:Need Help with Turing Game
Okay, I'll make sure I read that.
Kyle Biro
Posted: 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!
Tony
Posted: 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
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
Posted: 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?
Tony
Posted: 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.