Author Topic:  PWR's PRGRSS - Learning C#  (Read 7687 times)

0 Members and 1 Guest are viewing this topic.

PWRBTTN

  • Legendary Member
  • *****
  • Posts: 1172
  • Professional Expert, Comrade
  • Awards Awarded to exemplary forum members 2 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: PWRBTTN
  • Steam: PWRBTTN
Re: PWR's PRGRSS - Learning C#
« Reply #30 on: February 07, 2016, 08:21:55 AM »
Yes to run the game outside visual studio you just need to run the .exe file that is produced when you build the solution.
Is there some way to attach music to this somehow? I would like to have some 8-bit music loop until the console window is closed.

I know a great 8-bit sequencer that lets me pretend I am good at music.


EDIT: I posted entry 004 on the original post. This entry is more describing a couple experiences than sharing data or asking a question.
« Last Edit: February 07, 2016, 09:01:33 AM by PWRBTTN »
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

Jack Of Shades

  • Supreme Member
  • *****
  • Posts: 3312
  • ☽♕۞♕☾
  • Awards 4 years on site 3 years on site Donated a giveaway prize. (Must be a prize of cash value.) 2 years on site Members who have unlocked the robotic avatar 1 year on site Was in the top 10 of the forum stats page as of 4/12/15 Former MotM winners
    • View Profile
    • Jack Of Shades Youtube Gaming Channel
    • Awards
Re: PWR's PRGRSS - Learning C#
« Reply #31 on: February 07, 2016, 01:15:34 PM »
and I've started learning classes, arrays, generics, and enums.
These are perfect for your Inventory.

Create an enum that defines all the items in your game.

    enum Item
    {
        None,
        Knife,
        Flashlight,
        Wrench,
        Water,
        Chitin,
        Map,
        Dogtag,
    }

Your Inventory class uses a generic list to store the items, and a foreach loop to enumerate the items and print.

    struct InventoryItem
    {
        public Item Item;
        public int Count;
    }

    class Inventory
    {
        public List<InventoryItem> Items = new List<InventoryItem>();

        public void Print()
        {
            foreach (var item in Items)
            {
                Console.WriteLine("{0}: {1}", item.Item, item.Count);
            }
        }
    }

Wow! You described this to me and I actually understand it. I DEFINITELY saw this on one exercise I did and was curious as to what it meant/did.

Nefty

  • Supreme Member
  • *****
  • Posts: 2431
  • This Hotel Sucks
  • Awards Site art contributions/TPs in-game/Contest winners Former MotM winners 5 years on site 4 years on site 3 years on site 2 years on site 1 year on site
    • View Profile
    • Youtube
    • Awards
  • Steam: neftygaming1
Re: PWR's PRGRSS - Learning C#
« Reply #32 on: February 07, 2016, 01:18:00 PM »
This is not really a C# question, but next semester I start of few different classes regarding coding. AP Computer Science, Game Programming, Mobile App Development etc.... The head of the department says these are J.A.V.A. based classes (which I'm not bad at). I'm wondering how that compares to C# if anyone knows? I've heard they are rather similar.
Miner Of The Month - April 2014

Jack Of Shades

  • Supreme Member
  • *****
  • Posts: 3312
  • ☽♕۞♕☾
  • Awards 4 years on site 3 years on site Donated a giveaway prize. (Must be a prize of cash value.) 2 years on site Members who have unlocked the robotic avatar 1 year on site Was in the top 10 of the forum stats page as of 4/12/15 Former MotM winners
    • View Profile
    • Jack Of Shades Youtube Gaming Channel
    • Awards
Re: PWR's PRGRSS - Learning C#
« Reply #33 on: February 07, 2016, 02:47:25 PM »
This is not really a C# question, but next semester I start of few different classes regarding coding. AP Computer Science, Game Programming, Mobile App Development etc.... The head of the department says these are J.A.V.A. based classes (which I'm not bad at). I'm wondering how that compares to C# if anyone knows? I've heard they are rather similar.
I know I can't fully answer this, but on an off note this is a C# question ;). Anyway I heard it has similarities but cannot say for sure. I believe Craig could better answer it. Although, now...I am curious as well. lol

PWRBTTN

  • Legendary Member
  • *****
  • Posts: 1172
  • Professional Expert, Comrade
  • Awards Awarded to exemplary forum members 2 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: PWRBTTN
  • Steam: PWRBTTN
Re: PWR's PRGRSS - Learning C#
« Reply #34 on: February 07, 2016, 07:10:49 PM »
Entry:005 added.

This one shows off something really neat, and asks a question.
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

PWRBTTN

  • Legendary Member
  • *****
  • Posts: 1172
  • Professional Expert, Comrade
  • Awards Awarded to exemplary forum members 2 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: PWRBTTN
  • Steam: PWRBTTN
Re: PWR's PRGRSS - Learning C#
« Reply #35 on: February 07, 2016, 08:36:41 PM »
I'd like to mess around some cosmetic commands like CursorSize and ConsoleColor.

Any suggestions anyone? I'm having a hard time finding these.
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

Craig

  • Developer
  • Extreme Member
  • ******
  • Posts: 6650
  • Dig Deep
  • Awards Developer 5 years on site 4 years on site 3 years on site 2 years on site Was in the top 10 of the forum stats page as of 4/12/15 Members who have unlocked the robotic avatar 1 year on site Day 1 w/25+ posts (4/12/13)
    • View Profile
    • Twitter
    • Awards
  • Steam: tota1miner
Re: PWR's PRGRSS - Learning C#
« Reply #36 on: February 07, 2016, 08:37:14 PM »
Could someone explain what benefits it would have to use enums over classes?
You generally wouldn't use enums over classes, generally you'd use enums in combination with classes. Enums are just a way to conveniently use language to describe numeric ID's - i.e. it's easier to pass the word Knife to some method, rather than some ID number like 8 - it also allows the compiler to detect errors that it wouldn't be able to detect if you just used a basic int number.

With the way your inventory class is currently set up, it doesn't allow the player to carry more than 1 of the same item (except the dog tags), although that's fairly easy to allow by changing the bools to ints.

But most importantly, every time you add a new item into the game or change, rename or remove an existing item, you'll have to add, change or remove code to your inventory class, both for the storage and the methods - like the print method. You definately don't want to be doing that.

The Inventory class I posted means you don't have to change any code in the Inventory class whenever items are added, changed, renamed, removed, because it stores the Item as an enum variable rather than a hard coded field, and it allows carrying more than 1 of the same item.

The benefits will also become more clear as you add more functionality to your Inventory class, like querying if it contains a specific item, or dropping items, etc, you don't want to be changing all that code as well every time a new item is added, changed, renamed or removed. You don't want to be changing any code, anywhere in your game, when items are added, changed or removed, except in the one place they are defined.

PWRBTTN

  • Legendary Member
  • *****
  • Posts: 1172
  • Professional Expert, Comrade
  • Awards Awarded to exemplary forum members 2 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: PWRBTTN
  • Steam: PWRBTTN
Re: PWR's PRGRSS - Learning C#
« Reply #37 on: February 07, 2016, 08:44:21 PM »
Could someone explain what benefits it would have to use enums over classes?
that's incredibly helpful. Thanks.

Where exactly would I stick this?

-----
    enum Item
    {
        None,
        Knife,
        Flashlight,
        Wrench,
        Water,
        Chitin,
        Map,
        Dogtag,
    }

Your Inventory class uses a generic list to store the items, and a foreach loop to enumerate the items and print.

    struct InventoryItem
    {
        public Item Item;
        public int Count;
    }

    class Inventory
    {
        public List<InventoryItem> Items = new List<InventoryItem>();

        public void Print()
        {
            foreach (var item in Items)
            {
                Console.WriteLine("{0}: {1}", item.Item, item.Count);
            }
        }
    }
-----

In the inventory class?

Also, what all of my previous method should I keep and remove?
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

Craig

  • Developer
  • Extreme Member
  • ******
  • Posts: 6650
  • Dig Deep
  • Awards Developer 5 years on site 4 years on site 3 years on site 2 years on site Was in the top 10 of the forum stats page as of 4/12/15 Members who have unlocked the robotic avatar 1 year on site Day 1 w/25+ posts (4/12/13)
    • View Profile
    • Twitter
    • Awards
  • Steam: tota1miner
Re: PWR's PRGRSS - Learning C#
« Reply #38 on: February 07, 2016, 10:58:26 PM »
Where exactly would I stick this?
In the inventory class?
Also, what all of my previous method should I keep and remove?

It replaces your current Inventory class.

PWRBTTN

  • Legendary Member
  • *****
  • Posts: 1172
  • Professional Expert, Comrade
  • Awards Awarded to exemplary forum members 2 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: PWRBTTN
  • Steam: PWRBTTN
Re: PWR's PRGRSS - Learning C#
« Reply #39 on: February 08, 2016, 01:29:24 AM »
Can anyone tell me how I go about making something like this?

Type 'help' for a list of commands.

I've tried it so many times!! But it won't work!!!
if you're talking about just making it display on screen, use..

Console.WriteLine ("Type Help for list of commands.");

But if you're talking about making a list of things for it to display when typed, you'll find that on the first page of the topic. If I remember correctly, it's the first post by Craig.
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

PWRBTTN

  • Legendary Member
  • *****
  • Posts: 1172
  • Professional Expert, Comrade
  • Awards Awarded to exemplary forum members 2 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: PWRBTTN
  • Steam: PWRBTTN
Re: PWR's PRGRSS - Learning C#
« Reply #40 on: February 08, 2016, 02:06:43 AM »
I've spent the last hour and a half messing with this stuff. Craig, please clear this up. I read through it, I still don't understand enums very well. I was going to read through it when it was working, but it's not working. So I can't read through it. So I don't understand enums. It's simply how I learn. There are three issues presented, and they're all on the same screen. So to save time, I took a picture.

[Removed picture]
« Last Edit: February 09, 2016, 11:45:25 PM by PWRBTTN »
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

Craig

  • Developer
  • Extreme Member
  • ******
  • Posts: 6650
  • Dig Deep
  • Awards Developer 5 years on site 4 years on site 3 years on site 2 years on site Was in the top 10 of the forum stats page as of 4/12/15 Members who have unlocked the robotic avatar 1 year on site Day 1 w/25+ posts (4/12/13)
    • View Profile
    • Twitter
    • Awards
  • Steam: tota1miner
Re: PWR's PRGRSS - Learning C#
« Reply #41 on: February 08, 2016, 03:07:54 AM »
I've spent the last hour and a half messing with this stuff...
The compiler is just telling you it doesn't know which .Net assembly to find the List<T> class (type) in.

Add the following line to the top of your .cs file.

using System.Collections.Generic;


Under the hood, Enums are just an integer number. But they allow us to use an english name when telling the compiler we want to use them.

So the Item enum is just an enumeration of numbers, ID numbers if you like, at the same time allowing the programmer to refer to them using their english name equivalent.

    enum Item
    {
        None,
        Knife,
        Flashlight,
        Wrench,
        Water,
        Chitin,
        Map,
        Dogtag,
    }

With the enum above, Item is an Int32 (32 bit integer) and the names have an ascending integer assigned to them:

i.e.

    enum Item
    {
        None = 0,
        Knife = 1,
        Flashlight = 2,
        Wrench = 3,
        Water = 4,
        Chitin = 5,
        Map = 6,
        Dogtag =7,
    }

Say you have a method like so:

void AddItem(Item item) { .. }

calling it using: AddItem(Item.Flashlight);

is essentially the same under the hood as:

void AddItem(int item) { .. }
calling: Additem(2);

So AddItem(Item.Flashlight) is easier to read/understand than AddItem(2), but to the compiler and the executable they're essentially the same thing.


« Last Edit: February 08, 2016, 03:16:46 AM by Craig »

PWRBTTN

  • Legendary Member
  • *****
  • Posts: 1172
  • Professional Expert, Comrade
  • Awards Awarded to exemplary forum members 2 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: PWRBTTN
  • Steam: PWRBTTN
Re: PWR's PRGRSS - Learning C#
« Reply #42 on: February 08, 2016, 03:58:19 AM »
I've spent the last hour and a half messing with this stuff...
Wow. I didn't now that. See, I wish the tutorial would have said that. I mean, I'm not sure knowing that would solve my problem of the program not working. You solved that, too, though.

You are a wizard! A technomancer!

I'm excited about this. My text adventure is going to be the best text-based game released in February of this year!

Well... I hope it'll be in February. I mean... it's just a text-based game.
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

Craig

  • Developer
  • Extreme Member
  • ******
  • Posts: 6650
  • Dig Deep
  • Awards Developer 5 years on site 4 years on site 3 years on site 2 years on site Was in the top 10 of the forum stats page as of 4/12/15 Members who have unlocked the robotic avatar 1 year on site Day 1 w/25+ posts (4/12/13)
    • View Profile
    • Twitter
    • Awards
  • Steam: tota1miner
Re: PWR's PRGRSS - Learning C#
« Reply #43 on: February 08, 2016, 06:21:31 AM »
Here's a flashier version of the Inventory.Print() method. You should be able to see what's different.

Code: [Select]
        public void Print()
        {
            Console.WriteLine();

            if (Items.Count < 1)
            {
                Console.WriteLine("Inventory: Empty");
            }
            else
            {
                Console.WriteLine("Inventory:");
                foreach (var item in Items)
                {
                    Console.WriteLine("{0}: {1}", item.Item, item.Count);
                }
            }

            Console.WriteLine();
        }
« Last Edit: February 08, 2016, 06:29:51 AM by Craig »

Craig

  • Developer
  • Extreme Member
  • ******
  • Posts: 6650
  • Dig Deep
  • Awards Developer 5 years on site 4 years on site 3 years on site 2 years on site Was in the top 10 of the forum stats page as of 4/12/15 Members who have unlocked the robotic avatar 1 year on site Day 1 w/25+ posts (4/12/13)
    • View Profile
    • Twitter
    • Awards
  • Steam: tota1miner
Re: PWR's PRGRSS - Learning C#
« Reply #44 on: February 08, 2016, 06:28:45 AM »
Here's a method to allow you to add items to the inventory:

Code: [Select]
        // this method is public so that code outside the Inventory class can call it
        public void AddItem(Item item)
        {
            // see if we already have the item in inventory
            var i = GetIndex(item);

            // if i >= 0 then we already have the item in inventory
            if (i >= 0)
            {
                // we already have item, so just increase the count
                var ii = Items[i];
                ii.Count++;
                Items[i] = ii;
            }
            else
            {
                // we don't have the item, so add a new one with a count of 1.
                var ii = new InventoryItem() { Item = item, Count = 1 };
                Items.Add(ii);
            }
        }

        // this method is private, it's only used internally by the Inventory class
        int GetIndex(Item item)
        {
            // enumerate each item in the item list and return its index if found, otherwise return -1
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].Item == item) return i;
            }
            return -1;
        }
« Last Edit: February 08, 2016, 06:38:05 AM by Craig »