Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished deliverables #269

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
29 changes: 29 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="/styles.css" />
</head>
<body>
<div id="gameContainer">
<h1>Tic Tac Toe</h1>
<h2 id="statusText"></h2>
<div id="cellContainer">
<div cellIndex="0" class="cell a"></div>
<div cellIndex="1" class="cell b"></div>
<div cellIndex="2" class="cell c"></div>
<div cellIndex="3" class="cell d"></div>
<div cellIndex="4" class="cell e"></div>
<div cellIndex="5" class="cell f"></div>
<div cellIndex="6" class="cell g"></div>
<div cellIndex="7" class="cell h"></div>
<div cellIndex="8" class="cell i"></div>
</div>
<button id="restartBtn">Restart</button>
</div>
<script src="/js/main.js"></script>
</body>
</html>
83 changes: 83 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const cells = document.querySelectorAll(".cell");
const statusText = document.querySelector("#statusText");
const restartBtn = document.querySelector("#restartBtn");
const winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
let options = ["", "", "", "", "", "", "", "", ""];
let currentPlayer = "X";
let running = false;

initializeGame();

function initializeGame(){
cells.forEach(cell => cell.addEventListener("click", cellClicked));
restartBtn.addEventListener("click", restartGame);
statusText.textContent = `${currentPlayer}'s turn`;
running = true;
}
function cellClicked(){
const cellIndex = this.getAttribute("cellIndex");

if(options[cellIndex] != "" || !running){
return;
}

updateCell(this, cellIndex);
checkWinner();
}
function updateCell(cell, index){
options[index] = currentPlayer;
cell.textContent = currentPlayer;
}
function changePlayer(){
currentPlayer = (currentPlayer == "X") ? "O" : "X";
statusText.textContent = `${currentPlayer}'s turn`;
}
function checkWinner(){
let roundWon = false;

for(let i = 0; i < winConditions.length; i++){
const condition = winConditions[i];
const cellA = options[condition[0]];
const cellB = options[condition[1]];
const cellC = options[condition[2]];

if(cellA == "" || cellB == "" || cellC == ""){
continue;
}
if(cellA == cellB && cellB == cellC){
roundWon = true;
break;
}
}

if(roundWon){
statusText.textContent = `${currentPlayer} wins!`;
running = false;
}
else if(!options.includes("")){
statusText.textContent = `Draw!`;
running = false;
}
else{
changePlayer();
}
}
function restartGame(){
currentPlayer = "X";
options = ["", "", "", "", "", "", "", "", ""];
statusText.textContent = `${currentPlayer}'s turn`;
cells.forEach(cell => cell.textContent = "");
running = true;
}



51 changes: 51 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
* {
box-sizing: border-box;
}

body {
margin: 0;
font-family: Helvetica;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #0095ff;
}

.cell{
width: 75px;
height: 75px;
border: 2px solid;
box-shadow: 0 0 0 2px;
line-height: 75px;
font-size: 50px;
cursor: pointer;
}


#gameContainer{
font-family:'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;

}
#cellContainer{
display: grid;
grid-template-columns: repeat(3, auto);
width: 225px;
margin: auto;
}

button {
padding: 10px 30px;
margin-top: 30px;
border: none;
border-radius: 5px;
background-color: #000;
color: #fff;
text-transform: uppercase;
}