$.fn.tictactoe = function tictactoe(){
//tic-tac-toe game written as a jQuery plugin
//derek anderson @ cox media group 2014
//
//pc plays first open spot 1-1 or 0-0
//first player is rand
//pc blocks player on diags/rows/cols
//
var canPlayerClick = false;
var props = {
$domBoard: $(this),
board:[
[0,0,0],
[0,0,0],
[0,0,0],
],
turns: {
"0":"empty",
"1":"playerTurn",
"2":"computerTurn"
},
currentTurn: -1,
};
function clearBoard() {
//no need to remove events below this
props.$domBoard.html("");
};
function renderBoard() {
//renders props.board into an array of 3 ordered lists for each row
var out = $.map(props.board, function(oRow, iRow){
var out = $.map(oRow, function(oColumn, iColumn){
return $("<li/>", {
"class": props.turns[oColumn],
"data-column": iColumn,
});
});
return $("<ol/>", {
"data-row" : iRow,
}).append(out);
});
return out;
};
function isSquareAvailable($square){
//checks the prop board to see if spot was claimed
var row = $square.parent().attr("data-row");
var column = $square.attr("data-column")
return (props.board[row][column] == 0)
};
function playerBoardSelection(e){
if(canPlayerClick){
//if the player is allowed to click, then they can do this
var $square = $(e.target);
var row = $square.parent().attr("data-row");
var column = $square.attr("data-column")
if(isSquareAvailable($square)){
//set the board to the current players turn number
props.board[row][column] = props.currentTurn;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.