cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/p5.min.js"></script>
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/addons/p5.dom.js"></script>
  </head>
  <body>
    
  </body>
</html>

            
          
!
            
              /* https://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}

body {
	display: flex;
	justify-content: center;
}
            
          
!
            
              var scl,rows,cols;
var grid = [];
var XHistoryX = [];
var XHistoryY = [];
var OHistoryX = [];
var OHistoryY = [];

var userPreference = confirm("Would you like to play as X?");

if(userPreference) {
	var userPreferenceX = XHistoryX;
	var userPreferenceY = XHistoryY;
	var userPreference = "X";
} else {
	var userPreferenceX = OHistoryX;
	var userPreferenceY = OHistoryY;
	var userPreference = "O";
}

function setup() {
	createCanvas(601, 601);
	rows = floor(height/3);
	cols = floor(width/3);
	scl = floor(height/3);
	for(var i = rows; i <= rows*3; i+=rows) {
		for(var j = cols; j <= cols*3; j+=cols) {
			var cell = new Cell(i, j);
			grid.push(cell);
		}
	}
}

function draw() {
	background(51);
	translate(-rows, -cols);

	for(var i = 0; i < grid.length; i++) {
		grid[i].show();
	}
	if(userPreference == "O") {
		for(var i = 0; i < userPreferenceX.length; i++) {
			fill(255);
			ellipse(userPreferenceX[i]+scl/2, userPreferenceY[i]+scl/2, scl, scl);
		}
	} else if(userPreference == "X") {
		for(var i = 0; i < userPreferenceX.length; i++) {
			fill(255);
			star(userPreferenceX[i]+scl/2, userPreferenceY[i]+scl/2, scl/2, scl/10, 4);
		}
	}

	if(userPreference == "O") {
		for(var i = 0; i < XHistoryX.length; i++) {
			fill(255);
			star(XHistoryX[i]+scl/2, XHistoryY[i]+scl/2, scl/2, scl/10, 4);
		}
	} else if(userPreference == "X") {
		for(var i = 0; i < OHistoryX.length; i++) {
			fill(255);
			ellipse(OHistoryX[i]+scl/2, OHistoryY[i]+scl/2, scl, scl);
		}
	}

}

function getIndex(col, row) {
	for(var i = 0; i < grid.length; i++) {
		if(grid[i].col == col && grid[i].row == row) {
			return i;
		}
	}
}

function Cell(i, j) {
	this.x = i;
	this.y = j;
	this.col = this.x/scl;
	this.row = this.y/scl;
	this.color = 255;
	this.taken = false;
	this.type = "";
	this.show = function() {
		fill(255);
		fill(this.color);
		rect(this.x, this.y, scl, scl);
	}

	this.checkNeighbors = function() {
		 if(this.taken == true) {
		 	if(grid[getIndex(this.col, this.row+1)] !== undefined && grid[getIndex(this.col, this.row-1)] !== undefined) {
		 		if(grid[getIndex(this.col, this.row+1)].taken == true && grid[getIndex(this.col, this.row+1)].type == this.type) {
		 			if(grid[getIndex(this.col, this.row-1)].taken == true && grid[getIndex(this.col, this.row-1)].type == this.type) {
		 				alert("WIN: " + this.type);
		 				document.location.reload(true);
		 				return;
		 			}
		 		}
		 	}

		 	if(grid[getIndex(this.col+1, this.row)] !== undefined && grid[getIndex(this.col-1, this.row)] !== undefined) {
		 		if(grid[getIndex(this.col+1, this.row)].taken == true && grid[getIndex(this.col+1, this.row)].type == this.type) {
		 			if(grid[getIndex(this.col-1, this.row)].taken == true && grid[getIndex(this.col-1, this.row)].type == this.type) {
		 				alert("WIN: " + this.type);
		 				document.location.reload(true);
		 				return;
		 			}
		 		}
		 	}

		 	if(grid[getIndex(this.col+1, this.row+1)] !== undefined && grid[getIndex(this.col-1, this.row-1)] !== undefined) {
		 		if(grid[getIndex(this.col+1, this.row+1)].taken == true && grid[getIndex(this.col+1, this.row+1)].type == this.type) {
		 			if(grid[getIndex(this.col-1, this.row-1)].taken == true && grid[getIndex(this.col-1, this.row-1)].type == this.type) {
		 				alert("WIN: " + this.type);
		 				document.location.reload(true);
		 				return;
		 			}
		 		}
		 	}

		 	if(grid[getIndex(this.col+1, this.row-1)] !== undefined && grid[getIndex(this.col-1, this.row+1)] !== undefined) {
		 		if(grid[getIndex(this.col+1, this.row-1)].taken == true && grid[getIndex(this.col+1, this.row-1)].type == this.type) {
		 			if(grid[getIndex(this.col-1, this.row+1)].taken == true && grid[getIndex(this.col-1, this.row+1)].type == this.type) {
		 				alert("WIN: " + this.type);
		 				document.location.reload(true);
		 				return;
		 			}
		 		}
		 	}

		 }
	}

}

function computerPlace() {
	var picked = false;

	var draw = 0;
	for(var i = 0; i < grid.length; i++) {
		if(grid[i].taken == true) {
			draw++;
			grid[i].checkNeighbors();
		}
	}

	if(draw >= 8) {
		document.location.reload(true);
		return;
	}

	while(!picked) {

		var compX = floor(random(width));
		var compY = floor(random(height));

		for(var i = 0; i < grid.length; i++) {
			if(compX < grid[i].x && compX > grid[i].x-scl && compY < grid[i].y && compY > grid[i].y-scl && grid[i].taken == false) {
				if(userPreference == "X") {
					OHistoryX.push(grid[i].x);
					OHistoryY.push(grid[i].y);
					grid[i].taken = true;
					grid[i].type = "O";
					picked = true;
					grid[i].checkNeighbors();
				} else if(userPreference == "O") {
					XHistoryX.push(grid[i].x);
					XHistoryY.push(grid[i].y);
					grid[i].taken = true;
					grid[i].type = "X";
					picked = true;
					grid[i].checkNeighbors();
				}
			}
		}
	}

}


function mousePressed() {
	for(var i = 0; i < grid.length; i++) {
		if(mouseX < grid[i].x && mouseX > grid[i].x-scl && mouseY < grid[i].y && mouseY > grid[i].y-scl && grid[i].taken == false) {
			userPreferenceX.push(grid[i].x);
			userPreferenceY.push(grid[i].y);
			grid[i].taken = true;
			grid[i].type = userPreference;
			grid[i].checkNeighbors();
		}
	}
	computerPlace();
}

function star(x, y, radius1, radius2, npoints) {
  var angle = TWO_PI / npoints;
  var halfAngle = angle/2.0;
  beginShape();
  for (var a = 0; a < TWO_PI; a += angle) {
    var sx = x + cos(a) * radius2;
    var sy = y + sin(a) * radius2;
    vertex(sx, sy);
    sx = x + cos(a+halfAngle) * radius1;
    sy = y + sin(a+halfAngle) * radius1;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console