parent
f00206cfa9
commit
78ad6610e4
@ -0,0 +1,88 @@
|
|||||||
|
window.onload = function() {
|
||||||
|
|
||||||
|
console.log('app.js');
|
||||||
|
|
||||||
|
var turn = true;
|
||||||
|
|
||||||
|
var Xpositions = [];
|
||||||
|
var Opositions = [];
|
||||||
|
|
||||||
|
var windex = [
|
||||||
|
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
||||||
|
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
||||||
|
[0, 4, 8], [2, 4, 6]
|
||||||
|
];
|
||||||
|
|
||||||
|
var squares = document.getElementsByClassName('square');
|
||||||
|
|
||||||
|
var play = function() {
|
||||||
|
|
||||||
|
if (!this.className.includes("x") && !this.className.includes("o")) {
|
||||||
|
|
||||||
|
if (turn) {
|
||||||
|
this.innerHTML = '<img src="x.png"/>';
|
||||||
|
this.className += ' x';
|
||||||
|
Xpositions.push(this.id);
|
||||||
|
} else {
|
||||||
|
this.innerHTML = '<img src="o.png"/>';
|
||||||
|
this.className += ' o';
|
||||||
|
Opositions.push(this.id);
|
||||||
|
}
|
||||||
|
turn = !turn;
|
||||||
|
checkWin();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i=0; i < squares.length; i++) {
|
||||||
|
squares[i].addEventListener('click', play);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var checkWin = function() {
|
||||||
|
var squares = document.getElementsByClassName('square');
|
||||||
|
if (checkX()) {
|
||||||
|
console.log('X WON!!!!!');
|
||||||
|
for (var i=0; i < squares.length; i++) {
|
||||||
|
squares[i].innerHTML = '<img src="egg.png"/>'
|
||||||
|
}
|
||||||
|
} else if (checkO()) {
|
||||||
|
console.log('O WON!!!!!');
|
||||||
|
for (var j=0; j < squares.length; j++) {
|
||||||
|
squares[j].innerHTML = '<img src="depression.png"/>'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var checkX = function() {
|
||||||
|
for (var i=0; i < windex.length; i++) {
|
||||||
|
var hits = 0;
|
||||||
|
for (var j=0; j < windex[i].length; j++) {
|
||||||
|
for (var k=0; k < Xpositions.length; k++) {
|
||||||
|
if (windex[i][j] == Xpositions[k]) {
|
||||||
|
hits++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hits == 3) { return true };
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var checkO = function() {
|
||||||
|
for (var i=0; i < windex.length; i++) {
|
||||||
|
var hits = 0;
|
||||||
|
for (var j=0; j < windex[i].length; j++) {
|
||||||
|
for (var k=0; k < Opositions.length; k++) {
|
||||||
|
if (windex[i][j] == Opositions[k]) {
|
||||||
|
hits++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hits == 3) { return true };
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
After Width: | Height: | Size: 92 KiB |
|
After Width: | Height: | Size: 120 KiB |
@ -0,0 +1,22 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>tic tac toe</title>
|
||||||
|
<link rel="stylesheet" type="text/css" href="style.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="container">
|
||||||
|
<div id="board">
|
||||||
|
<div class="square" id="0"></div>
|
||||||
|
<div class="square" id="1"></div>
|
||||||
|
<div class="square" id="2"></div>
|
||||||
|
<div class="square" id="3"></div>
|
||||||
|
<div class="square" id="4"></div>
|
||||||
|
<div class="square" id="5"></div>
|
||||||
|
<div class="square" id="6"></div>
|
||||||
|
<div class="square" id="7"></div>
|
||||||
|
<div class="square" id="8"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script type="text/javascript" src="app.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
After Width: | Height: | Size: 22 KiB |
@ -0,0 +1,27 @@
|
|||||||
|
#board {
|
||||||
|
min-width: 300px;
|
||||||
|
max-width: 800px;
|
||||||
|
min-height: 300px;
|
||||||
|
max-height: 800px;
|
||||||
|
background-color: pink;
|
||||||
|
border-radius: 5%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.square {
|
||||||
|
position: relative;
|
||||||
|
display: inline-block;
|
||||||
|
left: 1.5%;
|
||||||
|
top: 2px;
|
||||||
|
height: 32%; width: 32%;
|
||||||
|
border-radius: 10%;
|
||||||
|
overflow: hidden;
|
||||||
|
background-color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
height: 100%; width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
|
||||||
|
}
|
||||||
|
After Width: | Height: | Size: 993 B |
Loading…
Reference in new issue