# A little darkness puzzle.

I have the classic lamp in dark room situation.

Sometimes the dark room is illuminated by one of a number of different lights in addition to the lamp. It seemed easier to check for darkness when the lamp is temporarily off.

I postulate that the lamp battery cannot be changed when the room is not otherwise illuminated. So I check whether the room is otherwise illuminated by switching the lamp off and checking for darkness, then turning it on again but even if only the lamp was on, the darkness test seems to fail. I.e. z is always 0. Is there a timing thing here? Does the ‘if in darkness’ test only work on the conditions at the start of the turn?

```Instead of tying a battery to the lamp: let z be 0; if the lamp is lit: now the lamp is not lit; if in darkness: now z is 1; now the lamp is lit; say "z=[z]."; if the lamp is not lit or z is 0: ...then carry on and xhange the battery```

I can check whether the room is currently otherwise illuminated, but it’s a longer-winded process, and it’d be nice to use this method if possible.

Yes, darkness is evaluated once per turn. If you look at the turn sequence rulebook in the index, you’ll see “adjust light rule” listed.

If the lamp is the only mobile light source, you can just check whether the location is lighted or dark. Otherwise you’ll need to get into some low-level fiddling.

Altered it very slightly:

```Instead of tying a battery to the lamp: let z be 0; if the lamp is lit: now the lamp is not lit; >> if the location is not lighted: now z is 1; now the lamp is lit; say "z=[z]."; if the lamp is not lit or z is 0: ...make the battery exchange..... ```

Now z becomes 1 regardless of whether the room is otherwise illuminated. Go figure…

I think I’d better do a methodical assessment of the lighted condition of the room by checking all possible alternate light sources, rather than this trick of switching off the lamp and looking around. The thing is that while the lamp is lit, the location is illuminated, and switching it off in mid ‘Instead’ rule is probably not going to work.

Stepping back for a second, have you considered:

``````Instead of tying a battery to the lamp when the lamp is lit:
instead say "You'll have to turn off the lamp first."``````

Hey, vlaviano, that’s a cute one. Wish I’d thought of that before I implemented a rather fat little ‘to decide whether too-dark’ subroutine which cycles through all possible sources of light in the location and reports back. I can’t bear to backtrack now!

I never thought of using “tying” (which includes “attaching”) a battery to a light source. I’ve always created a container and made it part of the light source and only allowed it to light up when something is in the “battery compartment” (which rejects insertion of anything except a battery). I suppose with tying you just make the battery “Part of” the light source and check similarly. Neat shortcut. I think the more instinctive thing is to “insert” a battery into most devices…so you could “check inserting a battery…try attaching the battery instead…”

In fact, I don’t even make the battery part of the lamp. I give the lamp a life attribute, and the battery a life attribute. When I ‘attach’ the battery, what I actually do behind the scenes is save the remaining life of the lamp in a variable x, confer the life of the battery to the lamp, then give the value of x to the life of the battery, and drop the battery. So, the battery is never actually in or part of the lamp. It also means that there are always one or more discarded batteries with some life left in them, which may be handy in extremis.

However, I shall ensure that ‘insert’ a battery in a lamp is equivalent to attaching it. Thanks for the pointer.

And, Vince, I found I could bear it. I reverted to your solution!

I can’t believe I wrote a game with rechargeable batteries. Never again!