Tic Tac Toe Lab Setup

Setup

Open your Terminal application and navigate to your ~/code/ga/labs directory:

cd ~/code/ga/labs

Navigate to GitHub and create a new repository named javascript-browser-game-tic-tac-toe-lab.

Using the Quick Setup option, clone your newly created repo into your ~/code/ga/labs directory with the git clone command:

git clone https://github.com/<your-username>/javascript-browser-game-tic-tac-toe-lab.git

Note: In the link above, where it says <your-username>, you should see the username from your GitHub account.

Next, cd into your new cloned directory, javascript-browser-game-tic-tac-toe-lab:

cd javascript-browser-game-tic-tac-toe-lab

Then, create a js directory, and a css directory. You can make both of these directories at the same time by adding a space between them after the mkdir command!

mkdir js css

Next, create an app.js file in the js directory, a style.css file in the css directory, and an index.html file in the root of the project. Create all of these files at the same time by adding a space between them after the touch command. These files will hold your work for this lab:

touch ./js/app.js ./css/style.css index.html

With the files created, open the contents of the directory in VS Code:

code .

Starter code

There is some starter code associated with this lab. First the HTML for your index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./css/style.css">
  <script defer src="./js/app.js"></script>
  <title>Tic-Tac-Toe</title>
</head>
<body>
  <h1>Tic-Tac-Toe</h1>
  <h2 id="message">Message</h2>

  <section class="board">
    <div class="sqr" id="0"></div>
    <div class="sqr" id="1"></div>
    <div class="sqr" id="2"></div>
    <div class="sqr" id="3"></div>
    <div class="sqr" id="4"></div>
    <div class="sqr" id="5"></div>
    <div class="sqr" id="6"></div>
    <div class="sqr" id="7"></div>
    <div class="sqr" id="8"></div>
  </section>
</body>
</html>

Take a moment to review this starter HTML. Specifically take note of these elements that will be important to the logic of this game:

After you’ve reviewed that, add this CSS into your css/style.css file:

body {
  background-color: gainsboro;
  /* Use a system font, if none are available use an available sans-sarif */
  font-family: system-ui, sans-serif;
  margin: 0;
}

.board {
  display: flex;
  flex-wrap: wrap;
  width: 325px;
}

.sqr {
  width: 30%;
  aspect-ratio: 1/1;
  border: 3px solid black;
  font-size: 64px;
  text-align: center;
}

While you’ve seen quite a bit of CSS like this before, there are a few declarations here that we should discuss:

Finally, there is a bit of optional boilerplate you can add to your JavaScript to make it easier to find sections inside of it. Add it to your js/app.js if you’d like.

/*-------------------------------- Constants --------------------------------*/



/*---------------------------- Variables (state) ----------------------------*/



/*------------------------ Cached Element References ------------------------*/



/*-------------------------------- Functions --------------------------------*/



/*----------------------------- Event Listeners -----------------------------*/



Start the app

Open the index.html file in your browser and access the console output in your browser’s dev tools.