7 Replies - 1225 Views - Last Post: 16 December 2011 - 07:57 PM Rate Topic: -----

#1 Kasper1992   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-December 11

I am having a problem with an array in my code.

Posted 16 December 2011 - 02:10 PM

So i am very new to programming and only know basic code right now, but i've decided to start a little text based game mainly to just get used to C#.

My problem is using an array to let the player have an inventory.. so far i have:

 public static void inventory()
        {
            maxInv = new string[5];
            maxInv[noItems++] = "sword";            

            Console.WriteLine("Your Items: ");
            for (int i = 0; i < noItems; ++i)
            {
                Console.WriteLine("" + maxInv[i] + "");
            }
        }


here are my declarations for that procedure:
static string[] maxInv;
        static int noItems;


This is me trying to add to the inventory:
  if (addInv == "y")
                {
                    maxInv[noItems++] = "opal gem";
                    maxInv[noItems++] = "leather robe(provides minimal defense)";

                    for (int i = 0; i < noItems; ++i)
                    {
                        Console.WriteLine("" + maxInv[i] + "");
                    }

                    nextChoice();
                }



Sometimes the program crashes, other time it works, other times it adds 2 empty lines instead of the items.


ANY help at all will be really handy.

Is This A Good Question/Topic? 0
  • +

Replies To: I am having a problem with an array in my code.

#2 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: I am having a problem with an array in my code.

Posted 16 December 2011 - 02:15 PM

Maybe use a list?

It is dynamic (you don't have to specify the size of it)

List<string> inventory = new List<string>();



Then add inventory like this:
inventory.Add("Code Tags -> The Greatest Weapon Of All Times");



Some info on the list class:
List<T> Class (MSDN Article)

This post has been edited by RexGrammer: 16 December 2011 - 04:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: I am having a problem with an array in my code.

Posted 16 December 2011 - 03:52 PM

We see this approach all the time:

" I don't really know how to code, but I'm going to write a DND game "

It never turns out well. This is just one example of why. If you don't know what data types are at your disposal then how can you code this? The idea that a simple string[] or List<string> could cover the inventory of an adventure game is just naive. You need to understand how to make classes, so you can have things like

Class Weapon 
Class Blade : Weapon 
Class Knife : Blade 
Class Sword : Blade 
Class Projectile : Weapon 
Class Bow : Projectile 
Class CrossBow : Bow 
Class Sling : Projectile  
Class Money Class Gold : Money


And so on


Quote

I have a little programming experience but I need to write ...


You need to start there. I can't say "I have little experience in speaking Russian, but I have been assigned to write a mystery novel in Russian. Can you help me?"

We can help you by saying "First learn basic programming and the language of C#. Then take on assignments." Could someone here write this program for you? Sure. Could someone here map out all the processes you need to follow and do the Software Design part of this in the slim hope you could code it from there? Sure. But we don't volunteer to do the job that you're either getting paid for, or getting a grade for. You may want to read this.

For now, just work on the lessons. Do a self-teaching book from cover to cover. Then consider writing a program.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Nov 2011
Spoiler

Was This Post Helpful? 3
  • +
  • -

#4 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: I am having a problem with an array in my code.

Posted 16 December 2011 - 04:07 PM

True as ever...

I usually try to avoid games (it's still too complicated for me).
This made me wanting to start and make a game! (Let's see if it goes well... :tt2:)

Just a few questions for my self-enlightenment:
Would it be good to create a abstract class called item, and all of the items inherit from it?
Would it be great to create a class called 'Inventory' to house all of the items?

This post has been edited by RexGrammer: 16 December 2011 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: I am having a problem with an array in my code.

Posted 16 December 2011 - 04:16 PM

A lot of that depends on how someone wants to architect the game. To some degree its a matter of style and preference.

In your mind, is everything an 'item'? Or do you prefer to keep like-types separate? Weapons, Wealth, Health, Magic

If you had a class for 'inventory' you could then do things like

.Add(item newIncomingItme)
// Check the weight of the new item and see if it exceeds the total weight I can carry
// If I am limited to 5 weapons is this number 6?
// If this is a food item note the datetime so it can spoil after a week
// blah blah blah


Plan, plan, plan. There should be a week minimum in front of a whiteboard on this before the first key is ever pressed. You're talking about developing an entire virtual world populated with people, non-people, a story line, a goal, achievements, interaction... This is not something to be built by staring that the ceiling for 5 minutes then saying to yourself "I'll figure it out as I go"
Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: I am having a problem with an array in my code.

Posted 16 December 2011 - 04:20 PM

Thanks, I'm going to put serious effort into this one. My first HUGE project.

I think we hijacked this thread enough. :D

This post has been edited by RexGrammer: 16 December 2011 - 04:20 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: I am having a problem with an array in my code.

Posted 16 December 2011 - 04:35 PM

If everything is an Item then the Weapon should inherit the class Item
The you can have different classes of item.

Class Clothing :

Preferable I would use Interfaces rather than Classes as class only have single inheritance. With the Interface method you can have objects that
have multiple "identities"
Interface Item

Interface IWeapon : Item
Interface IBlade : IWeapon
Interface IClothing : Item
Interface IFootwear : IClothing
Class SpikyBoots
Implements IFootwear
Implements IWeapon
Was This Post Helpful? 1
  • +
  • -

#8 Kasper1992   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-December 11

Re: I am having a problem with an array in my code.

Posted 16 December 2011 - 07:57 PM

View PosttlhIn`toq, on 16 December 2011 - 03:52 PM, said:

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


This has helped me a lot! Thank you, i will take a break from carrying it on, and buy a book or two :)

Really appreciate the time and effort you put into helping me out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1