简体   繁体   中英

Ternary operator inside an if-statement that results in nothing

Ok, I'm probably going about this all wrong, but I can't seem to find a good solution, so any pointers would help. I have the following statement in my code:

if (!mapDict.ContainsKey(_thisRoom.Item1))
        {

            MapGraphItem roomMGI = new MapGraphItem();


            var rndOrderRooms = roomList.OrderBy(i => rnd.Next());
            foreach (MapGraphItem room in rndOrderRooms)
            {
                if (!room._flags.IsFlagSet(GlobalValues.MapTileType.start) && !room._flags.IsFlagSet(GlobalValues.MapTileType.exit)
                    && ((_thisRoom.Item2 == 'N') ? room._north : (_thisRoom.Item2 == 'S') ? room._south : (_thisRoom.Item2 == 'E') ? room._east : room._west)
                    && ((mapOpenings.Count < 4) : !room._flags.IsFlagSet(GlobalValues.MapTileType.deadend) ? *ignore this section*))
                {
                    roomMGI = room;
                    earlyRooms.Add(room);
                    goto Exit;
                }
            }

            Exit:
            //MapGraphItem _room = earlyRooms[rnd.Next(0, earlyRooms.Count)];

            GameObject _roomGO = (GameObject)Instantiate(roomMGI.gameObject, _thisRoom.Item1, Quaternion.identity);
            roomMGI._position = GlobalValues.MapTilePos.Early;
            mapDict.Add(_roomGO.transform.position, roomMGI);

            _mapUsed++;

            if (roomMGI._north) _n = true;
            if (roomMGI._south) _s = true;
            if (roomMGI._east) _e = true;
            if (roomMGI._west) _w = true;

            Debug.Log ("Early room added at: " + _thisRoom.Item1.ToString() + " N? " + (_n ? "yes" : "no") + " S? " + (_s ? "yes" : "no") + 
                " E? " + (_e ? "yes" : "no") + " W? " + (_w ? "yes" : "no"));

            GetRoomOpenings(_roomGO.transform.position, _n, _s, _e, _w);

            _tiles--;
        }

Basically, the *ignore this section* area means I want nothing to happen.

In other words, assuming the first two lines of the if are correct, if the .Count is less than four, there's an additional condition. If .Count is four or more, that condition is not needed.

I can't set it to the opposite, just room._flags.IsFlagSet(GlobalValues.MapTileType.deadend) because I don't want it to be forced to be a dead end. I just want to make sure that it's NOT a dead end if there are fewer than four mapOpening left.

You can rewrite

((mapOpenings.Count < 4)) ? !room._flags.IsFlagSet(GlobalValues.MapTileType.deadend) : *ignore this section*)

as

((mapOpenings.Count < 4) || !room._flags.IsFlagSet(GlobalValues.MapTileType.deadend))

The below code both answers the question and refactors your code, which is in desperate need of improvement in the legibility department (as noted by many people in the comments).

if (room._flags.IsFlagSet(GlobalValues.MapTileType.start) {
 return;
}

if (room._flags.IsFlagSet(GlobalValues.MapTileType.exit) {
 return;
}

bool dirFlag = false;
if (_thisRoom.Item2 == 'N') {
 dirFlag = room._north;
}else if (_thisRoom.Item2 == 'S') {
 dirFlag = room._south;
}else if (_thisRoom.Item2 == 'E') {
 dirFlag = room._east;
}else {
 dirFlag = room._west;
}

if (!dirFlag) {
 return;
}

if (mapOpenings.Count < 4 && room._flags.IsFlagSet(GlobalValues.MapTileType.deadend)) {
 return;
}

roomMGI = room;
earlyRooms.Add(room);
goto Exit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM