HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<head>
<title>Tic<span class="dash">-</span>tac-toe</title>
<link href="https://fonts.googleapis.com/css?family=Coming+Soon|Reenie+Beanie" rel="stylesheet">
</head>
<body>
<h1>Tic-Tac-Toe</h1>
<div id="board">
<div id="choose_players">
<div class="round-button-choice">
<div id="humans" class="button">
<div class="words">Human v. Human</div>
</div>
</div>
<div class="round-button-choice">
<div id="computer" class="button">
<div class="words">Human v. Computer</div>
</div>
</div>
</div>
<div id="XorO">
<div class="round-button-choice">
<div class="X button words" data-toggle="tooltip" title="You start.">
<div class="words2">X</div>
</div>
</div>
<div class="round-button-choice">
<div class="O button words" data-toggle="tooltip" title="Your opponent starts.">
<div class="words2">O</div>
</div>
</div>
</div>
</div>
<div id="gameboard">
<div class="row">
<div id="one" class="one" data-idx="0">
</div>
<div id="two" class="two" data-idx="1">
</div>
<div id="three" class="three" data-idx="2">
</div>
</div>
<div class="row">
<div id="four" class="four" data-idx="3">
</div>
<div id="five" class="five" data-idx="4">
</div>
<div id="six" class="six" data-idx="5">
</div>
</div>
<div class="row">
<div id="seven" class="seven" data-idx="6">
</div>
<div id="eight" class="eight" data-idx="7">
</div>
<div id="nine" class="nine" data-idx="8">
</div>
</div>
</div>
<div class="score-reset">
<span class="player1">Player 1 Score: </span><span class="player1-score">0</span>
<span class="player2">Player 2 Score: </span><span class="player2-score">0</span>
</div>
<div id="reset" class="round-button">
<div class="round-button-circle"><a href="#" class="round-button">Reset</a></div>
</div>
</body>
body {
padding-top: 13px;
text-align: center;
background-color: #1862ff;
width: 100%;
}
h1 {
margin: 10px auto 0px auto;
padding-bottom: 13px;
font-size: 5.38rem;
letter-spacing: 7px;
font-family: 'Reenie Beanie', cursive;
color: #c3b2ea;
}
#gameboard {
width: 360px;
margin: 0px auto 30px auto;
margin-bottom: 15px;
font-size: 5rem;
text-align: center;
color: #c3b2ea;
border-radius: 25px;
vertical-align: center;
font-family: 'Coming Soon', cursive;
background-color: #a974ee;
box-shadow: 5px 5px 7px #5A5352;
border: 5px solid #898e00;
}
.row {
clear: both;
overflow: hidden;
}
.one,
.two,
.four,
.five {
float: left;
width: 119px;
height: 119px;
border-bottom: 1px solid #898e00;
border-right: 1px solid #898e00;
}
.three,
.six {
float: left;
width: 119px;
height: 119px;
border-bottom: 1px solid #898e00;
}
.seven,
.eight {
float: left;
width: 119px;
height: 119px;
border-right: 1px solid #898e00;
}
.nine {
float: left;
width: 119px;
height: 119px;
}
#board {
font-family: 'Coming Soon', cursive;
margin: auto;
text-align: center;
width: 100%;
}
#choose_players {
display: inline-block;
}
#humans, #computer {
cursor: pointer;
}
#XorO {
margin: 0 auto;
margin-top: 14px;
display: inline-block;
color: #c3b2ea;
}
.round-button {
width: 50px;
margin: auto;
margin-top: 10px;
}
.round-button-circle {
width: 100%;
height: 0;
padding-bottom: 100%;
border-radius: 50%;
border: 5px solid #a94464;
overflow: hidden;
background: #963d44;
box-shadow: 0 0 3px gray;
}
.round-button-circle:hover {
background: #029dd6;
border-color: #424491;
box-shadow: 0 0 3px gray;
}
.round-button a {
display: block;
width: 100%;
padding-top: 50%;
padding-bottom: 50%;
line-height: 1em;
margin-top: -0.5em;
text-align: center;
color: #e2eaf3;
font-family: 'Reenie Beanie', cursive;
font-size: 19px;
text-decoration: none;
}
.round-button a:hover {
color: #424491;
}
.round-button-choice {
width: 120px;
margin: 10px;
display: inline-block;
cursor: pointer;
}
.button {
width: 100%;
height: 0;
padding-bottom: 100%;
border-radius: 50%;
font-size: .8em;
border: 5px solid #a94464;
overflow: hidden;
background: #963d44;
box-shadow: 0 0 3px gray;
}
.button:hover {
background: #029dd6;
border-color: #424491;
box-shadow: 0 0 3px gray;
}
.words {
display: block;
width: 100%;
padding-top: 50%;
padding-bottom: 50%;
line-height: 1em;
margin-top: -0.8em;
text-align: center;
color: #e2eaf3;
font-size: 17px;
text-decoration: none;
}
.words:hover, .words2:hover {
color: #424491;
}
.words2 {
font-size: 45px;
}
.score-reset {
display: inline;
font-family: 'Coming Soon', cursive;
font-size: 1em;
margin-top: 15px;
color: #c3b2ea;
}
.player1-score {
margin-right: 30px;
}
.player1-score, .player2-score {
color: #963d44;
font-weight: 900;
font-size: 1.2em;
}
@media only screen and (max-width: 500px) {
h1 {
font-size: 4.1em;
}
#gameboard {
width: 298px;
font-size: 4em;
margin: 0 auto;
box-shadow: none;
}
.one,
.two,
.three,
.four,
.five,
.six,
.seven,
.eight,
.nine {
width: 98.6px;
height: 98.6px;
}
}
@media only screen and (max-width: 330px) {
h1 {
font-size: 3.6em;
}
#gameboard {
width: 286px;
font-size: 3.6em;
margin: 0 auto;
box-shadow: none;
}
.one,
.two,
.three,
.four,
.five,
.six,
.seven,
.eight,
.nine {
width: 94.6px;
height: 94.6px;
}
}
/*jshint loopfunc: true */
$(document).ready(function() {
var compendium_of_squares = {
one: {
name: "one",
marked: false,
symbol: undefined
},
two: {
name: "two",
marked: false,
symbol: undefined
},
three: {
name: "three",
marked: false,
symbol: undefined
},
four: {
name: "four",
marked: false,
symbol: undefined
},
five: {
name: "five",
marked: false,
symbol: undefined
},
six: {
name: "six",
marked: false,
symbol: undefined
},
seven: {
name: "seven",
marked: false,
symbol: undefined
},
eight: {
name: "eight",
marked: false,
symbol: undefined
},
nine: {
name: "nine",
marked: false,
symbol: undefined
}
};
var game_mode;
var available_squares = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
//E = empty
var game_model = ["E", "E", "E", "E", "E", "E", "E", "E", "E"];
var player1 = {
symbol: undefined,
your_turn: undefined,
score: 0
};
var player2 = {
symbol: undefined,
your_turn: undefined,
score: 0
};
var computer = {
symbol: undefined,
your_turn: undefined,
score: 0
};
var winner = {
X: false,
O: false,
win_array: undefined
};
var tie = false;
var game_finished = false;
$('#gameboard, #XorO, .score-reset').hide();
$('.round-button, .round-button-circle').hide();
$('#choose_players').show();
$('#humans').click(function() {
game_mode = 'humans';
$('#choose_players').hide();
$('#XorO').show();
});
$('#computer').click(function() {
game_mode = 'computer';
$('#choose_players').hide();
$('#XorO').show();
$('.player2').text("");
$('.player2').text("Computer Score: ");
});
$(".X").click(function() {
$('.round-button, .round-button-circle, .score-reset').show();
$('#choose_players, #XorO').hide();
$('#gameboard').show();
if (game_mode === 'humans') {
player1.symbol = "X";
player1.your_turn = true;
player2.symbol = "O";
player2.your_turn = false;
} else {
player1.symbol = "X";
player1.your_turn = true;
computer.symbol = "O";
computer.your_turn = false;
}
});
$(".O").click(function() {
$('.round-button, .round-button-circle, .score-reset').show();
$('#choose_players, #XorO').hide();
$('#gameboard').show();
if (game_mode === 'humans') {
player1.symbol = "O";
player1.your_turn = false;
player2.symbol = "X";
player2.your_turn = true;
} else {
player1.symbol = "O";
player1.your_turn = false;
computer.symbol = "X";
computer.your_turn = true;
computer_move();
}
});
$(".one, .two, .three, .four, .five, .six, .seven, .eight, .nine").click(function() {
//get individual square ID
var square_class = this.className;
if (compendium_of_squares[square_class].marked === true) {
return;
}
available_squares.splice(available_squares.indexOf(square_class), 1);
var idx = document.getElementById(square_class);
if (player1.your_turn === true) {
draw(player1, square_class);
game_model[idx.dataset.idx] = player1.symbol;
if (check_if_endgame()) {
if (tie) {
setTimeout(function() {
alert("It's a tie!");
}, 120);
setTimeout(function() {
reset_game();
}, 120);
tie = false;
return;
} else {
player1.score++;
$('.player1-score').text(player1.score);
setTimeout(function() {
alert("Congratulations, Player 1: you won!");
}, 120);
$("." + winner.win_array[0] + ", ." + winner.win_array[1] + ", ." + winner.win_array[2]).css("background", "#963d44");
setTimeout(function() {
reset_game();
}, 120);
return;
}
}
player1.your_turn = false;
if (game_mode === 'humans') {
player2.your_turn = true;
} else {
computer.your_turn = true;
computer_move();
}
} else {
draw(player2, square_class);
game_model[idx.dataset.idx] = player2.symbol;
if (check_if_endgame()) {
if (tie) {
setTimeout(function() {
alert("It's a tie!");
}, 120);
setTimeout(function() {
reset_game();
}, 120);
tie = false;
return;
} else {
player2.score++;
$('.player2-score').text(player2.score);
setTimeout(function() {
alert("Congratulations, Player 2: you won!");
}, 120);
$("." + winner.win_array[0] + ", ." + winner.win_array[1] + ", ." + winner.win_array[2]).css("background", "#963d44");
setTimeout(function() {
reset_game();
}, 120);
return;
}
}
player2.your_turn = false;
player1.your_turn = true;
}
});
function computer_move() {
var square_class;
if (computer.your_turn === true) {
/* Tic-tac-toe strategy from https://en.wikipedia.org/wiki/Tic-tac-toe
Computer chooses the first available move from a prioritized list,
as used in Newell and Simon's 1972 tic-tac-toe program
1: win (get third square in row)
2: block opponent's win
3: take center square if available
4: if opponent takes corner, take opposite corner;
5: take any corner
5: take empty side space space */
computer_strategy();
if (compendium_of_squares[square_class].marked === true) {
return;
}
draw(computer, square_class);
available_squares.splice(available_squares.indexOf(square_class), 1);
var idx = document.getElementById(square_class);
game_model[idx.dataset.idx] = computer.symbol;
//console.log(square_class);
computer.your_turn = false;
player1.your_turn = true;
if (check_if_endgame()) {
if (tie) {
setTimeout(function() {
alert("It's a tie!");
}, 120);
setTimeout(function() {
reset_game();
}, 120);
tie = false;
return;
} else {
if ((computer.symbol === "X" && winner.X === true) || (computer.symbol === "O" && winner.O === true)) {
computer.score++;
$('.player2-score').text(computer.score);
setTimeout(function() {
alert("Congratulations, Computer: you won!");
}, 120);
$("." + winner.win_array[0] + ", ." + winner.win_array[1] + ", ." + winner.win_array[2]).css("background", "#963d44");
setTimeout(function() {
reset_game();
//computer_move();
}, 120);
return;
}
}
}
}
function computer_strategy() {
if (computer.symbol === "X") {
//priority 1: a chance to win
if (game_model[0] === "E" && ((game_model[1] === "X" && game_model[2] === "X") || (game_model[3] === "X" && game_model[6] === "X") || (game_model[4] === "X" && game_model[8] === "X"))) {
square_class = "one";
return square_class;
} else if (game_model[1] === "E" && ((game_model[4] === "X" && game_model[7] === "X") || (game_model[0] === "X" && game_model[2] === "X"))) {
square_class = "two";
return square_class;
} else if (game_model[2] === "E" && ((game_model[0] === "X" && game_model[1] === "X") || (game_model[5] === "X" && game_model[8] === "X") || (game_model[4] === "X" && game_model[6] === "X"))) {
square_class = "three";
return square_class;
} else if (game_model[3] === "E" && ((game_model[0] === "X" && game_model[6] === "X") || (game_model[4] === "X" && game_model[5] === "X"))) {
square_class = "four";
return square_class;
} else if (game_model[4] === "E" && ((game_model[1] === "X" && game_model[7] === "X") || (game_model[0] === "X" && game_model[8] === "X") || (game_model[2] === "X" && game_model[6] === "X") || (game_model[3] === "X" && game_model[5] === "X"))) {
square_class = "five";
return square_class;
} else if (game_model[5] === "E" && ((game_model[3] === "X" && game_model[4] === "X") || (game_model[2] === "X" && game_model[8] === "X"))) {
square_class = "six";
return square_class;
} else if (game_model[6] === "E" && ((game_model[7] === "X" && game_model[8] === "X") || (game_model[0] === "X" && game_model[3] === "X") || (game_model[2] === "X" && game_model[4] === "X"))) {
square_class = "seven";
return square_class;
} else if (game_model[7] === "E" && ((game_model[1] === "X" && game_model[4] === "X") || (game_model[6] === "X" && game_model[8] === "X"))) {
square_class = "eight";
return square_class;
} else if (game_model[8] === "E" && ((game_model[6] === "X" && game_model[7] === "X") || (game_model[2] === "X" && game_model[5] === "X") || (game_model[0] === "X" && game_model[4] === "X"))) {
square_class = "nine";
return square_class;
//priority 2: a chance to block human's win
} else if (game_mode[7] === "E" && (game_model[5] === "O" && game_model[6] === "O" && game_model[8] === "O")) {
square_class = "eight";
return square_class;
} else if (game_model[0] === "E" && ((game_model[1] === "O" && game_model[2] === "O") || (game_model[3] === "O" && game_model[6] === "O") || (game_model[4] === "O" && game_model[8] === "O"))) {
square_class = "one";
return square_class;
} else if (game_model[1] === "E" && ((game_model[4] === "O" && game_model[7] === "O") || (game_model[0] === "O" && game_model[2] === "O"))) {
square_class = "two";
return square_class;
} else if (game_model[2] === "E" && ((game_model[0] === "O" && game_model[1] === "O") || (game_model[5] === "O" && game_model[8] === "O") || (game_model[4] === "O" && game_model[6] === "O"))) {
square_class = "three";
return square_class;
} else if (game_model[3] === "E" && ((game_model[0] === "O" && game_model[6] === "O") || (game_model[4] === "O" && game_model[5] === "O"))) {
square_class = "four";
return square_class;
} else if (game_model[4] === "E" && ((game_model[1] === "O" && game_model[7] === "O") || (game_model[0] === "O" && game_model[8] === "O") || (game_model[6] === "X" && game_model[2] === "X") || (game_model[3] === "O" && game_model[5] === "O"))) {
square_class = "five";
return square_class;
} else if (game_model[5] === "E" && ((game_model[3] === "O" && game_model[4] === "O") || (game_model[2] === "O" && game_model[8] === "O"))) {
square_class = "six";
return square_class;
} else if (game_model[6] === "E" && ((game_model[7] === "O" && game_model[8] === "O") || (game_model[0] === "O" && game_model[3] === "O") || (game_model[2] === "O" && game_model[4] === "O"))) {
square_class = "seven";
return square_class;
} else if (game_model[7] === "E" && ((game_model[1] === "O" && game_model[4] === "O") || (game_model[6] === "O" && game_model[8] === "O"))) {
square_class = "eight";
return square_class;
} else if (game_model[8] === "E" && ((game_model[6] === "O" && game_model[7] === "O") || (game_model[2] === "O" && game_model[5] === "O") || (game_model[0] === "O" && game_model[4] === "O"))) {
square_class = "nine";
return square_class;
//priority 3: prevent forking
} else if ((game_model[0] === "O" && game_model[8] === "O") || (game_model[6] === "O" && game_model[2] === "O")) {
if (compendium_of_squares["two"].marked === false) {
square_class = "two";
} else if (compendium_of_squares["four"].marked === false) {
square_class = "four";
} else if (compendium_of_squares["six"].marked === false) {
square_class = "six";
} else if (compendium_of_squares["eight"].marked === false) {
square_class = "eight"
} else {
square_class = "five";
}
return square_class;
//priority 4: take center square
} else if (game_model[4] === "E") {
square_class = "five";
return square_class;
// priority 5: take opposite corner
} else if (game_model[0] === "E" && (game_model[2] === "O" || game_model[6] === "O")) {
square_class = "one";
return square_class;
} else if (game_model[2] === "E" && (game_model[0] === "O" || game_model[8] === "O")) {
square_class = "three";
return square_class;
} else if (game_model[6] === "E" && (game_model[0] === "O" || game_model[8] === "O")) {
square_class = "seven";
return square_class;
} else if (game_model[8] === "E" && (game_model[2] === "O" || game_model[6] === "O")) {
square_class = "nine";
return square_class;
//priority 6: take available corner
} else if (game_model[0] === "E") {
square_class = "one";
return square_class;
} else if (game_model[2] === "E") {
square_class = "three";
return square_class;
} else if (game_model[6] === "E") {
square_class = "seven";
return square_class;
} else if (game_model[8] === "E") {
square_class = "nine";
//priority 7: take side space
} else if (game_model[1] === "E") {
square_class = "two";
return square_class;
} else if (game_model[5] === "E") {
square_class = "six";
return square_class;
} else if (game_model[7] === "E") {
square_class = "eight";
return square_class;
} else if (game_model[3] === "E") {
square_class = "four";
return square_class;
}
}
if (computer.symbol === "O") {
if (game_model[0] === "E" && ((game_model[1] === "O" && game_model[2] === "O") || (game_model[3] === "O" && game_model[6] === "O") || (game_model[4] === "O" && game_model[8] === "O"))) {
square_class = "one";
return square_class;
} else if (game_model[1] === "E" && ((game_model[4] === "O" && game_model[7] === "O") || (game_model[0] === "O" && game_model[2] === "O"))) {
square_class = "two";
return square_class;
} else if (game_model[2] === "E" && ((game_model[0] === "O" && game_model[1] === "O") || (game_model[5] === "O" && game_model[8] === "O") || (game_model[4] === "O" && game_model[6] === "O"))) {
square_class = "three";
return square_class;
} else if (game_model[3] === "E" && ((game_model[0] === "O" && game_model[6] === "O") || (game_model[4] === "O" && game_model[5] === "O"))) {
square_class = "four";
return square_class;
} else if (game_model[4] === "E" && ((game_model[1] === "O" && game_model[7] === "O") || (game_model[0] === "O" && game_model[8] === "O") || (game_model[2] === "X" && game_model[6] === "X") || (game_model[3] === "O" && game_model[5] === "O"))) {
square_class = "five";
return square_class;
} else if (game_model[5] === "E" && ((game_model[3] === "O" && game_model[4] === "O") || (game_model[2] === "O" && game_model[8] === "O"))) {
square_class = "six";
return square_class;
} else if (game_model[6] === "E" && ((game_model[7] === "O" && game_model[8] === "O") || (game_model[0] === "O" && game_model[3] === "O") || (game_model[2] === "O" && game_model[4] === "O"))) {
square_class = "seven";
return square_class;
} else if (game_model[7] === "E" && ((game_model[1] === "O" && game_model[4] === "O") || (game_model[6] === "O" && game_model[8] === "O"))) {
square_class = "eight";
return square_class;
} else if (game_model[8] === "E" && ((game_model[6] === "O" && game_model[7] === "O") || (game_model[2] === "O" && game_model[5] === "O") || (game_model[0] === "O" && game_model[4] === "O"))) {
square_class = "nine";
return square_class;
//priority 2: a chance to block human's win
} else if (game_mode[7] === "E" && (game_model[5] === "X" && game_model[6] === "X" && game_model[8] === "X")) {
square_class = "eight";
return square_class;
} else if (game_model[0] === "E" && ((game_model[1] === "X" && game_model[2] === "X") || (game_model[3] === "X" && game_model[6] === "X") || (game_model[4] === "X" && game_model[8] === "X"))) {
square_class = "one";
return square_class;
} else if (game_model[1] === "E" && ((game_model[4] === "X" && game_model[7] === "X") || (game_model[0] === "X" && game_model[2] === "X"))) {
square_class = "two";
return square_class;
} else if (game_model[2] === "E" && ((game_model[0] === "X" && game_model[1] === "X") || (game_model[5] === "X" && game_model[8] === "X") || (game_model[4] === "X" && game_model[6] === "X"))) {
square_class = "three";
return square_class;
} else if (game_model[3] === "E" && ((game_model[0] === "X" && game_model[6] === "X") || (game_model[4] === "X" && game_model[5] === "X"))) {
square_class = "four";
return square_class;
} else if (game_model[4] === "E" && ((game_model[1] === "X" && game_model[7] === "X") || (game_model[0] === "X" && game_model[8] === "X") || (game_model[6] === "X" && game_model[2] === "X") || (game_model[3] === "X" && game_model[5] === "X"))) {
square_class = "five";
return square_class;
} else if (game_model[5] === "E" && ((game_model[3] === "X" && game_model[4] === "X") || (game_model[2] === "X" && game_model[8] === "X"))) {
square_class = "six";
return square_class;
} else if (game_model[6] === "E" && ((game_model[7] === "X" && game_model[8] === "X") || (game_model[0] === "X" && game_model[3] === "X") || (game_model[2] === "X" && game_model[4] === "X"))) {
square_class = "seven";
return square_class;
} else if (game_model[7] === "E" && ((game_model[1] === "X" && game_model[4] === "X") || (game_model[6] === "X" && game_model[8] === "X"))) {
square_class = "eight";
return square_class;
} else if (game_model[8] === "E" && ((game_model[6] === "X" && game_model[7] === "X") || (game_model[2] === "X" && game_model[5] === "X") || (game_model[0] === "X" && game_model[4] === "X"))) {
square_class = "nine";
return square_class;
//priority 3: prevent forking
} else if ((game_model[0] === "X" && game_model[8] === "X") || (game_model[6] === "X" && game_model[2] === "X")) {
if (compendium_of_squares["two"].marked === false) {
square_class = "two";
} else if (compendium_of_squares["four"].marked === false) {
square_class = "four";
} else if (compendium_of_squares["six"].marked === false) {
square_class = "six";
} else if (compendium_of_squares["eight"].marked === false) {
square_class = "eight"
} else {
square_class = "five";
}
return square_class;
//priority 4: take center square
} else if (game_model[4] === "E") {
square_class = "five";
//square_class = available_squares[Math.floor(Math.random() * available_squares.length)];
return square_class;
// priority 5: take opposite corner
} else if (game_model[0] === "E" && (game_model[2] === "X" || game_model[6] === "X")) {
square_class = "one";
return square_class;
} else if (game_model[2] === "E" && (game_model[0] === "X" || game_model[8] === "X")) {
square_class = "three";
return square_class;
} else if (game_model[6] === "E" && (game_model[0] === "X" || game_model[8] === "X")) {
square_class = "seven";
return square_class;
} else if (game_model[8] === "E" && (game_model[2] === "X" || game_model[6] === "X")) {
square_class = "nine";
return square_class;
//priority 6: take available corner
} else if (game_model[0] === "E") {
square_class = "one";
return square_class;
} else if (game_model[2] === "E") {
square_class = "three";
return square_class;
} else if (game_model[6] === "E") {
square_class = "seven";
return square_class;
} else if (game_model[8] === "E") {
square_class = "nine";
//priority 7: take side space
} else if (game_model[1] === "E") {
square_class = "two";
return square_class;
} else if (game_model[5] === "E") {
square_class = "six";
return square_class;
} else if (game_model[7] === "E") {
square_class = "eight";
return square_class;
} else if (game_model[3] === "E") {
square_class = "four";
return square_class;
}
}
}
}
function draw(player, square_class) {
$("." + square_class).text(player.symbol);
compendium_of_squares[square_class].marked = true;
compendium_of_squares[square_class].symbol = player.symbol;
}
function check_if_endgame() {
var x_count = 0;
var o_count = 0;
var x_arr = [];
var o_arr = [];
wins_array = [
["one", "two", "three"],
["four", "five", "six"],
["seven", "eight", "nine"],
["one", "four", "seven"],
["two", "five", "eight"],
["three", "six", "nine"],
["one", "five", "nine"],
["three", "five", "seven"]
];
for (var prop in compendium_of_squares) {
if (compendium_of_squares[prop].symbol === "X") {
x_count++;
x_arr.push(compendium_of_squares[prop].name);
}
if (compendium_of_squares[prop].symbol === "O") {
o_count++;
o_arr.push(compendium_of_squares[prop].name);
}
}
if (x_count < 3 && o_count < 3) {
return false;
}
check_winner(wins_array, x_arr, o_arr);
if ((x_count === 5 || o_count === 5) && (winner.X === false && winner.O === false)) {
tie = true;
game_finished = true;
}
return game_finished;
}
function check_winner(wins_array, arr1, arr2) {
for (var i = 0; i < wins_array.length; i++) {
var check_x = wins_array[i].every(function(val) {
return arr1.indexOf(val) >= 0;
});
var check_o = wins_array[i].every(function(val) {
return arr2.indexOf(val) >= 0;
});
if (check_x === true) {
winner.X = true;
game_finished = true;
winner.win_array = wins_array[i];
}
if (check_o === true) {
winner.O = true;
game_finished = true;
winner.win_array = wins_array[i];
}
}
return game_finished;
}
//reset button resets everything
$('#reset').click(function() {
game_mode = undefined;
player1 = {
symbol: undefined,
your_turn: undefined,
score: 0
};
player2 = {
symbol: undefined,
your_turn: undefined,
score: 0
};
computer = {
symbol: undefined,
your_turn: undefined,
score: 0
};
$('.player2').text("");
$('.player2').text("Player 2 Score: ");
$('.player1-score').text("0");
$('.player2-score').text("0");
$('#gameboard, #reset, .score-reset').hide();
$('#choose_players').show();
reset_game();
});
function reset_game() {
available_squares = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
game_model = ["E", "E", "E", "E", "E", "E", "E", "E", "E"];
compendium_of_squares = {
one: {
name: "one",
marked: false,
symbol: undefined
},
two: {
name: "two",
marked: false,
symbol: undefined
},
three: {
name: "three",
marked: false,
symbol: undefined
},
four: {
name: "four",
marked: false,
symbol: undefined
},
five: {
name: "five",
marked: false,
symbol: undefined
},
six: {
name: "six",
marked: false,
symbol: undefined
},
seven: {
name: "seven",
marked: false,
symbol: undefined
},
eight: {
name: "eight",
marked: false,
symbol: undefined
},
nine: {
name: "nine",
marked: false,
symbol: undefined
}
};
winner = {
X: false,
O: false,
win_array: undefined
};
tie = false;
game_finished = false;
$(".one, .two, .three, .four, .five, .six, .seven, .eight, .nine").text("");
$(".one, .two, .three, .four, .five, .six, .seven, .eight, .nine").css("background", "none");
if (computer.symbol === "X") {
computer.your_turn = true;
computer_move();
}
}
});
Also see: Tab Triggers