Code viewer for World: Chess : Mind vs Random
AB.runReady       = false;
AB.clockTick = 1;


let s = document.createElement("script");
s.src = "https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js";                          
s.onload = function() { 
    console.log("chess loaded");
    
    AB.runReady = true; 
};        // function to be called when JS is loaded
document.head.appendChild(s);  

	
function World() 
{ 
    
    
    let chess;
    let scl = 40;
    let xoff = 100;
    let yoff = $(document).height()/2;
    let turn = 0;
    
    let ctx;
    let self = this;

    let bk, bq, bn, br, bb, bp, wk, wq, wn, wr, wb, wp, board;
    
    
    
    
    
    
    function drawBoard(board)
    {
        let square = 0;
        
        for(let cur of board)
        {
            switch(cur)
            {
                case "r":
                    ctx.drawImage(br, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "n":
                    ctx.drawImage(bn, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "b":
                    ctx.drawImage(bb, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "q":
                    ctx.drawImage(bq, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "k":
                    ctx.drawImage(bk, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "p":
                    ctx.drawImage(bp, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "R":
                    ctx.drawImage(wr, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "N":
                    ctx.drawImage(wn, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "B":
                    ctx.drawImage(wb, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "Q":
                    ctx.drawImage(wq, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "K":
                    ctx.drawImage(wk, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                case "P":
                    ctx.drawImage(wp, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    // text(cur, xoff + scl*(square%8), yoff + scl*(Math.floor(square/8)));
                    square++;
                    break;
                    
                
                case "/":
                    break;
                    
                    
                case " ":
                    return;
                    
                
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                    square += +cur;
                    break;
                    
                
                default:
                    console.log("Error in drawBoard");
            }
        }
    }
    
    
    function drawGrid()
    {
        ctx.drawImage(board,xoff,yoff);
    }
    
    
    
    
    
 
	this.newRun = function()
	{
		
		threeworld.init ( "white" ); 	
		ctx = threeworld.getContext ( "2d" );
	
		chess = new Chess();
    	
    	bk = new Image(); 
        bk.src = '/uploads/mathias/bk.png';
        
        bq = new Image(); 
        bq.src = '/uploads/mathias/bq.png';
        
        bn = new Image(); 
        bn.src = '/uploads/mathias/bn.png';
        
        br = new Image(); 
        br.src = '/uploads/mathias/br.png';
        
        bb = new Image(); 
        bb.src = '/uploads/mathias/bb.png';
        
        bp = new Image(); 
        bp.src = '/uploads/mathias/bp.png';
        
        wk = new Image(); 
        wk.src = '/uploads/mathias/wk.png';
        
        wq = new Image(); 
        wq.src = '/uploads/mathias/wq.png';
        
        wn = new Image(); 
        wn.src = '/uploads/mathias/wn.png';
        
        wr = new Image(); 
        wr.src = '/uploads/mathias/wr.png';
        
        wb = new Image(); 
        wb.src = '/uploads/mathias/wb.png';
        
        wp = new Image(); 
        wp.src = '/uploads/mathias/wp.png';
        
        board = new Image();
    	board.src = '/uploads/mathias/board.png'
        
    	
        drawBoard(chess.fen());
        
	};
 

	this.takeAction = function ( action )		 
	{
	    if (!chess.game_over())
        {
            turn++;

            if(chess.turn() == 'w')
            {
                $("#user_span1").html("<p>Turn " + turn + "</p> <p>White has to make a move</p>");
                chess.move(action);
            }
            else //Black player makes his move
            {
                $("#user_span1").html("<p>Turn " + turn + "</p> <p>Black has to make a move</p>");
                let moves = chess.moves();
                let move = moves[Math.floor(Math.random() * moves.length)];
                chess.move(move); //random legal move
            }
            
            
            drawGrid();
            drawBoard(chess.fen());
            console.log(chess.ascii());
        }
        else 
        {
            ABRun.abortRun = true;
            
        }
	};


 
	this.getState = function()
	{
	    return chess.fen();
	};

	this.getScore = function()
	{
	    let score = 200; //base score
	    let fen = chess.fen();
	    let i = 0;
	    
	    
	    //points for the pieces
	    while (fen[i] != " ")
	    {
	        switch(fen[i])
	        {
	            case 'r':
	                score -= 5;
	                break;
	                
	            case 'n':
	            case 'b':
	                score -= 3;
	                break;
	                
	            case 'p':
	                score -= 1;
	                break;
	                
	            case 'q':
	                score -= 9;
	                break;
	           
	            case 'R':
	                score += 5;
	                break;
	                
	            case 'N':
	            case 'B':
	                score += 3;
	                break;
	                
	            case 'P':
	                score += 1;
	                break;
	                
	            case 'Q':
	                score += 9;
	                break;
	        }
	  
	        i++;
	    }
	    
	    console.log("pieces points : ", score);
	    
	    
	    //points for the win/lose and time
	    if(chess.in_checkmate())
        {
            let timePoints = 100 - turn;
            if (timePoints < 0) timePoints = 0;
            
            console.log("time points", timePoints);
            
            if (chess.turn() == 'b')
            {
                console.log("win ponts : ", 100);
                score += 100;
                score += timePoints;
            }
            else
            {
                console.log("lose ponts : ", -100);
                score -= 100;
                score -= timePoints;
            }
        }
	    
	    
	    return score;
	}
	
	
	this.endRun = function()
	{
	    
	    let s = "Game Over";
        if (chess.in_draw())
        {
            s += " : Draw";
                
            if (chess.insufficient_material()) s += " (insufficient material)";
            if (chess.in_stalemate()) s += " (stalemate)";
            // if (chess.in_threefold_repetition()()) s += " (threefold repetition)";
        }
        else if(chess.in_checkmate())
        {
            if (chess.turn() == 'b') s+= " : White wins (Black has been ckeckmated)";
           else s+= " : Black wins (White has been ckeckmated)";
        }
        console.log(s);
        $("#user_span2").html("<p>" + s + "</p>");
        ctx.font = "20px Arial";
        ctx.fillText(s,xoff,yoff - 20); 
        
        $("#user_span2").html("<p>Score : " + self.getScore() + "</p>");
	}

}