Code viewer for World: Pegboard
const	 	CLOCKTICK 	= 100;	
const       turns = 4;
const		MAXSTEPS 	= 3 * turns - 2; //maximum number of moves needed to solve problem

const  SCREENSHOT_STEP = 1;    


const yposition = -1;
const skyLW = turns * 800;
const skyH = turns * 450;

const SKYCOLOR 	= 0xffffcc;			
const startRadiusConst	 	= skyH * 0.8;
const maxRadiusConst 		= skyH * 3 ;

const sphereR = 100;
const sphereWH = 300;
const boxL = turns * 400 + 400;
const boxWH = 200;
const spaceofspherecentre = 220;
var array = [];
var array2 = [];
var occupied = [];
var self = this;
var arr = array;
var sphere = arr[0];
var one = 1;
var two = 2;
//var theta = 0;
//var thetaa = 0;
var show3d = true;
var ypos = 0;
var step = 0;


var check = 0;

var materialArray = [
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/land.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/land.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/sky.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/sea.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/landl.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/landl.jpg" ), side: THREE.BackSide } ) )
 	];
 
var cover = new THREE.MeshBasicMaterial ({ map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/ball1.jpg" )});
var cover2 = new THREE.MeshBasicMaterial({ map: THREE.ImageUtils.loadTexture( "/uploads/ahmedr2/ball2.jpg" )});
var shape = new THREE.SphereGeometry( sphereR, sphereWH, sphereWH );

function swap (a)
{
    if(a==array)
        return array2;
    else if(a==array2)
        return array;
}

function World() { 
var self = this;

function initWalls()
{
        var skyGeometry = new THREE.CubeGeometry ( skyLW, skyH, skyLW );	
        var skyMaterial = new THREE.MeshFaceMaterial ( materialArray );
        var theskybox = new THREE.Mesh ( skyGeometry, skyMaterial );
        threeworld.scene.add( theskybox );
        
        var geometry = new THREE.BoxGeometry( boxWH, boxWH, boxL);
        var material = new THREE.MeshFaceMaterial( materialArray );
        var cube = new THREE.Mesh( geometry, material );
        threeworld.scene.add( cube );
          cube.rotation.y = THREE.Math.degToRad(90);
          ypos = cube.position.y;
}
function initSpheres ()	 
{
        occupied.push(0);
        for(var i = 0; i < turns; i++)
        {
            sphere = new THREE.Mesh( shape, cover);
            sphere.position.x = -spaceofspherecentre * (i+1);
            sphere.position.y = ypos + boxWH;
            array.push(sphere);
            occupied.push(1);
        }
        
        for(i = 0; i < turns; i++)
        {
            sphere = array[i];
            threeworld.scene.add(sphere);
        }
        
        for(i = 0; i < turns; i++)
        {
            sphere = new THREE.Mesh( shape, cover2);
            sphere.position.x = spaceofspherecentre * (i + 1);
            sphere.rotation.y = THREE.Math.degToRad(180);
            sphere.position.y = ypos + boxWH;
            array2.push(sphere);
            occupied.push(1);
        }
        
        for(i = 0; i < turns; i++)
        {
            sphere = array2[i];
            threeworld.scene.add(sphere);
        }
}


this.endCondition = false;
function updateStatus()		 
{
 var status =  "Place the spheres on the left with the spheres on the right. Moving one sphere at a time. There's only one empty space in the middle in the beginning.   Steps: " + step; 
 $("#user_span1").html( status );
}


function jumpright (s)
{/*
    var x = s.position.x;
    s.position.x = x + 100;
    step = step + 1;*/
}

function jumpleft (s)
{/*
    var x = s.position.x;
    s.position.x = x - 100;
    step = step + 1;*/
}
    
function inc(a)
{
    if(a==array)
        one = one + 2;
    else if(a==array2)
        two = two + 2;
}

function dec(a)
{
    if(a==array)
        one = one - 2;
    else if(a==array2)
        two = two - 2;
}
    
function move(a)
{
    if(a==array)
    {
        for(var i = 0; i < one; i++)
        {
            sphere = a[i];
            jumpright(sphere);
        }
    }
    
    else if(a==array2)
    {
        for(var i = 0; i < two; i++)
        {
            sphere = a[i];
            jumpleft(sphere);
        }
    }
}
    
function moveback(a)
{
    if(a==array)
    {
        for(var i = turns - one; i < turns; i++)
        {
            sphere = a[i];
            jumpright(sphere);
        }
    }
    
    else if(a==array2)
    {
        for(var i = turns - two; i < turns; i++)
        {
            sphere = a[i];
            jumpleft(sphere);
        }
    }
}

function jumpAll()
{
        for(var i = 0; i < turns - 1; i++)
        {
            move(arr);
            inc(arr);
            arr = swap(arr);
        }
    
        one = turns;
        two = turns;
        
        for(i = 0; i < turns; i++)
        {
            move(arr);
            arr = swap(arr);
        }

        if(arr==array)
        {
            one = turns - 1;
            two = turns - 2;
        }
        else if(arr==array2)
        {
            one = turns - 2;
            two = turns - 1;
        }
        
        for(i = turns - 1; i > 0; i--)
        {
            moveback(arr);
            dec(arr);
            arr = swap(arr);
        }
        this.endCondition = true; 

}


this.newRun = function() 
{
  this.endCondition = false;
  if ( true  )
  {
	  threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 
	  initWalls();
      initSpheres();
  }		
};

this.getState = function()
{
    if(check==turns)
        this.endCondition==true;
    else
    {
        var s1 = array[check];
        var s2 = array2[check];
        check = check + 1;
        return ([s1.position.x, s2.position.x]);
    }
};


this.takeAction = function (a)
{
  if ( true  )
  {
      if(this.endCondition==false)
      {
        var s1 = array[check-1];
        var s2 = array2[check-1];
        s1.position.x = a[1];
        s2.position.x = a[0];
        jumpAll();
      }
      updateStatus();
  }
};

this.endRun = function()
{
};

this.getScore = function()
{
    var x = (step/MAXSTEPS) * 100;
 return x;
};


}