Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The challenge is twofold:

Make a program that builds a door. ASCII, HTML or otherwise

Make the door functional. Opens and closes

Either open-able through input or interaction!

  • Non-functional door +5 points.
  • Just a open-able door +10 points.
  • Interactive door +15 points.
  • Fancy door +20 points. This means revolving,bifold etc
  • Animated +20 points.
  • <100 characters +50 points.
  • -100 points for using a program specifically designed for drawing or animation.

If you have criteria suggestions leave them in the comments.

Non-functional open door example:

<?php
$idiots_in_room=true;

if($idiots_in_room)
{

$count=20;
$count2=7;
for($i=0;$i<$count;$i++)
{

if($i==0)
{
echo str_repeat("-",10);
if($i==0){echo ".";}
echo "\n";
}
elseif($i==9)
{
echo str_repeat("-",10);
echo str_repeat(" ",7)."o"."|";
echo "\n";
}
elseif($i<=9)
{

echo str_repeat("-",1).str_repeat(" ",8).str_repeat("-",1);

echo ($i<5) ? str_repeat(" ",$i*2)."\\" : str_repeat(" ",8)."|";
echo "\n";
}
elseif($i<=14)
{
if($i>9){echo str_repeat(" ",$i)."\\";}
echo str_repeat(" ",$count2--)."|";
echo "\n";
}

}
}

Example Output:

----------.
-        -  \
-        -    \
-        -      \
-        -        \
-        -        |
-        -        |
-        -        |
-        -        |
----------       o|
          \       |
           \      |
            \     |
             \    |
              \   |
share|improve this question
Do you have any door examples? – beary605 Jul 13 '12 at 18:24
@beary605 Non-functional example provided – Event_Horizon Jul 13 '12 at 18:30
1  
define a "door" – Joel Cornett Jul 13 '12 at 21:06

5 Answers

JavaScript, 4380 characters, 65(?) points

ASCII? Check. HTML? Check. Is a door? Check. Openable door? Check. Interactive? Check. Fancy? Double doors with properly positioned hinges, I hope that counts. Animated? Check. Under 100 characters? Ha. Not using facilities intended for drawing? Check.

Live demo. (Note: In my testing with Firefox, clicking the doors more than once doesn't work — for some reason the event handler doesn't fire again and I'm baffled as to why; pointing out what I did wrong would be welcome. Though, you might want to run this in Chrome anyway for decent JS performance.)

<title>Door</title>
<pre onmouseup="turn();" style="display: table; margin: auto; font-family: 'Monaco', monospace; font-size: 0.6em; line-height: 0.7em;">
</pre>
<p>Click doors to open or close.</p>
<script>

  // Appearance of hit surface - global used to avoid allocating a record to return
  var mat;

  // Scene construction tools
  function box(size,ms) {
    return function (x, y, z) {
      var vdist0 = Math.abs(x) - size[0];
      var vdist1 = Math.abs(y) - size[1];
      var vdist2 = Math.abs(z) - size[2];
      mat = vdist0 > vdist1 && vdist0 > vdist2 ? ms[0] :
            vdist1 > vdist0 && vdist1 > vdist2 ? ms[1] :
            ms[2];
      return Math.max(vdist0, vdist1, vdist2);
    };
  }
  function translate(vec, obj) {
    var dx = vec[0];
    var dy = vec[1];
    var dz = vec[2];
    return function (x, y, z) { return obj(x - dx, y - dy, z - dz); };
  }
  function mirror(obj) {
    return function (x, y, z) { return obj(-x, y, z); };
  }
  function spin(obj) {
    return function (x, y, z) {
      var a = Date.now() / 1000;
      var s = Math.sin(a);
      var c = Math.cos(a);
      return obj(
        x * c + z * s,
        y,
        x * -s + z * c
      );
    };
  }
  function doorturn(obj) {
    return function (x, y, z) {
      var a = pos;
      var s = Math.sin(a);
      var c = Math.cos(a);
      return obj(
        x * c + z * s,
        y,
        x * -s + z * c
      );
    };
  }
  function rotx(a, obj) {
    return function (x, y, z) {
      var s = Math.sin(a);
      var c = Math.cos(a);
      return obj(
        x,
        y * c + z * s,
        y * -s + z * c
      );
    };
  }
  function roty(a, obj) {
    return function (x, y, z) {
      var s = Math.sin(a);
      var c = Math.cos(a);
      return obj(
        x * c + z * s,
        y,
        x * -s + z * c
      );
    };
  }
  function union(as, bs) {
    return function (x, y, z) {
      var a = as(x, y, z); var am = mat;
      var b = bs(x, y, z);
      if (a < b) {
        mat = am;
        return a;
      } else {
        return b;
      }
    };
  }

  // Display parameters
  var vw = 80, vh = 80;
  var timestep = 1/30;

  // Scene
  var wallhwidth = 30;
  var wallhheight = 35;
  var wallmat = [";", "\u2014", ":"];
  var dhwidth = 10;
  var dhheight = 20;
  var hthick = 2;
  var door = translate([-dhwidth*2, 0, 0], doorturn(translate([hthick, 0, dhwidth], box([hthick, dhheight, dhwidth], [".", "\u2014", "|"]))));
  var doors = union(door, mirror(door));
  var wall = union(
    union(
      translate([dhwidth*2+wallhwidth, 0, -hthick], box([wallhwidth, wallhheight, hthick], wallmat)),
      translate([-dhwidth*2-wallhwidth, 0, -hthick], box([wallhwidth, wallhheight, hthick], wallmat))),
    translate([0, wallhheight-(wallhheight-dhheight)/2, -hthick], box([dhwidth*2, (wallhheight-dhheight)/2, hthick], wallmat)));
  var floor = translate([0, -dhheight - 1.1, 0], box([100, 1, 100], ["/","/","/"]));
  var sill = translate([0, -dhheight - 1, -hthick], box([dhwidth*2, 1, hthick], ["\\","%","\\"]));
  var sbox = translate([0, 0, -12], spin(box([8, 8, 8], ["x", "y", "z"])))
  var scene = union(sbox, union(union(wall, doors), union(floor, sill)));
  var view = translate([vw/2, vh/2, -100], rotx(0.2, roty(-0.6, scene)));

  // Animation state
  var pos = -Math.PI/2;
  var dpos = 0;
  var interval;

  // Main loop function
  function r() {
    // Update state
    pos += dpos * timestep;
    if (Math.abs(pos) >= Math.PI/2) {
      dpos = 0;
      pos = Math.PI/2 * pos / Math.abs(pos);
      if (pos < 0) { // no animation needed
        clearInterval(interval); interval = undefined;
      }
    }

    // Render scene
    var t = [];
    for (var y = vh - 1; y >= 0; y--) {
      for (var x = 0; x < vw; x++) {
        var z = 0, distance;
        while ((distance = view(x,y,z)) > 0.12) {
          z -= distance;
          if (!isFinite(z) || z < -1000) {
            mat = " ";
            break;
          }
        }
        t.push(mat);
      }
      t.push("\n");
    }
    document.getElementsByTagName("pre")[0].textContent = t.join("");
  }

  // Click handler
  function turn() {
    if (dpos !== 0) {
      dpos *= -1;
    } else {
      dpos = (pos < 0 ? 1 : -1) * 2.3;
    }
    if (!interval) {
      interval = setInterval(r, timestep*1000);
    }
  }

  // Render initial state
  r();
</script>

When closed, the doors look like this:

(Screenshot of closed doors.)

share|improve this answer
Gotta admit, thats some beautiful work there. – Event_Horizon Jul 16 '12 at 13:18
That is awesome. – MrZander Jul 17 '12 at 21:34

HTML & CSS3, 55 points

Fancy, interactive, animated door is 55 points, I think.

Yes, this opens like any other door, but if a sliding door counts as fancy, why doesn't a rotating one? If a rotating one isn't fancy, well, a sliding door is no problem :)

A demo is available at http://result.dabblet.com/gist/3132160/ac475112dbba493d2dd7d98493d4f4ceaa209a7c. Click the doorknob to open and close. No JavaScript involved; it's just the magic of CSS3.

#wall {
    background-color: #eee;
    bottom: 0;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    transform: rotateX(-10deg);
    transform-origin: 0 100%;
    transform-style: preserve-3d;
}

#door-container {
    background-color: black;
    height: 100%;
    margin: 0 auto;
    width: 300px;
}

#door {
    background-color: brown;
    height: 100%;
    margin: auto;
    position: relative;
    transform-origin: 0 0;
    transition: transform 0.5s ease;
    width: 300px;
}

#door .knob {
    background-color: gold;
    border-radius: 10px;
    height: 20px;
    margin-top: -10px;
    position: absolute;
    right: 10px;
    top: 50%;
    width: 20px;
}

#open:target + #wall #door {
    transform: rotateY(-145deg);
}

#open:target + #wall #open-link {
    display: none;
}

#close-link {
    display: none;
}

#open:target + #wall #close-link {
    display: inline;
}
<span id="open"></span>
<div id="wall">
    <div id="door-container">
        <div id="door">
            <a href="#open" id="open-link" class="knob"></a>
            <a href="#closed" id="close-link" class="knob"></a>
        </div>
    </div>
</div>
share|improve this answer
By Sliding I originally meant "Sliding glass door" like for a patio, but I could see how it wouldn't be considered fancy (especially in coding terms, as its much easier than a rotating). Also, by Rotating door I meant Revolving. Will correct. – Event_Horizon Jul 18 '12 at 13:32

Mathematica 271 chars

Manipulate[a = {0, 0, 0}; b = {0, 0, h}; p = Polygon; c = Cuboid; t = Rotate;Graphics3D[{c@{{-w - 1, 0, 0}, {-w, 1, h}}, c@{{w + 1, 0, 0}, {w, 1, h}},t[p@{a, b, {-w, 0, h}, {-w, 0, 0}}, r, {0, 0, 1}, {- 2 w/3, -w/3, 0}], t[p@{a, b, {w, 0, h}, {w, 0, 0}}, -r, {0, 0, 1}, { 2 w/3, -w/3, 0}]}],{{r, 0}, 0, 3/2}, {{w, 2}, 1, 3}, {{h, 4}, 3, 5}]

doors

The double doors

  • open by rotation from zero to 90 degrees (using the slider r)
  • can be have their height and width set by sliders (h and w).
  • are in a 3D lighting environment
  • can be interactively rotated to be viewed from different angles.

The code is based on a program by Sándor Kabal.

share|improve this answer

Python - 65 points, 86 chars

Interactive and less than 100 chars.

Waits for input and shows you the door. Valid input is "open" and "close" and "bye".

g,s=1,'open close'
while g:
 i=raw_input()
 print '_'+'/_ '[s.find(i)/5]+'_'
 g=i in s
share|improve this answer
You could probably set it to toggle open/close without typing commands and still meet the requirements--it would save you a few chars. – Joel Cornett Jul 14 '12 at 22:14
2  
Probably, but then again, this isn't code-golf, so it doesn't really matter ;) – Daniero Jul 15 '12 at 22:31
1  
pretty boring looking door, but a great looking mousetrap – ardnew Jul 18 '12 at 18:27

Mathematica 127 chars

This is a more streamlined implementation than the one I submitted earlier. It has a single door. The single door

  • opens by rotation from zero to 90 degrees (using the slider o)
  • is in a 3D lighting environment
  • can be interactively rotated to be viewed from different angles.

However, it uses a fixed door height and width.

Manipulate[a = {0, 0, 0}; Graphics3D[{Tube[{a, {1, 0, 0}, {1, 0, 2}, {0, 0, 2}, a}, .03],Rotate[Cuboid@{a, {1, -.1, 2}}, o, {0, 0, 1}, a]}], {o, 0, -Pi/2}]

door2

share|improve this answer
You should probably have editted your previous submission, rather than submitting a new one. – Joe the Person Jul 15 '12 at 17:19
@fireDude67 If this had been a Code Golf challenge, I would have simply replaced my earlier entry with the shorter code. However, the SO indicated interest in both short programs and more complicated programs (doors with more features). – David Carraher Jul 16 '12 at 16:30
oh sorry i got confused then – Joe the Person Jul 16 '12 at 17:17
@fireDude67 No problem. – David Carraher Jul 16 '12 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.