var start_row = ["tour","cavalier","fou","dame","roi","fou","cavalier","tour"]
"pion": [[0.1,0.9],[0.5,0.1],[0.9,0.9]],
"cavalier": [[0.1,0.1],[0.9,0.1],[0.9,0.4],[0.5,0.4],[0.5,0.9],[0.1,0.9]],
"tour": [[0.2,0.1],[0.8,0.1],[0.8,0.3],[0.7,0.3],[0.7,0.6],[0.8,0.6],[0.8,0.9],[0.2,0.9],[0.2,0.6],[0.3,0.6],[0.3,0.3],[0.2,0.3]],
"fou": [[0.2,0.1],[0.8,0.9],[0.2,0.9],[0.8,0.1]],
"roi": [[0.1,0.1],[0.25,0.4],[0.5,0.1],[0.75,0.4],[0.9,0.1],[0.9,0.9],[0.1,0.9]],
"dame": [[0.3,0.1],[0.7,0.1],[0.7,0.3],[0.9,0.3],[0.9,0.7],[0.7,0.7],[0.7,0.9],[0.3,0.9],[0.3,0.7],[0.1,0.7],[0.1,0.3],[0.3,0.3]]
createCanvas(windowWidth, windowHeight);
o.x = windowWidth/2 - scl*7;
o.y = windowHeight/2 - scl*7;
for (var x = 0; x < 14; x++) {
for (var y = 0; y < 14; y++) {
for (var i=0; i < 8; i++) {
var cells = [mat[3+i][12], mat[1][3+i], mat[3+i][1], mat[12][3+i]]
for (var j = 0; j<4; j++) {
cells[j].col = ["red","blue","yellow","green"][j];
cells = [mat[3+i][13], mat[0][3+i], mat[3+i][0], mat[13][3+i]]
for (var j=0; j<4; j++) {
cells[j].col = ["red","blue","yellow","green"][j];
cells[j].piece = start_row[i];
for(var x = 0; x < 14; x++) {
for (var y = 0; y < 14; y++) {
if ((x+y)%2 == 0) bg = 0;
drawCell(x,y,mat[x][y].piece || "",mat[x][y].col || "purple", bg);
var mx = floor((mouseX-o.x)/scl);
var my = floor((mouseY-o.y)/scl);
rect(o.x + mx*scl, o.y + my*scl, o.x + (mx+1)*scl, o.y + (my+1)*scl);
if (sel[0] != -1) showmoves(sel[0], sel[1]);
else if (inbounds(mx,my)) showmoves(mx,my);
function drawCell(cx,cy,piece,col,bg) {
var points = shapes[piece];
for (var point of points) {
vertex(point[0],point[1]);
vertex(points[0][0], points[0][1]);
return ((x>=3 && x<=10) || (y>=3 && y<=10)) && (x>=0 && x<14 && y>=0 && y<14)
function getmoves(x,y,cell) {
if (cell.piece == "pion") {
var os = {"red":[0,-1],"blue":[1,0],"yellow":[0,1],"green":[-1,0]}[cell.col]
if (!cell.moved) os = [os[0]*2,os[1]*2];
if (inbounds(x+os[0], y+os[1])) moves.push([x+os[0], y+os[1]])
} else if (cell.piece == "cavalier") {
var offsets = [[-1,-2],[-1,2],[1,-2],[1,2],[-2,-1],[-2,1],[2,-1],[2,1]];
for (var os of offsets) {
if (inbounds(x+os[0], y+os[1])) moves.push([x+os[0],y+os[1]]);
} else if (cell.piece == "roi") {
var offsets = [[-1,-1],[-1,1],[1,-1],[1,1],[1,0],[-1,0],[0,1],[0,-1]]
for (var os of offsets) {
if (inbounds(x+os[0], y+os[1])) moves.push([x+os[0],y+os[1]]);
} else if (cell.piece == "fou" || cell.piece == "tour" || cell.piece == "dame") {
if (cell.piece == "fou") {
offsets = [[-1,-1],[-1,1],[1,-1],[1,1]]
} else if (cell.piece == "tour") {
offsets = [[1,0],[-1,0],[0,1],[0,-1]];
offsets = [[-1,-1],[-1,1],[1,-1],[1,1],[1,0],[-1,0],[0,1],[0,-1]]
for (var os of offsets) {
var px = x+os[0]; var py = y+os[1]
while (inbounds(px,py) && mat[px][py].empty) {
if (inbounds(px,py) && mat[px][py].col != cell.col) moves.push([px,py]);
function showmoves(x,y) {
var moves = getmoves(x,y,cell);
for (var move of moves) {
var mx = move[0]; var my = move[1];
if (mat[mx][my].empty) fill(0,255,100,100);
else if (mat[mx][my].col != cell.col) fill(255,100,100,100);
function mousePressed() {
var mx = floor((mouseX-o.x)/scl); var my = floor((mouseY-o.y)/scl);
if (sel[0] == -1) { if(!mat[mx][my].empty) sel = [mx,my]; }
else if (sel[0] == mx && sel[1] == my) sel = [-1,-1];
var cell = mat[sel[0]][sel[1]];
for (var move of getmoves(sel[0],sel[1],cell)) {
if (move[0]==mx && move[1]==my) {
mat[sel[0]][sel[1]] = {empty:true,bounds:true}