Home > Mobile >  How do i use a parameter input for an object reference?
How do i use a parameter input for an object reference?

Time:11-21

I really had no idea how to phrase that question correctly since i'm so very new at this, so here goes:

I am trying to create a "make your own adventure" with only two answer options on every question. At first, i was going to make it completely procedural with dozens of nested if statements, but i was recommended to use OOP instead. This is my absolutely first OOP project, so i feel really clueless about what i should make objects of. The only idea i have so far is to make an object out of the "give a piece of text, and inquire an answer" situation. I also put in an idea in the if statements which i though was genius, but apparently didn't work, where i tried to use the alt parameters as object names for the next quest to run.

namespace Historia
{
    public class Story
    {
        string text;
        public void Quest (string text, object alt1, object alt2)
        {
            Console.WriteLine(text);
            string input = Console.ReadLine();
            if (input == "1")
            {
                alt1.Quest();
            }
            else if (input == "2")
            {
                alt2.Quest();
            }
        }
    }
}

The other page contains this:

namespace Historia
{
    class Program
    {
        static void Main(string[] args)
        {
            Story A1 = new Story();
            Story A2 = new Story();
            Story A3 = new Story();
            A1.Quest("Welcome, are you there?", A2, A3);

        }
    }
}

Again, i feel really clueless as how to progress and would appreciate some input.

CodePudding user response:

On your Quest method you declare alt1 and alt2 as object, so it is an anonymous object and your code will not know that it is a Story object.

Your property string text; is not being used since in your quest you use a string text and it is another parameter.

When you are coding on OOP you usually create a constructor* as you will see in my code below where when you initialize the class like Story A1 = new Story("Welcome, are you there?"); you also insert the text.

As you can see I am assigning the input parameter to the class property so I can use it later when you call A1.Quest(answer). A1 already has the story texts.

namespace Historia
{
    public class Story
    {
        string text_alt1;
        string text_alt2;

        // Constructor
        public Story (string text_alt1, string text_alt2 = null)
        {
            this.text_alt1 = text_alt1;
            this.text_alt2 = text_alt2;
        }

        // Method
        public string Quest(string answer = null)
        {
            // If there is no answer because it is the first question
            // show text1
            if (answer == "1" || answer == null)
            {
                Console.WriteLine(text_alt1);
            }
            else if (answer == "2")
            {
                Console.WriteLine(text_alt2);
            }
            //Return the user input
            return Console.ReadLine();
        }

    }
}

Notice the following line codes on the code above for the rest of the explanation: We changed "void"** for "string" it is the type of value that will be returned.

public string Quest(string answer = null)

and the next code where we return the user input

return Console.ReadLine();

On the other file, we declare all the stories in first place with the stories texts using the constructor.

Then, we initialize the quests. As we placed a return it will give us the user input so we store it on answer and put it in next quest and again we store in asnwer overriding last value and so on.

It is also possible to add all those objects on a list and iterate it like a loop so you don't need to write answer = AX.Quest(answer); all the time, just once. - If you want that I upgrade this answer with that just tell me but I don't want to write too much.

namespace Historia
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declaring stories
            Story A1 = new Story("Welcome, are you there?");
            Story A2 = new Story("Alternative1 A2", "Alternative2 A2");
            Story A3 = new Story("Alternative1 A3", "Alternative2 A3");

            //User interacting with stories
            var answer = A1.Quest();
            answer = A2.Quest(answer);
            answer = A3.Quest(answer);
            //...
        }
    }
}

If you have more questions just ask =).

Note: This example doesn't cover that if in A1 you choose 1, in A2 you can choose between A4, A5 and if you choose 2 in A3 you can choose between A6, A7. It is lineal with 2 answer in each one. It means I choose in A1 1, so in A2 i have A4 and A5 and if I choose in A1 2 in A3 I have A4 and A5 to choice.

*Constructors are methods declared with the same name of the class, there can be multiple constructors in a single class but with different input parameters and usually you set properties with input parameters of the constructor to know the difference to properties you must write this.PropertyName which is needed if a property and a parameter has the same name.

string text_alt1;
string text_alt2;

// Constructor 1
public Story (string text_alt1, string text_alt2 = null)
{
    this.text_alt1 = text_alt1;
    this.text_alt2 = text_alt2;
}
// Constructor 2
public Story(string text_alt1, string text_alt2, string text_alt3 = null)
{
    this.text_alt1 = text_alt1;
    this.text_alt2 = text_alt2;
}

**Void it means that a method will not return any value.

EDIT increased answer after comment

Keeping in mind what you tell on your comment, this aproach should do the work.

Basically a constructor where you put your Story objects instead of anonymous and then a Quest() without input parameters.

public class Historia
    {
        public string text;
        public Story story1;
        public Story story2;

        // Constructor 2
        public Story(string text, Story story1 = null, Story story2 = null)
        {
            this.text = text;
            this.story1 = story1;
            this.story2 = story2;
        }

        public void Quest()
        {
            Console.WriteLine(text);
            string input = Console.ReadLine();
            if (input == "1" && story1 != null)
            {
                story1.Quest();
            }
            else if (input == "2" && story2 != null)
            {
                story2.Quest();
            }
        }

    }

And on the main file, you define the text for each story and then you decide which story is connected with each other.

class Program
{
    static void Main(string[] args)
    {
        Story A1 = new Story("Welcome, are you there?");
        Story A2 = new Story("Alternative A2");
        Story A3 = new Story("Alternative A3");
        Story A4 = new Story("A2 with alternative A4");
        Story A5 = new Story("A2 with alternative A5");
        Story A6 = new Story("A3 with alternative A6");
        Story A7 = new Story("A3 with alternative A7");
        Story End = new Story("Concurrency for an specific time of the story or Ending");
        Story timeTravel = new Story("TimeTraveling if you choose 2 at end", A2, A3);

        //Declaring which stories are connected with other.
        A1.story1 = A2;
        A1.story2 = A3;
        A2.story1 = A4;
        A2.story2 = A5;
        A3.story1 = A6;
        A3.story2 = A7;

        //Possible to do a connect again even if the stories were different and then split again
        A4.story1 = End;
        A5.story1 = End;
        A6.story1 = End;
        A7.story1 = End;
        End.story2 = timeTravel;

        A1.Quest();

    }
}

If it resolve your question upvote and check as answer would be nice.

  •  Tags:  
  • c#
  • Related