JavaScript
var DIRECCION={arriba:0, derecha:1, abajo:2, izquierda:3 };
function TRON(canvas){
this.canvas=canvas;
this.ctx=this.canvas.getContext("2d");
this.ladoCelda=10;
this.colores=["white","black","red"]
this.mapa=[
[1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1]
];
this.timer=10;
var y = 2;
var x = 4;
var y = Math.floor(Math.random()*this.mapa.length)+1;
var x = Math.floor(Math.random()*this.mapa[0].length)+1;
y = (y==this.mapa.length ? y-2 : y);
x = (x==this.mapa[0].length ? x-2 : x);
this.jugador = [y,x];
document.getElementById('coordenadas').innerHTML = 'Coordenadas: x: '+x+' | y:'+y;
this.muerto=false;
this.puntos=0;
this.canvas.width=this.mapa[0].length*this.ladoCelda;
this.canvas.height=this.mapa.length*this.ladoCelda;
this.mapa[this.jugador[0]][this.jugador[1]]=2;
this.run();
}
TRON.prototype.dibujar=function()
{
var ny=this.mapa.length;
var nx=this.mapa[0].length;
for (var x=0;x<nx;x++)
{
for (var y=0;y<ny;y++)
{
this.ctx.fillStyle=this.colores[this.mapa[y][x]];
this.ctx.strokeStyle="#333";
this.ctx.lineWidth=0.2;
this.ctx.fillRect(x*this.ladoCelda,y*this.ladoCelda,this.ladoCelda,this.ladoCelda);
this.ctx.strokeRect(x*this.ladoCelda,y*this.ladoCelda,this.ladoCelda,this.ladoCelda);
}
}
};
TRON.prototype.run=function()
{
var self=this;
var direccion=this.ia();
this.mover(direccion);
this.mapa[this.jugador[0]][this.jugador[1]]=2;
this.dibujar();
if (!this.muerto) setTimeout(function(){ self.run(); },self.timer);
document.getElementById('puntos').innerHTML = 'Puntos:...