Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 16 additions & 41 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Tic Tac Toe</title>
<!--bootstrap & style links -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="tictactoe.css">
<!-- link to JavaScript code -->
<script src="dom-tictactoe.js"></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Welcome to Tic Tac Toe</h1>
<p>Get ready to play!</p>
<!-- uses the built-in onclick attribute to call the resetBoard function -->
<button onclick="resetBoard()" type="button" class="btn btn-primary btn-lg" name="button">Restart</button>
</div>
<html lang="en">

<table align="center">
<tr>
<!-- Each table-data (td) has an id that matches it's position in the array or arrays [0][1] -->
<td id='0-0' onclick="handleClick(this)" ></td>
<td id='0-1' onclick="handleClick(this)" ></td>
<td id='0-2' onclick="handleClick(this)" ></td>
</tr>
<tr>
<!-- onclick calls a function called handleClick and passes itself to it("this") -->
<td id='1-0' onclick="handleClick(this)" ></td>
<td id='1-1' onclick="handleClick(this)" ></td>
<td id='1-2' onclick="handleClick(this)" ></td>
</tr>
<tr>
<td id='2-0' onclick="handleClick(this)" ></td>
<td id='2-1' onclick="handleClick(this)" ></td>
<td id='2-2' onclick="handleClick(this)" ></td>
</tr>
</table>
</div>
</body>
</html>
<head>
<meta charset="utf-8">
<title>Tic Tac Toe</title>
<!--bootstrap & style links -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="tictactoe.css">
<!-- link to JavaScript code -->
<!-- <script src="dom-tictactoe.js"></script> -->
</head>
<body>
<p>Built 100% using JavaScript</p>

<script src="test.js"></script>
</body>
</html>
120 changes: 120 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

let a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
let p = document.createElement('p')
let wh = "50px"
let xo = 'X'
let av = { 1: '', 2: '', 3: '', 4: '', 5: '', 6: '', 7: '', 8: '', 9: '' }
let h1, h2, h3, v1, v2, v3, d1, d2

let createTable = () => {
let tbl = document.createElement('table');

let addCell = (tr, v) => {
let td = document.createElement('td');
td.id = v;
tr.appendChild(td);
};

let addRow = (tbl, a) => {
let tr = document.createElement('tr');
a.forEach(e => {
addCell(tr, e);
});
tbl.appendChild(tr);
};

a.forEach(e => {
addRow(tbl, e);
});
tbl.border = 1
tbl.cellPadding = 0
tbl.cellSpacing = 0

document.body.appendChild(tbl);
};

createTable();

let tbl = document.querySelector('table');
let td = tbl.getElementsByTagName('td');

/*
let styleTable = (td) => {
for (var cell of td) {
cell.style.border = '1px solid black';
cell.style.width = wh;
cell.style.height = wh;
cell.style.textAlign = 'center';
cell.style.fontSize = '40px'
};
};
*/


//styleTable(td);

let handleTdClick = (i) => {
let cell = document.getElementById(i);
if (cell.innerHTML == '') {
cell.innerHTML = xo;
av[i] = xo;
xo == 'X' ? xo = 'O' : xo = 'X';
checkForWinner();
}
}

let addClickListener = (arr) => {
for (var cell of arr) {
cell.addEventListener('click', e => {
handleTdClick(e.target.id);
})
}
}

let checkForWinner = () => {
h1 = av[1] + av[2] + av[3]
h2 = av[4] + av[5] + av[6]
h3 = av[7] + av[8] + av[9]
v1 = av[1] + av[4] + av[7]
v2 = av[2] + av[5] + av[8]
v3 = av[3] + av[6] + av[9]
d1 = av[1] + av[5] + av[9]
d2 = av[3] + av[5] + av[7]
let hvd = [h1, h2, h3, v1, v2, v3, d1, d2]

hvd.forEach(e => {
if (e == "XXX" || e == "OOO") {
p.innerHTML = 'Winner Winner Chicken Dinner!!!'
}
})
}

addClickListener(td);

let newGame = () => {
p.innerHTML = '';
for (let i in av) {
av[i] = ''
}

for (let i in td) {
td[i].innerHTML = ''
}
}

let createPandBtn = () => {
p.style.height = '25px'
document.body.appendChild(p)
let btn = document.createElement('button');
btn.textContent = "Start New Game"
btn.addEventListener('click', newGame);
btn.type = "button";
btn.name = "button";
btn.classList.add("btn");
btn.classList.add("btn-primary");
btn.classList.add("btn-lg");
document.body.appendChild(btn);
}


createPandBtn()
2 changes: 2 additions & 0 deletions tictactoe.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ td{
border: 5px solid black;
font-size: 100px;
}