JS viewer

Syntax-highlighted view of JS of World: Heads Up Poker AI(unfinished&aborted)








//My comments
//I started working on this but after a while realised to deliver anything decent would
//require a very large amount of time and effort.
//Decided to do a simpler game to have a complete deliverable
//My other game was connect 4.
//I choose that as it is easy to see that a simple random AI is of no use against a human


// =============================================================================================
// More complex starter World for WWM
// 3d-effect Maze World (really a 2-D problem)
// Mark Humphrys, 2016.
//
// This more complex World shows:
// - Skybox
// - Internal maze (randomly drawn each time)
// - Enemy actively chases agent
// - Music/audio
// - 2D world (clone this and set show3d = false)
// - User keyboard control (clone this and comment out Mind actions to see)
// =============================================================================================


// =============================================================================================
// Scoring:
// Bad steps = steps where enemy is within one step of agent.
// Good steps = steps where enemy is further away. 
// Score = good steps as percentage of all steps.
//
// There are situations where agent is trapped and cannot move.
// If this happens, you score zero.
//
// Scoring on the server side is done by taking average of n runs.
// Runs where you get trapped and score zero can seriously affect this score. 
// =============================================================================================






// World must define these:
 
const	 	CLOCKTICK 	= 100;					// speed of run - move things every n milliseconds
const		MAXSTEPS 	= 1000;					// length of a run before final score
 





//---- global constants: -------------------------------------------------------

const gridsize = 9;						// number of squares along side of world	   

const NOBOXES =  Math.trunc ( (gridsize * gridsize) / 10 );
		// density of maze - number of internal boxes
		// (bug) use trunc or can get a non-integer 

const squaresize = 100;					// size of square in pixels
const MAXPOS = gridsize * squaresize;		// length of one side in pixels 
	
const SKYCOLOR 	= 0xddffdd;				// a number, not a string 
const BLANKCOLOR 	= SKYCOLOR ;			// make objects this color until texture arrives (from asynchronous file read)




const show3d = true;						// Switch between 3d and 2d view (both using Three.js) 
 
const startRadiusConst	 	= MAXPOS * 0.8 ;		// distance from centre to start the camera at
const skyboxConst			= MAXPOS * 3;		// where to put skybox 
const maxRadiusConst 		= MAXPOS * 10  ;		// maximum distance from camera we will render things  





//--- Mind can pick one of these actions -----------------

const ACTION_LEFT 		= 0;		   
const ACTION_RIGHT 		= 1;
const ACTION_UP 			= 2;		 
const ACTION_DOWN 		= 3;
const CHECK 		= 4;

// in initial view, (smaller-larger) on i axis is aligned with (left-right)
// in initial view, (smaller-larger) on j axis is aligned with (away from you - towards you)



// contents of a grid square

const GRID_BLANK 	= 0;
const GRID_WALL 	= 1;
const GRID_MAZE 	= 2;
 
 

var cardsOut = new Array(7);


 
// --- some useful random functions  -------------------------------------------


function randomfloatAtoB ( A, B )			 
{
 return ( A + ( Math.random() * (B-A) ) );
}

function randomintAtoB ( A, B )			 
{
 return  ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
  
function randomBoolean()			 
{
 if ( Math.random() < 0.5 ) { return false; }
 else { return true; }
}



/*function Player(){
    this.name =;
    
    this.getName = function(){
        return this.name;
    };
}*/





//---- start of World class -------------------------------------------------------
 
function World() { 


// most of World can be private 
// regular "var" syntax means private variables:

var playerClient = {
    balance: 100,
    raised: false,
    checked: false,
    fold: false,
    call: false,
    turn: true,
    bet: 0,
};

var playerEnemy = {
    balance: 100,
    raised: false,
    checked: false,
    fold: false,
    call: false,
    turn: false,
    bet: 0,
    
};

var gamePreFlop = true;
var gameFlop = false;
var gameTurn = false;
var gameRiver = false;
var betAmount;


//coded on the fly, not tested at all
/*function betting(a){
    
    playerClient.bet = 1;
    playerEnemy.bet = 2;
    
    while(playerClient.turn === true){
        //attempt to raise the other players bet
        if ( a == ACTION_LEFT ){
            if(playerClient.bet >= (playerEnemy.bet*2) ){
                playerEnemy.turn = true;
                betAmount = playerClient.bet;
                playerClient.turn = false;
            }
        } 	
         else if ( a == ACTION_RIGHT ) {
             //call the other plaers bet
             playerClient.bet = playerEnemy.bet;
             playerEnemy.turn = true;
         }
            
         else if ( a == ACTION_UP ) 
            playerClient.bet++;
         else if ( a == ACTION_DOWN ) 
            playerClient.bet++;
        else if( a == CHECK){
            //this player no option to check on flop
            if(gamePreFlop === false){
                playerClient.turn = false;
                playerEnemy.turn = true;
            }
            //if(gameFlop){
                
           // }
                
        }
    }
    
    //test purposes
    //should be should calling enemy mind instead of this
    while(playerEnemy.turn === true){
        //attempt to raise the other players bet
        if ( a == ACTION_LEFT ){
            if(playerEnemy.bet >= (playerClient.bet*2) ){
                playerClient.turn = true;
                betAmount = playerEnemy.bet;
                playerEnemy.turn = false;
            }
        } 	
         else if ( a == ACTION_RIGHT ) {
             //call the other plaers bet
             playerEnemy.bet = playerClient.bet;
             playerClient.turn = true;
         }
            
         else if ( a == ACTION_UP ) 
            playerClient.bet++;
         else if ( a == ACTION_DOWN ) 
            playerClient.bet++;
         else if( a == CHECK){
            //this player has option to check on flop
            if(gamePreFlop){
                playerClient.turn = true;
                playerEnemy.turn = false;
                //draw the flop
                //chenge flop status
                //change preflop status
            }
            //if(gameFlop){
                
           // }
                
        }
    }
    
    
}*/

//testing
function preFlopBetting(a){
    playerClient.bet = 22;
}

//testing
console.log(playerClient.balance);

var BOXHEIGHT;		// 3d or 2d box height 


var GRID 	= new Array(gridsize);			// can query GRID about whether squares are occupied, will in fact be initialised as a 2D array   
var WALLS 	= new Array ( 4 * gridsize );		// need to keep handles to wall and maze objects so can find them later to paint them 
//var MAZE 	= new Array ( NOBOXES );
var theagent, theenemy;

var ac, as, ah, ad, kc, ks, kh, kd, qc, qs, qh, qd, jc, js, jh, jd, c10, s10, h10, d10, c9, s9, h9, d9, c8, s8, h8, d8;
var c7, s7, h7, d7, c6, s6, h6, d6, c5, s5, h5, d5, c4, s4, h4, d4, c3, s3, h3, d3, c2, s2, h2, d2;

var cards = [ac, as, ah, ad, kc, ks, kh, kd, qc, qs, qh, qd, jc, js, jh, jd, c10, s10, h10, d10, c9, s9, h9, d9, c8, s8, h8, d8,
            c7, s7, h7, d7, c6, s6, h6, d6, c5, s5, h5, d5, c4, s4, h4, d4, c3, s3, h3, d3, c2, s2, h2, d2];

// enemy and agent position on squares
var ei, ej, ai, aj;

var badsteps;
var goodsteps;
var  step;

var enemyBal = 100, playerBal= 100;

var e1, e2, p1, p2;  //to draw player cards
var flop1, flop2, flop3, turn, river;



var firstCards = [e1, e2, p1, p2, flop1, flop2, flop3];

 	var self = this;						// needed for private fn to call public fn - see below  




// regular "function" syntax means private functions:
var p1i = 3, p1j = 8, p2i =5, p2j = 8, e1i = 3, e1j = 0, e2i = 5, e2j = 0;
var flop1i = 0 , flop1j = 4, flop2i = 2, flop2j = 4, flop3i = 4, flop3j = 4;
var turni = 6; turnj = 4, riveri = 8 , riverj = 4;

function initGrid()
{
 for (var i = 0; i < gridsize ; i++) 
 {
  GRID[i] = new Array(gridsize);		// each element is an array 

 }
   GRID[3][8] = p1 ;
   GRID[5][8] = p2 ;
   GRID[3][0] = e1 ;
   GRID[5][0] = e2 ;
   GRID[0][4] = flop1 ;
   GRID[2][4] = flop2 ;
   GRID[4][4] = flop3 ;
   GRID[6][4] = turn ;
  GRID[8][4] = river ;
  
 
}

function occupied ( i, j )		// is this square occupied
{
 if ( ( ei == i ) && ( ej == j ) ) return true;		// variable objects 
 if ( ( ai == i ) && ( aj == j ) ) return true;

 if ( GRID[i][j] == GRID_WALL ) return true;		// fixed objects	 
 if ( GRID[i][j] == GRID_MAZE ) return true;		 
	 
 return false;
}

 
// logically, coordinates are: y=0, x and z all positive (no negative)    
// logically my dimensions are all positive 0 to MAXPOS
// to centre everything on origin, subtract (MAXPOS/2) from all dimensions 

function translate ( x ) 
{
 return ( x - ( MAXPOS/2 ) );
}





//--- skybox ----------------------------------------------------------------------------------------------


function initSkybox() 
{

// x,y,z positive and negative faces have to be in certain order in the array 
 
// mountain skybox, credit:
// http://stemkoski.github.io/Three.js/Skybox.html

  var materialArray = [
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/kevin501/ai.pngg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/kevin501/dealer.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/kevin501/dealer.jpeg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/kevin501/table.png" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/kevin501/ai.plng" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/kevin501/ai.png" ), side: THREE.BackSide } ) )
 	];

  var skyGeometry = new THREE.CubeGeometry ( skyboxConst, skyboxConst, skyboxConst );	
  var skyMaterial = new THREE.MeshFaceMaterial ( materialArray );
  var theskybox = new THREE.Mesh ( skyGeometry, skyMaterial );
  threeworld.scene.add( theskybox );						// We are inside a giant cube
}


// This does the file read the old way using loadTexture.
// (todo) Change to asynchronous TextureLoader. A bit complex:
// Make blank skybox. Start 6 asynch file loads to call 6 return functions.
// Each return function checks if all 6 loaded yet. Once all 6 loaded, paint the skybox.		 


  


/*
// --- alternative skyboxes: ------------------------------

// space skybox, credit:
// http://en.spaceengine.org/forum/21-514-1
// x,y,z labelled differently

   var materialArray = [
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_pos_z.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_neg_z.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_pos_y.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_neg_y.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_pos_x.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_neg_x.jpg" ), side: THREE.BackSide } ) ),
 	];

// urban photographic skyboxes, credit:
// http://opengameart.org/content/urban-skyboxes

   var materialArray = [
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/posx.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/negx.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/posy.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/negy.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/posz.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/negz.jpg" ), side: THREE.BackSide } ) ),
 	];

*/






// --- asynchronous load textures from file ----------------------------------------
// credits:
// http://commons.wikimedia.org/wiki/File:Old_door_handles.jpg?uselang=en-gb
// https://commons.wikimedia.org/wiki/Category:Pac-Man_icons
// https://commons.wikimedia.org/wiki/Category:Skull_and_crossbone_icons
// http://en.wikipedia.org/wiki/File:Inscription_displaying_apices_(from_the_shrine_of_the_Augustales_at_Herculaneum).jpg
  
// loader return can call private function


function loadTextures()
{

 var loader1 = new THREE.TextureLoader();
 loader1.load ( '/uploads/starter/door.jpg',		function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		paintWalls ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
	} ); 

/* var loader2 = new THREE.TextureLoader();
 loader2.load ( '/uploads/starter/latin.jpg',		function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		paintMaze ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
 	} );*/ 

 var loader3 = new THREE.TextureLoader();
 loader3.load ( '/uploads/starter/pacman.jpg',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		theagent.material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} ); 

 var loader4 = new THREE.TextureLoader();
 loader4.load ( '/uploads/starter/ghost.3.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		theenemy.material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} ); 
	
	
 var loader5 = new THREE.TextureLoader();
 loader5.load ( '/uploads/kevin501/10_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[51].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} ); 
	
	var loader6 = new THREE.TextureLoader();
 loader6.load ( '/uploads/kevin501/10_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[50].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} ); 
	
		var loader7 = new THREE.TextureLoader();
 loader7.load ( '/uploads/kevin501/10_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[49].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader8 = new THREE.TextureLoader();
 loader8.load ( '/uploads/kevin501/10_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[48].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader9 = new THREE.TextureLoader();
 loader9.load ( '/uploads/kevin501/2_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[47].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader10 = new THREE.TextureLoader();
 loader10.load ( '/uploads/kevin501/2_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[46].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader11 = new THREE.TextureLoader();
 loader11.load ( '/uploads/kevin501/2_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[45].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader12 = new THREE.TextureLoader();
 loader12.load ( '/uploads/kevin501/2_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[44].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader13 = new THREE.TextureLoader();
 loader13.load ( '/uploads/kevin501/3_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[43].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader14 = new THREE.TextureLoader();
 loader14.load ( '/uploads/kevin501/3_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[42].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader15 = new THREE.TextureLoader();
 loader15.load ( '/uploads/kevin501/3_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[41].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader16 = new THREE.TextureLoader();
 loader16.load ( '/uploads/kevin501/3_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[40].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader17 = new THREE.TextureLoader();
 loader17.load ( '/uploads/kevin501/4_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[39].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader18 = new THREE.TextureLoader();
 loader18.load ( '/uploads/kevin501/4_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[38].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader19 = new THREE.TextureLoader();
 loader19.load ( '/uploads/kevin501/4_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[37].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader20 = new THREE.TextureLoader();
 loader20.load ( '/uploads/kevin501/4_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[36].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader21 = new THREE.TextureLoader();
 loader21.load ( '/uploads/kevin501/5_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[35].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader22 = new THREE.TextureLoader();
 loader22.load ( '/uploads/kevin501/5_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[34].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader23 = new THREE.TextureLoader();
 loader23.load ( '/uploads/kevin501/5_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[33].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader24 = new THREE.TextureLoader();
 loader24.load ( '/uploads/kevin501/5_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[32].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader25 = new THREE.TextureLoader();
 loader25.load ( '/uploads/kevin501/6_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[31].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader26 = new THREE.TextureLoader();
 loader26.load ( '/uploads/kevin501/6_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[30].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader27 = new THREE.TextureLoader();
 loader27.load ( '/uploads/kevin501/6_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[29].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader28 = new THREE.TextureLoader();
 loader28.load ( '/uploads/kevin501/6_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[28].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader29 = new THREE.TextureLoader();
 loader29.load ( '/uploads/kevin501/queen_of_hearts2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[27].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader30 = new THREE.TextureLoader();
 loader30.load ( '/uploads/kevin501/7_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[26].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader31 = new THREE.TextureLoader();
 loader31.load ( '/uploads/kevin501/7_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[25].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader32 = new THREE.TextureLoader();
 loader32.load ( '/uploads/kevin501/7_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[24].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader33 = new THREE.TextureLoader();
 loader33.load ( '/uploads/kevin501/queen_of_spades2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[23].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader34 = new THREE.TextureLoader();
 loader34.load ( '/uploads/kevin501/7_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[22].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader35 = new THREE.TextureLoader();
 loader35.load ( '/uploads/kevin501/8_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[21].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader36 = new THREE.TextureLoader();
 loader36.load ( '/uploads/kevin501/8_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[20].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader37 = new THREE.TextureLoader();
 loader37.load ( '/uploads/kevin501/8_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[19].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader38 = new THREE.TextureLoader();
 loader38.load ( '/uploads/kevin501/8_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[18].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader39 = new THREE.TextureLoader();
 loader39.load ( '/uploads/kevin501/9_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[17].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader40 = new THREE.TextureLoader();
 loader40.load ( '/uploads/kevin501/9_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[16].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader41 = new THREE.TextureLoader();
 loader41.load ( '/uploads/kevin501/9_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[15].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader42 = new THREE.TextureLoader();
 loader42.load ( '/uploads/kevin501/9_of_spades.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[14].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader43 = new THREE.TextureLoader();
 loader43.load ( '/uploads/kevin501/ace_of_clubs.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[13].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader44 = new THREE.TextureLoader();
 loader44.load ( '/uploads/kevin501/ace_of_diamonds.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[12].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader45 = new THREE.TextureLoader();
 loader45.load ( '/uploads/kevin501/ace_of_hearts.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[11].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader46 = new THREE.TextureLoader();
 loader46.load ( '/uploads/kevin501/ace_of_spades2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[10].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader47 = new THREE.TextureLoader();
 loader47.load ( '/uploads/kevin501/jack_of_clubs2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[9].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader48 = new THREE.TextureLoader();
 loader48.load ( '/uploads/kevin501/jack_of_diamonds2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[8].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader49 = new THREE.TextureLoader();
 loader49.load ( '/uploads/kevin501/jack_of_hearts2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[7].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader50 = new THREE.TextureLoader();
 loader50.load ( '/uploads/kevin501/jack_of_spades2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[6].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader51 = new THREE.TextureLoader();
 loader51.load ( '/uploads/kevin501/king_of_clubs2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[5].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader52 = new THREE.TextureLoader();
 loader52.load ( '/uploads/kevin501/king_of_diamonds2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[4].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader53 = new THREE.TextureLoader();
 loader53.load ( '/uploads/kevin501/king_of_hearts2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[3].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader54 = new THREE.TextureLoader();
 loader54.load ( '/uploads/kevin501/king_of_spades2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[2].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader55 = new THREE.TextureLoader();
 loader55.load ( '/uploads/kevin501/queen_of_clubs2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[1].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
	
		var loader56 = new THREE.TextureLoader();
 loader56.load ( '/uploads/kevin501/queen_of_diamonds2.png',	function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		cards[0].material =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} );
  
}




 


// --- add fixed objects ---------------------------------------- 
   
 
/*function initLogicalWalls()		// set up logical walls in data structure, whether doing graphical run or not	
{
 for (var i = 0; i < gridsize ; i++) 
  for (var j = 0; j < gridsize ; j++) 
   if ( ( i==0 ) || ( i==gridsize-1 ) || ( j==0 ) || ( j==gridsize-1 ) )
   {
    	GRID[i][j] = GRID_WALL ;		 
   }
}*/


function initThreeWalls()		// graphical run only, set up blank boxes, painted later 	
{
 var t = 0;
 for (var i = 0; i < gridsize ; i++) 
  for (var j = 0; j < gridsize ; j++) 
   if ( GRID[i][j] == GRID_WALL )
   {
 	 var shape    = new THREE.BoxGeometry( 120, BOXHEIGHT, 170 );			 
 	 var thecube  = new THREE.Mesh( shape );
	 thecube.material.color.setHex( BLANKCOLOR  );			  
 
    	 thecube.position.x =  translate ( i * squaresize );   	//squaresize	// translate my simple (i,j) block-numbering coordinates to three.js (x,y,z) coordinates 
    	 thecube.position.z =   translate ( j * squaresize );   	
    	 thecube.position.y =  0;//translate(-300);	
 
 	 threeworld.scene.add(thecube);
	 WALLS[t] = thecube;				// save it for later
	 t++; 
   }
}


function paintWalls ( material )		 
{
 for ( var i = 0; i < WALLS.length; i++ )
 { 
   if ( WALLS[i] ) 
    WALLS[i].material = material;
 }
}





/*function initLogicalMaze()		 
{
 for ( var c=1 ; c <= NOBOXES ; c++ )
 {
  	var i = randomintAtoB(1,gridsize-2);	// inner squares are 1 to gridsize-2
  	var j = randomintAtoB(1,gridsize-2);
    	GRID[i][j] = GRID_MAZE ;		 
 }
}*/


function initThreeMaze()		  	
{
 var t = 0;
 for (var i = 0; i < gridsize ; i++) 
  for (var j = 0; j < gridsize ; j++) 
   if ( GRID[i][j] == GRID_MAZE )
   {
   	var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
  	var thecube  = new THREE.Mesh( shape );
	thecube.material.color.setHex( BLANKCOLOR  );			  

  	thecube.position.x = translate ( i * squaresize );   	
  	thecube.position.z = translate ( j * squaresize );   	
  	thecube.position.y =  0;//translate(-100);	
 
 	threeworld.scene.add(thecube);
	MAZE[t] = thecube;		// save it for later
	t++; 
   }
}

/*
function paintMaze ( material )		 
{
 for ( var i = 0; i < MAZE.length; i++ )
 { 
   if ( MAZE[i] )  MAZE[i].material = material;
 }
}



*/



// --- enemy functions -----------------------------------
function drawCard( a , b){
    
    if(b == 0){
        var x = translate ( p1i * squaresize );   	
        var z = translate ( p1j * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 1){
        var x = translate ( p2i * squaresize );   	
        var z = translate ( p2j * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 2){
        var x = translate ( e1i * squaresize );   	
        var z = translate ( e1j * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 3){
        var x = translate ( e2i * squaresize );   	
        var z = translate ( e2j * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 4){
        var x = translate ( flop1i * squaresize );   	
        var z = translate ( flop1j * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 5){
        var x = translate ( flop2i * squaresize );   	
        var z = translate ( flop2j* squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 6){
        var x = translate ( flop3i * squaresize );   	
        var z = translate ( flop3j * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 7){
        var x = translate ( turni * squaresize );   	
        var z = translate ( turnj * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
    if(b == 8){
        var x = translate ( riveri * squaresize );   	
        var z = translate ( riverj * squaresize );   	
        var y =  0;
        
        a.position.x = x;
        a.position.y = y;
        a.position.z = z;
        threeworld.scene.add(a);
    }
}

function getCard(){
    return s2;
}


function initFirstCards()
{
    //should be only 4 random cards drawn first
    for(var i = 0; i < 7; i++){
         var shape   = new THREE.BoxGeometry( 120, BOXHEIGHT, 170 );	
         
         var randomCardno;
         
         //ensure the same card aint drawn twice
         while(true){
              randomCardno = randomintAtoB(0,51); 
             
             if(cardsOut.indexOf(randomCardno) == -1)
                break;
         }
         cardsOut[i] = randomCardno;
         //testing
         console.log(cardsOut[i]);
         console.log("hhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiiiii");
         //var randomCard = cards[randomCardno];  // set the random her
         var num = i;
         cards[randomCardno] = new THREE.Mesh( shape );          
         cards[randomCardno].material.color.setHex( BLANKCOLOR  );	
         drawCard(cards[randomCardno], num);		  
    }
}

function drawEnemy()	// given ei, ej, draw it 
{
  var x = translate ( ei * squaresize );   	
  var z = translate ( ej * squaresize );   	
  var y =  0;	


 /*   ac.position.x = x;
 ac.position.y = y;
 ac.position.z = z;
 threeworld.scene.add(ac);

 threeworld.lookat.copy ( ac.position );*/
 
 theenemy.position.x = x;
 theenemy.position.y = y;
 theenemy.position.z = z;
 threeworld.scene.add(theenemy);

 threeworld.lookat.copy ( theenemy.position );		// if camera moving, look back at where the enemy is  
}


function initLogicalEnemy()
{
// start in random location:
 var i, j;
 do
 {
  i = randomintAtoB(1,gridsize-2);
  j = randomintAtoB(1,gridsize-2);
 }
 while ( occupied(i,j) );  	  // search for empty square 

 ei = i;
 ej = j;
}


function initThreeEnemy()
{
 var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
 theenemy = new THREE.Mesh( shape );
 theenemy.material.color.setHex( BLANKCOLOR  );	
 drawEnemy();		  
}


function moveLogicalEnemy()
{ 
// move towards agent 
// put some randomness in so it won't get stuck with barriers 

 var i, j;
 if ( ei < ai ) i = randomintAtoB(ei, ei+1); 
 if ( ei == ai ) i = ei; 
 if ( ei > ai ) i = randomintAtoB(ei-1, ei); 

 if ( ej < aj ) j = randomintAtoB(ej, ej+1); 
 if ( ej == aj ) j = ej; 
 if ( ej > aj ) j = randomintAtoB(ej-1, ej); 
 
 if ( ! occupied(i,j) )  	// if no obstacle then move, else just miss a turn
 {
  ei = i;
  ej = j;
 }
}





// --- agent functions -----------------------------------


function drawAgent()	// given ai, aj, draw it 
{
  var x = translate ( ai * squaresize );   	
  var z = translate ( aj * squaresize );   	
  var y =  0;	

 theagent.position.x = x;
 theagent.position.y = y;
 theagent.position.z = z;
 threeworld.scene.add(theagent);

 threeworld.follow.copy ( theagent.position );		// follow vector = agent position (for camera following agent)
}


function initLogicalAgent()
{
// start in random location:
 var i, j;
 do
 {
  i = randomintAtoB(1,gridsize-2);
  j = randomintAtoB(1,gridsize-2);
 }
 while ( occupied(i,j) );  	  // search for empty square 

 ai = i;
 aj = j;
}

function initThreeAgent()
{
 var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
 theagent = new THREE.Mesh( shape );
 theagent.material.color.setHex( BLANKCOLOR );	
 drawAgent(); 		  
}


function moveLogicalAgent( a )			// this is called by the infrastructure that gets action a from the Mind 
{ 
 var i = ai;
 var j = aj;		 

      if ( a == ACTION_LEFT ) 	i--;
 else if ( a == ACTION_RIGHT ) 	i++;
 else if ( a == ACTION_UP ) 		j++;
 else if ( a == ACTION_DOWN ) 	j--;

 if ( ! occupied(i,j) ) 
 {
  ai = i;
  aj = j;
 }
}



/*function keyHandler(e)		
// user control 
// Note that this.takeAction(a) is constantly running at same time, redrawing the screen.
{
    if (e.keyCode == 37)  moveLogicalAgent ( ACTION_LEFT 	);
    if (e.keyCode == 38)  moveLogicalAgent ( ACTION_DOWN  	);
    if (e.keyCode == 39)  moveLogicalAgent ( ACTION_RIGHT 	);
    if (e.keyCode == 40)  moveLogicalAgent ( ACTION_UP	);
}*/

function keyHandler(e)		
// user control 
// Note that this.takeAction(a) is constantly running at same time, redrawing the screen.
{
    if (e.keyCode == 37)  preFlopBetting ( ACTION_LEFT 	);
    if (e.keyCode == 38)  preFlopBetting ( ACTION_DOWN  	);
    if (e.keyCode == 39)  preFlopBetting ( ACTION_RIGHT 	);
    if (e.keyCode == 40)  preFlopBetting ( ACTION_UP	);
    if (e.keyCode == 97)  preFlopBetting ( CHECK	);
}





// --- score: -----------------------------------

function getPlayerBal(){
    return playerBal;    
}

function getEnemyBal(){
    return enemyBal;    
}


function badstep()			// is the enemy within one square of the agent
{
 if ( ( Math.abs(ei - ai) < 2 ) && ( Math.abs(ej - aj) < 2 ) ) return true;
 else return false;
}


function agentBlocked()			// agent is blocked on all sides, run over
{
 return ( 	occupied (ai-1,aj) 		&& 
		occupied (ai+1,aj)		&&
		occupied (  ai,aj+1)		&&
		occupied (  ai,aj-1) 	);		
} 


function updateStatusBefore(a)
// this is called before anyone has moved on this step, agent has just proposed an action
// update status to show old state and proposed move 
{
 var x = self.getState();
 //var status = " Step: <b> " + playerBal + " </b> &nbsp; x = (" + x.toString() + ") &nbsp; a = (" + a + ") "; 
  var status = " Player balance " + playerBal + "</b> &nbsp;EnemyBal = "  + playerClient.bet; 
 //var status = " Your Bal <b> " + self.getPlayerBal() + " </b> &nbsp; EnemyBal = (" + self.getEnemyBal() + ") &nbsp;

 $("#user_span3").html( status );
}


function   updateStatusAfter()		// agent and enemy have moved, can calculate score
{
 // new state after both have moved
 var y = self.getState();
 //var status = " &nbsp; y = (" + y.toString() + ") <BR> "; 
  
 $("#user_span4").html( status );

 //var score = self.getScore();

 /*var status = "   Bad steps: " + badsteps + 
		" &nbsp; Good steps: " + goodsteps + 
		" &nbsp; Score: " + score.toFixed(2) + "% "; */
		
		var status = "   Bad steps:  &nbsp; Good steps: " +  
		" &nbsp; Score: % " + playerClient.bet; 

 $("#user_span5").html( status );
}






//--- public functions / interface / API ----------------------------------------------------------


	this.endCondition;			// If set to true, run will end. 



this.newRun = function() 
{

// (subtle bug) must reset variables like these inside newRun (in case do multiple runs)

  this.endCondition = false;
  badsteps = 0;	
  goodsteps = 0;
	step = 0;


 // for all runs:

 	initGrid();
	//initLogicalWalls(); 
	//initLogicalMaze();
	//initLogicalAgent();
	initLogicalEnemy();

 // for graphical runs only:

  if ( true  )
  {
	if ( show3d )
	{
	 BOXHEIGHT = squaresize;
	 threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 	
	}	     
	else
	{
	 BOXHEIGHT = 1;
	 threeworld.init2d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 		     
	}

	initSkybox();
 	initMusic();

	// Set up objects first:

	initThreeWalls(); 
	initThreeMaze();
	initThreeAgent();
	initThreeEnemy();
	initFirstCards();

	// Then paint them with textures - asynchronous load of textures from files. 
	// The texture file loads return at some unknown future time in some unknown order.
	// Because of the unknown order, it is probably best to make objects first and later paint them, rather than have the objects made when the file reads return.
	// It is safe to paint objects in random order, but might not be safe to create objects in random order. 

	loadTextures();	

	document.onkeydown = keyHandler;	 
  }

};




this.getState = function()
{
 var x = [ ai, aj, ei, ej ];
  return ( playerClient.bet );  
};



this.takeAction = function ( a )
{
  step++;

  if ( true  )
   updateStatusBefore(a);			// show status line before moves 

  moveLogicalAgent(a);
  preFlopBetting(a);
  

  if ( ( step % 2 ) == 0 )		// slow the enemy down to every nth step
    moveLogicalEnemy();


  if ( badstep() )
   badsteps++;
  else
   goodsteps++;

  if ( true  )
  {
   drawAgent();
   drawEnemy();
   updateStatusBefore(a);			// show status line after moves  
  }


  if ( agentBlocked() )			// if agent blocked in, run over 
  {
	this.endCondition = true;
	goodsteps = 0;			// you score zero as far as database is concerned 			 
  	if ( true  )
  	{
	 //musicPause();
	 //soundAlarm();
	}
  }

};



this.endRun = function()
{
 if ( true  )
 {
 // musicPause(); 
  if ( this.endCondition )
    $("#user_span6").html( " &nbsp; <font color=red> <B> Agent trapped. Final score zero. </B> </font>   "  );
  else
    $("#user_span6").html( " &nbsp; <font color=red> <B> Run over. </B> </font>   "  );
 }
};


this.getScore = function()
{
 return ( ( goodsteps / step ) * 100 );
};


}

//---- end of World class -------------------------------------------------------








// --- music and sound effects ----------------------------------------
// credits:
// http://www.dl-sounds.com/royalty-free/defense-line/
// http://soundbible.com/1542-Air-Horn.html 



function initMusic()
{
	// put music element in one of the spans
  	var x = "<audio  id=theaudio  src=/uploads/starter/Defense.Line.mp3   autoplay loop> </audio>" ;
  //	$("#user_span1").html( x );
} 
 
/*
function musicPlay()  
{
	// jQuery does not seem to parse pause() etc. so find the element the old way:
 	document.getElementById('theaudio').play();
}


function musicPause() 
{
 	document.getElementById('theaudio').pause();
}


function soundAlarm()
{
 	var x = "<audio    src=/uploads/starter/air.horn.mp3   autoplay  > </audio>";
  	$("#user_span2").html( x );
}
*/