mirror of
https://github.com/Aviortheking/Langton-s-Ant.git
synced 2025-04-22 18:42:09 +00:00
correction & tweaks
This commit is contained in:
parent
79f580ba9d
commit
eb410782c0
25
index.html
25
index.html
@ -14,14 +14,19 @@
|
|||||||
.black {
|
.black {
|
||||||
background: black;
|
background: black;
|
||||||
}
|
}
|
||||||
|
.hidden {
|
||||||
|
visibility: hidden;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="prgm">
|
<div class="prgm">
|
||||||
|
<input value="250" class="geninput"/>
|
||||||
|
<button onclick="gen()">Generate</button>
|
||||||
</div>
|
</div>
|
||||||
<button onclick="moving()" style="position: fixed;top: 0;">Move</button>
|
<button class="hidden" onclick="moving()" style="position: fixed;top: 0;">Move</button>
|
||||||
<input style="position: fixed;bottom: 0;" class="moves"/>
|
<input style="position: fixed;bottom: 0;" class="moves hidden"/>
|
||||||
</body>
|
</body>
|
||||||
<script type="text/javascript" src="prgm.js"></script>
|
<script type="text/javascript" src="prgm.js"></script>
|
||||||
<script>
|
<script>
|
||||||
@ -30,11 +35,15 @@
|
|||||||
Prgm.Ant.move();
|
Prgm.Ant.move();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window.onload = function() {
|
|
||||||
stop = false;
|
function gen() {
|
||||||
Prgm.setElement("test");
|
Prgm.setElement("AntProgram");
|
||||||
Prgm.init(document.getElementsByClassName("prgm")[0], 250);
|
document.getElementsByClassName("hidden")[0].classList.remove("hidden")
|
||||||
Prgm.Ant.setAt(50, 200);
|
document.getElementsByClassName("hidden")[0].classList.remove("hidden")
|
||||||
};
|
square = document.getElementsByClassName("geninput")[0].value;
|
||||||
|
Prgm.init(document.getElementsByClassName("prgm")[0], square);
|
||||||
|
Prgm.Ant.setAt(Math.trunc(square/2), Math.trunc(square/2));
|
||||||
|
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</html>
|
</html>
|
50
prgm.js
50
prgm.js
@ -5,61 +5,55 @@ var Prgm = {
|
|||||||
this.Ant.element= elementName;
|
this.Ant.element= elementName;
|
||||||
},
|
},
|
||||||
init: function(element, width) {
|
init: function(element, width) {
|
||||||
width++;
|
|
||||||
table = "<table id="+this.element+">";
|
table = "<table id="+this.element+">";
|
||||||
for (t = 0; t < width; t++) {
|
for (t = 0; t < width; t++) {
|
||||||
table += '<tr line="'+t+'">';
|
table += "<tr>";
|
||||||
for (i = 0; i < width; i++) {
|
for (i = 0; i < width; i++) {
|
||||||
table += '<td col="'+i+'"></td>';
|
table += '<td col="'+t+'" line="'+i+'"></td>';
|
||||||
}
|
}
|
||||||
table += "</tr>";
|
table += "</tr>";
|
||||||
}
|
}
|
||||||
element.innerHTML += table;
|
element.innerHTML = table;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
Ant: {
|
Ant: {
|
||||||
element: "table",
|
element: "table",
|
||||||
prepos: [0,0],
|
prepos: [0,0],
|
||||||
pos: [0,0],
|
pos: [0,0],
|
||||||
setAt: function(line, col) {
|
setAt: function(line, col) {
|
||||||
//console.log("Entering : Ant.setAt");
|
|
||||||
col--;
|
col--;
|
||||||
line--;
|
line--;
|
||||||
document.getElementById(this.element).childNodes[0].childNodes[line].childNodes[col].classList.add("ant");
|
//window.alert(col + ", " + line + ", " + AntElement);
|
||||||
this.pos = [line, col];
|
document.getElementById(this.element).childNodes[0].childNodes[col].childNodes[line].classList.add("ant");
|
||||||
this.prepos = [line+1, col];
|
pos = document.getElementsByClassName("ant")[0];
|
||||||
|
this.pos = [pos.getAttribute("line"), pos.getAttribute("col")];
|
||||||
|
this.prepos = this.pos;
|
||||||
|
this.prepos[0] = this.pos[0] - 1;
|
||||||
},
|
},
|
||||||
move: function() {
|
move: function() {
|
||||||
//console.log("Entering : Ant.move");
|
window.alert(parseInt(this.prepos[0])+parseInt(this.pos[0])+", "+(parseInt(this.prepos[1])+parseInt(this.pos[1])));
|
||||||
//console.log("prepos : " + this.prepos + "\npos : " + this.pos);
|
|
||||||
//console.log("Diff : "+(parseInt(this.prepos[0])-parseInt(this.pos[0]))+", "+(parseInt(this.prepos[1])-parseInt(this.pos[1])));
|
|
||||||
if(document.getElementsByClassName("ant")[0].classList.contains("black")) {
|
|
||||||
document.getElementsByClassName("ant")[0].classList.remove("black");
|
|
||||||
//console.log("Black tile !\nSending : " + (parseInt(this.prepos[1])-parseInt(this.pos[1]))+", "+ (-(parseInt(this.prepos[0])-parseInt(this.pos[0]))));
|
|
||||||
this.a(parseInt(this.prepos[1])-parseInt(this.pos[1]), -(parseInt(this.prepos[0])-parseInt(this.pos[0])));
|
|
||||||
|
|
||||||
|
if(pos.classList.contains("black")) {
|
||||||
|
//window.alert(this.prepos[0]+this.pos[0]+", "+this.prepos[1]+this.pos[1]);
|
||||||
|
//window.alert(this.prepos);
|
||||||
|
this.a();
|
||||||
} else {
|
} else {
|
||||||
document.getElementsByClassName("ant")[0].classList.add("black");
|
//window.alert(parseInt(this.prepos[0])+parseInt(this.pos[0])+", "+(parseInt(this.prepos[1])+parseInt(this.pos[1])));
|
||||||
//console.log("White tile !\nSending : " + (-(parseInt(this.prepos[1])-parseInt(this.pos[1])))+", "+ (parseInt(this.prepos[0])-parseInt(this.pos[0])));
|
//window.alert(this.prepos);
|
||||||
this.a(-(parseInt(this.prepos[1])-parseInt(this.pos[1])), parseInt(this.prepos[0])-parseInt(this.pos[0]));
|
|
||||||
}
|
}
|
||||||
|
//window.alert(this.pos);
|
||||||
},
|
},
|
||||||
a: function(line, col) {
|
a: function(line, col) {
|
||||||
//console.log("Entering : Ant.a");
|
//window.alert(line+", "+col);
|
||||||
document.getElementsByClassName("ant")[0].classList.remove("ant");
|
//window.alert((line+parseInt(this.pos[0]))+", "+(col+parseInt(this.pos[1])));
|
||||||
//console.log("Setting Ant Position to :\nLine : " + (line+parseInt(this.pos[0])) + "\nColumn : " + (col+parseInt(this.pos[1])) );
|
pos = document.getElementsByClassName("ant")[0].classList.remove("ant");
|
||||||
document
|
document
|
||||||
.getElementById(this.element)
|
.getElementById(this.element)
|
||||||
.childNodes[0]
|
.childNodes[0]
|
||||||
.childNodes[line+parseInt(this.pos[0])]
|
.childNodes[line-1+parseInt(this.prepos[0])]
|
||||||
.childNodes[col+parseInt(this.pos[1])]
|
.childNodes[col-1+parseInt(this.prepos[1])]
|
||||||
.classList
|
.classList
|
||||||
.add("ant");
|
.add("ant");
|
||||||
this.prepos = this.pos;
|
this.prepos = this.pos;
|
||||||
this.pos = [line+parseInt(this.prepos[0]), col+parseInt(this.prepos[1])];
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user