Skip to content

Instantly share code, notes, and snippets.

@Steven24K
Created December 26, 2024 12:43
Show Gist options
  • Select an option

  • Save Steven24K/7eb42baf2a05ed6a43a959bd8c9bc8dd to your computer and use it in GitHub Desktop.

Select an option

Save Steven24K/7eb42baf2a05ed6a43a959bd8c9bc8dd to your computer and use it in GitHub Desktop.
Tic Tac Toe HTML, CSS, JS exercise
const game_grid = document.getElementById('game-grid');
const grid_size = 3;
const player_x = document.getElementById('player-x')
const player_o = document.getElementById('player-o')
const header = document.getElementById('header');
const reset_button = document.getElementById('reset');
let current_player = player_x;
const rounds = grid_size ** 2;
let current_round = 0;
function draw_grid() {
for (let y = 0; y < grid_size; y++) {
const game_row = document.createElement('div');
game_row.className = "game-row";
game_row.id = `y-${y}`;
for (let x = 0; x < grid_size; x++) {
const game_field = document.createElement('div');
game_field.className = "game-field";
game_field.id = `y-${y}-x-${x}`;
game_field.addEventListener('click', game_play);
game_row.append(game_field);
}
game_grid.append(game_row);
}
}
function game_play() {
if (current_round >= rounds) return;
current_round++;
const newSymbol = document.createElement('div')
newSymbol.innerHTML = current_player.innerHTML;
newSymbol.className = "player";
this.append(newSymbol);
if (check_winner()) {
current_round = rounds;
const winnerText = document.createElement('h2');
winnerText.innerText = `${current_player.innerText} is the winner!`;
header.append(winnerText);
} else {
if (current_player.id === "player-x") {
current_player = player_o;
player_x.classList.remove('turn');
player_o.classList.add('turn');
} else {
current_player = player_x;
player_x.classList.add('turn');
player_o.classList.remove('turn');
}
if (current_round >= rounds) {
const tieText = document.createElement('h2');
tieText.innerText = "It's a tie!";
header.append(tieText);
}
}
this.removeEventListener('click', game_play);
}
function grid(x, y) {
return document.getElementById(`y-${y}-x-${x}`).innerText;
}
function check_winner() {
// Check horizontal and vertical lines
for (let i = 0; i < grid_size; i++) {
// Horizontal check (row i)
if (grid(0, i) !== '' && grid(0, i) === grid(1, i) && grid(0, i) === grid(2, i)) {
return true;
}
// Vertical check (column i)
if (grid(i, 0) !== '' && grid(i, 0) === grid(i, 1) && grid(i, 0) === grid(i, 2)) {
return true;
}
}
// Top-left to bottom-right diagonal
if (grid(0, 0) !== '' && grid(0, 0) === grid(1, 1) && grid(0, 0) === grid(2, 2)) {
return true;
}
// Top-right to bottom-left diagonal
if (grid(2, 0) !== '' && grid(2, 0) === grid(1, 1) && grid(2, 0) === grid(0, 2)) {
return true;
}
return false;
}
draw_grid();
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<header class="header" id="header">
<h1>Welcome to the world famous Tic Tac Toe game!</h1>
<p>
Tic-tac-toe, noughts and crosses, or Xs and Os is a paper-and-pencil game for two players
who take turns marking the spaces in a three-by-three grid with X or O.
The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner.
</p>
<a target="_blank" aria-label="Wikipedia" href="https://en.wikipedia.org/wiki/Tic-tac-toe">Wikipedia</a>
<a aria-label="Reset" href="index.html">Reset game</a>
</header>
<main class="container">
<div id="game-grid" class="game-grid"></div>
<div class="players">
<div id="player-x" class="player turn">&#9932;</div>
<div id="player-o" class="player">&#9898;</div>
</div>
</main>
<script src="./game.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
}
body {
background-color: #e8e8e8;
}
.container {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin: 1rem;
}
.header {
margin: 25px;
padding: 25px;
text-align: center;
background-color: white;
border-radius: 25px;
}
.header h1 {
color: #000000;
}
.header h2 {
margin-top: 15px;
}
.header p {
margin-top: 10px;
margin-bottom: 15px;
color: #575555;
}
.header a {
margin: 5px;
padding: 10px;
text-decoration: none;
color: white;
background-color: #c63f2a;
}
.header a:hover {
background-color: #cb7c70;
}
.game-grid {
display: flex;
margin: 2rem;
height: 300px;
width: 300px;
}
.game-row:not(:last-child) {
border-right: 4px solid black;
}
.game-field:not(:first-child, :last-child) {
border-top: 4px solid black;
border-bottom: 4px solid black;
}
.game-field {
height: 100px;
width: 100px;
}
.players {
display: flex;
gap: 5px;
margin: 10px;
}
.player {
margin: auto;
padding-top: 15px;
text-align: center;
font-size: 3em;
width: 80px;
height: 80px;
}
.turn {
border-bottom: 4px solid red;
}
@Steven24K
Copy link
Author

The Game plan.

The goals of building this Tic Tac Toe game are:

  • To get understanding of DOM manipulation based on user interactions
  • To see and experience how HTML, CSS and Javascript/Typescript can work together to accomplish a shared purpose.

Stepp by stepp guide on how to accomplish the end result.

  1. Create a new HTML file.
  2. Make a standard layout. PRO TIP: just type doc in an empty HTML file in VS Code and generate a basic layout with the click of a button.
  3. Create the Game Grid
  4. Assign class names
  5. Assign ID's to identify each element.
  6. Create a CSS file. (Don't forget to reference it in your HTML layout)
  7. Style the grid: Use flex layouts to position the game fields, add borders to be able to see each field.
  8. Create fields for 'X' and 'O', or use emoji's, that's will be used to determain who's turn it is.
  9. Create a game.ts file. (Don't forget to reference it in your HTML layout)
  10. Build the main game function.
  11. Grab current player element.
  12. Wait for user to click on a field
  13. Place current player symbol in field.
  14. Go to player 2.
  15. Repeat logic until 1 player has won, or if it is a tie.
  16. Display Game Result
  17. Display Re-Match button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment