JavaScript
var DIRECCION={arriba:0, derecha:1, abajo:2, izquierda:3 };
function TRON(canvas){
this.timer=100;
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.jugador=[2,2]; //Y, X
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);
else alert("¡MUERTO! Puntos: "+this.puntos);
};
TRON.prototype.mover=function(direccion)
{
this.puntos++;
switch(direccion)
{
case DIRECCION.arriba:
this.jugador[0]--;
break;
case DIRECCION.derecha:
this.jugador[1]++;
break;
case DIRECCION.abajo:
...