7 Replies - 244 Views - Last Post: 20 July 2012 - 02:40 AM Rate Topic: -----

#1 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

I am having an issue getting my desired outcome.

Posted 19 July 2012 - 04:33 PM

Basically, I am trying to check and see if a player types in a cretin command such as "!help" it will write some text to the console. I could use help with Syntax..and even logic..as I am not sure my method is the easiest/best. Here is what I have:
    class Commands
    {
        public void cmd()
        {
        string help = "!help";
        string attack = "!attack";
        string equipment = "!equipment";
        string clothing = "!clothing";
        string estates = "!estates";
        string location = "!location";
        string work = "!work";
        string train = "!train";
        string go = "!go";
        string track = "!track";
        string powers = "!powers";
        string money = "!money";
        string stats = "!stats";
        string skills = "!skills";
        string eat = "!eat";
        string armor = "!armor";
        string north = "!north";
        string south = "!south";
        string east = "!east";
        string west = "!west";
        string yes = "!yes";
        string no = "!no";

        for (; ; )
        {
            // statements
            string holdValue = "";
        if (Console.ReadLine() == "!help")
        {
            Console.WriteLine(help);
            
        }

        if (Console.ReadLine() == "!attack")
        {
            Console.WriteLine("You are Attacking!");
            
        }
        }
        }


Then in my main I have this:
static void Main(string[] args)
        {
            Commands commands = new Commands();
            commands.cmd();
        }

the problem is sometimes when I type in !attack it will say you are attacking and sometimes it wont..not sure why I am getting this outcome. If anyone is curious..its just a lil Mini mud game.

Is This A Good Question/Topic? 0
  • +

Replies To: I am having an issue getting my desired outcome.

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,453
  • Joined: 28-April 09

Re: I am having an issue getting my desired outcome.

Posted 19 July 2012 - 05:03 PM

It's because you have 2 console.readline commands. If you type !attack twice in a row, it will match on the if that checks for attack but not on the one that checks for help.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 818
  • View blog
  • Posts: 1,961
  • Joined: 04-October 09

Re: I am having an issue getting my desired outcome.

Posted 19 July 2012 - 05:05 PM

You are accepting input on two different lines (32 and 38). When the program is on line 32, it only recognizes the "!help" command, and when on line 38 it only recognizes the "!attack" command.

Otherwise, your code is going to become a nightmare very quickly. I'd look around for tutorials on arrays and enumerated values.
Was This Post Helpful? 0
  • +
  • -

#4 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I am having an issue getting my desired outcome.

Posted 19 July 2012 - 06:37 PM

Okay, well let me ask this: How do I constantly check for input, and then if it matches a cretin string typed then it outputs a certain set of strings.
Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,453
  • Joined: 28-April 09

Re: I am having an issue getting my desired outcome.

Posted 19 July 2012 - 07:23 PM

you could do something like

string holdValue = string.Empty;

for (;;)/>
{
    holdValue = Console.ReadLine();
    // rest of code
}



Then just check the value of holdValue

This post has been edited by Nakor: 19 July 2012 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I am having an issue getting my desired outcome.

Posted 20 July 2012 - 01:01 AM

ya, I dont think that would work for me cause I would need a Console.ReadLine(); for each command and that puts me back where I started ;/.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2037
  • View blog
  • Posts: 6,060
  • Joined: 05-May 12

Re: I am having an issue getting my desired outcome.

Posted 20 July 2012 - 01:23 AM

View PostTailean, on 20 July 2012 - 01:01 AM, said:

ya, I dont think that would work for me cause I would need a Console.ReadLine(); for each command and that puts me back where I started ;/.


I beg to differ... You don't need a Console.ReadLine() for each command.

Consider this code:
var code = new List<string>();
bool quit = false;
while(!quit)
{
    Console.WriteLine("Ready.");
    string command = Console.ReadLine();
    switch(command)
    {
    case "quit":
        quit = true;
        break;

    case "list":
        foreach(var line in code)
            Console.WriteLine(line);
        break;

    case "run":
        Console.WriteLine("Can't run code yet.");
        break;

    default:
        code.Add(command);
        break;
    }
}



One Console.ReadLine(), but so far the code above has 3 commands. It's brute force code. A more elegant solution would use a chain of responsibility pattern to find the command, and a strategy pattern to run the command.

This post has been edited by Skydiver: 20 July 2012 - 01:24 AM

Was This Post Helpful? 1
  • +
  • -

#8 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I am having an issue getting my desired outcome.

Posted 20 July 2012 - 02:40 AM

Point taken.. my in experience with Syntax is getting the better of me..thanks for your post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1