Author Topic:  PWR's PRGRSS - Learning C#  (Read 6999 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
PWR's PRGRSS - Learning C#
« on: February 05, 2016, 09:16:00 PM »
I'm learning C#.

On this topic, I will talk about my experience. I may also ask for solutions to problems.

Entry: 001
I've learnt a good portion of the basic material, and I've started learning classes, arrays, generics, and enums.

I wanted to start on a simple project, so I asked the first developer I could think of... Fabian Vikingnamedrop. He's got a bunch of games on the Xbox market place, and tonnes of experience with different styles of games.

He suggested I try a Console Application with a text parser. I was actually quite excited about this because I like old text adventure games. However, I can't figure out how to parse text.

For example, old text based games usually had a "help" command. When you type in "help", all of the possible commands would come up on screen. How do I do this?


Entry: 002
I learnt a really neat program while trolling around the web.
Code: [Select]
using System;
using System.Text;

class CodePage437
{
     public static void Main(string[] args)
     {
          // Set the window size and title
          Console.Title = "Code Page 437: MS-DOS ASCII Characters";
     
          for (byte b = 0; b < byte.MaxValue; b++)
          {
               char c = Encoding.GetEncoding(437).GetChars(new byte[] {b})[0];
               switch (b)
               {
                    case 8: // Backspace
                    case 9: // Tab
                    case 10: // Line feed
                    case 13: // Carriage return
                        c = '.';
                        break;
                }
       
               Console.Write("{0:000} {1}   ", b, c);
       
               // 7 is a beep -- Console.Beep() also works
               if (b == 7) Console.Write(" ");
       
               if ((b + 1) % 8 == 0)
                   Console.WriteLine();
           }
          Console.WriteLine();
      }
}
What this does is open up a console application that lets me access the ASCII characters. I'll show a picture below of both what it makes, and what you can do with it.


You may need to enlarge by clicking on the image, or just zoom in.


Fantastic, right? This will be the title of my first project. If it turns out nicely, I may even make it into a trilogy.

-"Intelligent Martian Parasites Attack"

-"Intelligent Martian Parasites Attack II: Intelligent Martian Parasites Strike Back"

-"Intelligent Martian Parasites Attack III: Intelligent Martian Parasites Last Stand"

Well, I'll be writing some more code for the rest of the strange hours of the morning, and probably into midday. I'm having too much fun with this coding stuff.


Entry: 003
Well, I've written a class that is the player's inventory. I hope that's a common practice, I'd be disappointed if I did something wrong or... like... not as good as I could have. In any case, upon doing this, and testing if it works, I found out that my parsing solution stopped working. I'm pretty sure it's because it only works if it's next in code, and I really don't want to have to paste them in every possible useable location. If I'm not clear, I need a way to always be able to access my words wherever I am in the program.

So that's my question this time. How do I make it so that my parser can detect anywhere? I hope that's understandable.

Until I figure this out, I can't really test if my inventory system works.


Entry: 004
Well, I did something really neat when I screwed up a bit of code. I meant to cut and paste it into notepad, but I forgot to save the document.

It made words change colours and letters. I was trying to do an important text thing, but I think I put my goto in the wrong place near my random number generator and I... I'm not sure. It was cool, though. If I can figure it out, it'd be neat to turn it into a "garbled speech" encounter in a future project.

I'm also doing another really neat thing with ASCII that I will show you all later. It's directly got to do with my game, but I need to finish it.

I can't finish it right now, though. I need sleep. Since I started learning C# on... Thursday, I think, I've gotten maybe 6-8 hours of sleep altogether. I personally don't notice it, but gravity is wearing out my beautiful face, and I keep seeing that stupid cat that I know darn well I got rid of a week or two ago. Clarification, I'm not actually seeing a cat. I'm seeing black blurs due to sleep deprivation, but my brain is telling me that they look like that darn cat.

At least it's obvious I'm enjoying this newfound hobby.

Toodles!


Entry:005
First off, I finished the ASCII project I mentioned to you all earlier. It's the map of the space ship you'll be playing on. Now, there aren't technically any spoilers, but if you do plan on playing this when I finish, I would recommend not looking at it. I've played my fair share of text adventures to know that going in blind is so satisfying. However, that decision will be up to you.
Spoiler for Map of ship:


Key:
CRDR = Corridor
MNTC = Maintenance
QTRS = Quarters


Also on my list of things to say, I'm going to try and take Craig's advice about enums (on page 2), but I'm still sort of confused. Could someone explain what benefits it would have to use enums over classes? I'll show an example of my current inventory code.

    class Inventory
    {
        public bool knife = true;
        public bool flashlight = true;
        public bool wrench = true;
        public bool water = true;
        public bool chitin = true;
        public bool map = true;
        public bool dogtags = true;
        public int dogtagsqty = 2;
        public string action = "";

Then when someone types in "check bag" this runs...

public void print()
        {
            if (knife == true)
            {
                Console.WriteLine(" - knife");
                Console.WriteLine();
            }
            if (flashlight == true)
            {
                Console.WriteLine(" - flashlight");
                Console.WriteLine();
            }
            if (wrench == true)
            {
                Console.WriteLine(" - wrench");
                Console.WriteLine();
            }
            if (water == true)
            {
                Console.WriteLine(" - water");
                Console.WriteLine();
            }
            if (chitin == true)
            {
                Console.WriteLine(" - chitin");
                Console.WriteLine();
            }
            if (map == true)
            {
                Console.WriteLine(" - map");
                Console.WriteLine();
            }
            if (dogtags == true)
            {
                Console.WriteLine(" - " + dogtagsqty + " dogtags");
                Console.WriteLine();
            }
        }

This way they can see all of what they have in their inventory shows up, and what they don't have doesn't show up.


Entry: 006
I'm having a lot of fun. Learning a lot. However despite what I've been learning, it still hasn't seemed to be enough. I know I've only been doing this for a few days, but new errors keep coming up that I'm completely lost about. Like one I'm currently having problems with...

Type or namespace definition, or end-of-file expected.

This is at the very bottom on the last closed curly bracket. I haven't even touched down there.

I don't know. I mean, I am truly having fun. It just seems so strange that I'm still lost on trying to translate what errors mean. I find myself taking a couple breaks over 2 or 3 hours of trying to understand the error. Eventually (normally) i give up trying to figure it out myself, then go ask google. Which returns very little... I rectract... A LOT of stuff... however, so little of it has meaning to my beginner brain that I'm still lost. So I come ask you guys (mainly Craig). Which is helpful, but then I feel like it's cheating.

In any case, I'm not even anywhere close to quitting, even if it seems like it. I also encourage anyone reading this who wants to start learning coding to not give up when this stuff happens. It's tough, but it's worth it, I feel.

Well, that's it for this entry. I hope you all are enjoying reading so far.


Entry: 007
Frustrated... taking a break of a couple days.


Entry: 008
Not exactly sure what all to say, but I feel as though I need to say something.

My studying has gone further in the last two days than my entire experience combined. I've been trolling around the web and I found some sotes that nicely document different C# commands. I've been reading them. Some of them feel out of order, like... badly. For example, they start talking about how to manipulate some things before telling you what the things wot you're manipulating. Which is mostly fine. Skip forward a few pages to read about the thing, and then go back to learn. However, my main problem is that the one site wot has the best documentation has no order whatsoever. Some things can't even be found from I. Annoying, very, to say the least.

That aside, I've been practicing some other console-based code. I managed to figure out how to slow down the visual portion of the program. I'll use this for "cinematic" effect. A better word is actually stylistic... but whatever. Basically I just need to use the Console.Beep command, but give it my desired length and a kHz level of 0. It's really neat.

I've also looked into graphics engines for a future secret project. Sort of secret. Technically, you all know about it, but you don't know how it's going to work. Eh... you'll understand eventually.

Turns out, though, most graphics engines don't actually support 2D. It's all 3D, but made to look 2D. Which... isn't a huge problem, but actually kind of throws a gear in my sticks.

That's about it, I'm pretty sure... In any case, I've got to get ready for school soon.
« Last Edit: February 17, 2016, 06:31:26 AM 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 #1 on: February 05, 2016, 10:47:29 PM »
Maybe something like this, I'm not 100% sure, haven't done a console app for a while.

var run = true;

while (run)
{
   var input = Console.ReadLine();

   switch (input.ToLower())
   {
      case "help":
         Console.WriteLine("Help Commands:");
         Console.WriteLine("Cmd1 - Something1");
         Console.WriteLine("Cmd2 - Something2");
         Console.WriteLine("Exit - Exit game.");
         // etc
         break;

      case "cmd1":
         // process cmd1 here
         break;

      case "cmd2":
         // process cmd2 here
         break;

      case "exit":
         run = false;
         break;
   }
}

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 #2 on: February 05, 2016, 11:57:41 PM »
Craig, you glorious man! That's exactly what I needed! Thank you a thousand times, thank you!

I tried so many solutions!

Now that this is working, I can start other things like... I don't know... an enemy randomiser.

EDIT: Didn't feel like making another post, but wanted to let you all know that I posted another entry in the original post.
« Last Edit: February 06, 2016, 03:10:42 AM 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 #3 on: February 06, 2016, 05:15:10 AM »
// 7 is a beep -- Console.Beep() also works
if (b == 7) Console.Write(" ");

Seems to me the code above is a bug, it rather should be part of the switch statement like so:

case 7: // Beep

Dryym

  • Supreme Member
  • *****
  • Posts: 4334
  • What's yours is mine. ~TM's Resident Master Thief~
  • Awards Members who have unlocked the robotic avatar Awarded to exemplary forum members 2 years on site 3 years on site Site art contributions/TPs in-game/Contest winners 1 year on site
    • View Profile
    • Awards
  • Xbox: dryym
Re: PWR's PRGRSS - Learning C#
« Reply #4 on: February 06, 2016, 11:17:11 AM »
I love Text based adventure games.
Mind filling us in on a bit of the story, Maybe some mechanics you have planned?


Will it be a puzzle adventure akin to Zork?

Creator of the term "Deathmine "
Jared [19|Oct 07:12 PM]: "It changes everything" Dryym

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 #5 on: February 06, 2016, 11:30:18 AM »
I believe, but don't hold me to this... just started this stuff... but I believe that because the ASCII characters; space, tab, linefeed, and carriage return aren't visible characters, they kept them out...? However, since beep is technically one of the ASCII signals, it still gets played when it's hexcode comes up... I think. I'm honestly not sure.

If what I said doesn't explain what you were talking about, then... um... I don't know. I just know that it beeps at the end of the printing where it's hexcode would be.
/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 #6 on: February 06, 2016, 11:54:42 AM »
It's closest entity would be Zork, yes. I love Zork, as I have probably stated somewhere else in another postI know I have. Probably talking about an about more arcade games in Total Miner. I may make a dungeon crawler if I can figure out the solution for doing that. I'm pretty sure I'll need to use ConsoleKey or something, as well as figure out how to keep the map generated as you move around.

As for the storyline for Intelligent Martian Parasites Attack, it's not entirely flushed out, but here's what I've got...

Years into the future, it's 2023!
You are Captain (name here) of the I.S.S. (name here). Your mission was simple; collect water samples from Mars and return to Earth so it could be studied. There's only one catch! Mars has parasites that are intelligent, and they boarded your ship. Your entire crew has already been eaten, and you can't let the intelligent martian parasites get to Earth! Point the ship away from Earth and take an escape pod to save humanity!

Coming soon, to a RadioShack near you!



EDIT: For the time being, you'll need to have some sort of programmeing studio to play it. I mean... I think. I'm using Visual Studio and Xamarin when Visual Studio decides to act up. It may be possible to run without a studio so long as you click open the right folder... I don't entirely know.
« Last Edit: February 06, 2016, 12:03:25 PM by PWRBTTN »
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

Stoneart

  • Sr. Member
  • ****
  • Posts: 255
  • "There's always time to get your block's off"
  • Awards Members who have unlocked the robotic avatar 1 year on site
    • View Profile
    • Discord link to my game (Coven)
    • Awards
  • Xbox: stoneart
  • PSN: Xx_Phoenix_Down_xX
Re: PWR's PRGRSS - Learning C#
« Reply #7 on: February 06, 2016, 01:15:13 PM »
Sounds cool! Can't wait to be able to try it sometime.

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 #8 on: February 06, 2016, 05:20:18 PM »
Entry: 003 updated to the original post.
/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 #9 on: February 06, 2016, 06:20:03 PM »
I believe, but don't hold me to this... just started this stuff... but I believe that because the ASCII characters; space, tab, linefeed, and carriage return aren't visible characters, they kept them out...? However, since beep is technically one of the ASCII signals, it still gets played when it's hexcode comes up... I think. I'm honestly not sure.

If what I said doesn't explain what you were talking about, then... um... I don't know. I just know that it beeps at the end of the printing where it's hexcode would be.
Backspace, Tab, LF and CR are all printable characters, but printing them adversely affects the format of the printed table, which is why they're replaced with a '.'. I think Beep falls into the same category, it shouldn't be printed, it has no visual output.

What do you mean the parser stopped working?

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 #10 on: February 06, 2016, 06:51:13 PM »
What do you mean the parser stopped working?
It works, but it seems like it has to be the next thing in the code for it to work. So if I have the story come before the parser, it'll meed to be at the parsing step. If I have story after the parser, it doesnt seem to work...
/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 #11 on: February 06, 2016, 08:34:39 PM »
Here is a basic structure to start from - this code would go in your program.cs file:

A class called IMPAGame is defined. An object is an instance of a class. The Main method of the program instantiates an IMPAGame object and calls it's Run method. The IMPAGame.Run method is the programs main control loop, it receives the user input, parses and processes the input, performs the appropriate output, then loops again to receive the next input.

using System;
using System.Text;

namespace IMPA
{
    class Program
    {
        static void Main(string[] args)
        {
            var game = new IMPAGame();
            game.Run();
        }
    }

    // Class encapsulating everything about the Game (high level).
    class IMPAGame
    {
        Player player;

        public void Run()
        {
            player = new Player();

            // Main game control loop.
            // Get player input, parse/process input, perform appropriate output.
            // Loop for next player input.
            // Break the loop if the player exits the game.
            while (true)
            {
                var input = Console.ReadLine();
                if (!ParseInput(input)) break;
            }
        }

        bool ParseInput(string input)
        {
            switch (input.ToLower())
            {
                case "exit":
                    return false;

                case "help":
                    Console.WriteLine("Help Commands:");
                    Console.WriteLine("Cmd1 - Something1");
                    Console.WriteLine("Cmd2 - Something2");
                    Console.WriteLine("Exit - Exit game.");
                    // etc
                    break;

                case "cmd1":
                    // process cmd1 here
                    break;

                case "cmd2":
                    // process cmd2 here
                    break;
            }

            return true;
        }
    }

    // Class encapsulating everything about a Player.
    class Player
    {
        public Inventory Inventory;

        public Player()
        {
            Inventory = new Inventory();
        }
    }

    // Class encapsulating everything about an Inventory.
    class Inventory
    {
        // bla bla
    }
}

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 #12 on: February 06, 2016, 08:42:00 PM »
/snail       /snail    /snail         /snail /snail      /snail   /snail                /snail        

Jack Of Shades

  • Supreme Member
  • *****
  • Posts: 3308
  • ☽♕۞♕☾
  • Awards 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 #13 on: February 06, 2016, 08:44:35 PM »
Craig stuff.

Craig, you're like a poet of C# aren't you?

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 #14 on: February 06, 2016, 08:50:09 PM »
Sometimes its fun revisiting the basics.

PWR if you use any code I post, ensure you fully understand it before moving on, otherwise it's not helping. I'm happy to answer any questions about anything I post here, no matter how simple they might seem. It's easy for me to overlook something or not explain something at a sufficienly simple level, so if that happens, just ask.