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
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.
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 | ||
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:
This lets you add new objects whenever: just make another instance of Item and set its properties:
Once you have items, you can even put them into arrays:
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 | ||
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 |
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! |