langton.htm

戻る

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML LANG="ja">

<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=Shift_JIS">
<TITLE>ラングトンの蟻</TITLE>
</HEAD>

<BODY BGCOLOR="#CCFFFF">
<CENTER>
<B>ラングトンの蟻</B>
<BR><BR>

<FORM>
<TABLE><TR>
<TD VALIGN=TOP><DIV ID="back1" STYLE="position:relative; width:481px; height:481px"></DIV></TD>
<TD WIDTH=10></TD>
<TD VALIGN=TOP NOWRAP><BR>
初期パターン<BR>
<INPUT TYPE=RADIO NAME="pat" ID="pat_n" CHECKED onClick="pat_sel_none()"> なし<BR>
<INPUT TYPE=RADIO NAME="pat" ID="pat_1" onClick="pat_sel_1()"> パターン 1<BR>
<INPUT TYPE=RADIO NAME="pat" ID="pat_2" onClick="pat_sel_2()"> パターン 2<BR>
<INPUT TYPE=RADIO NAME="pat" ID="pat_3" onClick="pat_sel_3()"> パターン 3<BR>
<INPUT TYPE=RADIO NAME="pat" ID="pat_4" onClick="pat_sel_4()"> パターン 4<BR>
<INPUT TYPE=RADIO NAME="pat" ID="pat_r" onClick="pat_sel_rnd()"> ランダム<BR>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR>
<TD WIDTH=20></TD>
<TD>
<INPUT TYPE=TEXT ID="rnd" SIZE=4 DISABLED> % (≦ 50%)<BR>
<INPUT TYPE=BUTTON ID="gen" VALUE="生成" DISABLED onClick="gen_random()"><BR>
</TD>
</TR></TABLE>
<BR>
速度<BR>
<INPUT TYPE=RADIO NAME="spd" ID="spd_f" CHECKED> 速く<BR>
<INPUT TYPE=RADIO NAME="spd"> ゆっくり<BR><BR><BR>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR>
<TD><INPUT TYPE=BUTTON ID="start" VALUE="開始" onClick="start_walking()"></TD>
<TD WIDTH=10></TD>
<TD><INPUT TYPE=BUTTON VALUE="リセット" onClick="window.reset()"></TD>
</TR></TABLE>
<BR><BR>
回数: <SPAN ID="cnt">_</SPAN><BR>
<SPAN ID="out" STYLE="position:relative; left:2em; font-weight:bold; color:red; visibility:hidden">場外!</SPAN><BR>
<INPUT TYPE=TEXT ID="skip" SIZE=8 VALUE="10000"> 回まで <INPUT TYPE=BUTTON VALUE="スキップ" onClick="set_skip()">
</TD>
</TR></TABLE>
</FORM>

</CENTER>

<SCRIPT LANGUAGE="JavaScript1.3" TYPE="text/javascript">
<!--

  COLOR0 = "#9999FF";
  COLOR1 = "#FF6666";

// 整数 キー入力チェック
function check_int(e) {
  return (e.which < 0x20 || e.which >= 0x30 && e.which <= 0x39 || e.ctrlKey || e.metaKey);
}

// 小数 キー入力チェック
function check_float(e) {
  return (e.which < 0x20 || e.which >= 0x30 && e.which <= 0x39 || e.which == 0x2e || e.ctrlKey || e.metaKey);
}

function pat_sel_none() {
  elem_rnd.disabled = elem_gen.disabled = true;

  pat_clear();
}

function pat_sel_1() {
  elem_rnd.disabled = elem_gen.disabled = true;

  gen_pat(pat_cond_1);
}

function pat_cond_1(x, y) {
  return (!((x + 3) % 6) && !((y + 3) % 6));
}

function pat_sel_2() {
  elem_rnd.disabled = elem_gen.disabled = true;

  gen_pat(pat_cond_2);
}

function pat_cond_2(x, y) {
  return (x % 6 > 3 || y % 6 > 3);
}

function pat_sel_3() {
  elem_rnd.disabled = elem_gen.disabled = true;

  gen_pat(pat_cond_3);
}

function pat_cond_3(x, y) {
  return (!(((x + 4) % 7 < 4) ^ (y % 7 < 4)));
}

function gen_pat(cond) {
  var i = 0;
  for(var y = 0; y < 120; y++) {
    for(var x = 0; x < 120; x++)
      elem_back2.childNodes[i++].style.backgroundColor = (cond(x, y)) ? color1 : color0;
  }
}

function pat_sel_4() {
  elem_rnd.disabled = elem_gen.disabled = true;

  var i = -1;
  var j = 0;
  for(var y = 0; y < 120; y++) {
    var bit = 0;
    for(var x = 0; x < 120; x++) {
      var data;
      if(!bit) {
        data = Marilyn[++i];
        bit = 0x80000000;
      }
      elem_back2.childNodes[j++].style.backgroundColor = (data & bit) ? color1 : color0;
      bit >>>= 1;
    }
  }
}

function pat_sel_rnd() {
  pat_clear();

  elem_rnd.disabled = elem_gen.disabled = false;
}

function gen_random() {
  var val = elem_rnd.value;
  if(val.search(/^\s*\d*\.?\d*\s*$/) != -1) {
    val = parseFloat(val);
    if(!isNaN(val)) {
      if(val <= 50) {
        val = Math.floor(14400/* 120*120 */ * val / 100 + 0.5);
        pat_clear();
        while(val) {
          with(elem_back2.childNodes[Math.floor(Math.random() * 14400)].style) {
            if(backgroundColor == color0) {
              backgroundColor = color1;
              val--;
            }
          }
        }
        return;
      }
    }
  }
  alert("割合の入力に誤りがあります。");
}

function start_walking() {
  elem_pat_n.disabled = elem_pat_1.disabled = elem_pat_2.disabled
    = elem_pat_3.disabled = elem_pat_4.disabled = elem_pat_r.disabled = true;
  elem_rnd.disabled = elem_gen.disabled = true;

  elem_start.disabled = true;

  walk();
}

function walk() {
  for(; ; ) {
    with(elem_back2.childNodes[ant_y * 120 + ant_x].style) {
      if(backgroundColor == color0) {
        ant_dir = (ant_dir - 1) & 0x3;
        backgroundColor = color1;
      }
      else {
        ant_dir = (ant_dir + 1) & 0x3;
        backgroundColor = color0;
      }
    }
    elem_ant.childNodes[ant_dir & 0x1      ].style.visibility = "visible";
    elem_ant.childNodes[ant_dir & 0x1 ^ 0x1].style.visibility = "hidden";
    var out;
    switch(ant_dir) {
    case 0:
      out = (--ant_y < 0);
      break;
    case 1:
      out = (++ant_x >= 120);
      break;
    case 2:
      out = (++ant_y >= 120);
      break;
    case 3:
      out = (--ant_x < 0);
      break;
    }
    count++;
    if(out)
      break;
    if(count >= skip)
      break;
  };

  elem_ant.style.left = String(ant_x << 2) + "px";
  elem_ant.style.top  = String(ant_y << 2) + "px";
  node_cnt.nodeValue = String(count);

  if(out) {
    elem_out.style.visibility = "visible";
    return;
  }

  to_id = setTimeout(walk, (elem_spd_f.checked) ? 50 : 200);
}

function reset() {
  if(to_id != undefined) {
    clearTimeout(to_id);
    to_id = undefined;
  }

  if     (elem_pat_1.checked) {
    pat_sel_1();
  }
  else if(elem_pat_2.checked) {
    pat_sel_2();
  }
  else if(elem_pat_3.checked) {
    pat_sel_3();
  }
  else if(elem_pat_4.checked) {
    pat_sel_4();
  }
  else {
    pat_clear();
    if(elem_pat_r.checked)
      elem_rnd.disabled = elem_gen.disabled = false;
  }

  elem_ant.childNodes[0].style.visibility = "visible";
  elem_ant.childNodes[1].style.visibility = "hidden";
  elem_ant.style.left = elem_ant.style.top = "240px";
  ant_x = ant_y = 60;
  ant_dir = 0;

  elem_pat_n.disabled = elem_pat_1.disabled = elem_pat_2.disabled
    = elem_pat_3.disabled = elem_pat_4.disabled = elem_pat_r.disabled = false;
  elem_start.disabled = false;

  node_cnt.nodeValue = "";
  elem_out.style.visibility = "hidden";

  count = 0;
  skip = 0;
}

function set_skip() {
  var val = elem_skip.value;
  if(val.search(/^\s*\d+\s*$/) != -1) {
    val = parseInt(val, 10);
    if(!isNaN(val)) {
      skip = val;
      return;
    }
  }
  alert("回数の入力に誤りがあります。");
}

function pat_clear() {
  for(var i = 0; i < 14400/* 120*120 */; i++)
    elem_back2.childNodes[i].style.backgroundColor = color0;
}

  // パターン 4 用ビットマップ
  Marilyn = [
    0x17ffffff, 0xfff80000, 0x01ffffff, 0xffffff00,
    0x1fffffff, 0xffc00000, 0x01ffffff, 0xffffff00,
    0x07ffffff, 0xff800000, 0x00ffffff, 0xffffff00,
    0x03fe0fff, 0xff000000, 0x007fffff, 0xffffff00,
    0x000a07ff, 0xfc000000, 0x407fffff, 0xffffff00,
    0x001083ff, 0xfc000000, 0x601fffff, 0xffffff00,
    0x000080e0, 0x78000000, 0x601fffff, 0xffffff00,
    0x00000141, 0xb0000000, 0x200fffff, 0xffffff00,
    0x00000183, 0xf03e0000, 0x0007ffff, 0xffffff00,
    0x00000003, 0xe0700000, 0x0003ffff, 0xffffff00,
    0x00000203, 0xe3f00000, 0x0002ffff, 0xffffff00,
    0x00000003, 0xc0000008, 0x00007fff, 0xffffff00,
    0x00000007, 0xc000000c, 0x18007fff, 0xffffff00,
    0x00000067, 0xc0000006, 0xb8001fff, 0xffffff00,
    0x0000007f, 0x80000007, 0xf0001fff, 0xffffff00,
    0x0000007f, 0x86000002, 0xf0001fff, 0xffffff00,
    0x000000ff, 0x8ffff800, 0x40000fff, 0xffffff00,
    0x000000ff, 0x9f83fc80, 0x00000fff, 0xffffff00,
    0x000003ff, 0x1f8ffe40, 0x00000fff, 0xffffff00,
    0x000003ff, 0x1c7fffe0, 0x000007ff, 0xffffff00,
    0x000007ff, 0x31ffff80, 0x000003ff, 0xffffff00,
    0x00000fff, 0x33ffff00, 0x000001ff, 0xffffff00,
    0x00000ffe, 0x23fff000, 0x000001ff, 0xffffff00,
    0x00000e7e, 0x03ff4000, 0x000001ff, 0xffffff00,
    0x00000f5e, 0x07fd4400, 0x0070007f, 0xffffff00,
    0x00010f9f, 0x07fd7c00, 0x0018003f, 0xffffff00,
    0x00018fff, 0x03fd3800, 0x0000003f, 0xffffff00,
    0x0001cfff, 0x03c00000, 0x0000001f, 0xffffff00,
    0x0001efff, 0x03800000, 0x000000ff, 0xffffff00,
    0x0000ffff, 0x83000000, 0x0000007f, 0xffffff00,
    0x0000efff, 0xc0000000, 0x0000003f, 0xffffff00,
    0x0000ffff, 0xc0000000, 0x0000001f, 0xffffff00,
    0x0000ffff, 0xe0000000, 0x0000001f, 0xffffff00,
    0x00007fff, 0xf0000000, 0x0000000f, 0xffffff00,
    0x00007fff, 0xf0000000, 0x0000000f, 0xffffff00,
    0x00001fff, 0xf8000000, 0x00000007, 0xffffff00,
    0x00100fff, 0xf8000000, 0x00000001, 0xffffff00,
    0x000003ff, 0xf8000000, 0x00000001, 0xffffff00,
    0x000001ff, 0xf8000000, 0x00000000, 0xffffff00,
    0x000003ff, 0xf8000000, 0x00000000, 0x7fffff00,
    0x00001fff, 0xf8000000, 0x00000000, 0x3fffff00,
    0x0000ffff, 0xf8000000, 0x00000000, 0x1fffff00,
    0x00007fff, 0xf8000000, 0x00000000, 0x1fffff00,
    0x00007fff, 0xf8000000, 0x00ff0000, 0x0fffe400,
    0x0000ffff, 0xfc000000, 0x08000000, 0x03e6c000,
    0x0001ffff, 0xfc000000, 0x00000000, 0x00000100,
    0x000bffff, 0xfff00000, 0x00000010, 0x00800000,
    0x000bffff, 0xffff8000, 0x00000010, 0x00fe0000,
    0x000fffff, 0xffff8000, 0x00000018, 0x00740000,
    0x00077fff, 0xffff8000, 0x00000038, 0x00000000,
    0x001c7fff, 0xfc03c000, 0x00000018, 0x00000000,
    0x001fffff, 0xf803f000, 0x00000010, 0x00000000,
    0x001fffff, 0xf000fe00, 0x00000000, 0x00780000,
    0x001fffff, 0xf0007e00, 0x00000000, 0x000c0000,
    0x001fffff, 0xf8003f00, 0x07f80000, 0x000c0000,
    0x001fffff, 0xf8001f00, 0x0fffc000, 0x000c0000,
    0x001fffff, 0xff000f80, 0x3dfff000, 0x00040000,
    0x001fffff, 0xff020f80, 0x1dffe000, 0x00000000,
    0x001fffff, 0xffffef80, 0x000f0000, 0x00000000,
    0x000fffff, 0xfffc6f80, 0x00000000, 0x00000000,
    0x000fffff, 0xfffc0f00, 0x00000000, 0x3c000000,
    0x000fffff, 0xfff80f00, 0x00000000, 0x7f800000,
    0x0007ffff, 0xffc00f00, 0x00000003, 0x7f800000,
    0x00013fff, 0xffc00f00, 0x00000007, 0x3e000000,
    0x000017ff, 0xfe001f00, 0x00000007, 0x20000000,
    0x000187ff, 0xf8001f00, 0x00000003, 0x60000000,
    0x00039fff, 0xf0001f80, 0x00000002, 0x60000000,
    0x0003ffff, 0xf0003fc3, 0x00000002, 0x20000000,
    0x0003bfff, 0xe0003fe7, 0x80000002, 0x00000000,
    0x0003bfff, 0xe0003ff1, 0xc0000002, 0x00000000,
    0x0003dfff, 0xe0003ff0, 0x00000002, 0x00000000,
    0x000e0fff, 0xe0003fe0, 0x00000002, 0x00000000,
    0x000c01ff, 0xe0003f80, 0x00000002, 0x00001000,
    0x001f00ff, 0xe0001f00, 0x00000000, 0x00003000,
    0x001fc07f, 0xe0000000, 0x00000000, 0x00003000,
    0x001cf03f, 0xf0000000, 0x00000001, 0x00007000,
    0x003cffff, 0xf0000040, 0x00000001, 0x10787000,
    0x00007fff, 0xf00003f0, 0x08000003, 0x13fce000,
    0x0000ffff, 0xf80007dc, 0x1c000001, 0x1ffc0000,
    0x0000ffff, 0xfc000fff, 0xfe000000, 0x0ffc0000,
    0x00007fff, 0xfc001ffc, 0xff000000, 0x0ffc2000,
    0x00000fff, 0xfe003fc0, 0x3f800000, 0x0ffce000,
    0x00000fff, 0xfe003f80, 0x1f800000, 0x27fde000,
    0x00000fff, 0xff003f80, 0x1f000000, 0x07ffe000,
    0x00001fff, 0xff803fe4, 0x6f000010, 0x0ffbe000,
    0x00007fff, 0xff801fe0, 0x3e00003c, 0x0ff2e000,
    0x00007bff, 0xffc00fe0, 0xf800003e, 0x43c6c000,
    0x0000fbff, 0xffc00fff, 0x0000003f, 0xc7840000,
    0x0000ffff, 0xffe00000, 0x0000003f, 0xfc1e0000,
    0x0001ffff, 0xffe00000, 0x0000007f, 0xfffc0000,
    0x0003ffff, 0xfff00000, 0x0000007f, 0xfdfc0000,
    0x0007ffff, 0xfff80000, 0x0000017f, 0xf8fc0000,
    0x0007efff, 0xfffc0000, 0x0000017f, 0x70fc0000,
    0x0007e7ff, 0xfffc0000, 0x0000037f, 0xc07c0000,
    0x0007ffff, 0xfffe0000, 0x0000067f, 0xc0f80000,
    0x0007ffff, 0xffff0000, 0x00001c7d, 0xd0f00000,
    0x0007ffff, 0xffff8000, 0x0000387f, 0xf0600000,
    0x0007ffff, 0xffff8000, 0x0000f87f, 0xfa600000,
    0x0000ffff, 0xffffc000, 0x0001f07f, 0xe0400000,
    0x00003fff, 0xffffe000, 0x000ff07f, 0xfe000000,
    0x00003fff, 0xfffff800, 0x003fe07f, 0xfe000000,
    0x0001ffff, 0xfffffe00, 0x00ffc07f, 0xfe000000,
    0x0003ffff, 0xfffffffb, 0xffff807f, 0xffc00000,
    0x0003ffff, 0xffffffff, 0xffff007f, 0xffd00000,
    0x0007ffff, 0xffffffff, 0xfffe007f, 0xff700000,
    0x000fffff, 0xffffffff, 0xfffc00ff, 0xffe00000,
    0x000fffff, 0xffffffff, 0xfff000ff, 0xffc00000,
    0x0007ffff, 0xffffffff, 0xffe000ff, 0xffc00000,
    0x0003ffff, 0xffffffff, 0xff8000ff, 0xffc00000,
    0x0000ffff, 0xffffffff, 0xfe0000ff, 0xff800000,
    0x00007fff, 0xffffffff, 0xfc0001ff, 0xfe000000,
    0x00003fff, 0xffffffff, 0xf80001ff, 0xfc000000,
    0x00000fff, 0xffffffff, 0xf00001f3, 0xfc000000,
    0x00001fff, 0xffffffff, 0xe00001ff, 0xf8000000,
    0x00001f7f, 0xffffffff, 0xc00001ff, 0xf0000000,
    0x00000f7f, 0xffffffff, 0xc00001ff, 0xe0000000,
    0x00000fff, 0xffffffff, 0x800001ff, 0xc0000000,
    0x00000fff, 0xffffffff, 0x000001ff, 0x80000000,
    0x00000fff, 0xfffffffe, 0x000001ff, 0xc0000000,
    0x000007ff, 0xfffffffe, 0x000001fe, 0x00000000
  ];

  with((elem_back2 = document.createElement("DIV")).style) {
    position = "relative";
    width = height = "481px";
  }

  with(document.createElement("SPAN").style) {
    backgroundColor = COLOR0;
    color0 = backgroundColor;
    backgroundColor = COLOR1;
    color1 = backgroundColor;
  }

  // マトリックス
  for(y = 0; y < 120; y++) {
    style_y = String(y << 2) + "px";
    for(x_ = 0; x_ < 120; x_++) {
      with((elem = document.createElement("SPAN")).style) {
        position = "absolute";
        left = String(x_ << 2) + "px";
        top = style_y;
        width = height = "4px";
        backgroundColor = color0;
      }
      elem_back2.appendChild(elem);
    }
  }
  with((elem = document.createElement("SPAN")).style) {
    position = "absolute";
    left = top = "0";
    width = height = "481px";
    backgroundImage = "url(images/cell.gif)";
  }
  elem_back2.appendChild(elem);

  // 蟻
  with((elem_ant = document.createElement("SPAN")).style) {
    position = "absolute";
    left = top = "240px";
    width = height = "5px";
  }
  with(elem = document.createElement("IMG")) {
    width = height = 5;
    src = "images/ant0.gif";
    style.position = "absolute";
    style.left = style.top = "0";
    style.visibility = "visible";
  }
  elem_ant.appendChild(elem);
  with(elem = elem.cloneNode(false)) {
    src = "images/ant1.gif";
    style.visibility = "hidden";
  }
  elem_ant.appendChild(elem);
  elem_back2.appendChild(elem_ant);
  ant_x = ant_y = 60;
  ant_dir = 0;

  document.getElementById("back1").appendChild(elem_back2);

  elem_pat_n = document.getElementById("pat_n");
  elem_pat_1 = document.getElementById("pat_1");
  elem_pat_2 = document.getElementById("pat_2");
  elem_pat_3 = document.getElementById("pat_3");
  elem_pat_4 = document.getElementById("pat_4");
  elem_pat_r = document.getElementById("pat_r");
  elem_rnd = document.getElementById("rnd");
  elem_gen = document.getElementById("gen");
  elem_spd_f = document.getElementById("spd_f");
  elem_start = document.getElementById("start");
  elem_out = document.getElementById("out");
  elem_skip = document.getElementById("skip");

  (node_cnt = document.getElementById("cnt").firstChild).nodeValue = "";

  elem_rnd.onkeypress = check_float;
  elem_skip.onkeypress = check_int;

  count = 0;
  skip = 0;
  to_id = undefined;

  // ページを再ロードしたときのため
  document.forms[0].reset();
  elem_pat_n.disabled = elem_pat_1.disabled = elem_pat_2.disabled
    = elem_pat_3.disabled = elem_pat_4.disabled = elem_pat_r.disabled = false;
  elem_rnd.disabled = elem_gen.disabled = true;
  elem_start.disabled = false;

//-->
</SCRIPT>

</BODY>

</HTML>