Code viewer for World: Chess With mind

// Cloned by Mathias Bazin on 26 Jul 2018 from World "Chess" by Mathias Bazin 
// Please leave this clone trail here.
 
AB.runReady      = false;


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


function World() 
{ 
	this.newRun = function()
	{
	    threeworld.init (  "white"  ); 
	};
}




let chess;
let scl = 40;
let xoff = 250;
let yoff = 300;
let speed = 30;

let bk, bq, bn, br, bb, bp, wk, wq, wn, wr, wb, wp;



function preload()
{
    bk = loadImage("/uploads/mathias/bk.png");
    bq = loadImage("/uploads/mathias/bq.png");
    bn = loadImage("/uploads/mathias/bn.png");
    br = loadImage("/uploads/mathias/br.png");
    bb = loadImage("/uploads/mathias/bb.png");
    bp = loadImage("/uploads/mathias/bp.png");
    wk = loadImage("/uploads/mathias/wk.png");
    wq = loadImage("/uploads/mathias/wq.png");
    wr = loadImage("/uploads/mathias/wr.png");
    wn = loadImage("/uploads/mathias/wn.png");
    wb = loadImage("/uploads/mathias/wb.png");
    wp = loadImage("/uploads/mathias/wp.png");
}

function aftersetup()       
{
	chess = new Chess();
	frameRate(speed);
	drawGrid();
	drawBoard(chess.fen());
}

function drawBoard(board)
{
    let square = 0;
    
    for(let cur of board)
    {
        switch(cur)
        {
            case "r":
                image(br, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "n":
                image(bn, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "b":
                image(bb, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "q":
                image(bq, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "k":
                image(bk, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "p":
                image(bp, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "R":
                image(wr, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "N":
                image(wn, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "B":
                image(wb, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "Q":
                image(wq, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "K":
                image(wk, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                square++;
                break;
            case "P":
                image(wp, xoff + scl*(square%8), yoff + scl*(floor(square/8)));
                // text(cur, xoff + scl*(square%8), yoff + scl*(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()
{
    for (let i = 0; i<9; i++) //horizontal limes
    {
        line(xoff, yoff + i*scl,xoff + 8*scl, yoff + i*scl);
    }
    
    for (let i = 0; i<9; i++) //vertical lnes
    {
        line(xoff + i*scl, yoff, xoff + i*scl, yoff + 8*scl);
    }
}

function draw()            
{
     if (!chess.game_over())
        {
            background(255);
            
            if(chess.turn() == 'w')
            {
                chess.move(chess.moves()[0]);
            }
            else //Black player makes his move
            {
                let moves = chess.moves();
                let move = moves[Math.floor(Math.random() * moves.length)];
                chess.move(move);
            }
            
            
            drawGrid();
            drawBoard(chess.fen());
            console.log(chess.ascii());
        }
        else 
        {
            console.log("Game Over");
        }
}