Skip to content

Instantly share code, notes, and snippets.

@grifdail
Created May 16, 2019 12:09
Show Gist options
  • Save grifdail/03644420bd794903c416921e6700e6ab to your computer and use it in GitHub Desktop.
Save grifdail/03644420bd794903c416921e6700e6ab to your computer and use it in GitHub Desktop.
function setup() {
createCanvas(16000, 16000);
noLoop();
}
function draw() {
background(220);
drawSquare([0,0,0,0,0,0,0,0,0],0,0,width,height, 0);
}
function drawSquare(config, x,y,w,h, turn) {
if (turn == 9) {
return
}
line(floor(x+w*0.333), floor(y), floor(x+w*0.333), floor(y+h));
line(floor(x+w*0.666), floor(y), floor(x+w*0.666), floor(y+h));
line(floor(x), floor(y + h*0.333), floor(x+w), floor(y+h*0.333));
line(floor(x), floor(y+h*0.666), floor(x+w), floor(y+h*0.666));
var mw = w*0.333-4;
var mh = h*0.333-4;
var winConfig = findWin(config, turn);
for(var i = 0; i<9; i++) {
var cx = x + (i%3)*w*0.333+2;
var cy = y + floor(i/3)*h*0.333+2;
//if (winConfig >= 0) {
// if (winConfig != i) {
// continue;
// }
//}
if (config[i] != 0 || lineFound(config)) {
drawShape(floor(cx),floor(cy), floor(mw), floor(mh), config[i]);
} else {
var newConf = config.slice();
newConf[i] = (turn%2)+1
drawSquare(newConf, cx, cy , mw, mh, turn+1)
}
}
}
function drawShape(x,y,w, h, shape) {
if (shape==0) {
return;
}
noFill();
if (shape==1) {
stroke("red");
if (w>3) {
ellipse(x+w*0.5, y+h*0.5,w*0.8, h*0.8)
} else {
point(x+w*0.5,y+w*0.5);
}
} else {
stroke("blue")
if (w>3) {
line(x+w, y+h, x, y);
line(x+w, y, x, y+h);
} else {
point(x+w*0.5,y+w*0.5);
}
}
stroke("black");
}
function findWin(config, turn) {
for(var i = 0; i<9; i++) {
var newConf = config.slice();
newConf[i] = (turn%2)+1
if(lineFound(newConf)) {
return i;
}
}
return -1;
}
function lineFound(config) {
return (config[0] == config[1] && config[0] == config[2] && config[0] > 0) ||
(config[3] == config[4] && config[3] == config[5] && config[3] > 0) ||
(config[6] == config[7] && config[6] == config[8] && config[6] > 0) ||
(config[0] == config[3] && config[0] == config[6] && config[0] > 0) ||
(config[1] == config[4] && config[1] == config[7] && config[1] > 0) ||
(config[2] == config[5] && config[2] == config[8] && config[2] > 0) ||
(config[0] == config[4] && config[0] == config[8] && config[0] > 0) ||
(config[2] == config[4] && config[2] == config[6] && config[2] > 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment