0

I have a quick question to assigning values.
In my new program I am trying to assign values to booleans depending on the value of an integer. Here a quick example of what I mean:

bool northDoorAvailable;
int roomLocation;

// set player Location in some code
roomLocation = 2;

// now set if the north door is available

Is this only possible with a function in that I write alot of if-statements?

public void checkDoors() 
{
    if ( roomLocation == 1 )
    {
        northDoorAvailable = false;
    }
    if ( roomLocation == 2 )
    {
        northDoorAvailable = true;
    }
}

or can this process be automized?

Glad for any replies.

7
  • You could use a number of different methods, from storing the values in dictionaries to simply reducing it to a single line: northDoorAvailable = roomLocation != 1; for example. Commented Oct 30, 2015 at 18:41
  • 3
    Are those the only two possible conditions? What if roomlocation == 3? Commented Oct 30, 2015 at 18:41
  • @RonBeyer How would I do that in a code? Sorry, I am pretty new to coding compared to you apparently :) Commented Oct 30, 2015 at 18:43
  • @DStanley No there would be others so if roomlocation != possibleLocations it would do northDoorAvailable = false Commented Oct 30, 2015 at 18:44
  • @IanH. Just like I showed you (that is a valid line of code). You can replace the entire checkDoors function with that, but this is assuming that northDoorAvailable is true only if the room location is not equal to 1. If you have other requirements, then its better to use a dictionary. I will type an answer that shows how you can use it. Commented Oct 30, 2015 at 18:45

3 Answers 3

2

A better syntax depends greatly on all possible conditions. Right now you could use

 northDoorAvailable = (roomLocation == 2);

or

 northDoorAvailable = (roomLocation != 1);

since X == Y returns true or false depending on whether or not the two are "equal". (and vice-versa for !=)

But what if roomLocation is 3? 4?

In general, usally it is possible to simplify these conditions into a single logical expression, but it may not always be the case. One way to simplify is to put all possible conditions in a "truth table" and see what the equivalent logical expression is.

there would be others so if roomlocation != possibleLocations it would do northDoorAvailable = false

I'm assuming that possibleLocations is a collection if integers, and if it contains roomlocation then northDoorAvailable would be true. In that case you could use

northDoorAvailable = (possibleLocations.Contains(roomlocation));
Sign up to request clarification or add additional context in comments.

2 Comments

@D Stanley Ahhh, now I understand. One last question: Is it possible to do this in the background while the other code is running so it doesn't have to be called over and over again?
Not like you mean, but you could make it a property and do the calculation when you "get" the property. Do some research on properties, getters, and setters, and ask another specific question if you're stuck. This is pretty basic C# stuff so you can probably find the answers out on your own.
0

You could use a dictionary for this:

public class MyGameClass
{
    private Dictionary<int, bool> _northDoorAvailable = new Dictionary<int, bool>();

    public MyGameClass()
    {
        _northDoorAvailable[1] = false;
        _northDoorAvailalbe[2] = true;
        _northDoorAvailable[3] = false;
        _northDoorAvailable[4] = false;
        _northDoorAvailable[5] = false;
    }

    public bool IsNorthDoorAvailable(int room)
    {
        bool available = false;
        _northDoorAvailable.TryGetValue(room, out available);
        return available;
    }
}

The dictionary is a key/value store where, in this case, the key is the room number, and the value (the other side of the = mark) is if the door is available or not. You can quickly add cases to the dictionary in the constructor method for whatever class you are using, and you can access the dictionary internally, or publically through the IsNorthDoorAvailable method.

Comments

0

What you could do is have a sorted list and parse a word doc or db table with a list of rooms and their corresponding boolean values then just loop through the list

1 - true

2 - true

3 - false

etc...

SortedList<int, bool> roomsList = new SortedList<int, bool>();

using (StreamReader reader = new StreamReader(@"C:\temp\RoomList.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                string[] rooms = line.Split('-');
                roomsList.Add(int.Parse(rooms[0]), bool.Parse(rooms[1]));
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.